fill_tables.c.ftp
来自「麻省理工学院的人工智能工具箱,很珍贵,希望对大家有用!」· FTP 代码 · 共 1,224 行 · 第 1/3 页
FTP
1,224 行
* * A.13 SET up heading for Geometry section* FUNCTION locate_sec_heading !locate heading in input file* IF (didn't see it) THEN* RETURN 1* ENDIF*/ strcpy (heading, "Pre-defined geometries"); if (locate_sect_heading (infile, heading, &line)) { DPRINT ("Leaving fill_tables with error status=1\n"); return(1); }/*** A.14 FOR (each line until end of file)*/ for (cnt=0; !feof(infile); ) {/** A.14.1 FUNCTION read_file_entry !get next line* IF (error) THEN* RETURN 1* ENDIF*/ if (read_file_entry (infile, &line, blk)) break;/* * A.14.2 IF (line doesn't start with number) THEN* SKIP line !loop again* ENDIF*/ if (!isdigit (blk[0])) continue;/** A.14.3 IF (not able to extract geom_id and string)* THEN* PRINT error* RETURN 1*/ if (sscanf (blk, "%d%s", &id, dummy) != 2) { fprintf(stderr, "Line %d: fail to get Geom_id, Geom_name\n", line); DPRINT ("Leaving fill_tables with status=1\n"); return(1);}/** ELSE if (id is out of range )* PRINT error* RETURN 1* ENDIF*/ else if (id<0 || id >255) { fprintf(stderr, "Line %d: Geom_id=%d, must be between 0 and 255\n", line, id); DPRINT ("Leaving fill_tables with status=1\n"); return(1); }/* * A.14.4 STORE geometry name and info, using id as index*/ strncpy (mgotab.geom_name[id], strstr(blk,dummy), 60); cnt++; } /*FOr*//** A.14 ENDFOR*/ DPRINT ("Geometry Defn table has %d entries\n", cnt); /* ** A.15 CLOSE the Table Defn file*/ fclose(infile); infile= NULL;/** * A.16 RETURN 0 !success* END OF FUNCTION fill_tables*/ DPRINT ("Leaving fill_tables with no errors, status=0\n"); return (0); }/***** ===================================================================* B. FUNCTION locate_sect_heading* PURPOSE: search input file for the specified heading string* until found/end of file;* INPUT: FILE *infile Table file reading from* char *heading String to look for* long *line Line number w/in infile** RETURN CODE:* 1> got end of file;* 0> successful, variable line has been updated * ===================================================================*/int locate_sect_heading (FILE *infile, char *heading, long *line){ DPRINT ("Entering locate_sect_heading w/heading=(%s)\n", heading);/** B.1 WHILE (not end of file yet )* READ next line* INCREMENT line counter* IF (find Heading in line just read) THEN* BREAK out of while loop* ENDIF* ENDWHILE*/ while (!feof(infile)) /* locate the correct section first */ { fgets(blk, sizeof(blk), infile); *line += 1; if (strstr (blk, heading) != NULL) break; }/* ** B.2 IF (end of file already) THEN* PRINT cannot locate message* CLOSE file* RETURN 1* ENDIF*/ if (feof(infile)) { fprintf(stderr, "Error, cannot locate '%s'\n",heading); fclose(infile); DPRINT ("Leaving locate_sect_heacing with bad status=1\n"); return(1); }/*** B.3 RETURN 0 !success* END OF locate_sect_heading **/ DPRINT ("located (%s)\n", heading); return(0);} /****===================================================================* 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;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?