static BomList * bom_insert (char *refdes, char *descr, char *value, BomList * bom) { BomList *newlist, *cur, *prev = NULL; if (bom == NULL) { /* this is the first element so automatically create an entry */ if ((newlist = (BomList *) malloc (sizeof (BomList))) == NULL) { fprintf (stderr, "malloc() failed in bom_insert()\n"); exit (1); } newlist->next = NULL; newlist->descr = strdup (descr); newlist->value = strdup (value); newlist->num = 1; newlist->refdes = string_insert (refdes, NULL); return (newlist); } /* search and see if we already have used one of these components */ cur = bom; while (cur != NULL) { if ((NSTRCMP (descr, cur->descr) == 0) && (NSTRCMP (value, cur->value) == 0)) { cur->num++; cur->refdes = string_insert (refdes, cur->refdes); break; } prev = cur; cur = cur->next; } if (cur == NULL) { if ((newlist = (BomList *) malloc (sizeof (BomList))) == NULL) { fprintf (stderr, "malloc() failed in bom_insert()\n"); exit (1); } prev->next = newlist; newlist->next = NULL; newlist->descr = strdup (descr); newlist->value = strdup (value); newlist->num = 1; newlist->refdes = string_insert (refdes, NULL); } return (bom); }
void format_number(string s, u64 x, int base, int pad) { if ((x > 0) || (pad > 0)) { format_number(s, x/base, base, pad - 1); string_insert(s, hex_digits[x%base]); } }
int string_replace(string *s, size_t pos, size_t len, char *data) { int e; e = string_erase(s, pos, len); if (e == -1) return -1; return string_insert(s, pos, data); }
/* ED: Edit Delete ('delete') */ void cmd_ed (int argc, char *argv[]) { pad_t *pad = e->cpad; int xpos; int ypos; line_t *cline; line_t *cline2; int intab; int len; if (!(e->cpad->flags & FILE_WRITE)) { output_message_c ("ed", "Pad is read-only."); return; } ypos = pad->curs_y + pad->offset_y; if (ypos > pad->line_count) { pad_grow (pad, ypos); pad_modified (pad); return; } cline = LINE_get_line_at (pad, ypos); len = xpos = 0; if (cline->str) { xpos = get_string_pos (pad->curs_x + pad->offset_x, cline->str->data, &intab); len = strlen (cline->str->data); } if (xpos < len) { string_remove (cline->str, xpos, 1); } else { cline2 = LINE_get_line_at (pad, ypos + 1); if (cline2 != NULL) { if (cline2->str != NULL) { if (cline->str == NULL) cline->str = string_alloc (""); if (cline->str) string_insert (cline->str, xpos, "%s", cline2->str->data); } LINE_remove (pad, cline2); } } pad_modified (pad); }
void add_space (char* str, int length) { int max_spaces = max_length - length; //счётчик недостающих пробелов int i=0; int spaces=0; int spaces_left=0; while (i!=length) { if (str[i]==' ') spaces++; i++; } if (spaces != 0) { int k = max_spaces/spaces; //кол-во пробелов на промежуток spaces_left = max_spaces % spaces; for (i = 0; i<=max_length; i++) { if (max_spaces != 0) if (str[i]==' ') for (int j=1; j<=k; j++) { string_insert(str,' ',i+1); i++; } } } i = string_length(str); if (i < max_length) { while (str[i]!=' ') i--; while (spaces_left != 0) { string_insert(str,' ',i+1); spaces_left--; } } }
void vbprintf(string s, string fmt, va_list ap) { character i; int state = 0; int base = 0; int pad; int count = 0; string_foreach(fmt, i) { switch (state){ case 2: for (int j = 0; j < count; j++) string_insert(s, i); state = 0; break; case 0: base = 10; pad = 0; if (i == '%') state = 3; else string_insert(s, i); break; case 1: if ((i >= '0') && (i <= '9')) { pad = pad * 10 + digit_of(i); break; } else { state = 3; } case 3: switch (i) { case '0': state = 1; break; case '%': string_insert(s, '\%'); break; case 't': print_time(s, va_arg(ap, ticks)); break; case 'b': string_concat(s, (va_arg(ap, string))); break; case 'n': count = va_arg(ap, unsigned int); state = 2; break; case 'c': string_insert(s, va_arg(ap, int)); break; case 's': { char *c = va_arg(ap, char *); if (!c) c = (char *)"(null)"; int len = cstring_length(c); for (int i =0 ; i < pad; i++) string_insert(s, ' '); pad = 0; for (; *c; c++) string_insert(s, *c); } break; case 'S': { unsigned int x = va_arg(ap, unsigned int); for (int i =0 ; i < x; i++) string_insert(s, ' '); break; } case 'p': pad = 16; unsigned long x = va_arg(ap, unsigned long); format_number(s, x, 16, pad?pad:1); break; case 'l': pad = 0; unsigned long z = va_arg(ap, unsigned long); format_number(s, z, 10, pad?pad:1); break; case 'x': base=16; case 'o': if (base == 10) base=8; case 'u': { unsigned int x = va_arg(ap, unsigned int); format_number(s, x, base, pad?pad:1); break; } // xxx - layer violation..meh // also generalize string pad support case 'v': if (pad) { // xxx transient or resizable stack head buffer b = allocate_string(s->h); print_value(b, va_arg(ap, void *)); // xxx utf token length for (int i =0 ; i < (pad-buffer_length(b)); i++) string_insert(s, ' '); buffer_append(s, bref(b, 0), buffer_length(b)); pad = 0; state = 0; } else print_value(s, va_arg(ap, void *)); break; case 'r': if (pad) { // xxx transient or resizable stack head buffer b = allocate_string(s->h); print_value_raw(b, va_arg(ap, void *)); // xxx utf token length for (int i =0 ; i < (pad-buffer_length(b)); i++) string_insert(s, ' '); buffer_append(s, bref(b, 0), buffer_length(b)); pad = 0; state = 0; } else print_value_raw(s, va_arg(ap, void *)); break; case 'V': print_value_vector(s, va_arg(ap, void *)); break; case 'X': // xxx - utf8 will break this { buffer xx = va_arg(ap, buffer); string_foreach(xx, i){ print_byte(s, i); } } break; case 'd': case 'i': { int x = va_arg(ap, int); if (x <0){ string_insert(s, '-'); x = -x; } format_number(s, (unsigned int)x, base, pad?pad:1); break; } default: break; } // badness if (state == 3) state = 0; break; } }
int input_box(char* msg, char* result, int result_size, char* def_input, int def_cursor) { gInputBoxMsg = msg; int result2 = 0; gInputBoxCursor = def_cursor; string_put(gInputBoxInput, def_input); gView = input_box_view; while(1) { xclear(); view(); //input_box_view(); refresh(); /// input /// int meta; int key = xgetch(&meta); if(key == 10 || key == 13) { result2 = 0; break; } else if(key == 6 || key == KEY_RIGHT) { input_box_cursor_move(gInputBoxInput, &gInputBoxCursor, 1); } else if(key == 2 || key == KEY_LEFT) { input_box_cursor_move(gInputBoxInput, &gInputBoxCursor, -1); } else if(key == 8 || key == KEY_BACKSPACE) { // CTRL-H if(gInputBoxCursor > 0) { char* str2 = string_c_str(gInputBoxInput); int utfpos = str_pointer2kanjipos(gKanjiCode, str2, str2 + gInputBoxCursor); char* before_point = str_kanjipos2pointer(gKanjiCode, str2, utfpos-1); int new_cursor = before_point-str2; string_erase(gInputBoxInput, before_point - str2, (str2 + gInputBoxCursor) - before_point); gInputBoxCursor = new_cursor; } } else if(key == 4 || key == KEY_DC) { // CTRL-D DELETE char* str2 = string_c_str(gInputBoxInput); if(string_length(gInputBoxInput) > 0) { if(gInputBoxCursor < string_length(gInputBoxInput)) { int utfpos = str_pointer2kanjipos(gKanjiCode, str2, str2 + gInputBoxCursor); char* next_point = str_kanjipos2pointer(gKanjiCode, str2, utfpos+1); string_erase(gInputBoxInput, gInputBoxCursor, next_point - (str2 + gInputBoxCursor)); } } } else if(key == 1 || key == KEY_HOME) { // CTRL-A input_box_cursor_move(gInputBoxInput, &gInputBoxCursor, -999); } else if(key == 5 || key == KEY_END) { // CTRL-E input_box_cursor_move(gInputBoxInput, &gInputBoxCursor, 999); } else if(key == 11) { // CTRL-K string_erase(gInputBoxInput, gInputBoxCursor, string_length(gInputBoxInput)-gInputBoxCursor); } else if(key == 21) { // CTRL-U string_put(gInputBoxInput, ""); gInputBoxCursor = 0; } else if(key == 23) { // CTRL-W if(gInputBoxCursor > 0) { const char* s = string_c_str(gInputBoxInput); int pos = gInputBoxCursor-1; if(s[pos]==' ' || s[pos]=='/' || s[pos]=='\'' || s[pos]=='"') { while(pos>=0 && (s[pos]==' ' || s[pos]=='/' || s[pos]=='\'' || s[pos]=='"')) { pos--; } } while(pos>=0 && s[pos]!=' ' && s[pos]!='/' && s[pos]!='\'' && s[pos]!='"') { pos--; } string_erase(gInputBoxInput, pos+1, gInputBoxCursor-pos-1); gInputBoxCursor = pos+1; } } else if(meta==1 && key == 'd') { // Meta-d const char* s = string_c_str(gInputBoxInput); if(s[gInputBoxCursor] != 0) { int pos = gInputBoxCursor; pos++; while(s[pos]!=0 && (s[pos] == ' ' || s[pos] == '/' || s[pos] == '\'' || s[pos] == '"')) { pos++; } while(s[pos]!=0 && s[pos] != ' ' && s[pos] != '/' && s[pos] != '\'' && s[pos] != '"') { pos++; } string_erase(gInputBoxInput, gInputBoxCursor, pos-gInputBoxCursor); } } else if(meta==1 && key == 'b') { // META-b if(gInputBoxCursor > 0) { const char* s = string_c_str(gInputBoxInput); int pos = gInputBoxCursor; pos--; while(pos>=0 && (s[pos] == ' ' || s[pos] == '/' || s[pos] == '\'' || s[pos] == '"')) { pos--; } while(pos>=0 && s[pos] != ' ' && s[pos] != '/' && s[pos] != '\'' && s[pos] != '"') { pos--; } gInputBoxCursor = pos+1; } } else if(meta==1 && key == 'f') { // META-f const char* s = string_c_str(gInputBoxInput); if(s[gInputBoxCursor] != 0) { int pos = gInputBoxCursor; pos++; while(s[pos]!=0 && (s[pos] == ' ' || s[pos] == '/' || s[pos] == '\'' || s[pos] == '"')) { pos++; } while(s[pos]!=0 && s[pos] != ' ' && s[pos] != '/' && s[pos] != '\'' && s[pos] != '"') { pos++; } gInputBoxCursor = pos; } } else if(key == 3 || key == 7 || key == 27) { // CTRL-C -G Escape result2 = 1; break; } else if(key == 12) { // CTRL-L xclear_immediately(); } else { if(meta == 0 && !(key >= 0 && key <= 27)) { char tmp[128]; snprintf(tmp, 128, "%c", key); string_insert(gInputBoxInput, gInputBoxCursor, tmp); gInputBoxCursor++; } } } gView = NULL; int maxx = mgetmaxx(); int maxy = mgetmaxy(); xstrncpy(result, string_c_str(gInputBoxInput), result_size); mmove_immediately(maxy -2, 0); #if defined(__CYGWIN__) xclear_immediately(); // 画面の再描写 view(); refresh(); #endif return result2; }
int main(int argc, char *argv[]) { string *s = NULL; string *s2 = NULL; string *s3 = NULL; s = string_append(s, "Hello There"); test(s, "Hello There"); s = string_append(s, "123"); test(s, "Hello There123"); s = string_insert(s, 1, "<new>"); test(s, "H<new>ello There123"); s = string_insert(s, 23, "<new>"); test(s, "H<new>ello There123<new>"); s = string_remove(s, 11, 5); test(s, "H<new>ello 123<new>"); s = string_replace(s, "<new>", "<nothing>"); test(s, "H<nothing>ello 123<nothing>"); s = string_remove(s, 0, 30); test(s, ""); s = string_append(s, "H<nothing>ello 123<nothing>"); test(s, "H<nothing>ello 123<nothing>"); s = string_trunc(s); test(s, ""); string_free(&s); s = string_append_int(s, 1); test(s, "1"); s = string_append_int(s, 10); test(s, "110"); s = string_append_int(s, 100); test(s, "110100"); s = string_append_int(s, 1000000000); test(s, "1101001000000000"); s = string_append_int(s, -2); test(s, "1101001000000000-2"); s = string_append_int(s, -345); test(s, "1101001000000000-2-345"); s = string_append_int(s, -3.45); test(s, "1101001000000000-2-345-3"); s = string_trunc(s); test(s,""); s = string_prepend_int(s, 1); test(s, "1"); s = string_prepend_int(s, 10); test(s, "101"); s = string_prepend_int(s, 100); test(s, "100101"); s = string_prepend_int(s, 1000000000); test(s, "1000000000100101"); s = string_prepend_int(s, -2); test(s, "-21000000000100101"); s = string_prepend_int(s, -345); test(s, "-345-21000000000100101"); s = string_prepend_int(s, -3.45); test(s, "-3-345-21000000000100101"); s = string_trunc(s); test(s,""); string_free(&s); s = string_new("\"Quoted String\""); test(s, "\"Quoted String\""); s = string_replace(s, "\"", ""); test(s, "Quoted String"); string_free(&s); s = string_prepend(s, "Nothing"); test(s, "Nothing"); s = string_prepend(s, "12345678901234567890"); test(s, "12345678901234567890Nothing"); s = string_prepend(s, "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); test(s, "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890Nothing"); string_free(&s); s = string_append(s, "Nothing"); test(s, "Nothing"); s = string_append(s, "12345678901234567890"); test(s, "Nothing12345678901234567890"); s = string_append(s, "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); test(s, "Nothing12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); string_free(&s); s = string_new("printf_append"); test(s, "printf_append"); s = string_printf_append(s, "%8s %3d %10.6f", "string", 20, 3.4); test(s, "printf_append string 20 3.400000"); string_free(&s); s = string_new(""); s = string_printf(s, "%8s %3d %10.6f", "string", 20, 3.4); test(s, " string 20 3.400000"); s2 = string_copy(s); test(s2, " string 20 3.400000"); testi(string_equal_char(s, string_string(s2)), 1); testi(string_equal(s, s2), 1); testi(string_equal_char(s, " string 20 3.40"), 0); s3 = string_new(" string 20 3.40"); testi(string_equal(s, s3), 0); string_free(&s2); string_free(&s3); s = string_remove(s, 100000, 20); test(s, " string 20 3.400000"); s = string_remove(s, 15, 20); test(s, " string 20 "); s = string_remove(s, 13, -1); test(s, " string 20 "); s = string_remove(s, 1, 1); test(s, " string 20 "); s = string_remove(s, 7, 2); test(s, " string20 "); s = string_remove(s, 9, 1); test(s, " string20"); s = string_remove(s, -1, -1); test(s, ""); s = string_insert(s, -1, " string20"); test(s, " string20"); s = string_insert(s, 9, " "); test(s, " string20 "); s = string_insert(s, 7, " "); test(s, " string 20 "); s = string_insert(s, 1, " "); test(s, " string 20 "); s = string_insert(s, 13, " "); test(s, " string 20 "); s = string_insert(s, 15, "3.400000"); test(s, " string 20 3.400000"); s = string_insert(s, 10000, " morestuff"); test(s, " string 20 3.400000 morestuff"); s = string_insert(s, -1, " morestuff"); test(s, " morestuff string 20 3.400000 morestuff"); s2 = string_substr(s, 1, 4); test(s2, "more"); string_free(&s2); s2 = string_substr(s, 5, 7); test(s2, "stuff "); string_free(&s2); string_free(&s); return retval; }