char *Ssfull (Tobj ko, Tobj vo) { sbufp[(sbufi = 0)] = '\000'; if (ko) scalarstr (ko), appends (" = "); switch (Tgettype (vo)) { case T_STRING: case T_INTEGER: case T_REAL: case T_CODE: scalarstr (vo); break; } appends (";"); return copysbuf (); }
static MENUHDL appends(MENUSYS *sys, const MSYSITEM *item) { MENUHDL ret; MENUHDL cur; ret = append1(sys, item); cur = ret; while(1) { if (cur == NULL) { goto ap_err; } if (item->child) { cur->child = appends(sys, item->child); } if (item->flag & MENU_DELETED) { break; } item++; cur->next = append1(sys, item); cur = cur->next; } return(ret); ap_err: return(NULL); }
static void scalarstr (Tobj to) { switch (Tgettype (to)) { case T_INTEGER: appendi (Tgetinteger (to)); break; case T_REAL: appendd (Tgetreal (to)); break; case T_STRING: appends ("\""), appends (Tgetstring (to)), appends ("\""); break; case T_CODE: codestr (to, 0); break; } }
char *Sabstract (Tobj ko, Tobj vo) { sbufp[(sbufi = 0)] = '\000'; scalarstr (ko), appends (" = "); switch (Tgettype (vo)) { case T_STRING: case T_INTEGER: case T_REAL: scalarstr (vo); break; case T_CODE: appends ("function (...) { ... }"); break; case T_TABLE: appends ("[ ... ]"); break; } appends (";"); return copysbuf (); }
BRESULT menusys_create(const MSYSITEM *item, void (*cmd)(MENUID id), UINT16 icon, const OEMCHAR *title) { MENUSYS *ret; LISTARRAY r; MENUHDL hdl; ret = &menusys; ZeroMemory(ret, sizeof(MENUSYS)); ret->icon = icon; if (cmd == NULL) { cmd = defcmd; } ret->cmd = cmd; if (title) { milstr_ncpy(ret->title, title, NELEMENTS(ret->title)); } r = listarray_new(sizeof(_MENUHDL), 32); if (r == NULL) { goto mscre_err; } ret->res = r; hdl = appends(ret, s_root); if (hdl == NULL) { goto mscre_err; } ret->root = hdl; if (item) { while(hdl->next) { hdl = hdl->next; } hdl->next = appends(ret, item); } return(SUCCESS); mscre_err: return(FAILURE); }
int serialize_bloom_filter(const char* key, bloom_filter* bf, output_data* output) { output->pos = 0; if (appendf(output, "if %s == nil then %s = bloom_filter.new(%d, %g) end\n", key, key, bf->items, bf->probability)) { return 1; } if (appendf(output, "%s:fromstring(\"", key)) { return 1; } if (serialize_binary(bf->data, bf->bytes, output)) return 1; if (appends(output, "\")\n")) { return 1; } return 0; }
int main(void) { printf("初始化LinkedList及其正反两个方向的迭代器..."); al = list_create(string, LinkedList, NULL); fwd = list_iterator(al, Forward); bwd = list_iterator(al, Reverse); printf("Ok!\n"); printf("TEST1:空列表时的查询\n"); show(); cont(); printf("TEST2: 用append添加一个元素\n"); appends(1); show(); cont(); printf("TEST3: 用remove_at删除一个元素\n"); printf("删除了%zu个元素\n", list_remove_at(al, 0)); show(); cont(); printf("TEST4: 用insert从头部开始连续添加18个元素\n"); inserts(18, 0); show(); int pos = 0; int from = -1; while ((pos = list_search(al, from, Forward, "South Korea", string, 11)) != -1) { printf("正向搜索所有韩国: %d\n", pos); from = pos + 1; } from = -1; while ((pos = list_search(al, from, Reverse, "Brazil", string, 6)) != -1) { printf("反向搜索所有巴西: %d\n", pos); from = pos - 1; } cont(); printf("TEST5: 用remove删除所有Brazil\n"); list_remove(al, "Brazil", string, 6); show(); cont(); printf("TEST6: 用removeall删除所有元素\n"); list_removeall(al); show(); cont(); printf("TEST7: 用push连续添加12个元素后进行递增快速排序\n"); int i, j; for (i = 0; i < 12; i++) { j = rand() % 16; list_push(al, nations[j], string, strlen(nations[j])); } printf("排序前:\n"); type(); printf("排序后:\n"); list_qsort(al, Asc); type(); printf("二分搜索找韩国: %d\n", list_bi_search(al, "South Korea", string, strlen("South Korea"))); printf("二分搜索找中国: %d\n", list_bi_search(al, "中华人民共和国", string, strlen("中华人民共和国"))); cont(); printf("TEST8: 用enqueue连续添加12个元素后进行递减插入排序\n"); for (i = 0; i < 12; i++) { j = rand() % 16; list_enqueue(al, nations[j], string, strlen(nations[j])); } printf("排序前:\n"); type(); printf("排序后:\n"); list_isort(al, Desc); type(); printf("二分搜索找日本: %d\n", list_bi_search(al, "Japan", string, 5)); printf("二分搜索找台湾: %d\n", list_bi_search(al, "中华民国", string, strlen("中华民国"))); printf("反向排列所有元素\n"); list_reverse(al); type(); printf("二分搜索找韩国: %d\n", list_bi_search(al, "South Korea", string, strlen("South Korea"))); printf("二分搜索找中国: %d\n", list_bi_search(al, "中华人民共和国", string, strlen("中华人民共和国"))); printf("二分搜索找日本: %d\n", list_bi_search(al, "Japan", string, 5)); printf("二分搜索找台湾: %d\n", list_bi_search(al, "中华民国", string, strlen("中华民国"))); cont(); printf("TEST9: 用迭代器迭代删除所有元素\n"); Iterator delit = list_iterator(al, Forward); Element ele = NULL; while ((ele = it_next(delit))) { it_remove(delit); printf("删除元素:\"%s\"\n", POINTOF(ele, char)); free(ele); } type(); cont(); printf("TEST10: 模拟堆栈\n"); printf("连续PUSH三次:\n"); j = rand() % 16; list_push(al, nations[j], string, strlen(nations[j])); type(); j = rand() % 16; list_push(al, nations[j], string, strlen(nations[j])); type(); j = rand() % 16; list_push(al, nations[j], string, strlen(nations[j])); type(); printf("用stacktop读取栈顶元素:"); ele = list_stacktop(al); printf(" \"%s\"\n", POINTOF(ele, char)); free(ele); printf("用pop弹空堆栈:\n"); ele = list_pop(al); printf("\"%s\"\n", POINTOF(ele, char)); free(ele); ele = list_pop(al); printf("\"%s\"\n", POINTOF(ele, char)); free(ele); ele = list_pop(al); printf("\"%s\"\n", POINTOF(ele, char)); free(ele); type(); cont(); printf("TEST11: 模拟队列\n"); printf("连续enqueue三次:\n"); j = rand() % 16; list_enqueue(al, nations[j], string, strlen(nations[j])); type(); j = rand() % 16; list_enqueue(al, nations[j], string, strlen(nations[j])); type(); j = rand() % 16; list_enqueue(al, nations[j], string, strlen(nations[j])); type(); printf("用queuehead读取栈顶元素:"); ele = list_queuehead(al); printf(" \"%s\"\n", POINTOF(ele, char)); free(ele); printf("用dequeue全部出队:\n"); ele = list_dequeue(al); printf("\"%s\"\n", POINTOF(ele, char)); free(ele); ele = list_dequeue(al); printf("\"%s\"\n", POINTOF(ele, char)); free(ele); ele = list_dequeue(al); printf("\"%s\"\n", POINTOF(ele, char)); free(ele); type(); cont(); printf("TEST12: 两个列表相加\n"); Container list2 = list_create(string, LinkedList, NULL); appends(9); printf("原列表:\n"); type(); printf("加上一个空列表:\n"); list_plus(al, list2); type(); printf("加上一个有9个元素的列表:\n"); for (i = 0; i < 9; i++) { j = rand() % 16; list_append(list2, nations[j], string, strlen(nations[j])); } list_plus(al, list2); type(); cont(); printf("再减去这个列表:\n"); list_minus(al, list2); type(); printf("再减去一个空列表:\n"); Container empty = list_create(string, LinkedList, NULL); list_minus(al, empty); type(); cont(); printf("添加到18个元素后再进行retain操作,类似取交集\n"); appends(18); printf("原列表:\n"); type(); printf("list2:\n"); for (i = 0; i < 9; i++) { ele = list_get(list2, i); printf("%s, ", POINTOF(ele, char)); free(ele); } printf("\n"); list_retain(al, list2); printf("retain后:\n"); type(); printf("retain一个空列表:\n"); list_retain(al, empty); type(); printf("FIN: 销毁列表和迭代器..."); it_destroy(fwd); it_destroy(bwd); list_destroy(al); list_destroy(list2); list_destroy(empty); printf("Ok!\n"); return 0; }
char *Stfull (Tobj ko) { sbufp[(sbufi = 0)] = '\000'; scalarstr (ko), appends (" = ["); return copysbuf (); }
char *Sseen (Tobj ko, char *path) { sbufp[(sbufi = 0)] = '\000'; scalarstr (ko), appends (" = "), appends (path), appends (";"); return copysbuf (); }
char *Spath (char *path, Tobj ko) { sbufp[(sbufi = 0)] = '\000'; appends ((path) ? path : ""); scalarstr (ko); return copysbuf (); }
static void codestr (Tobj co, int ci) { int ct, ct1; int ci1, ci2; if (highci == ci) appends (" >> "); switch ((ct = TCgettype (co, ci))) { case C_ASSIGN: codestr (co, (ci1 = TCgetfp (co, ci))); appends (" = "); codestr (co, TCgetnext (co, ci1)); break; case C_OR: case C_AND: case C_EQ: case C_NE: case C_LT: case C_LE: case C_GT: case C_GE: case C_PLUS: case C_MINUS: case C_MUL: case C_DIV: case C_MOD: codestr (co, (ci1 = TCgetfp (co, ci))); switch (ct) { case C_OR: appends (" | "); break; case C_AND: appends (" & "); break; case C_EQ: appends (" == "); break; case C_NE: appends (" ~= "); break; case C_LT: appends (" < "); break; case C_LE: appends (" <= "); break; case C_GT: appends (" > "); break; case C_GE: appends (" >= "); break; case C_PLUS: appends (" + "); break; case C_MINUS: appends (" - "); break; case C_MUL: appends (" * "); break; case C_DIV: appends (" / "); break; case C_MOD: appends (" % "); break; } codestr (co, TCgetnext (co, ci1)); break; case C_NOT: appends ("~"); codestr (co, TCgetfp (co, ci)); break; case C_UMINUS: appends ("-"); codestr (co, TCgetfp (co, ci)); break; case C_PEXPR: appends ("("); codestr (co, TCgetfp (co, ci)); appends (")"); break; case C_FCALL: codestr (co, (ci1 = TCgetfp (co, ci))); appends (" ("); codestr (co, TCgetnext (co, ci1)); appends (")"); break; case C_INTEGER: appendi (TCgetinteger (co, ci)); break; case C_REAL: appendd (TCgetreal (co, ci)); break; case C_STRING: appends ("\""), appends (TCgetstring (co, ci)), appends ("\""); break; case C_GVAR: case C_LVAR: ci1 = TCgetfp (co, ci); appends (TCgetstring (co, ci1)); if (ct == C_LVAR) ci1 = TCgetnext (co, ci1); for ( ci1 = TCgetnext (co, ci1); ci1 != C_NULL; ci1 = TCgetnext (co, ci1) ) { switch (TCgettype (co, ci1)) { case C_STRING: appends ("."), appends (TCgetstring (co, ci1)); break; case C_INTEGER: appends ("["); appendi (TCgetinteger (co, ci1)); appends ("]"); break; case C_REAL: appends ("["); appendd (TCgetreal (co, ci1)); appends ("]"); break; default: appends ("["); codestr (co, ci1); appends ("]"); } } break; case C_PVAR: appends ("<var>"); break; case C_FUNCTION: ci1 = TCgetnext (co, TCgetnext (co, TCgetfp (co, ci))); appends ("function ("); codestr (co, ci1); ci1 = TCgetnext (co, ci1); if (TCgettype (co, ci1) == C_INTERNAL) { appends (") internal \""); appends (Ifuncs[TCgetinteger (co, TCgetfp (co, ci1))].name); appends ("\""); } else { appends (") {"); INDINC (2); for (; ci1 != C_NULL; ci1 = TCgetnext (co, ci1)) { appendnl (); if (TCgettype (co, ci1) == C_DECL) appends ("local "), codestr (co, ci1), appends (";"); else codestr (co, ci1); } INDDEC (2); appendnl (); appends ("}"); } break; case C_TCONS: appends ("["); INDINC (2); ci1 = TCgetfp (co, ci); while (ci1 != C_NULL) { appendnl (); codestr (co, ci1); appends (" = "); ci1 = TCgetnext (co, ci1); codestr (co, ci1); appends (";"); ci1 = TCgetnext (co, ci1); } INDDEC (2); appendnl (); appends ("]"); break; case C_DECL: ci1 = TCgetfp (co, ci); while (ci1 != C_NULL) { appends (TCgetstring (co, ci1)); ci1 = TCgetnext (co, ci1); if (ci1 != C_NULL) appends (", "); } break; case C_STMT: ci1 = TCgetfp (co, ci); if (ci1 == C_NULL) { appends (";"); break; } if (TCgetnext (co, ci1) == C_NULL) { codestr (co, ci1); ct1 = TCgettype (co, ci1); if (!C_ISSTMT (ct1)) appends (";"); } else { appends (" {"); INDINC (2); for (; ci1 != C_NULL; ci1 = TCgetnext (co, ci1)) { appendnl (); codestr (co, ci1); } INDDEC (2); appendnl (); appends ("}"); } break; case C_IF: ci1 = TCgetfp (co, ci); appends ("if ("); codestr (co, ci1); appends (")"); ci1 = TCgetnext (co, ci1); ci2 = TCgetfp (co, ci1); if (ci2 == C_NULL || TCgetnext (co, ci2) == C_NULL) { INDINC (2); appendnl (); codestr (co, ci1); INDDEC (2); } else { codestr (co, ci1); } ci1 = TCgetnext (co, ci1); if (ci1 == C_NULL) break; if (ci2 == C_NULL || TCgetnext (co, ci2) == C_NULL) { appendnl (); appends ("else"); } else { appends (" else"); } ci2 = TCgetfp (co, ci1); if (ci2 == C_NULL || TCgetnext (co, ci2) == C_NULL) { INDINC (2); appendnl (); codestr (co, ci1); INDDEC (2); } else { codestr (co, ci1); } break; case C_WHILE: ci1 = TCgetfp (co, ci); appends ("while ("); codestr (co, ci1); ci1 = TCgetnext (co, ci1); ci2 = TCgetfp (co, ci1); if (ci2 == C_NULL || TCgetnext (co, ci2) == C_NULL) { appends (")"); INDINC (2); appendnl (); codestr (co, ci1); INDDEC (2); } else { appends (")"); codestr (co, ci1); } break; case C_FOR: ci1 = TCgetfp (co, ci); appends ("for ("); codestr (co, ci1); appends ("; "); ci1 = TCgetnext (co, ci1); codestr (co, ci1); appends ("; "); ci1 = TCgetnext (co, ci1); codestr (co, ci1); ci1 = TCgetnext (co, ci1); ci2 = TCgetfp (co, ci1); if (ci2 == C_NULL || TCgetnext (co, ci2) == C_NULL) { appends (")"); INDINC (2); appendnl (); codestr (co, ci1); INDDEC (2); } else { appends (")"); codestr (co, ci1); } break; case C_FORIN: ci1 = TCgetfp (co, ci); appends ("for ("); codestr (co, ci1); appends (" in "); ci1 = TCgetnext (co, ci1); codestr (co, ci1); ci1 = TCgetnext (co, ci1); ci2 = TCgetfp (co, ci1); if (ci2 == C_NULL || TCgetnext (co, ci2) == C_NULL) { appends (")"); INDINC (2); appendnl (); codestr (co, ci1); INDDEC (2); } else { appends (")"); codestr (co, ci1); } break; case C_BREAK: appends ("break;"); break; case C_CONTINUE: appends ("continue;"); break; case C_RETURN: ci1 = TCgetfp (co, ci); appends ("return"); if (ci1 != C_NULL) { appends (" "); codestr (co, ci1); } appends (";"); break; case C_ARGS: ci1 = TCgetfp (co, ci); while (ci1 != C_NULL) { codestr (co, ci1); ci1 = TCgetnext (co, ci1); if (ci1 != C_NULL) appends (", "); } break; default: panic1 (POS, "codestr", "bad object type: %d", ct); } }
static void append_value(lua_State *L, write_state *state) { char numbuf[NUM_BUF_SIZE]; int ltype = lua_type(L, -1); switch (ltype) { case LUA_TNIL: { appends("null", state); break; } case LUA_TNUMBER: { LTdouble n = lua_tonumber(L, -1); snprintf(numbuf, NUM_BUF_SIZE, "%.14g", n); appends(numbuf, state); break; } case LUA_TBOOLEAN: { int b = lua_toboolean(L, -1); if (b) appends("true", state); else appends("false", state); break; } case LUA_TSTRING: { const char *s = lua_tostring(L, -1); appendc('"', state); while (*s != 0) { switch (*s) { case '\n': appends("\\n", state); break; case '\r': appends("\\r", state); break; case '\t': appends("\\t", state); break; case '\b': appends("\\b", state); break; case '\f': appends("\\f", state); break; case '\\': appends("\\\\", state); break; case '\"': appends("\\\"", state); break; default: appendc(*s, state); break; } s++; } appendc('"', state); break; } case LUA_TTABLE: { int is_array = 1; lua_pushnil(L); if (!lua_next(L, -2)) { // empty table. write out as empty array, since // that seems like it would be the more common case. appends("[]", state); break; } else { if (lua_type(L, -2) == LUA_TSTRING) is_array = 0; lua_pop(L, 2); // pop key and value } if (is_array) { appendc('[', state); int i = 1; lua_rawgeti(L, -1, i++); while (1) { append_value(L, state); lua_rawgeti(L, -1, i++); if (lua_isnil(L, -1)) { lua_pop(L, 1); // pop nil break; } appendc(',', state); } appendc(']', state); } else { appendc('{', state); lua_pushnil(L); lua_next(L, -2); while (1) { if (lua_type(L, -2) != LUA_TSTRING) { // ignore entries without string fields. lua_pop(L, 1); // pop value, leaving key if (!lua_next(L, -2)) break; continue; } lua_pushvalue(L, -2); // push key append_value(L, state); // write key appendc(':', state); append_value(L, state); // write value, key now on top if (!lua_next(L, -2)) break; appendc(',', state); } appendc('}', state); } break; } default: appends("null", state); } lua_pop(L, 1); // pop value }