summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael J. Chudobiak <mjc@avtechpulse.com>2014-11-05 09:52:30 -0500
committerMichael J. Chudobiak <mjc@avtechpulse.com>2014-11-05 09:52:30 -0500
commit745646df4067c97f1e14a66c3ce0efdc455f6308 (patch)
treebe708517eac11e4b9801d95c3c30b99adc62b145
parent92608b74e18aa81f50967c8841fdf88957220ce3 (diff)
Simplify and improve break_up_string
-rw-r--r--lcd.c91
1 files changed, 29 insertions, 62 deletions
diff --git a/lcd.c b/lcd.c
index 7079aad..526cf8e 100644
--- a/lcd.c
+++ b/lcd.c
@@ -18,81 +18,43 @@ char LCD_Data[LCD_rows][LCD_cols]; /* shadow copy of LCD display in local RAM, t
static void break_up_string (char *in_string, int N, char **str1, char **str2, char **str3)
{
+ gchar** words = g_strsplit(in_string, " ", -1);
-#define SUBSTRING_CNT 3
+ GString* rows[3];
+ rows[0] = g_string_new(NULL);
+ rows[1] = g_string_new(NULL);
+ rows[2] = g_string_new(NULL);
- int input_length;
- int j, k, copypos;
- int n;
+ int row = 0;
+ int col = 0;
+ gchar** word;
- char *copy_input;
- char *p;
+ for (word = words; *word != NULL; word++) {
+ int wordlen = strlen(*word);
- j=k=copypos=0;
- input_length=strlen(in_string);
- copy_input = g_strdup(in_string);
+ if (col + wordlen > N) {
+ col = 0;
+ row++;
+ }
- for (j=0; j<SUBSTRING_CNT; j++) {
+ if (row < 3) {
+ g_string_append(rows[row], *word);
- gchar *interm_str = g_strnfill (N, 0);
+ col += wordlen;
- for (k=0; k<N; k++) {
- copypos++; // next symbol
- if (copypos>input_length) {
- break;
- }
- if ((*(copy_input+k)==' ')&&(copypos<input_length)) { // space found
- //find next space
- p=strchr(copy_input+k+1,' ');
- if (p!=NULL) {
- // next space is beyond N char limit for one line
- if((p-copy_input)/sizeof(char)>=N) {
- break;
- }
- } else {
- // No more spaces. Does the last chunk of text fit?
- if (strlen(copy_input+k+1)>N-k) {
- break;
- }
- }
+ if (col != 0 && (col + 1) < N) {
+ g_string_append(rows[row], " ");
+ col++;
}
- // valid character, copy to output string
- *(interm_str+k)=*(copy_input+k);
-
}
- // remove processed part from the input
- if (copypos<=input_length) {
- strcpy(copy_input, in_string+copypos);
- }
- n = strlen(interm_str);
- for ((k=n-1); (k=0); k--) { //trim space
- if (*(interm_str+k)==' ') {
- *(interm_str+k)=0;
- } else {
- break;
- }
- }
- switch (j) {
- case 0:
- *str1 = g_strdup(interm_str);
- break;
- case 1:
- *str2 = g_strdup(interm_str);
- break;
- case 2:
- *str3 = g_strdup(interm_str);
- break;
- }
- g_free(interm_str);
-
}
-
- g_free(copy_input);
+ *str1 = g_string_free(rows[0], FALSE);
+ *str2 = g_string_free(rows[1], FALSE);
+ *str3 = g_string_free(rows[2], FALSE);
+ g_strfreev(words);
}
-
-
void LCD_clear()
{
int i;
@@ -153,6 +115,11 @@ static void LCD_RAM_write(int RAM_start,int row, int col, char *LCD_string)
enable_one_lcd=0x01;
}
+ // bounds check
+ if ((cur_row >= LCD_rows) || (cur_col >= LCD_cols)) {
+ break;
+ }
+
if (LCD_string[i]!=LCD_Data[cur_row][cur_col]) {
if (!last_op_a_write) {
/* move to new position */