/**************************************************************************** * Name: bdf_getglyphinfo * * Description: * Obtains the information for an individual glyph. The BDF properties * taken into account are: * - ENCODING * - DWIDTH * - BBX * BDF properties ignored: * - SWIDTH * - SWIDTH1 * - DWIDTH1 * - VVECTOR * * Input Parameters: * file - The input file stream pointing to the first line of the * glyph's information (right after STARTCHAR). * ginfo - A glyphinfo_t struct to fill with the glyph's information. * ****************************************************************************/ static void bdf_getglyphinfo(FILE *file, glyphinfo_t *ginfo) { char line[BDF_MAX_LINE_LENGTH]; char lineCopy[BDF_MAX_LINE_LENGTH]; char *str, *token, *saveptr1; bool done; done = false; while(fgets(line, BDF_MAX_LINE_LENGTH, file) != NULL && !done) { trimLine(line); strcpy(lineCopy, line); str = line; while ((token = (char *)strtok_r(str, " ", &saveptr1))) { /* ENCODING information */ if(strcmp(token, "ENCODING") == 0) { token = (char *)strtok_r(NULL, " ", &saveptr1); ginfo->encoding = atoi(token); } /* DWIDTH information */ if(strcmp(token, "DWIDTH") == 0) { token = (char *)strtok_r(NULL, " ", &saveptr1); ginfo->dw_x0 = atoi(token); token = (char *)strtok_r(NULL, " ", &saveptr1); ginfo->dw_y0 = atoi(token); } /* BBX information */ else if(strcmp(token, "BBX") == 0) { int bbxinfo[4]; bdf_parseintline(lineCopy, 4, bbxinfo); ginfo->bb_w = bbxinfo[0]; ginfo->bb_h = bbxinfo[1]; ginfo->bb_x_off = bbxinfo[2]; ginfo->bb_y_off = bbxinfo[3]; /* This is the last BDF property of interest*/ done = true; } str = NULL; } } }
/* * writeRange - write a range of lines in an fcb to current file */ static vi_rc writeRange( linenum s, linenum e, fcb *cfcb, long *bytecnt, bool write_crlf, bool last_eol ) { line *cline; int len1, len; char *buff; vi_rc rc; if( s > e ) { return( ERR_NO_ERR ); } rc = GimmeLinePtrFromFcb( s, cfcb, &cline ); if( rc != ERR_NO_ERR ) { return( rc ); } buff = WriteBuffer; /* * copy data into buffer */ len = 0; for( ; s <= e; ++s ) { len1 = trimLine( buff, cline ); buff += len1; len += len1; if( s != e || last_eol ) { if( write_crlf ) { *buff++ = CR; len++; } *buff++ = LF; len++; } cline = cline->next; } /* * now write the buffer */ if( fileHandle == 0 ) { len1 = fwrite( WriteBuffer, 1, len, stdout ); } else { len1 = write( fileHandle, WriteBuffer, len ); } if( len1 != len ) { return( ERR_FILE_WRITE ); } *bytecnt += (long)len; return( ERR_NO_ERR ); } /* writeRange */
/* processMoves(void) reads movement commands, processing each command, in turn to move the corresponding car within the carpark. Each movement command is constrained to ensure the corresponding car does not collide with any other car and that the car remains within the bounds of the carpark. A movement command is performed (and the carpark updated to reflect the movement) if the corresponding car can move in the given direction. At the completion of each movement command (regardless of whether the command lead to actual movement), the current state of the carpark is printed. The function completes immediately if a movement command leads to the target car exiting. The function terminates the program if any command is invalid. */ static void processMoves(void) { char line[BUFSIZ]; char car; char direction; int amount; DIRECTION d; int i = 0; while(!hasExited()) // make sure the TARGETCAR is not at the exit location { printPrompt(); // prompt the user for a move fgets(line, sizeof line, stdin); trimLine(line); sscanf(line, "%c %c %d", &car, &direction, &amount); // assign the users move to appropiate variables if(!isValidCarparkEntry(car)) // check the car is defined in the carpark { fprintf(stderr, "Fatal Error: car %c was not found in the grid\n", car); exit(EXIT_FAILURE); } /* The following code sets the enum value from the user input */ if(direction == 'N' || direction == 'n') { d = NORTH; } else if(direction == 'S' || direction == 's') { d = SOUTH; } else if(direction == 'E' || direction == 'e') { d = EAST; } else if(direction == 'W' || direction == 'w') { d = WEST; } moveCar(car, d, amount); i = moveCar(car, d, amount); printf("Processed move: %c %c %d\n", car, direction, amount); printCarpark(); } printf("The target car is now free after %d moves!\n", i); }
/* readCarparkFile(filename) reads the contents of the file named filename as a carpark-file, "filling" the global carpark variable with information read from the file. The function terminates the program if the file is not a valid carpark-file or if the corresponding grid would be too large to store in the global carpark variable. */ static void readCarparkFile(char filename[]) { FILE *fp; // Create a file pointer char line[BUFSIZ]; int i = 0; int j = 0; fp = fopen(filename, "r"); // Open the file and assign to file pointer if(fp == NULL) // If file is empty then display error { fprintf(stderr, "The file %s: cannot be found\n", filename); exit(EXIT_FAILURE); } fscanf(fp, "EXIT: %d %d\n", &carpark.exit.row, &carpark.exit.col); // scan through file for the exit location and assign to exit variables while(fgets(line, sizeof line, fp) != NULL) // while not at the end of a line { if(strlen(line) > MAXCARPARKSIZE) // if row is bigger than MAXCARPARKSIZE then display error { fprintf(stderr, "The file %s: is too large for the grid\n", filename); exit(EXIT_FAILURE); } if(i > MAXCARPARKSIZE) // if col is bigger than MAXCARPARKSIZE then display error { fprintf(stderr, "The file %s: is too large for the grid\n", filename); exit(EXIT_FAILURE); } trimLine(line); // trim the line j = 0; while(line[j] != '\0') // while character does not equal last in row { carpark.grid[i][j] = line[j]; j++; } i++; carpark.nCols = j; } carpark.nRows = i; fclose(fp); // close the file }
bool CQRobotSettingList::RBLine::Decode( const char *lineP ) { if ( !lineP || *lineP==0 || *lineP == '#' ) return false; char *lineBuf = new char[ strlen( lineP )+1 ]; strcpy( lineBuf, lineP ); char *line = lineBuf; // Get the Name char *element = line; line = strchr( line, ',' ); if ( line ) { *line = 0; m_userAssignedName = element; } else { m_userAssignedName = element; delete[] lineBuf; return false; } line++; // Get the Logfile Stamp element=line; #if DEF_UNIX trimLine( line ); // Proper way for all OperSYSs #else line = strchr( line, '\n' ); if( line ) { *line = 0; } #endif m_logFileStamp=element; delete[] lineBuf; return true; }
/* print the longest input line */ int main() { int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ max = 0; while ((len = getLine(line, MAXLINE)) > 0) { if (len > 0) { copy(longest, line); int newlen; newlen = trimLine(longest, len); if (newlen > 0) printf("%s", longest); } } return 0; }
/** Parses the in.file to get the name of all the input files */ PROCESS *parseFiles(char *fname) { //attempt to open file FILE *fp; if ((fp = fopen(fname,"r")) == NULL) { char error[BUFSIZ]; sprintf(error,"Error opening %s",fname); perror(error); usage(); exit(0); } else { files = (char**) realloc(files,(nfiles+1)*sizeof(char*)); //files = malloc(BUFSIZ*sizeof(char*)); if (files == NULL) { perror("Cannot allocate to files"); exit(0); } //Parsing all the filenames. char* strCheck; char line[BUFSIZ]; while (INFILE(fp) && (strCheck = fgets(line,sizeof line,fp)) != NULL ) { files = realloc(files,(nfiles+1)*sizeof(char*)); //Yes, C99 prefers you use void pointers. //TODO: what about an empty line? trimLine(line); files[nfiles] = malloc(sizeof line); if (files[nfiles] == NULL) { perror("Cannot allocate space for file name"); exit(0); } strcpy(files[nfiles],line); ++nfiles; } } fclose(fp); return readFiles(); }
/**************************************************************************** * Name: bdf_getglyphbitmap * * Description: * Obtains the character bitmap information for an individual glyph. * * Input Parameters: * file - The input file stream pointing to the first line of the * glyph's bitmap (right after BITMAP). * ginfo - A glyphinfo_t struct to fill with the glyph's bitmap. * ****************************************************************************/ static void bdf_getglyphbitmap(FILE *file, glyphinfo_t *ginfo) { char line[BDF_MAX_LINE_LENGTH]; uint64_t *bitmap; bool readingbitmap; bitmap = ginfo->bitmap; readingbitmap = true; while (readingbitmap) { if (fgets(line, BDF_MAX_LINE_LENGTH, file) != NULL) { trimLine(line); if(strcmp(line, "ENDCHAR") == 0) { readingbitmap = false; } else { char *endptr; *bitmap = strtoul(line, &endptr, 16); bitmap++; } } else { /* error condition */ readingbitmap = false; } } }
/**This reads each file that in.file actually contains, one by one, and stores them in a struct*/ PROCESS *readFiles() { if (lf) fprintf(logger,"Job has %d files\n",nfiles); PROCESS *processes = malloc(nfiles*sizeof(PROCESS)); if (files == NULL) { perror("Cannot allocate to processes"); exit(1); } FILE *fp; PROCESS *pp = processes; //Reading the files in one by one and storing to "processes" for(int fileCount = 0; fileCount < nfiles; fileCount++) { char **fparse = files + fileCount; if ((fp = fopen(*fparse,"r")) == NULL) { char error[BUFSIZ]; sprintf(error,"Cannot open %s",*files); fprintf(logger, "Fatal Error: %s\n", error); perror(error); exit(1); //Exit if reading file fails -- MAY NOT BE THE CASE! } else { //Parse the file line-by-line char line[BUFSIZ]; if (fgets(line,sizeof line,fp) == NULL) {//Read first line perror("Cannot process file"); exit(0); } else { trimLine(line); if (isint(line)) { pp->pname = calloc(sizeof(char),strlen(*fparse)+1); strcpy(pp->pname,*fparse); //file is valid and has a start time at the beginning pp->stime = strtol(line,NULL,10); //construct a new process and initialise its default values pp->nlines = pp->nifs = pp->runningTime = 0; pp->curLine = 1; //apparently causes a memory access error if not first set to NULL pp->iflines = (IFLINE*) NULL; pp->scheduledTimeSlots = (int*) NULL; pp->durationTimeSlots = (int*) NULL; pp->nTimeSlots = 0; pp->lines = (char**) NULL; } else { fprintf(stderr,"Start time missing from %s\n",*fparse); fprintf(logger, "Fatal Error: Start time missing from %s\n",*fparse); exit(1); } while (INFILE(fp)) { //Read rest of proc. if (fgets(line,sizeof line,fp) != NULL) { trimLine(line); ++(pp->nlines); pp->lines = (char**)realloc(pp->lines, pp->nlines * (sizeof(char*))); //explictly initialise pointer to NULL to avoid memory referential issues (pp->lines)[pp->nlines-1] = (char*) NULL; (pp->lines)[pp->nlines-1] = malloc((strlen(line) + 1) * sizeof(char)); strcpy((pp->lines)[pp->nlines-1], line); //check for existence of ifline if (findIfLine(pp,line,pp->nlines)) { IFLINE il = (pp->iflines)[pp->nifs - 1]; fprintf(logger,"If-Line found at line %d: "\ "if %c < %d goto %d\n",il.originline, il.ifvar,il.loopLimit,il.gotoline); } } } } pp->currtime = 0; pp->runningTime = findRunningTime(pp); if (lf) fprintf(logger,"Read file %s\n",*fparse); fparse++; pp++; } fclose(fp); } pp = processes; if (lf) for (int i = 0; i < nfiles; i++,pp++) fprintf(logger,"Process %s has starttime %d and %d if-lines\n", files[i],pp->stime,pp->nifs); return processes; }
int main(int argc, char **argv) { FILE *file, *out; char line[BDF_MAX_LINE_LENGTH]; char lineCopy[BDF_MAX_LINE_LENGTH]; char *str, *token, *saveptr1; char *input, *output; /* FONTBOUNDINGBOX properties*/ int fbb_x = 0; int fbb_y = 0; int fbb_x_off = 0; int fbb_y_off = 0; /* Input BDF file */ input = argv[1]; if (input == NULL) { printf("%s: no input file\n", argv[0]); exit(0); } file = fopen(input, "r"); if (file == NULL) { printf("%s: error opening file %s\n", argv[0], input); exit(0); } else { #ifdef VERBOSE printf("Opening \"%s\"\n", input); #endif /* VERBOSE */ } /* Output file */ if (argv[2]) { output = argv[2]; } else { output = "nxfonts_myfont.h"; } out = fopen(output, "w"); if (out == NULL) { printf("%s: error opening file %s\n", argv[0], output); fclose(file); exit(0); } else { while (fgets(line, BDF_MAX_LINE_LENGTH, file) != NULL) { #ifdef DBG printf("--\n"); #endif /* DBG */ // Save a copy of the line strcpy(lineCopy,line); // Clean it trimLine(line); str = line; while ((token = (char *)strtok_r(str, " ", &saveptr1))) { /* FONTBOUNDINGBOX - Global font information */ if (strcmp(token, "FONTBOUNDINGBOX") == 0) { int fbbinfo[4]; bdf_parseintline(lineCopy, 4, fbbinfo); fbb_x = fbbinfo[0]; fbb_y = fbbinfo[1]; fbb_x_off = fbbinfo[2]; fbb_y_off = fbbinfo[3]; /* Print FONTBOUNDINGBOX information */ fprintf(out, "/* Maximum height and width of any"); fprintf(out, " glyph in the set */\n\n"); fprintf(out, "#define NXFONT_MAXHEIGHT %d\n", fbb_y); fprintf(out, "#define NXFONT_MAXWIDTH %d\n\n", fbb_x); } /* STARTCHAR - Individual glyph information */ if (strcmp(token, "STARTCHAR") == 0) { glyphinfo_t ginfo; /* Glyph name */ ginfo.name = (char *)strtok_r(NULL, " ", &saveptr1); #ifdef VERBOSE printf("Processing glyph: %s\n", ginfo.name); #endif /* VERBOSE */ /* Glyph information: * ENCODING * DWIDTH * BBX */ ginfo.encoding = 0; ginfo.dw_x0 = 0; ginfo.dw_y0 = 0; ginfo.bb_w = 0; ginfo.bb_h = 0; ginfo.bb_x_off = 0; ginfo.bb_y_off = 0; bdf_getglyphinfo(file, &ginfo); /* Glyph bitmap */ ginfo.bitmap = malloc(sizeof(uint64_t) * ginfo.bb_h); bdf_getglyphbitmap(file, &ginfo); #ifdef DBG bdf_printglyphinfo(&ginfo); #endif /* DBG */ /* Convert to nxfonts */ nx_fontmetric_t nxmetric; uint32_t stride; bdf_getstride(&ginfo, &stride); nxmetric.stride = stride; nxmetric.width = ginfo.bb_w; nxmetric.height = ginfo.bb_h; /* The NuttX font format does not support * negative X offsets. */ if (ginfo.bb_x_off < 0) { nxmetric.xoffset = 0; printf("%s: ignoring negative x offset for " "glyph '%s' (%d)\n", argv[0], ginfo.name, ginfo.encoding); } else { nxmetric.xoffset = ginfo.bb_x_off; } nxmetric.yoffset = fbb_y + fbb_y_off - ginfo.bb_y_off - ginfo.bb_h; #ifdef DBG bdf_printnxmetricinfo(&nxmetric); #endif /* DBG */ /* The space (32) character is treated differently */ if (ginfo.encoding == 32) { fprintf(out, "/* The width of a space */\n\n"); fprintf(out, "#define NXFONT_SPACEWIDTH %d\n\n", ginfo.dw_x0); } else { bdf_printoutput(out, &ginfo, &nxmetric); } /* Free memory */ free(ginfo.bitmap); } str = NULL; } } fclose(file); fclose(out); /* The End */ printf("Generated \"%s\"\n", output); } return EXIT_SUCCESS; }