int top_publish(char *name, char *message, RpcEndpoint *ep) { int ret = 0; Topic *st; if (tshm_get(topicTable, name, (void **)&st)) { ret = 1; pthread_mutex_lock(&(st->lock)); if (ll_size(st->regAUs) > 0L) { Iterator *iter; iter = ll_it_create(st->regAUs); if (iter) { Event *event; event = ev_create(name, message, ep, ll_size(st->regAUs)); if (event) { unsigned long id; while (it_hasNext(iter)) { (void) it_next(iter, (void **)&id); au_publish(id, event); } } it_destroy(iter); } } pthread_mutex_unlock(&(st->lock)); } return ret; }
// close csv writer void icsv_writer_close(iCsvWriter *writer) { if (writer) { if (writer->strings) { istring_list_delete(writer->strings); writer->strings = NULL; } #ifndef IDISABLE_FILE_SYSTEM_ACCESS if (writer->fp) { fclose(writer->fp); writer->fp = NULL; } #endif writer->mode = 0; it_destroy(&writer->string); it_destroy(&writer->output); ikmem_free(writer); } }
void icsv_reader_close(iCsvReader *reader) { if (reader) { if (reader->strings) { istring_list_delete(reader->strings); reader->strings = NULL; } if (reader->source) { istring_list_delete(reader->source); reader->source = NULL; } #ifndef IDISABLE_FILE_SYSTEM_ACCESS if (reader->fp) { fclose(reader->fp); reader->fp = NULL; } #endif reader->line = 0; reader->count = 0; it_destroy(&reader->string); ikmem_free(reader); } }
static void exec_action(int op,int arg1,int arg2) { int i, j; char *tmpstr; switch(op) { case 1000:goto_room(arg1-first_room);break; case 1001:goto_room(agt_rand(arg1,arg2)-first_room);break; case 1002:agt_var[arg1]=loc+first_room;break; case 1003:agt_var[arg1]=dobj;break; case 1004:agt_var[arg1]=iobj;break; case 1005:goto_room(agt_var[arg1]-first_room);break; case 1006:it_move(arg1,agt_var[arg2]);break; case 1007:get_obj(agt_var[arg1]);break; case 1008:msgout(agt_var[arg1],1);break; case 1009:get_obj(arg1);break; case 1010:get_obj(arg1);it_move(arg1,1000);break; case 1011:drop_obj(arg1);break; case 1012: if (it_loc(arg1)==1000) { if (PURE_WEAR) drop_obj(arg1); else it_move(arg1,1); } break; case 1013:fontcmd(0,arg1-1);break; /* Load font */ case 1014:pictcmd(1,pictable[arg1-1]);break; /* Show picture */ case 1015:changepict(arg1,arg2);break; /* ChangePicture */ case 1016: if (PICT_SUPPORT && yesno("Would you like to see the picture?")) pictcmd(1,pictable[arg1-1]);break; case 1017:pictcmd(2,arg1);break; /* Show room pix */ case 1018: if (PICT_SUPPORT && yesno("Would you like to see the picture?")) pictcmd(2,arg1-1);break; case 1019:musiccmd(1,arg1-1);break; case 1020:musiccmd(1,agt_rand(arg1,arg2)-1);break; case 1021:musiccmd(2,arg1-1);break; case 1022:musiccmd(3,-1); break; /* Stop Repeat */ case 1023:musiccmd(4,-1); break; /* Stop song */ case 1024:musiccmd(5,-1); break; /* Suspend song */ case 1025:musiccmd(6,-1);break; /* Resume song */ case 1026: if (tnoun(dobj)) noun[dobj-first_noun].movable=!noun[dobj-first_noun].movable; break; case 1027: it_newdesc(arg1,&msg_ptr[arg2-1]); break; case 1028: if (tnoun(arg1)) noun[arg1-first_noun].points=arg2; else if (tcreat(arg1)) creature[arg1-first_creat].points=arg2; else if (troom(arg1)) room[arg1-first_room].points=arg2; break; case 1029:it_destroy(iobj);break; case 1030: tmpstr=agt_readline(3); i=strlen(tmpstr)-1; if (i>0 && tmpstr[i]=='\n') tmpstr[i]=0; strncpy(userstr[arg1-1],tmpstr,80); rfree(tmpstr); break; case 1031:agt_var[arg1]=read_number();break; case 1032:agt_var[arg1]=curr_time;break; case 1033:curr_time=normalize_time(agt_var[arg1]);break; case 1034:curr_time=normalize_time(arg1); break; case 1035:add_time(arg1);break; case 1036:delta_time=arg1;break; /* 1037 and 1038 are subroutine commands */ case 1039:get_obj(dobj);break; case 1040:it_move(dobj,1000);break; case 1041:drop_obj(dobj);break; case 1042: if (it_loc(dobj)==1000) { if (PURE_WEAR) it_move(dobj,1); else drop_obj(dobj); } break; case 1043: /* drop all */ safecontloop(i,j,1) drop_obj(i); break; case 1044: /* remove all */ safecontloop(i,j,1000) drop_obj(i); break; case 1045:deadflag=1;break; case 1046:it_move(arg1,loc+first_room);break; case 1047:it_move(arg1,arg2);break; case 1048:it_reposition(arg1,arg2,1);break; /* RePosition */ case 1049:it_move(dobj,loc+first_room);break; case 1050:it_move(dobj,arg1);break; case 1051: safecontloop(i,j,1) it_move(i,arg1); safecontloop(i,j,1000) it_move(i,arg1); break; case 1052: nounloop(i) if (player_has(i+first_noun) && noun[i].points>arg2) it_move(i+first_noun,arg1); break; case 1053: safecontloop(i,j,arg1) if (tnoun(i)) it_move(i,arg2); break; case 1054:it_destroy(arg1);break; case 1055:it_destroy(dobj);break; case 1056:i=it_loc(arg1); it_move(arg1,it_loc(arg2)); it_move(arg2,i); break; case 1057:it_move(arg1,it_loc(arg2));break; case 1058:it_move(dobj,it_loc(arg2));break; case 1059: case 1060: /* Add to/remove from group */ if (tcreat(arg1)) creature[arg1-first_creat].groupmemb=(op==1059); break; case 1061: /* Move group */ safecontloop(i,j,loc+first_room) if (it_group(i)) it_move(i,arg1); break; /* 1062 is RedirectTo */ case 1063:msgout(agt_rand(arg1,arg2),1);break; case 1064:print_contents(arg1,1);break; case 1065: case 1066: case 1067: case 1068: obj_act(op-1065,arg1);break; case 1069: case 1070: case 1071: case 1072: obj_act(op-1069,dobj);break; case 1073:print_score();break; case 1074: tscore+=arg1;break; case 1075: tscore-=arg1;break; case 1076:v_inventory();break; case 1077:wait_return();break; case 1078:writeln("Time passes...");break; case 1079:agt_delay(arg1);break; case 1080:agt_clrscr();break; case 1081:it_describe(arg1);break; case 1082:look_room();break; /* LOOK */ case 1083:msgout(arg1,1);break; case 1084:writeln("");break; case 1085:if (PURE_TONE && sound_on) agt_tone(arg1,arg2);break; /* Tone */ case 1086:agt_number=ask_for_number(arg1,arg2);break; case 1087:agt_answer=ask_question(arg1);break; case 1088:change_passage(loc,arg1,arg2);break; case 1089:flag[arg1]=1;break; case 1090:flag[arg1]=0;break; case 1091:flag[arg1]=!flag[arg1];break; case 1092:room[loc].flag_noun_bits|=(1 << (arg1-1) ); break; /* Roomflag on */ case 1093:room[loc].flag_noun_bits&=~(1 << (arg1-1) );break; /* Off */ case 1094:room[loc].flag_noun_bits^=(1 << (arg1-1) );break; /* Toggle */ case 1095: /* if (agt_counter[arg1]==-1)*/ agt_counter[arg1]=1;break; case 1096:agt_counter[arg1]=-1;break; case 1097:agt_var[arg1]=arg2;break; case 1098:agt_var[arg1]+=arg2;break; case 1099:agt_var[arg1]-=arg2;break; case 1100:agt_var[arg1]+=agt_var[arg2];break; case 1101:agt_var[arg1]-=agt_var[arg2];break; case 1102:agt_var[arg1]=agt_rand(0,arg2);break; case 1103:agt_var[arg1]=dobj_rec->num;break; case 1104:agt_var[arg1]=iobj_rec->num;break; /* The following are v1.8x specific */ case 1105:quote(arg1);break; case 1106:add_time(arg1);break; case 1107:add_time(-arg1);break; case 1108:curr_time=(curr_time%100)+100*arg1;break; case 1109:curr_time=(curr_time/100)*100+arg1;break; case 1110:add_time(agt_var[arg1]);break; case 1111:add_time(-agt_var[arg1]);break; case 1112:curr_time=(curr_time%100)+100*agt_var[arg1];break; case 1113:curr_time=(curr_time/100)*100+agt_var[arg1];break; /* Now for the AGX additions */ case 1114:add_time(-arg1);break; /* ME-style SubtractFromTime */ case 1115: disambig_score=arg1; break; /* SetDisambigPriority */ case 1116:agt_var[arg1]=delta_time;break; case 1117: /* ChangeStatus */ statusmode=arg1; break; case 1118: if (!mult_rangecheck(agt_var[arg1],arg2)) break; agt_var[arg1]*=arg2;break; case 1119: if (arg2==0) { if (!PURE_ERROR) writeln("GAME ERROR: Division by zero."); } else agt_var[arg1]/=arg2; break; case 1120: if (arg2==0) { if (!PURE_ERROR) writeln("GAME ERROR: Attempt to divide by zero."); } else agt_var[arg1]%=arg2; break; case 1121:agt_waitkey();break; case 1122:last_he=arg1;break; /* SetHE */ case 1123:last_she=arg1;break; case 1124:last_it=arg1;break; case 1125:last_they=arg1;break; case 1126:msgout(arg1,0);break; case 1127: if (!PURE_ERROR) sysmsg(arg1,"GAME ERROR: Standard message not defined."); break; case 1128: msgout(arg1,1);break; /* FailMessage */ case 1129: /* StdMessage */ sysmsg(arg1,"GAME ERROR: Standard message not defined."); break; case 1130: msgout(arg2,1);break; /* ErrMessage */ case 1131: /* StdErrMessage */ sysmsg(arg1,"GAME ERROR: Standard message not defined."); break; case 1132: /* AND */ break; /* These don't do anything under normal circumstances */ case 1133: /* SetClass */ if (troom(arg1)) room[arg1-first_room].oclass=arg2; else if (tnoun(arg1)) noun[arg1-first_noun].oclass=arg2; else if (tcreat(arg1)) noun[arg1-first_creat].oclass=arg2; break; case 1134: agt_var[arg1]=it_class(arg2); break; /* SetVariableToClass */ /* Stack commands */ case 1135: push_stack(arg1); break; case 1136: agt_var[arg1]=pop_stack(); break; case 1137: case 1138: case 1139: case 1140: case 1141: op_stack(op-1137); /* +,-,*, /,% * */ break; case 1142: { /* DupStack */ long n; n=pop_stack(); push_stack(n); push_stack(n); break; } case 1143: pop_stack();break; /* Discard TOS */ case 1144: agt_var[arg1]=agt_number;break; /* SetVariableToInput */ case 1145: setattr(arg1,arg2,1); break; /* Set */ case 1146: setattr(arg1,arg2,0); break; /* Clear */ case 1147: push_stack(getprop(arg1,arg2));break; /* PushProp */ case 1148: setprop(arg1,arg2,pop_stack());break; /* PopProp */ /* 1149, 1150 handled by run_metacommand */ /* 1151 is EndDisambig */ /* 1152 is XRedirect */ case 1153: rstrncpy(userstr[arg1-1],userstr[arg2-1],81);break; case 1154: setcase(userstr[arg1-1],1); break; case 1155: setcase(userstr[arg1-1],0);break; case 1156: op_objflag(1,arg1,arg2);break; case 1157: op_objflag(0,arg1,arg2);break; case 1158: op_objflag(3,arg1,arg2);break; case 1159: push_stack(op_objprop(2,arg1,arg2,0)); break; case 1160: op_objprop(1,arg1,arg2,pop_stack()); break; case 1161: move_in_dir(arg1,arg2); break; default: writeln("INTERNAL ERROR: Action token not supported."); rprintf("Action #%d",op); writeln(""); } }
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; }
int main(int argc, char *argv[]) { LinkedList *ll = NULL; TreeSet *ts = NULL; char *sp; char pattern[4096]; RegExp *reg; Iterator *it; if (argc < 2) { fprintf(stderr, "Usage: ./fileCrawler pattern [dir] ...\n"); return -1; } /* * convert bash expression to regular expression and compile */ cvtPattern(pattern, argv[1]); if ((reg = re_create()) == NULL) { fprintf(stderr, "Error creating Regular Expression Instance\n"); return -1; } if (! re_compile(reg, pattern)) { char eb[4096]; re_status(reg, eb, sizeof eb); fprintf(stderr, "Compile error - pattern: `%s', error message: `%s'\n", pattern, eb); re_destroy(reg); return -1; } /* * create linked list and treeset */ if ((ll = ll_create()) == NULL) { fprintf(stderr, "Unable to create linked list\n"); goto done; } if ((ts = ts_create(scmp)) == NULL) { fprintf(stderr, "Unable to create tree set\n"); goto done; } /* * populate linked list */ if (argc == 2) { if (! processDirectory(".", ll, 1)) goto done; } else { int i; for (i = 2; i < argc; i++) { if (! processDirectory(argv[i], ll, 1)) goto done; } } /* * for each directory in the linked list, apply regular expression */ while (ll_removeFirst(ll, (void **)&sp)) { int stat = applyRe(sp, reg, ts); free(sp); if (! stat) break; } /* * create iterator to traverse files matching pattern in sorted order */ if ((it = ts_it_create(ts)) == NULL) { fprintf(stderr, "Unable to create iterator over tree set\n"); goto done; } while (it_hasNext(it)) { char *s; (void) it_next(it, (void **)&s); printf("%s\n", s); } it_destroy(it); /* * cleanup after ourselves so there are no memory leaks */ done: if (ll != NULL) ll_destroy(ll, free); if (ts != NULL) ts_destroy(ts, free); re_destroy(reg); return 0; }
int main(int argc, char *argv[]) { char buf[1024]; char *p; LinkedList *ll; long i, n; FILE *fd; char **array; Iterator *it; if (argc != 2) { fprintf(stderr, "usage: ./lltest file\n"); return -1; } if ((ll = ll_create()) == NULL) { fprintf(stderr, "Error creating linked list of strings\n"); return -1; } if ((fd = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "Unable to open %s to read\n", argv[1]); return -1; } /* * test of add() */ printf("===== test of add\n"); while (fgets(buf, 1024, fd) != NULL) { if ((p = strdup(buf)) == NULL) { fprintf(stderr, "Error duplicating string\n"); return -1; } if (!ll_add(ll, p)) { fprintf(stderr, "Error adding string to linked list\n"); return -1; } } fclose(fd); n = ll_size(ll); /* * test of get() */ printf("===== test of get\n"); for (i = 0; i < n; i++) { if (!ll_get(ll, i, (void **)&p)) { fprintf(stderr, "Error retrieving %ld'th element\n", i); return -1; } printf("%s", p); } /* * test of remove */ printf("===== test of remove\n"); for (i = n - 1; i >= 0; i--) { if (!ll_remove(ll, i, (void **)&p)) { fprintf(stderr, "Error removing string from linked list\n"); return -1; } free(p); } /* * test of destroy with NULL userFunction */ printf("===== test of destroy(NULL)\n"); ll_destroy(ll, NULL); /* * test of insert */ if ((ll = ll_create()) == NULL) { fprintf(stderr, "Error creating linked list of strings\n"); return -1; } fd = fopen(argv[1], "r"); /* we know we can open it */ printf("===== test of insert\n"); while (fgets(buf, 1024, fd) != NULL) { if ((p = strdup(buf)) == NULL) { fprintf(stderr, "Error duplicating string\n"); return -1; } if (!ll_insert(ll, 0, p)) { fprintf(stderr, "Error adding string to linked list\n"); return -1; } } fclose(fd); for (i = 0; i < n; i++) { if (!ll_get(ll, i, (void **)&p)) { fprintf(stderr, "Error retrieving %ld'th element\n", i); return -1; } printf("%s", p); } /* * test of set */ printf("===== test of set\n"); for (i = 0; i < n; i++) { char bf[1024], *q; sprintf(bf, "line %ld\n", i); if ((p = strdup(bf)) == NULL) { fprintf(stderr, "Error duplicating string\n"); return -1; } if (!ll_set(ll, i, p, (void **)&q)) { fprintf(stderr, "Error replacing %ld'th element\n", i); return -1; } free(q); } /* * test of toArray */ printf("===== test of toArray\n"); if ((array = (char **)ll_toArray(ll, &n)) == NULL) { fprintf(stderr, "Error in invoking ll_toArray()\n"); return -1; } for (i = 0; i < n; i++) { printf("%s", array[i]); } free(array); /* * test of iterator */ printf("===== test of iterator\n"); if ((it = ll_it_create(ll)) == NULL) { fprintf(stderr, "Error in creating iterator\n"); return -1; } while (it_hasNext(it)) { char *p; (void) it_next(it, (void **)&p); printf("%s", p); } it_destroy(it); /* * test of destroy with free() as userFunction */ printf("===== test of destroy(free)\n"); ll_destroy(ll, free); return 0; }