Files
Upsilon/apps/shared/toolbox_helpers.cpp
Léa Saviot 1967fce13a [apps] Toolbox layouts are created by parsing the command of the cell.
Change-Id: I511e8af2b2247d1df3ca968191566c2b3f01fe11
2018-01-15 11:58:06 +01:00

62 lines
1.7 KiB
C++

#include "toolbox_helpers.h"
#include <ion/charset.h>
#include <string.h>
namespace Shared {
namespace ToolboxHelpers {
int CursorIndexInCommand(const char * text) {
for (size_t i = 0; i < strlen(text); i++) {
if (text[i] == '(' || text[i] == '\'') {
return i + 1;
}
}
return strlen(text);
}
void TextToInsertForCommandMessage(I18n::Message message, char * buffer) {
const char * messageText = I18n::translate(message);
TextToInsertForCommandText(messageText, buffer);
}
void TextToInsertForCommandText(const char * command, char * buffer) {
int currentNewTextIndex = 0;
int numberOfOpenBrackets = 0;
bool insideQuote = false;
size_t commandLength = strlen(command);
for (size_t i = 0; i < commandLength; i++) {
if (command[i] == ')') {
numberOfOpenBrackets--;
}
if ((numberOfOpenBrackets == 0 || command[i] == ',') && (!insideQuote || command[i] == '\'')) {
buffer[currentNewTextIndex++] = command[i];
}
if (command[i] == '(') {
numberOfOpenBrackets++;
}
if (command[i] == '\'') {
insideQuote = !insideQuote;
}
}
buffer[currentNewTextIndex] = 0;
}
void TextToParseIntoLayoutForCommandMessage(I18n::Message message, char * buffer) {
const char * messageText = I18n::translate(message);
TextToInsertForCommandText(messageText, buffer);
size_t bufferLength = strlen(buffer);
for (size_t i = 0; i < bufferLength; i++) {
if (buffer[i] == '(' || buffer[i] == ',') {
// Shift the buffer to make room for the new char. Use memmove to avoid
// overwritting.
memmove(&buffer[i+2], &buffer[i+1], bufferLength - (i+1) + 1);
bufferLength++;
i++;
buffer[i] = Ion::Charset::Empty;
}
}
}
}
}