fill_tables.cf
来自「麻省理工学院的人工智能工具箱,很珍贵,希望对大家有用!」· CF 代码 · 共 993 行 · 第 1/2 页
CF
993 行
/****===================================================================* C. FUNCTION read_file_entry** PURPOSE: reads next line (default to 100 chars long) from file;* skip line if it's a blank line; ** RETURNS CODE:* -1: error; * 0: success, line is stored in 'final_block' with* no beginning and trailing spaces;* 1: end of section;* ================================================================== ... NOT USING D_PRINT IN HERE ...*/int read_file_entry (FILE *infile, long *line, char *final_block){int i, start; /* * C.1 INITIALIZE variables* !Null out final_block !string to be returned*/ final_block[0]='\0';/*** C.2 WHILE (not end of file and no error) DO*/ while (!feof(infile) && !ferror(infile)) {/** C.2.1 GET next line from file* IF (error) THEN* RETURN -1* ENDIF*/ if (fgets(blk, 100, infile) == NULL) return(-1);/** C.2.2 INCREMENT line counter*/ *line += 1; /* keep count *//** C.2.3 IF (it's a comment) !#####* THEN* RETURN 1 !end of this section* ENDIF*/ if (!strncmp (blk, "#####", 5)) return (1); /* end of Section*//** C.2.4 REPLACE newline char w/ null terminator*/ blk[strlen(blk)-1]= '\0'; /* replace newline char */ /** C.2.5 FIND if there are beginning spaces* IF (line is all spaces)* THEN* SKIP line !loop to get next line* ENDIF*/ start= strspn (blk, " "); /* beginning spaces */ if (start == strlen(blk)) continue; /* skip if blank line *//* * C.2.6 FOR (each trailing spaces)* REPLACE with null terminator* ENDFOR*/ for (i=strlen(blk)-1; blk[i]==' '; i--) blk[i]='\0'; /* eliminate trail blanks *//** C.2.7 COPY line over to final_block*/ strncpy (final_block, blk+start, 100); /* copy over */ return(0);/* * C.2.8 RETURN 0 !sucess* C.2 ENDWHILE */ } /* WHILE */}/*** END OF FUNCTION read_file_entry**//****===================================================================* D. FUNCTION load_table3** INPUT: FILE *infile Table file reading from* table3 *lvltab ptr to array to store Level info** RETURN CODE:* 0> succesful, lvltab has been filled with info;* 1> error;*===================================================================**/int load_table3 (FILE *infile, table3 *lvltab){int id; /* level id code */int octs; /* num of octets */int t3_cnt=0; /* num of entries in this table */int i; /* working var */ DPRINT (".. load_table3\n"); /* ---------------------------------------------- LOAD TABLE #3: LEVEL DEFINITIONS Table3's Format: Line 1: lvlid numocts "meaning" Line 2: "octet 11 meaning" (optional) Line 3: "octet 12 meaning" (optional) - no comments allowed; -'#####' is treated as end of this section; ---------------------------------------------- *//** D.1 SET up heading for Level section*/ strcpy (heading, "Line 3: Contents of octet 12");/*** D.2 FUNCTION locate_sect_heading !search input file* IF (didn't see heading) THEN* RETURN 1* ENDIF*/ if (locate_sect_heading (infile, heading,&line) ) return(1);/*** D.3 WHILE (not end of file yet) DO*/ while (!feof(infile)) {/** D.3.1 FUNCTION read_file_entry !get next line* IF (failed) THEN* BREAK !quit* ENDIF*/ if (read_file_entry (infile, &line, blk)) break;/** D.3.2 IF (line doesn't start w/a number) * THEN* SKIP line* ENDIF*/ if (!isdigit (blk[0])) continue;/** D.3.3 IF (extract lvlid, num octets, dummy string) is OK* THEN*/ if (sscanf (blk, "%d%d%s",&id,&octs,&dummy) == 3) /* got new Line 1 */ {/** D.3.3.1 IF (level id is out of range) THEN* PRINT error message* RETURN 1* ENDIF*/ if (id < 0 || id > 255) { fprintf(stderr, "Line %d: Levelid= %d, not between 0 and 255\n",line,id); return(1); }/** D.3.3.2 IF (number of octets is out of range) THEN* PRINT error message* RETURN 1* ENDIF*/ if (octs <0 || octs>2) { fprintf(stderr, "Line %d: num octets=%d out of range\n", line, octs); return(1); }/** D.3.3.3 STORE number of octets*/ lvltab[id].num_octets = octs; /* dummy is 1st word of Mening str, locate it in BLK and copy max of 100 chars; remove trailing spaces too *//** D.3.3.3 CHECK where level meaning begins* IF (failed) THEN* PRINT message* RETURN 1* ENDIF*/ if ((ptr= strstr (blk, dummy)) == NULL) { fprintf(stderr, "Line %d: Error reading Lvel_Meaning\n",line); return(1); }/** D.3.3.4 STORE meaning of level*/ strncpy (lvltab[id].meaning, ptr, 100);/* * D.3.3.5 DEFAULT with no contents1 & contents2*/ lvltab[id].contents1[0]= '\0'; lvltab[id].contents2[0]= '\0';/** D.3.3.6 SWITCH (number of octets)*/ switch (octs) {/** D.3.3.6.1 1: !expecting 2 more content lines* D.3.3.6.1.1 IF (end of file OR* error in FUNCTION read_file_entry)* THEN* BREAK;* ENDIF* D.3.3.6.1.2 STORE contents1* D.3.3.6.1.3 IF (end of file OR* error in FUNCTION read_file_entry)* THEN* BREAK;* ENDIF* D.3.3.6.1.2 STORE contents2* D.3.3.6.1 done*/ case 1: /* expecting 2 more content lines */ if (feof(infile) || read_file_entry (infile, &line, blk)) break; strncpy (lvltab[id].contents1, blk, 100); if (feof(infile) || read_file_entry (infile, &line, blk)) break; strncpy (lvltab[id].contents2, blk, 100); break; /** D.3.3.6.2 2: !expecting 1 more content line* D.3.3.6.2.1 IF (end of file OR* error in FUNCTION read_file_entry)* THEN* BREAK;* ENDIF* D.3.3.6.2.2 STORE contents1* D.3.3.6.2 done*/ case 2: /* expecting 1 more content line */ if (feof(infile) || read_file_entry (infile, &line, blk)) break; strncpy (lvltab[id].contents1, blk, 100); lvltab[id].contents2[0]= '\0'; break; }/** D.3.3.6 ENDSWITCH *//*** D.3.3.7 INCREMENT counter of entries*/ t3_cnt++; } /*If *//** D.3.3 ENDIF !extract ok*/ } /* WHile T3 *//*** D.3 ENDWHILE !not end of file */ DPRINT ("Level Defn table has %d entries\n", t3_cnt);/*** D.4 RETURN 0 !success*/ return(0);}/*** END OF FUNCTION *//****===================================================================* E. FUNCTION load_table2** INPUT: FILE *infile Table file reading from* table2 *parmtab ptr to array of structs type table2** RETURN CODE:* 0> succesful, lvltab has been filled with info;* 1> error;*===================================================================**/int load_table2 (FILE *infile, table2 *parmtab){int sub; /* which sub table, 0 means main table */int code; /* parm id */int Index;table2 *t2; /* points to beginning of Main table/Sub table */ DPRINT (".. load_table2\n"); /* Table2's Format: parmid "parm desc" "unit" -any blks that doesn't have a decimal digit in 1st column is treated as comment blks; -'#####' is treated as end of this section; *//** E.1 FOR (each Table 2 to load) * DO*/ for (sub=0; sub <= 5; sub++) {/** E.1.1 SWITCH (which table to load)* Table2: * SET up pointer to begining of Table 2* SET up table 2 heading* Table2-Sub A:* SET up pointer to begining of Table 2-A* SET up table 2-A heading* Table2-Sub B:* SET up pointer to begining of Table 2-B* SET up table 2-B heading* Table2-Sub C:* SET up pointer to begining of Table 2-C* SET up table 2-C heading* Table2-Sub D:* SET up pointer to begining of Table 2-D* SET up table 2-D heading* Table2-Sub E:* SET up pointer to begining of Table 2-E* SET up table 2-E heading* ENDSWITCH*/ switch (sub) { case 0: t2=parmtab; strcpy(heading,"Table 2"); break; case 1: t2=parmtab[250].sub_tab2; strcpy (heading, "Table 2 - Sub A"); break; case 2: t2=parmtab[251].sub_tab2; strcpy (heading, "Table 2 - Sub B"); break; case 3: t2=parmtab[252].sub_tab2; strcpy (heading, "Table 2 - Sub C"); break; case 4: t2=parmtab[253].sub_tab2; strcpy (heading, "Table 2 - Sub D"); break; case 5: t2=parmtab[254].sub_tab2; strcpy (heading, "Table 2 - Sub E"); break; } /* SWitch *//** E.1.2 IF (FUNCTION locate_sec_heading) failed* THEN* RETURN 1* ENDIF*/ if (locate_sect_heading(infile, heading, &line)!= 0) return(1);/** E.1.3 INIT entry counter*/ cnt=0; /* number of entries in this table *//** E.1.4 WHILE (not end of file yet) DO*/ while (!feof(infile)) {/** E.1.4.1 IF (FUNCTION read_file_entry failed)* THEN* BREAK* ENDIF*/ if (read_file_entry (infile, &line, blk)) break;/** E.1.4.2 IF (line doesn't start out with a number)* THEN* CONTINUE !skip line* ENDIF*/ if (!isdigit(blk[0])) continue; /* skip comments */ /** E.1.4.3 IF (fail to extract parm_id, dummy string)* THEN* CONTINUE !skip line* ENDIF*/ /* get Code, Dummy is 1st word of Param Field */ if (sscanf (blk, "%d%s", &code, dummy) != 2) { fprintf(stderr, "Line %d: fail to get code & param\n", line); return(1);} /** E.1.4.4 IF (parm_id is out of range)* THEN* CONTINUE !skip line* ENDIF*/ if (code<0 || code>255) continue; /* skip invalid code */ /* locate Param, move max of 75, then cap it where there are 3 consecutive spaces *//** E.1.4.5 IF (fail to track where string dummy starts)* THEN* PRINT error message* RETURN 1* ENDIF*/ if ((ptr= strstr (blk, dummy)) == NULL) { fprintf(stderr, "Line %d: Error reading Field_Param\n",line); return(1);}/** E.1.4.6 STORE parameter name*/ Index= ptr - blk; /* pos of 1st Parm word */ strncpy (t2[code].field_param, ptr, 75);/** E.1.4.7 IF (no two-spaces follow parm_name)* THEN* PRINT error* RETURN 1* ENDIF*/ if ((ptr= strstr (t2[code].field_param, " ")) == NULL) { fprintf(stderr, "Error on Line %d: must have 2 spaces between Field & Unit\n", line, code); return(1); }/** E.1.4.8 CAP off parm_name where it ends*/ t2[code].field_param[ptr-t2[code].field_param]='\0'; Index += strlen(t2[code].field_param); /* pos at end of Parm field */ /* skip over spaces bet/w Parm and Unit fields and then copy max of 25 chars into Unit; cap it where 2 spaces occur; *//** E.1.4.9 LOCATE where Unit field begins* E.1.4.10 STORE Unit field*/ Index= strspn (blk+Index, " "); strncpy (t2[code].unit, ptr+Index, 25); if ((ptr= strstr (t2[code].unit, " ")) != NULL) t2[code].unit[ptr-t2[code].unit]='\0'; if (t2[code].unit[strlen(t2[code].unit)-1] == ' ') t2[code].unit[strlen(t2[code].unit)-1] = '\0';/** E.1.4.11 INCREMENT entry counter*/ cnt++; } /* T2 *//** E.1.4 ENDWHILE !end of file*/ DPRINT ("Defn %s has %d entries\n", heading, cnt); } /* FOr each table *//** E.1 ENDFOR !each Table 2*/return(0);/*** E.2 RETURN 0 !success*/}/* * END OF FUNCTION*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?