//asmutil.c /*** asmutil.c int myToken() --- modified from the sample "strtok.c" Sample to do tokenize: /pub/CSIE/course/cs2/strtok.c CopyLeft by tsaiwn@csie.nctu.edu.tw ***************************************/ //#define DEBUG #include #include #include "asmprot.h" int myToken(char*, char[][38]); // Declare first, 先宣告一下 //////// /*** usage: ntoken = myToken(input_line, token_array); ***/ int myToken(char*x, char token[][38]) // split string x into tokens /* char * x --- input line token[][38] --- array for the tokens ******/ { char * p; int n=0; char tmp[80]; strcpy(tmp, x); // 因 strtok() 有破壞性, 如有必要, 先 copy 一份 comment[0] = 0; if(p=strchr(x, ';')) { // found comment, 規定註解用 分號 開始 strncpy(comment, p, 79); // comment[] is global variable tmp[ p-x ] = 0; // 把 複製的 tmp 中的 comment 部份去掉 } p= strtok(tmp, " ,;:\t"); // " ," 表示 空白和逗號 都是 delimeter while(p){ token[n][37] = 0; // Ensure Null-terminated strncpy(token[n++], p, 37); p= strtok(0, " ,;:\t"); // note that parameters; 注意 參數 } token[n][0] = 0; return n; }// myToken( /*************************************/ void myChop(char * s); // declare so that can be used now char * getNextLine(FILE* fp, char*buf) { int k; if(feof(fp)) return 0; // return NULL pointer if EOF initially buf[72] = '\n'; // for testing line too long fgets(buf, 73, fp); // at most read up to 72==73-1 characters if(feof(fp)) return 0; if(buf[72] == 0 && buf[71]!='\n'){ // line too long // discard the following charactes till newline or EOF #ifdef DEBUG //** debug 用的:-) printf("(==%d==%d==)", buf[71], buf[73]); #endif while(1){ // fpurge(stdin); // some compiler NOT work k=fgetc(fp); // 用土法把該 line 多餘部份全部讀入但丟掉! if(k=='\n' || k==EOF) break; // 直到遇見 newline 或 檔案結束 } return buf; } myChop(buf); // 咬掉尾巴的'\n' 如果有, 如果沒有當然不做事:-) return buf; } void myChop(char * s) { // remove the tail newLine '\n' if(*s == 0) return; // empty string, 直接回去 while(*s) ++s; --s; if(*s == '\n') *s = 0; // 若有 newline, 將之去掉 }