comparison nnchat.c @ 50:d08bf3b6561d

Use strdup; Added function to decode morse code.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 02 Nov 2008 18:43:38 +0200
parents 0bcc38910a77
children cf7789d88350
comparison
equal deleted inserted replaced
49:0bcc38910a77 50:d08bf3b6561d
187 187
188 188
189 void addRingBuf(ringbuf_t *buf, const char *str) 189 void addRingBuf(ringbuf_t *buf, const char *str)
190 { 190 {
191 if (buf->n < buf->size) { 191 if (buf->n < buf->size) {
192 buf->data[buf->n] = th_strdup(str); 192 buf->data[buf->n] = strdup(str);
193 buf->n++; 193 buf->n++;
194 } else { 194 } else {
195 th_free(buf->data[0]); 195 th_free(buf->data[0]);
196 memmove(&(buf->data[0]), &(buf->data[1]), buf->size - 1); 196 memmove(&(buf->data[0]), &(buf->data[1]), buf->size - 1);
197 buf->data[buf->size - 1] = th_strdup(str); 197 buf->data[buf->size - 1] = strdup(str);
198 } 198 }
199 } 199 }
200 200
201 201
202 typedef struct { 202 typedef struct {
623 623
624 return result; 624 return result;
625 } 625 }
626 626
627 627
628 char * tab2str(conv_ent_t *tab, size_t ntab, char *str)
629 {
630 char *result, *s = str;
631 size_t resSize, resPos = 0;
632
633 if (!str) return NULL;
634
635 resSize = (strlen(str) * 4) + SET_ALLOC_SIZE;
636 if ((result = th_malloc(resSize)) == NULL)
637 return NULL;
638
639 while (*s) {
640 size_t i;
641 BOOL found = FALSE;
642 for (i = 0; i < ntab; i++) {
643 size_t len = strlen(tab[i].code);
644 if (!strcmp(s, tab[i].code) && s[len] == ' ') {
645 s += len;
646 PUSHCHAR(tab[i].c);
647 found = TRUE;
648 break;
649 }
650 }
651 if (!found) PUSHCHAR(*s);
652
653 s++;
654 }
655 PUSHCHAR(0);
656
657 return result;
658 }
659
660
628 char *encodeStr1(char *str) 661 char *encodeStr1(char *str)
629 { 662 {
630 char *result, *s = str; 663 char *result, *s = str;
631 size_t resSize, resPos = 0; 664 size_t resSize, resPos = 0;
632 665
900 th_free(h); 933 th_free(h);
901 th_free(t); 934 th_free(t);
902 } else { 935 } else {
903 t = stripTags(s); 936 t = stripTags(s);
904 h = decodeStr2(t); 937 h = decodeStr2(t);
938 if (setMorseMode) {
939 th_free(t); t = h;
940 h = tab2str(morseTab, sizeof(morseTab) / sizeof(morseTab[0]), t);
941 }
905 printMsg("½5½<½15½%s½5½>½0½ %s\n", p, h); 942 printMsg("½5½<½15½%s½5½>½0½ %s\n", p, h);
906 th_free(h); 943 th_free(h);
907 th_free(t); 944 th_free(t);
908 } 945 }
909 946
1058 th_free(tmpStr); 1095 th_free(tmpStr);
1059 th_free(tmpStr2); 1096 th_free(tmpStr2);
1060 return 0; 1097 return 0;
1061 } else if (!strncmp(buf, "/to ", 4)) { 1098 } else if (!strncmp(buf, "/to ", 4)) {
1062 th_free(setTarget); 1099 th_free(setTarget);
1063 setTarget = th_strdup(buf + 4); 1100 setTarget = strdup(buf + 4);
1064 printMsg("Set prv target to '%s'\n", setTarget); 1101 printMsg("Set prv target to '%s'\n", setTarget);
1065 return 0; 1102 return 0;
1066 } else if (setPrvMode) { 1103 } else if (setPrvMode) {
1067 if (setTarget != NULL) { 1104 if (setTarget != NULL) {
1068 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", setTarget, buf); 1105 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", setTarget, buf);
1217 if (!optDaemon) { 1254 if (!optDaemon) {
1218 initscr(); 1255 initscr();
1219 raw(); 1256 raw();
1220 keypad(stdscr, TRUE); 1257 keypad(stdscr, TRUE);
1221 noecho(); 1258 noecho();
1259 meta(stdscr, TRUE);
1222 timeout(SET_DELAY); 1260 timeout(SET_DELAY);
1223 curVis = curs_set(0); 1261 curVis = curs_set(0);
1224 if (has_colors()) { 1262 if (has_colors()) {
1225 start_color(); 1263 start_color();
1226 1264
1303 int c, cnt = 0; 1341 int c, cnt = 0;
1304 BOOL update = FALSE; 1342 BOOL update = FALSE;
1305 1343
1306 /* Handle several buffered keypresses at once */ 1344 /* Handle several buffered keypresses at once */
1307 do { 1345 do {
1308 c = getch(); 1346 c = wgetch(stdscr);
1309 switch (c) { 1347 switch (c) {
1310 case KEY_RESIZE: 1348 case KEY_RESIZE:
1311 if (!initializeWindows()) { 1349 if (!initializeWindows()) {
1312 THERR("Error resizing ncurses windows\n"); 1350 THERR("Error resizing ncurses windows\n");
1313 isError = TRUE; 1351 isError = TRUE;