void fillIntArray(char *inputFileName, int inputLineNumber, int *inputArray, int numberOfElements) { // printf("you are on line: %d\n", inputLineNumber); initializeIntArray(inputArray, numberOfElements); char stringValue[MAX_INPUT_LINE_SIZE]; getLineFromFile(inputFileName, inputLineNumber, stringValue); // Establish string and get the first token int currentNumber; int currentInputArrayIdx = 0; char *token = strtok(stringValue, ",[]"); while ( token != 0 ) { sscanf(token, "%d", ¤tNumber); if (currentInputArrayIdx >= numberOfElements) { // printf("inputLineNumber: %d\n", inputLineNumber); // printf("Error (fillIntArray): Accessing out of bounds index in array\n"); break; } inputArray[currentInputArrayIdx++] = currentNumber; /* Get next token: */ token = strtok(0, ",[]"); } }
//http://poj.org/status?problem_id=1000&user_id=51isoft&result=&language= bool getStatus(string pid,string lang,string & result,string& ce_info,string &tu,string &mu) { int begin=time(NULL); string runid; tu=mu="0"; string ts; while (true) { FILE * fp=fopen(tfilename,"w+"); curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL); curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "pku.cookie"); string url=(string)"http://poj.org/status?problem_id="+pid+"&user_id="+username+"&language="+lang; //cout<<url; curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); res = curl_easy_perform(curl); curl_easy_cleanup(curl); } fclose(fp); if (res) return false; ts=getLineFromFile(tfilename,3); //cout<<ts; /*if (ts.find("alert(\"Login failed!)")!=string::npos) return false; */ if (ts.find("Error Occurred")!=string::npos||ts.find("The page is temporarily unavailable")!=string::npos) return false; runid=getRunid(ts); result=getResult(ts); //cout << result; if (result.find("Waiting")==string::npos &&result.find("Running")==string::npos &&result.find("Judging")==string::npos &&result.find("Queuing")==string::npos &&result.find("Compiling")==string::npos &&result!=""&&result[0]!='\n'&&result[0]!='\t'&&result[0]!='\r'&&result[0]!=' ') { break; } if (time(NULL)-begin>MAX_WAIT_TIME) break; } if (!(result.find("Waiting")==string::npos &&result.find("Running")==string::npos &&result.find("Judging")==string::npos &&result.find("Queuing")==string::npos &&result.find("Compiling")==string::npos &&result!=""&&result[0]!='\n'&&result[0]!='\t'&&result[0]!='\r'&&result[0]!=' ')) return false; if (result=="Compile Error") { result = "Compilation Error"; ce_info=getCEinfo(runid); } else ce_info=""; if (result=="Accepted") { tu=getUsedTime(ts); mu=getUsedMem(ts); } else if (result[result.length()-1]=='d') { //result.erase(result.end()-2,result.end()); } return true; }
void assembleFile(FILE *f_source, FILE *f_destination) { tDatabase label_values; constructLabelValuesDatabase(&label_values); //the first file parsing tAddress current_address = 0; while (!feof(f_source)) { tLine line = getLineFromFile(f_source); if (labelExistsInLine(line)) { tLine label = getLabelFromLine(line); //FIXME: check that name of label doesn't coinside with processor register name setLabelValue(&label_values, label, current_address); } if (commandTextExistsInLine(line)) { tLine command_text = getCommandTextFromLine(line); int command_size_in_bytes = calculateCommandSizeInBytes(command_text); //FIXME: implement it! current_address += command_size_in_bytes; } } //for debugging print labels with their values printLabelValues(label_values); //the second file parsing fseek(f_source, 0, SEEK_SET); //rewind to the start of file while (!feof(f_source)) { tLine line = getLineFromFile(f_source); if (commandTextExistsInLine(line)) { tLine command_text = getCommandTextFromLine(line); tCommandWithOperands command = parseCommandText(command_text, label_values); tCode code = makeCodeFromCommandWithOperands(command); writeCodeToBinaryFile(f_destination, code); free(code.words); } } printf("Successfully assembled file.\n"); destructLabelValuesDatabase(&label_values); //FIXME: implement it! }
//命令行循环 void cmd_ln_loop(const objfile& file){ char cmd[51] = {0}; int quit = 0; int length; while (1){ std::cout<<"<-"; std::cin.getline(cmd,50); length = strlen(cmd); if (!cmd[0]){ continue; } std::string opr = strtok(cmd," "); if (opr == "la"){ const char* opr1_chp = strtok(NULL," "); const char* fn_chp = strtok(NULL, " "); if (!(opr1_chp && fn_chp)){ printf("format: la [line] [file]\n"); continue; } std::string operand1(opr1_chp); std::string filename(fn_chp); short line = (short)atoi(operand1.c_str()); int filenum = file.f_name_to_num(filename); if (filenum == -1){ printf("no file named %s found\n",filename.c_str()); continue; } try{ int addr = file.l_to_a(line,filenum); printf("%d\n",addr); }catch (std::string s){ std::cout<<s<<std::endl; } }else if (opr == "al"){ const char* addr_chp = strtok(NULL," "); if (!addr_chp){ printf("format: al [addr]\n"); continue; } std::string operand (addr_chp); int addr = (short)atoi(operand.c_str()); int filenum = file.a_to_f(addr); int line = file.a_to_l(addr,filenum); std::string filename(file.f_to_n(filenum)); printf("line %d of %s\n",line,filename.c_str()); std::cout<< getLineFromFile(filename, line)<<std::endl; }else if (opr == "q") break; Sleep(50); } }
u_int32_t FF_query(void *c, dsrecord *pattern, dsrecord **list) { agent_private *ap; u_int32_t cat; dsattribute *a; dsdata *k, *k4, *k6; dsrecord *lastrec; char *fname; FILE *fp; char *line; dsrecord *item = NULL; dsrecord *host = NULL; char *fpath; long ts; int match, single_item, stamp; ff_cache_t *cache; struct stat sb; if (c == NULL) return 1; if (pattern == NULL) return 1; if (list == NULL) return 1; *list = NULL; lastrec = NULL; single_item = 0; stamp = 0; ap = (agent_private *)c; k = cstring_to_dsdata(CATEGORY_KEY); a = dsrecord_attribute(pattern, k, SELECT_META_ATTRIBUTE); dsdata_release(k); if (a == NULL) return 1; if (a->count == 0) return 1; cat = atoi(dsdata_to_cstring(a->value[0])); dsattribute_release(a); fname = categoryFilename[cat]; if (fname == NULL) return 1; k = cstring_to_dsdata(STAMP_KEY); a = dsrecord_attribute(pattern, k, SELECT_META_ATTRIBUTE); dsdata_release(k); if (a != NULL) { dsrecord_remove_attribute(pattern, a, SELECT_META_ATTRIBUTE); stamp = 1; } dsattribute_release(a); k = cstring_to_dsdata(SINGLE_KEY); a = dsrecord_attribute(pattern, k, SELECT_META_ATTRIBUTE); dsdata_release(k); if (a != NULL) { dsattribute_release(a); single_item = 1; } asprintf(&fpath, "%s/%s", ap->dir, fname); ts = 0; cache = NULL; if (ap->flags == 0) cache = cache_for_category(cat); if (cache != NULL) { ts = cache->modtime; } else { memset(&sb, 0, sizeof(struct stat)); if (stat(fpath, &sb) < 0) ts = 0; else ts = sb.st_mtime; } if (stamp == 1) { item = dsrecord_new(); add_validation(item, fpath, ts); *list = item; free(fpath); return 0; } if (cache != NULL) { free(fpath); return cache_query(cache, cat, single_item, pattern, list); } fp = fopen(fpath, "r"); if (fp == NULL) { free(fpath); return 1; } /* bootptab entries start after a "%%" line */ if (cat == LUCategoryBootp) { while (NULL != (line = getLineFromFile(fp))) { if (!strncmp(line, "%%", 2)) break; freeString(line); line = NULL; } if (line == NULL) { fclose(fp); free(fpath); return 0; } freeString(line); line = NULL; } while (NULL != (line = getLineFromFile(fp))) { if (line[0] == '#') { freeString(line); line = NULL; continue; } item = parse(line, cat); freeString(line); line = NULL; if (item == NULL) continue; match = dsrecord_match_select(item, pattern, SELECT_ATTRIBUTE); if (match == 1) { add_validation(item, fpath, ts); if (*list == NULL) *list = dsrecord_retain(item); else lastrec->next = dsrecord_retain(item); lastrec = item; if (cat == LUCategoryHost) { } else if (single_item == 1) { dsrecord_release(item); break; } } dsrecord_release(item); } free(fpath); fclose(fp); if ((cat == LUCategoryHost) && (single_item == 1)) { if ((*list) == NULL) return 0; if ((*list)->next == NULL) return 0; k = cstring_to_dsdata("name"); k4 = cstring_to_dsdata("ip_address"); k6 = cstring_to_dsdata("ipv6_address"); host = *list; for (item = host->next; item != NULL; item = item->next) { a = dsrecord_attribute(item, k, SELECT_ATTRIBUTE); dsrecord_merge_attribute(host, a, SELECT_ATTRIBUTE); dsattribute_release(a); a = dsrecord_attribute(item, k4, SELECT_ATTRIBUTE); dsrecord_merge_attribute(host, a, SELECT_ATTRIBUTE); dsattribute_release(a); a = dsrecord_attribute(item, k6, SELECT_ATTRIBUTE); dsrecord_merge_attribute(host, a, SELECT_ATTRIBUTE); dsattribute_release(a); } dsdata_release(k); dsdata_release(k4); dsdata_release(k6); dsrecord_release(host->next); host->next = NULL; } return 0; }
static ff_cache_t * load_cache(int cat, ff_cache_t *cache) { dsrecord *lastrec; char *fname; FILE *fp; char *line; dsrecord *item = NULL; char *fpath; int status; struct stat sb; if (cache == NULL) return NULL; dsrecord_release(cache->crecord); cache->crecord = NULL; fname = categoryFilename[cat]; if (fname == NULL) return NULL; asprintf(&fpath, "%s/%s", DEFAULT_FF_DIR, fname); memset(&sb, 0, sizeof(struct stat)); status = stat(fpath, &sb); if (status < 0) { free(fpath); return NULL; } cache->modtime = sb.st_mtime; fp = fopen(fpath, "r"); if (fp == NULL) { free(fpath); return NULL; } /* bootptab entries start after a "%%" line */ if (cat == LUCategoryBootp) { while (NULL != (line = getLineFromFile(fp))) { if (!strncmp(line, "%%", 2)) break; freeString(line); line = NULL; } if (line == NULL) { fclose(fp); free(fpath); return 0; } freeString(line); line = NULL; } lastrec = NULL; while (NULL != (line = getLineFromFile(fp))) { if (line[0] == '#') { freeString(line); line = NULL; continue; } item = parse(line, cat); freeString(line); line = NULL; if (item == NULL) continue; add_validation(item, fpath, cache->modtime); if (cache->crecord == NULL) cache->crecord = item; if (lastrec != NULL) lastrec->next = item; lastrec = item; } free(fpath); fclose(fp); return cache; }
static void getAppdirTemplate(char *filepath) { char buf[1024]; FILE *path; int found = 0; char *ptr; char defTemplate[MAXPATH]; sprintf(filepath,"%s/adm/users/profiles/user/%s", systemdir, UserName); path = fopen(filepath,"r"); if (path != NULL) { while (getLineFromFile(path, buf, sizeof(buf))) { if (strncmp(buf,"appdir",6) == 0) { ptr = &buf[6]; while ( (*ptr == ' ') || (*ptr == '\t') ) ptr++; if (strlen(ptr)) { if (*(ptr+strlen(ptr)-1) == '\n') { *(ptr+strlen(ptr)-1) = '\0'; } sprintf(filepath,"%s/adm/users/userProfiles/appdir%s.txt", systemdir, ptr); if ( ! access(filepath,R_OK) ) { /* found an existing template name */ fclose(path); return; } } } else if ( ! found && (strncmp(buf,"itype",5) == 0) ) { ptr = &buf[5]; while ( (*ptr == ' ') || (*ptr == '\t') ) ptr++; if (strlen(ptr)) { if (*(ptr+strlen(ptr)-1) == '\n') *(ptr+strlen(ptr)-1) = '\0'; /* use itype as default if appdir entry does not exist */ if ( ! strcmp(ptr,"LC-NMR/MS") ) sprintf(defTemplate,"%s/adm/users/userProfiles/appdirLcNmrMs.txt", systemdir); else sprintf(defTemplate,"%s/adm/users/userProfiles/appdir%s.txt", systemdir, ptr); found = 1; } } } fclose(path); } sprintf(filepath,"%s/adm/users/profiles/system/%s", systemdir, UserName); path = fopen(filepath,"r"); if (path != NULL) { while (getLineFromFile(path, buf, sizeof(buf))) { if (strncmp(buf,"appdir",6) == 0) { ptr = &buf[6]; while ( (*ptr == ' ') || (*ptr == '\t') ) ptr++; if (strlen(ptr)) { if (*(ptr+strlen(ptr)-1) == '\n') *(ptr+strlen(ptr)-1) = '\0'; sprintf(filepath,"%s/adm/users/userProfiles/appdir%s.txt", systemdir, ptr); if ( ! access(filepath,R_OK) ) { fclose(path); return; } } } else if ( ! found && (strncmp(buf,"itype",5) == 0) ) { ptr = &buf[5]; while ( (*ptr == ' ') || (*ptr == '\t') ) ptr++; if (strlen(ptr)) { /* use itype as default if appdir entry does not exist */ if (*(ptr+strlen(ptr)-1) == '\n') *(ptr+strlen(ptr)-1) = '\0'; if ( ! strcmp(ptr,"LC-NMR/MS") ) sprintf(defTemplate,"%s/adm/users/userProfiles/appdirLcNmrMs.txt", systemdir); else sprintf(defTemplate,"%s/adm/users/userProfiles/appdir%s.txt", systemdir, ptr); found = 1; } } } fclose(path); } if (found) { strcpy(filepath,defTemplate); } }
static void getappdirPaths() { #ifndef PSG_LC char filepath[MAXPATH]; char buf[1024]; FILE *path; int onOff; char sPath[1024]; char sLabel[1024]; char operator[MAXPATH]; int systemInList = 0; if (P_getstring(GLOBAL,"operator",operator,1,MAXPATH-1)) { strcpy(operator,UserName); } if ( (lastOperator[0] != '\0') && ! strcmp(operator,lastOperator) ) { /* operator has not changed. */ return; } strcpy(lastOperator, operator); path = NULL; if (appdirRight == -1) appdirRight = rightsEval("caneditappdir"); if (appdirRight) { sprintf(filepath,"%s/persistence/appdir_%s", userdir, operator); if ( ! access(filepath,R_OK) ) { path = fopen(filepath,"r"); } } /* Check for an owner based appdir file */ if ( (path == NULL) && strcmp(UserName,operator) ) { /* rightsEval is based on the operator parameter */ P_setstring(GLOBAL,"operator",UserName,1); appdirRight = rightsEval("caneditappdir"); if (appdirRight) { sprintf(filepath,"%s/persistence/appdir_%s", userdir, UserName); if ( ! access(filepath,R_OK) ) { path = fopen(filepath,"r"); } } P_setstring(GLOBAL,"operator",operator,1); appdirRight = rightsEval("caneditappdir"); } if (path == NULL) { getAppdirTemplate(filepath); path = fopen(filepath,"r"); } if (path != NULL) { releaseWithId("app"); appdirPaths = NULL; appdirLabels = NULL; appdirNumPaths = 0; while (getLineFromFile(path, buf, sizeof(buf))) { if ( (buf[0] == '0') || (buf[0] == '1') ) { sLabel[0] = '\0'; sscanf(buf,"%d;%[^;];%[^\n]\n", &onOff, sPath, sLabel); if (onOff) { ++appdirNumPaths; if ( ! strcmp(sPath,"USERDIR") ) strcpy(sPath, userdir); #ifdef __INTERIX //winPathToUnix(sPath); #endif if ( ! strcmp(sPath, systemdir) ) systemInList = 1; if (appdirPaths == NULL) { appdirPaths = newStringId(sPath,"app"); appdirLabels = newStringId(sLabel,"app"); } else { appdirPaths = newCatId(appdirPaths,";","app"); appdirPaths = newCatId(appdirPaths,sPath,"app"); appdirLabels = newCatId(appdirLabels,";","app"); appdirLabels = newCatId(appdirLabels,sLabel,"app"); } } } } fclose(path); } if ( ! systemInList ) { char title[STR64]; if (appdirPaths == NULL) { appdirPaths = newStringId(systemdir,"app"); sprintf(title,"%s system",RevTitle); appdirLabels = newStringId(title,"app"); } else { appdirPaths = newCatId(appdirPaths,";","app"); appdirPaths = newCatId(appdirPaths,systemdir,"app"); sprintf(title,";%s system",RevTitle); appdirLabels = newCatId(appdirLabels,title,"app"); } ++appdirNumPaths; } #endif }
//http://acm.hdu.edu.cn/status.php?first=&pid=1000&user=tjuvjudge&lang=1&status=0 bool getStatus(string pid,string lang,string & result,string& ce_info,string &tu,string &mu) { int begin=time(NULL); string runid; tu=mu="0"; string ts; int tried=0; while (true) { FILE * fp=fopen(tfilename,"w+"); curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL); curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "hdu.cookie"); string url=(string)"http://acm.hdu.edu.cn/status.php?first=&pid="+pid+"&user="******"&lang=&status=0"; //cout<<url; curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); res = curl_easy_perform(curl); curl_easy_cleanup(curl); } fclose(fp); if (res) return false; ts=getLineFromFile(tfilename,77); //cout<<ts; //writelog((char *)ts.c_str()); /*if (ts.find("alert(\"Login failed!)")!=string::npos) return false; */ if (ts.find("Connect(0) to MySQL Server failed.")!=string::npos||ts.find("<b>One or more following ERROR(s) occurred.")!=string::npos||ts.find("<h2>The requested URL could not be retrieved</h2>")!=string::npos||ts.find("PHP: Maximum execution time of")!=string::npos) { tried++; if (tried>=MAX_TRY_TIME) return false; continue; } runid=getRunid(ts); result=getResult(ts); /* minjie */ db_client.update("toj.Status", BSON("run_ID" << temp.runid), BSON("$set" << BSON("result"<<result)), true, false); //upsert, multi cout << result<<endl<<runid<<endl; if (result.find("Waiting")==string::npos &&result.find("Running")==string::npos &&result.find("Judging")==string::npos &&result.find("Queuing")==string::npos &&result.find("Compiling")==string::npos &&result.find("\n")&&result!="") { break; } /* minjie */ //if (time(NULL)-begin>30) sleep(500); if (time(NULL)-begin>MAX_WAIT_TIME) break; } if (!(result.find("Waiting")==string::npos &&result.find("Running")==string::npos &&result.find("Judging")==string::npos &&result.find("Queuing")==string::npos &&result.find("Compiling")==string::npos)) return false; if(result=="Compilation Error") ce_info = getCEinfo(runid); else ce_info=""; tu=getUsedTime(ts); mu=getUsedMem(ts); /* minjie update Status */ db_client.update("toj.Status", BSON("run_ID" << temp.runid), BSON("$set" << BSON("vrun_ID" << runid << "oj" << "HDU" << "result"<<result<<"time_used"<<tu<<"mem_used"<<mu<<"ce_info"<<ce_info)), true, false); /* minjie update User */ db_client.update("toj.User", BSON("username" << temp.user), BSON("$inc"<<BSON("total_submit"<<1)), false,true); //upsert,multi if(result == "Accepted") { db_client.update("toj.User", BSON("username" << temp.user), BSON("$inc"<<BSON("total_ac"<<1)), false,true); /* update speed */ bson::bo obj = db_client.findOne("toj.Status", BSON("username" << temp.user << "pid" << temp.pid << "speed" << 51)); bool fast = false; if(obj.isEmpty()) fast = true; else { int time_used = boost::lexical_cast<int>(obj.getStringField("time_used")); int mem_used = boost::lexical_cast<int>(obj.getStringField("mem_used")); int now_tu = boost::lexical_cast<int>(tu); int now_mu = boost::lexical_cast<int>(mu); if(now_tu < time_used || (now_tu == time_used && now_mu < mem_used)) fast = true; } if(fast) { db_client.update("toj.Status", BSON("username" << temp.user << "pid" << temp.pid), BSON("$set" << BSON("speed" << 50)), true, false); db_client.update("toj.Status", BSON("run_ID" << temp.runid), BSON("$set" << BSON("speed" << 51)), true, false); } } /* minjie update Problem */ db_client.update("toj.Problem", BSON("pid" << temp.pid), BSON("$inc"<<BSON("total_submit"<<1)), false,true); //upsert,multi string dbresult; switch(result[0]) { case 'A': dbresult = "total_ac"; break; case 'W': dbresult = "total_wa"; break; case 'P': dbresult = "total_pe"; break; case 'C': dbresult = "total_ce"; break; case 'R': dbresult = "total_re"; break; case 'T': dbresult = "total_tle"; break; case 'M': dbresult = "total_mle"; break; case 'O': dbresult = "total_ole"; break; default: dbresult = "total_other"; break; } db_client.update("toj.Problem", BSON("pid" << temp.pid), BSON("$inc"<<BSON(dbresult<<1)), false,true); //upsert,multi return true; }
//http://acm.hdu.edu.cn/status.php?first=&pid=1000&user=bnuvjudge&lang=1&status=0 bool getStatus(string pid,string lang,string & result,string& ce_info,string &tu,string &mu) { int begin=time(NULL); string runid; tu=mu="0"; string ts; int tried=0; while (true) { FILE * fp=fopen(tfilename,"w+"); curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL); curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "hdu.cookie"); string url=(string)"http://acm.hdu.edu.cn/status.php?first=&pid="+pid+"&user="******"&lang=&status=0"; //cout<<url; curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); res = curl_easy_perform(curl); curl_easy_cleanup(curl); } fclose(fp); if (res) return false; ts=getLineFromFile(tfilename,77); //cout<<ts; //writelog((char *)ts.c_str()); /*if (ts.find("alert(\"Login failed!)")!=string::npos) return false; */ if (ts.find("Connect(0) to MySQL Server failed.")!=string::npos||ts.find("<b>One or more following ERROR(s) occurred.")!=string::npos||ts.find("<h2>The requested URL could not be retrieved</h2>")!=string::npos||ts.find("PHP: Maximum execution time of")!=string::npos) { tried++; if (tried>=MAX_TRY_TIME) return false; continue; } runid=getRunid(ts); result=getResult(ts); cout << result<<endl<<runid<<endl; if (result.find("Waiting")==string::npos &&result.find("Running")==string::npos &&result.find("Judging")==string::npos &&result.find("Queuing")==string::npos &&result.find("Compiling")==string::npos &&result.find("\n")&&result!="") { break; } if (time(NULL)-begin>MAX_WAIT_TIME) break; } if (!(result.find("Waiting")==string::npos &&result.find("Running")==string::npos &&result.find("Judging")==string::npos &&result.find("Queuing")==string::npos &&result.find("Compiling")==string::npos)) return false; if (result=="Compilation Error") { //result="Compile Error"; ce_info=getCEinfo(runid); } else ce_info=""; tu=getUsedTime(ts); mu=getUsedMem(ts); /* if (result != "Accepted" && result[result.length()-1] == 'd') { result.erase(result.end()-2,result.end()); } */ return true; }