char* find_lcs(const char *a, int na, const char *b, int nb) { if (na > nb) { const char *c; int t; c = a, a = b, b = c; t = na, na = nb, nb = t; } if (na == 0) return alloc_str(1); if (na == 1) { for (int i = 0; i < nb; i++) { if (a[0] == b[i]) return substr(a, 0, 1); } return alloc_str(1); } static int t1[MAXN]; static int t2[MAXN]; int len = lcs_len(a, na, b, nb, t1); if (len == 0) return alloc_str(1); int half_len = na / 2; char *la = substr(a, 0, half_len); char *ra = substr(a, half_len, na - half_len); char *tb = reverse(b, nb); char *ta = reverse(ra, na - half_len); lcs_len(la, half_len, b, nb, t1); lcs_len(ta, na - half_len, tb, nb, t2); int split = -1; for (int i = 0; i <= nb; i++) { if (t1[i] + t2[nb-i] == len) { split = i; break; } } char *lb = substr(b, 0, split); char *rb = substr(b, split, nb - split); char *sl = find_lcs(la, half_len, lb, split); char *sr = find_lcs(ra, na - half_len, rb, nb - split); char *ret = cat(sl, sr); free(la), free(ra), free(ta); free(lb), free(rb), free(tb); free(sl), free(sr); return ret; }
char* cat(const char *sa, const char *sb) { int na = strlen(sa), nb = strlen(sb); char *t = alloc_str(na + nb + 1); memcpy(t, sa, na); memcpy(t+na, sb, nb); return t; }
void parse_query_string_len(nvlist_t *vars, const char *qs, size_t len) { struct qstr *name, *val; const char *cur, *end; int state; if (!qs || !len) return; name = alloc_str(VAR_NAME_MAXLEN); val = alloc_str(64 * 1024); ASSERT(name); ASSERT(val); state = QS_STATE_NAME; end = qs + len; cur = qs; for (; end > cur; cur++) { char c = *cur; if (state == QS_STATE_NAME) { if (c == '=') state = QS_STATE_VAL; else ASSERT0(append_char_str(name, c)); } else if (state == QS_STATE_VAL) { if (c == '&') { insert(vars, name, val); state = QS_STATE_NAME; reset_str(name); reset_str(val); } else { ASSERT0(append_char_str(val, c)); } } } if (state == QS_STATE_VAL) insert(vars, name, val); free_str(name); free_str(val); }
void str_test() { my_str *strp = alloc_str(test, strlen(test)); printf("content: %s, len: %d\n", STR_CONTENT(strp), STR_LENGTH(strp) + 1); free_str(strp); }
static void setup_trace_buffer(void) { if (vm_get_exec_env()->trace_buffer) return; vm_get_exec_env()->trace_buffer = alloc_str(); if (!vm_get_exec_env()->trace_buffer) error("out of memory"); }
const char * BWString::copy_str( const char * s) { if(s) { size_t len = strnlen(s, __BWString__MAX_LEN); alloc_str(len); strncpy((char *)_str, s, len); _str_len = len; } return _str; }
static void assert_tree_print(struct string *expected, struct tree_node *root) { struct string *str = alloc_str(); tree_print(root, str); assert_str_equals(expected->value, str->value); free_str(str); free_str(expected); }
TfwStr * make_plain_str(const char *data) { TfwStr *s = alloc_str(); s->len = strlen(data); s->ptr = (void *)data; return s; }
struct string *str_aprintf(const char *fmt, ...) { va_list ap; struct string *str; str = alloc_str(); va_start(ap, fmt); str_vappend(str, fmt, ap); va_end(ap); return str; }
void show_function(void *addr) { struct string *str; str = alloc_str(); if (show_exe_function(addr, str)) { trace_printf("%s", str->value); } else { trace_printf("<unknown>\n"); } free_str(str); }
int main(void) { printf("*****testing hash table*****\n\n"); htbl* t = ht_new(&good_hash, 10); FILE* cnets2015 = fopen("cnets2015", "r"); while (!feof(cnets2015)) { char* s = alloc_str(256); fgets(s, 256, cnets2015); char* l = trim_newline(s); free(s); if (strlen(l) > 0) { ht_ins(l, t); } free(l); } fclose(cnets2015); ht_show(t); printf("The hash table has %u buckets with %u entries (load factor %lg).\n\n", t->n_buckets, ht_n_entries(t), ht_load_factor(t)); printf("The bucket with the most items in it contains %u items.\n\n", ht_max_bucket(t)); printf("ht_member:\n"); printf("membership of cnet \"aardvark\" : %i\n", ht_member("aardvark", t)); printf("membership of cnet \"borja\" : %i\n", ht_member("borja", t)); printf("\n"); // resize printf("***now resizing***\n"); ht_resize(&t); printf("***resizing complete***\n\n"); // do everything again ht_show(t); printf("The hash table has %u buckets with %u entries (load factor %lg).\n\n", t->n_buckets, ht_n_entries(t), ht_load_factor(t)); printf("The bucket with the most items in it contains %u items.\n\n", ht_max_bucket(t)); printf("ht_member:\n"); printf("membership of cnet \"aardvark\" : %i\n", ht_member("aardvark", t)); printf("membership of cnet \"borja\" : %i\n", ht_member("borja", t)); printf("\n"); ht_free(t); return 0; }
TfwStr * make_compound_str2(const char *data1, const char *data2) { TfwStr *str, *chunk; str = alloc_str(); str->len = strlen(data1); str->ptr = (void *)data1; chunk = tfw_str_add_compound(str_pool, str); if (!chunk) return NULL; chunk->len = strlen(data2); chunk->ptr = (void *)data2; str->len = strlen(data1) + strlen(data2); return str; }
TfwStr * make_compound_str(const char *data) { TfwStr *str, *chunk; size_t chunk_len = 1; size_t total_len = strlen(data); str = alloc_str(); str->len = min(total_len, chunk_len); str->ptr = (void *)data; for (total_len -= str->len; total_len > 0; total_len -= chunk->len) { chunk = tfw_str_add_compound(str_pool, str); if (!chunk) return NULL; chunk->len = min(total_len, ++chunk_len % 8); chunk->ptr = (void *)(data + str->len); str->len += chunk->len; } return str; }
int folder_menu(char *path, int mode) { FILE *folder; register article_header *ah; news_header_buffer dgbuf; char buffer[256]; int more, length, re, menu_cmd, was_raw; memory_marker mem_marker; group_header fake_group; int cc_save; char folder_name[FILENAME], folder_file[FILENAME]; int orig_layout; char *orig_hdr_lines; orig_layout = fmt_linenum; orig_hdr_lines = header_lines; strcpy(folder_name, path); fake_group.group_name = folder_name; fake_group.kill_list = NULL; if (!expand_file_name(folder_file, folder_name, 1)) return ME_NO_REDRAW; fake_group.archive_file = path = folder_file; fake_group.next_group = fake_group.prev_group = NULL; fake_group.group_flag = G_FOLDER | G_FAKED; fake_group.master_flag = 0; fake_group.save_file = NULL; current_group = NULL; init_group(&fake_group); folder = open_file(path, OPEN_READ); if (folder == NULL) { msg("%s not found", path); return ME_NO_REDRAW; } switch (get_folder_type(folder)) { case 0: msg("Reading: %-.65s", path); break; case 1: case 2: msg("Reading %s folder: %-.50s", current_folder_type == 1 ? "mail" : "mmdf", path); break; default: msg("Folder is empty"); fclose(folder); return ME_NO_REDRAW; } rewind(folder); was_raw = no_raw(); s_keyboard = 0; current_group = &fake_group; mark_memory(&mem_marker); ah = alloc_art(); more = 1; while (more && (more = get_digest_article(folder, dgbuf)) >= 0) { if (s_keyboard) break; ah->a_number = 0; ah->flag = A_FOLDER; ah->attr = 0; ah->lines = digest.dg_lines; ah->hpos = digest.dg_hpos; ah->fpos = digest.dg_fpos; ah->lpos = digest.dg_lpos; if (digest.dg_from) { length = pack_name(buffer, digest.dg_from, Name_Length); ah->sender = alloc_str(length); strcpy(ah->sender, buffer); ah->name_length = length; if (mode == 1) fold_string(ah->sender); } else { ah->sender = ""; ah->name_length = 0; } if (digest.dg_subj) { length = pack_subject(buffer, digest.dg_subj, &re, 255); ah->replies = re; ah->subject = alloc_str(length); strcpy(ah->subject, buffer); ah->subj_length = length; if (mode == 1 && length > 1) fold_string(ah->subject + 1); } else { ah->replies = 0; ah->subject = ""; ah->subj_length = 0; } ah->t_stamp = digest.dg_date ? pack_date(digest.dg_date) : 0; add_article(ah); ah = alloc_art(); } fclose(folder); if (s_keyboard) { menu_cmd = ME_NO_REDRAW; } else if (n_articles == 0) { msg("Not a folder (no article header)"); menu_cmd = ME_NO_REDRAW; } else { if (n_articles > 1) { clrdisp(); prompt_line = 2; if (mode == 0 && !dont_sort_folders) sort_articles(-1); else if (mode == 1) { article_number n; for (n = 0; n < n_articles; n++) { ah = articles[n]; if (n == 0) ah->flag |= A_ROOT_ART; else if (strcmp(ah->sender, articles[n - 1]->sender) == 0) articles[n - 1]->flag |= A_NEXT_SAME; else ah->flag |= A_ROOT_ART; } bypass_consolidation = consolidated_manual ? 2 : 1; } else no_sort_articles(); } else no_sort_articles(); cc_save = cancel_count; cancel_count = 0; if (mode == 1) { fmt_linenum = -1; header_lines = "*"; } reenter_menu: ignore_fancy_select = 1; menu_cmd = menu(folder_header); ignore_fancy_select = 0; if (mode == 0 && cancel_count) { register article_number cur; cancel_count = 0; for (cur = 0; cur < n_articles; cur++) { if (articles[cur]->attr == A_CANCEL) cancel_count++; } } if (mode == 0 && cancel_count) { clrdisp(); tprintf("\rFolder: %s\n\rFile: %s\n\n\r", folder_name, folder_file); if (cancel_count == n_articles) tprintf("Cancel all articles and remove folder? "); else tprintf("Remove %d article%s from folder? ", cancel_count, plural((long) cancel_count)); fl; switch (yes(1)) { case 1: tprintf("\n\n"); if (cancel_count == n_articles) { if (unlink(group_path_name) < 0 && nn_truncate(group_path_name, (off_t) 0) < 0) { tprintf("\rCould not unlink %s\n\r", group_path_name); any_key(0); } } else rewrite_folder(); break; case 0: break; default: goto reenter_menu; } } cancel_count = cc_save; } release_memory(&mem_marker); if (fmt_linenum == -1) { fmt_linenum = orig_layout; header_lines = orig_hdr_lines; } if (was_raw) nn_raw(); return menu_cmd; }
char* substr(const char *s, int pos, int len) { char *t = alloc_str(len+1); memcpy(t, s+pos, len); return t; }
/* main: * read an optional size argument for hash table, * read in strings from standard input, * build hash table, * display hash table and a few statistics */ int main(int argc, char *argv[]) { htbl *t; int sz = DEFAULT_SIZE; int use_bad_hash = 0; char c; /* note: read about "getopt" to learn about command-line arg processing */ while ((c=getopt(argc,argv,"bs:"))!=-1) { switch (c) { case 'b': use_bad_hash = 1; break; case 's': sz = atoi(optarg); if (sz<1) { fprintf(stderr,"%s error: nonpositive table size %d\n",*argv,sz); exit(1); } break; case '?': usage(*argv); exit(1); } } /* build a new empty hash table */ t = htbl_new(use_bad_hash?&bad_hash:&good_hash,sz); /* read a line at a time from stdin, add non-empty strings to the table */ while (!feof(stdin)) { char *s = alloc_str(MAX_LINE_LEN); char *l; unsigned int n; fgets(s,MAX_LINE_LEN,stdin); l = trim_newline(s); free(s); if (strlen(l)>0) htbl_ins(l,t,&n); free(l); } /*unsigned int n = 0; char* s = "FORK"; htbl_ins(s, t, &n); */ htbl_show(stdout,t); printf("\nThe hash table has %i buckets with %i entries (load factor %lg).\n", t->n_buckets, htbl_num_entries(t), htbl_load_factor(t)); unsigned int max, max_index; htbl_max_bucket(t,&max,&max_index); printf("\nThe bucket with the most items in it contains %u items (bucket %u).\n", max,max_index); /* test hash table membership */ /* "aardvark" is not in the cnet file; "borja" is */ printf("\ntesting htbl_member:\n"); printf("membership of cnet \"aardvark\" (expecting 0): %i\n", htbl_member("aardvark",t)); printf("membership of cnet \"borja\" (expecting 1): %i\n", htbl_member("borja",t)); htbl_free(t); return 0; }
static ERL_NIF_TERM re2_match_ret_vlist(ErlNifEnv* env, const autohandle<re2::RE2>& re, const re2::StringPiece& s, const matchoptions& opts, std::vector<re2::StringPiece>& group, int n) { std::vector<ERL_NIF_TERM> vec; const std::map<std::string, int>& nmap = re->NamedCapturingGroups(); ERL_NIF_TERM VL,VH,VT; // empty StringPiece for unfound ValueIds const re2::StringPiece empty; for (VL=opts.vlist; enif_get_list_cell(env, VL, &VH, &VT); VL=VT) { int nid = 0; if (enif_get_int(env, VH, &nid) && nid > 0) { // ValueID int() if (nid < n) { const re2::StringPiece match = group[nid]; ERL_NIF_TERM res; if (!match.empty()) res = mres(env, s, group[nid], opts.ct); else res = mres(env, s, empty, opts.ct); if (enif_is_identical(res, a_err_alloc_binary)) return error(env, a_err_alloc_binary); else vec.push_back(res); } else { vec.push_back(mres(env, s, empty, opts.ct)); } } else if (enif_is_atom(env, VH)) { // ValueID atom() unsigned atom_len; char *a_id = alloc_atom(env, VH, &atom_len); if (a_id == NULL) return error(env, a_err_enif_alloc); if (enif_get_atom(env, VH, a_id, atom_len, ERL_NIF_LATIN1) > 0) { std::map<std::string, int>::const_iterator it = nmap.find(a_id); ERL_NIF_TERM res; if (it != nmap.end()) res = mres(env, s, group[it->second], opts.ct); else res = mres(env, s, empty, opts.ct); if (enif_is_identical(res, a_err_alloc_binary)) return error(env, a_err_alloc_binary); else vec.push_back(res); } else { enif_free(a_id); return error(env, a_err_get_atom); } enif_free(a_id); } else { // ValueID string() unsigned str_len; char *str_id = alloc_str(env, VH, &str_len); if (str_id == NULL) return error(env, a_err_enif_alloc); if (enif_get_string(env, VH, str_id, str_len, ERL_NIF_LATIN1) > 0) { std::map<std::string, int>::const_iterator it = nmap.find(str_id); ERL_NIF_TERM res; if (it != nmap.end()) res = mres(env, s, group[it->second], opts.ct); else res = mres(env, s, empty, opts.ct); if (enif_is_identical(res, a_err_alloc_binary)) return error(env, a_err_alloc_binary); else vec.push_back(res); } else { enif_free(str_id); return error(env, a_err_get_string); } enif_free(str_id); } } ERL_NIF_TERM list = enif_make_list_from_array(env,&vec[0],vec.size()); return enif_make_tuple2(env, a_match, list); }
LXDATA* lx__convert_data(const Protocol::Package *in, LXDATA *prealloc) { if (in == NULL) return NULL; LXDATA *out; if (prealloc == NULL) out = (LXDATA*) malloc(sizeof(struct lx_data)); else out = prealloc; int i, size; const Protocol::ByteBuffer *bb; switch (in->get_type()) { case Protocol::VOID_PACKAGE: out->type = LX_DATATYPE_VOID; out->value.intval = 0; break; case Protocol::SINT32_PACKAGE: out->type = LX_DATATYPE_INT; out->value.intval = ((Protocol::Sint32Package*)in)->get_val_value(); break; case Protocol::BOOL_PACKAGE: out->type = LX_DATATYPE_BOOL; out->value.intval = ((Protocol::BoolPackage*)in)->get_val_value(); break; case Protocol::DOUBLE_PACKAGE: out->type = LX_DATATYPE_DOUBLE; out->value.doubleval = ((Protocol::DoublePackage*)in)->get_val_value(); break; case Protocol::VARCHAR_PACKAGE: out->type = LX_DATATYPE_VARCHAR; bb = &((Protocol::VarcharPackage*)in)->get_val_value(); out->value.varcharval = alloc_str(bb->get_const_data(), bb->get_size()); out->value.varcharlen = bb->get_size(); break; case Protocol::REF_PACKAGE: out->type = LX_DATATYPE_REF; out->value.array = (LXDATA*) malloc(sizeof(struct lx_data)); out->value.refval = ((Protocol::RefPackage*)in)->get_val_value_id(); break; case Protocol::BAG_PACKAGE: out->type = LX_DATATYPE_BAG; size = ((Protocol::BagPackage*)in)->get_packages().size(); out->value.length = size; out->value.array = (LXDATA*) malloc(size * sizeof(struct lx_data)); for (i = 0; i < size; i++) lx__convert_data(((Protocol::BagPackage*)in)->get_packages()[i].get(), &out->value.array[i]); break; case Protocol::STRUCT_PACKAGE: out->type = LX_DATATYPE_STRUCT; size = ((Protocol::StructPackage*)in)->get_packages().size(); out->value.length = size; out->value.array = (LXDATA*) malloc(size * sizeof(struct lx_data)); for (i = 0; i < size; i++) lx__convert_data(((Protocol::StructPackage*)in)->get_packages()[i].get(), &out->value.array[i]); break; case Protocol::SEQUENCE_PACKAGE: out->type = LX_DATATYPE_SEQ; size = ((Protocol::SequencePackage*)in)->get_packages().size(); out->value.length = size; out->value.array = (LXDATA*) malloc(size * sizeof(struct lx_data)); for (i = 0; i < size; i++) lx__convert_data(((Protocol::SequencePackage*)in)->get_packages()[i].get(), &out->value.array[i]); break; case Protocol::BINDING_PACKAGE: out->type = LX_DATATYPE_BIND; out->value.length = 2; out->value.array = (LXDATA*) malloc(2 * sizeof(struct lx_data)); bb = &((Protocol::BindingPackage*)in)->get_val_binding_name(); out->value.array[0].type = LX_DATATYPE_VARCHAR; out->value.array[0].value.varcharval = alloc_str(bb->get_const_data(), bb->get_size()); out->value.array[0].value.varcharlen = bb->get_size(); lx__convert_data(&((Protocol::BindingPackage*)in)->get_val_value(), &out->value.array[1]); break; default: out->type = LX_DATATYPE_VOID; out->value.intval = -1; } return out; }