int main() { char *string[MAX]; char c = 0; int d = 0; char line[MAX]; int i = 0; for ( i = 0; ( len = ( getnextline(line, MAX)) > 0); i++ ) { copy ( string[i], line); } int k = 0; while ( k <= MAX ) { printf("%s \n", string[0]); string += 1; ++k; } }
void StartDefaultApps (void) { FILE *f; char *buf, *p, filename[128]; const char *home; int buflen, len; /* * First try ~/.xsmstartup, then system.xsm */ home = getenv ("HOME"); if (!home) home = "."; snprintf (filename, sizeof(filename), "%s/.xsmstartup", home); f = fopen (filename, "r"); if (!f) { f = fopen (SYSTEM_INIT_FILE, "r"); if (!f) { printf ("Could not find default apps file. Make sure you did\n"); printf ("a 'make install' in the xsm build directory.\n"); exit (1); } } buf = NULL; buflen = 0; while (getnextline(&buf, &buflen, f)) { char logtext[256]; if (buf[0] == '!') continue; /* a comment */ if ((p = strchr (buf, '\n'))) *p = '\0'; snprintf (logtext, sizeof(logtext), "Starting locally : %s\n", buf); add_log_text (logtext); len = strlen (buf); buf[len] = '&'; buf[len+1] = '\0'; /* let the shell parse the stupid args */ execute_system_command (buf); } if (buf) free (buf); }
int main () { char ret[MAXLEN]; while (getnextline(ret) != -1) { stripline(ret); printf("%s\n", ret); } return 0; }
int main () { char ret[MAXLEN]; while (getnextline(ret) != -1) { if (strlen(ret) > 0) printf("%s\n", ret); } return 0; }
int main(){ int i,len; char nextline[MAXLENGTH]; while( (len = getnextline(nextline)) > 0){ remove_trailing_whitespaces(nextline,len); printf("\n"); } return 0; }
static void parse_migration_matrix(FILE * fp, long line_count) { long i,j; size_t matrix_size = opt_migration * opt_migration; opt_migration_matrix = (double *)xmalloc(matrix_size*sizeof(double)); opt_migration_events = (double *)xcalloc(matrix_size,sizeof(double)); if (!getnextline(fp)) fatal("Incomplete 'migration' record (line %ld)", line_count+1); long matrix_dim = 0; opt_migration_labels = split_strings(line,&matrix_dim); if (!opt_migration_labels || matrix_dim == 0) fatal("Option 'migration' must be followed by the population labels (line %ld)", line_count+1); /* read matrix */ for (i = 0; i < matrix_dim; ++i) { long dim; if (!getnextline(fp)) fatal("Incomplete 'migration' record (line %ld)", line_count+2+i); char ** data = split_strings(line,&dim); if (dim != matrix_dim+1) fatal("Wrong number of parameters in migration matrix row (line %ld)" " Expected %ld found %ld", line_count+2+i, matrix_dim+1, dim); if (strcmp(data[0],opt_migration_labels[i])) fatal("Migration matrix label of row %ld does not match label of column " "%ld (line %ld)", i+1, i+1, line_count+2+i); for (j = 0; j < matrix_dim; ++j) if (!get_double(data[j+1], opt_migration_matrix+i*matrix_dim+j)) fatal("Migration matrix cell (%ld,%ld) is not a number (line %ld)", i+1, j+1, line_count+2+i); } }
/** string_match() * function that goes through file looking for matches to the given hashes */ void string_match(str_data_t *data_in) { assert(data_in); char *key1 = "Helloworld"; char *key2 = "howareyou"; char *key3 = "ferrari"; char *key4 = "whotheman"; char *key1_final = malloc(strlen(key1) + 1); char *key2_final = malloc(strlen(key2) + 1); char *key3_final = malloc(strlen(key3) + 1); char *key4_final = malloc(strlen(key4) + 1); //printf("%d %d\n", strlen(key1), strlen(key2)); compute_hashes(key1, key1_final); compute_hashes(key2, key2_final); compute_hashes(key3, key3_final); compute_hashes(key4, key4_final); int key_len; char * key_file; key_file = data_in->keys_file; char * cur_word = malloc(MAX_REC_LEN); char * cur_word_final = malloc(MAX_REC_LEN); bzero(cur_word, MAX_REC_LEN); bzero(cur_word_final, MAX_REC_LEN); while( (key_len = getnextline(cur_word, MAX_REC_LEN, key_file))>=0) { compute_hashes(cur_word, cur_word_final); if(!strcmp(key1_final, cur_word_final)) dprintf("FOUND: WORD IS %s\n", cur_word); if(!strcmp(key2_final, cur_word_final)) dprintf("FOUND: WORD IS %s\n", cur_word); if(!strcmp(key3_final, cur_word_final)) dprintf("FOUND: WORD IS %s\n", cur_word); if(!strcmp(key4_final, cur_word_final)) dprintf("FOUND: WORD IS %s\n", cur_word); key_file = key_file + key_len; bzero(cur_word, MAX_REC_LEN); bzero(cur_word_final, MAX_REC_LEN); } free(cur_word); free(cur_word_final); free(key1_final); free(key2_final); free(key3_final); free(key4_final); }
static int gobble(struct filepointer *filep, struct inclist *file, struct inclist *file_red) { char *line; int type; while ((line = getnextline(filep))) { switch(type = deftype(line, filep, file_red, file, FALSE)) { case IF: case IFFALSE: case IFGUESSFALSE: case IFDEF: case IFNDEF: type = gobble(filep, file, file_red); while ((type == ELIF) || (type == ELIFFALSE) || (type == ELIFGUESSFALSE)) type = gobble(filep, file, file_red); if (type == ELSE) (void)gobble(filep, file, file_red); break; case ELSE: case ENDIF: debug(0,("%s, line %d: #%s\n", file->i_file, filep->f_line, directives[type])); return(type); case DEFINE: case UNDEF: case INCLUDE: case INCLUDEDOT: case PRAGMA: case ERROR: case IDENT: case SCCS: case EJECT: case WARNING: case INCLUDENEXT: case INCLUDENEXTDOT: break; case ELIF: case ELIFFALSE: case ELIFGUESSFALSE: return(type); case -1: warning("%s", file_red->i_file); if (file_red != file) warning1(" (reading %s)", file->i_file); warning1(", line %d: unknown directive == \"%s\"\n", filep->f_line, line); break; } } return(-1); }
int main() { char s[MAX]; int len = 0; while ( (len = (getnextline(s, MAX)) > 0 ) ) { revers(s); printf(" %s \n", s ); } return 0; }
/* print the longest input line */ 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 = getnextline(line, MAXLINE)) > 0) if (len > MINLINE) { printf("%s \n", line); } return 0; }
int main() { char s[MAX]; int d = 0; while ( (len = getnextline(s, MAX) )> 1 ) { // printf("len equals: %d", len); revers(s); // for ( d = 0; d <= len ; d++ ) // printf("s[%c] equals %c \n", d, s[d]); printf("%s\n", s); } return 0; }
bool user_exists(t_server *serv, char *username) { char *s; int fd; if (strcasecmp(username, "anonymous") == 0) return (true); fd = xopen(serv->save_file, O_RDONLY, 0); while ((s = getnextline(fd)) != NULL) { if (strncmp(username, s, strlen(username)) == 0) return (true); free(s); } return (false); }
/** string_match_map() * Map Function that checks the hash of each word to the given hashes */ void *string_match_map(void *args) { assert(args); str_map_data_t* data_in = (str_map_data_t*)( ((map_args_t*)args)->data); int key_len, total_len = 0; char * key_file = data_in->keys_file; char * cur_word = (char*)malloc(MAX_REC_LEN); char * cur_word_final = (char*)malloc(MAX_REC_LEN); bzero(cur_word, MAX_REC_LEN); bzero(cur_word_final, MAX_REC_LEN); while( (total_len < ((map_args_t*)args)->length) && ((key_len = getnextline(cur_word, MAX_REC_LEN, key_file)) >= 0)) { compute_hashes(cur_word, cur_word_final); if(!strcmp(key1_final, cur_word_final)) dprintf("FOUND: WORD IS %s\n", cur_word); if(!strcmp(key2_final, cur_word_final)) dprintf("FOUND: WORD IS %s\n", cur_word); if(!strcmp(key3_final, cur_word_final)) dprintf("FOUND: WORD IS %s\n", cur_word); if(!strcmp(key4_final, cur_word_final)) dprintf("FOUND: WORD IS %s\n", cur_word); key_file = key_file + key_len; bzero(cur_word,MAX_REC_LEN); bzero(cur_word_final, MAX_REC_LEN); total_len+=key_len; COZ_PROGRESS; } free(cur_word); free(cur_word_final); return (void *)0; }
int getnext ( void ) { char *rc; int i,j; int done=TRUE; char *ptr; int interps; int printed; printed = FALSE; for (i=0; i<numfiles; i++) { rc = getnextline( i, bufline ); if (rc) { /* We have a non-comment line in "bufline" */ strcpy ( tokens[i], bufline ); /* Strip newlines */ ptr = strchr(tokens[i], '\n'); if (ptr) *ptr = '\0'; } } printf("%s", comments); comments[0] = '\0'; memset(buffer, 0, 960); /* Now we have to check to be sure there are no tandem interpretations or local comments in any of the spines and if there are, we have to space them out */ interps = TRUE; while ( interps ) { for (i=0; i<numfiles; i++) { if (tokens[i][0] == '*' && !closed[i]) { /* Found interpretations */ if (tokens[i][0] == '*' && tokens[i][1] == '*') { /* Found exclusive interpretation */ for (j=0; j<numfiles; j++) { if (!closed[j]) { if (printed) printf("\t"); if (j!=i) { if ( tokens[j][0] == '*' && tokens[j][1] == '*' ) { printf("%s", tokens[j]); printed = TRUE; rc = getnextline( j, tokens[j] ); ptr = strchr(tokens[j], '\n'); if (ptr) *ptr = '\0'; if (!rc) { tokens[j][0] = '\0'; } } else { ptr = tokens[j]; while (*ptr != '\0') { if (*ptr == '\t') { printf("*\t"); } ptr++; } printf("*"); printed = TRUE; } } else { printf("%s", tokens[j]); printed = TRUE; rc = getnextline ( j, tokens[j] ); if (!rc) { tokens[j][0] = '\0'; } else { ptr = strchr(tokens[j], '\n'); if (ptr) *ptr = '\0'; i--; } } } } /* for */ printf("\n"); printed = FALSE; } else if ( strstr (tokens[i], "*v") ) { /* Join spines */ for (j=0; j<numfiles; j++) { if (!closed[j]) { if (printed) printf("\t"); if (j!=i) { if (strchr(tokens[j], '*')) { if ( strstr( tokens[j], "*v" ) || (!strstr( tokens[j], "*-" ) && !strstr( tokens[j], "*x" ) && !strstr( tokens[j], "*+" ) && !strstr( tokens[j], "*^" ))) { ptr = tokens[j]; while (*ptr != '\0') { if (*ptr == '\t') { printf("*\t"); } ptr++; } printf("*"); printed = TRUE; } else { printf("%s",tokens[j]); printed = TRUE; rc = getnextline ( j, tokens[j] ); ptr = strchr( tokens[j], '\n'); if (ptr) *ptr = '\0'; if (!rc) { tokens[j][0] = '\0'; } } } else { ptr = tokens[j]; while (*ptr != '\0') { if (*ptr == '\t') { printf("*\t"); } ptr++; } printf ("*"); printed = TRUE; } } else { printf("%s", tokens[i]); printed = TRUE; rc = getnextline ( i, tokens[i] ); if (!rc) { tokens[i][0] = '\0'; } else { ptr = strchr( tokens[i], '\n'); if (ptr) *ptr = '\0'; i--; } } } } /* for */ printf("\n"); printed = FALSE; } else if ( strstr (tokens[i], "*x") ) { /* Exchange spines */ for (j=0; j<numfiles; j++) { if (!closed[j]) { if (printed) printf("\t"); if (j!=i) { if (strchr(tokens[j], '*')) { if ( strstr( tokens[j], "*x" ) || (!strstr( tokens[j], "*-" ) && !strstr( tokens[j], "*v" ) && !strstr( tokens[j], "*+" ) && !strstr( tokens[j], "*^" ))) { ptr = tokens[j]; while (*ptr != '\0') { if (*ptr == '\t') { printf("*\t"); } ptr++; } printf("*"); printed = TRUE; } else { printf("%s",tokens[j]); printed = TRUE; rc = getnextline ( j, tokens[j] ); if (!rc) { tokens[j][0] = '\0'; } ptr = strchr( tokens[j], '\n'); if (ptr) *ptr = '\0'; } } else { ptr = tokens[j]; while (*ptr != '\0') { if (*ptr == '\t') { printf("*\t"); } ptr++; } printf("*"); printed = TRUE; } } else { printf("%s", tokens[j]); printed = TRUE; rc = getnextline ( j, tokens[j] ); if (!rc) { tokens[j][0] = '\0'; } else { ptr = strchr( tokens[j], '\n'); if (ptr) *ptr = '\0'; i--; } } } } /* for */ printf("\n"); printed = FALSE; } else if ( strstr(tokens[i],"*^") || strstr(tokens[i],"*+") || strstr(tokens[i],"*-")) { /* other spine path indicator */ for (j=0; j<numfiles; j++) { if (!closed[j]) { if (printed) printf("\t"); if (j!=i) { if (strchr(tokens[j], '*')) { if ( !strstr( tokens[j], "*-" ) && !strstr( tokens[j], "*+" ) && !strstr( tokens[j], "*^" )) { ptr = tokens[j]; while (*ptr != '\0') { if (*ptr == '\t') { printf("*\t"); } ptr++; } printf("*"); printed = TRUE; } else { printf("%s",tokens[j]); printed = TRUE; rc = getnextline ( j, tokens[j] ); if (!rc) { tokens[j][0] = '\0'; } ptr = strchr( tokens[j], '\n'); if (ptr) *ptr = '\0'; } } else { ptr = tokens[j]; while (*ptr != '\0') { if (*ptr == '\t') { printf("*\t"); } ptr++; } printf("*"); printed = TRUE; } } else { printf("%s", tokens[j]); printed = TRUE; rc = getnextline ( j, tokens[j] ); if (!rc) { tokens[j][0] = '\0'; } else { ptr = strchr( tokens[j], '\n'); if (ptr) *ptr = '\0'; i--; } } } } /* for */ printf("\n"); printed = FALSE; } else /* Interpretation is only tandem */ { for (j=0; j<numfiles; j++) { if (!closed[j]) { if (printed) printf("\t"); if (j!=i) { if (tokens[j][0] == '*' && !strstr(tokens[j],"**") && !strstr(tokens[j],"*-") && !strstr(tokens[j],"*+") && !strstr(tokens[j],"*v") && !strstr(tokens[j],"*^") && !strstr(tokens[j],"*x")) { printf("%s", tokens[j]); printed = TRUE; rc = getnextline( j, tokens[j] ); ptr = strchr(tokens[j], '\n'); if (ptr) *ptr = '\0'; if (!rc) { tokens[j][0] = '\0'; } } else { ptr = tokens[j]; while (*ptr != '\0') { if (*ptr == '\t') { printf("*\t"); } ptr++; } printf("*"); printed = TRUE; } } else { printf("%s", tokens[i]); printed = TRUE; rc = getnextline ( i, tokens[i] ); if (!rc) { tokens[i][0] = '\0'; } else { ptr = strchr( tokens[i], '\n'); if (ptr) *ptr = '\0'; i--; } } } } /* for */ printf("\n"); printed = FALSE; } /* Line contains tandem interpretation */ } /* does line contain an interpretation? */ else if (tokens[i][0] == '!' && !closed[i]) { /* Local comment has been found */ for (j=0; j<numfiles; j++) { if (!closed[j]) { if (printed) printf("\t"); if (j!=i) { if (tokens[j][0] == '!') { printf("%s", tokens[j]); printed = TRUE; rc = getnextline( j, tokens[j] ); ptr = strchr(tokens[j], '\n'); if (ptr) *ptr = '\0'; if (!rc) { tokens[j][0] = '\0'; } } else { ptr = tokens[j]; while (*ptr != '\0') { if (*ptr == '\t') { printf("!\t"); } ptr++; } printf("!"); printed = TRUE; } } else { printf("%s", tokens[i]); printed = TRUE; rc = getnextline ( i, tokens[i] ); if (!rc) { tokens[i][0] = '\0'; } else { ptr = strchr( tokens[i], '\n'); if (ptr) *ptr = '\0'; i--; } } } } /* for */ printf("\n"); printed = FALSE; } /* Line contains local comment */ } interps = FALSE; for (i=0;i<numfiles;i++) { if (tokens[i][0]=='*') interps = TRUE; } } printed = FALSE; for (i=0; i<numfiles; i++) { if (printed && !closed[i]) strcat (buffer, "\t"); if (!closed[i]) { strcat (buffer, tokens[i]); printed = TRUE; } } strcat(buffer, "\n"); printf("%s", comments); comments[0] = '\0'; done = TRUE; for (i=0; i<numfiles && done; i++) if (!closed[i]) done = FALSE; return !done; }
void load_cfile_sim() { long line_count = 0; FILE * fp; /* the following variable is used for checking whether we have a newick string in the species&tree tag, in the case of 1 species. For species trees we do not accept a tree, whereas for network we require a newick string. The program always reads a line. If that line is a tree it is processed, otherwise this variable is set such that we do not read another line */ long line_not_processed = 0; fp = xopen(opt_simulate,"r"); while (line_not_processed || getnextline(fp)) { int valid = 0; char * token; char * value; long token_len; line_not_processed = 0; ++line_count; token_len = get_token(line,&token,&value); if (!token_len) continue; if (token_len < 0) fatal("Invalid syntax when parsing file %s on line %ld", opt_simulate, line_count); if (token_len == 4) { if (!strncasecmp(token,"seed",4)) { if (!get_long(value,&opt_seed)) fatal("Option 'seed' expects one integer (line %ld)", line_count); if (opt_seed == -1) opt_seed = (long)time(NULL); valid = 1; } else if (!strncasecmp(token,"arch",4)) { char * temp; if (!get_string(value,&temp)) fatal("Option %s expects a string (line %ld)", token, line_count); if (!strcmp(temp,"cpu")) opt_arch = PLL_ATTRIB_ARCH_CPU; else if (!strcasecmp(temp,"sse")) opt_arch = PLL_ATTRIB_ARCH_SSE; else if (!strcasecmp(temp,"avx")) opt_arch = PLL_ATTRIB_ARCH_AVX; else if (!strcasecmp(temp,"avx2")) opt_arch = PLL_ATTRIB_ARCH_AVX2; else fatal("Invalid instruction set (%s) (line %ld)", temp, line_count); free(temp); valid = 1; } } else if (token_len == 5) { if (!strncasecmp(token,"clock",5)) { if (!parse_clock(value)) fatal("Option 'clock' expects values '1', '2 a' or '3 a' (line %ld)", line_count); valid = 1; } else if (!strncasecmp(token,"model",5)) { if (!get_long(value,&opt_model) || opt_model < 0) fatal("Option 'model' expects value '%d' or '%d' (line %ld)", BPP_DNA_MODEL_JC69, BPP_DNA_MODEL_GTR, line_count); valid = 1; } } else if (token_len == 6) { if (!strncasecmp(token,"qrates",6)) { if (!parse_qrates(value)) fatal("Option 'qrates' expects one switch and 6 values (line %ld)", line_count); valid = 1; } } else if (token_len == 7) { if (!strncasecmp(token,"seqfile",7)) { if (!get_string(value, &opt_msafile)) fatal("Option '%s' expects a string (line %ld)", token, line_count); valid = 1; } else if (!strncasecmp(token,"diploid",7)) { if (!parse_diploid(value)) fatal("Option %s expects values 0 or 1 for each species (line %ld)", token,line_count); valid = 1; } } else if (token_len == 8) { if (!strncasecmp(token,"treefile",8)) { if (!get_string(value, &opt_treefile)) fatal("Option 'treefile' expects a string (line %ld)", line_count); valid = 1; } else if (!strncasecmp(token,"imapfile",8)) { if (!get_string(value, &opt_mapfile)) fatal("Option 'imapfile' expects a string (line %ld)", line_count); valid = 1; } } else if (token_len == 9) { if (!strncasecmp(token,"basefreqs",9)) { if (!parse_basefreqs(value)) fatal("Option 'basefreqs' expects one switch and 4 values (line %ld)", line_count); valid = 1; } else if (!strncasecmp(token,"migration",9)) { if (!get_long(value,&opt_migration)) fatal("Option 'migration' expects one integer (line %ld)", line_count); parse_migration_matrix(fp, line_count); valid = 1; } } else if (token_len == 10) { if (!strncasecmp(token,"concatfile",10)) { if (!get_string(value,&opt_concatfile)) fatal("Option 'concatfile' expects a string (line %ld)", line_count); valid = 1; } } else if (token_len == 11) { if (!strncasecmp(token,"loci&length",11)) { if (!parse_loci_and_lengths(value)) fatal("Option 'loci&length' expects two positive integer values (line %ld)", line_count); valid = 1; } } else if (token_len == 12) { if (!strncasecmp(token,"species&tree",12)) { /* TODO: Currently only the old BPP format is allowed. Make it also accept only the tree in newick format, i.e. one line instead of 3 */ long spcount = 0; if (!parse_speciesandtree(value,&spcount)) fatal("Erroneous format of 'species&tree' (line %ld)", line_count); if (!getnextline(fp)) fatal("Incomplete 'species&tree' record (line %ld)", line_count); ++line_count; if (!readandvalidatecount(line,spcount)) fatal("Erroneous enumeration of species sequences in 'species&tree' " "tag (line %ld).\nExpected number of species is %ld.\n", line_count, spcount); if (spcount > 1) { if (!getnextline(fp)) fatal("Incomplete 'species&tree' record (line %ld)", line_count); ++line_count; if (!get_tree_string_with_thetas(line,&opt_streenewick)) fatal("Expected newick tree string in 'species&tree' (line %ld) ", "with ending ';' character", line_count); } else if (spcount == 1) { /* TODO: This is an ugly hack to account for the case where we have 1 species and a network */ int reached_eof = 0; if (!getnextline(fp)) reached_eof = 1; ++line_count; line_not_processed = 1; if (!reached_eof && starts_with_opar(line)) { if (!get_string(line,&opt_streenewick)) fatal("Expected newick string in 'species&tree' (line %ld)", line_count); line_not_processed = 0; } else { opt_streenewick = (char *)xmalloc((size_t)(strlen(opt_reorder)+2) * sizeof(char)); strcpy(opt_streenewick, opt_reorder); opt_streenewick[strlen(opt_reorder)] = ';'; opt_streenewick[strlen(opt_reorder)+1] = '\0'; } if (reached_eof) break; } valid = 1; } } else if (token_len == 13) { if (!strncasecmp(token,"modelparafile",13)) { if (!get_string(value,&opt_modelparafile)) fatal("Option 'modelparafile' expects a string (line %ld)", line_count); valid = 1; } } else if (token_len == 14) { if (!strncasecmp(token,"alpha_siterate",14)) { if (!parse_siterate(value)) fatal("Erroneous format of 'alpha_siterate' (line %ld)", line_count); valid = 1; } } else if (token_len == 15) { if (!strncasecmp(token,"alpha_locusrate",15)) { if (!get_double(value, &opt_locusrate_alpha)) fatal("Option 'alpha_locusrate' expects one value (line %ld)", line_count); if (opt_locusrate_alpha < 0) fatal("Option 'alpha_locusrate' expects a non-negative value (line %ld)", line_count); if (opt_locusrate_alpha == 0) opt_est_locusrate = 0; else opt_est_locusrate = 1; valid = 1; } } if (!valid) fatal("Invalid syntax when parsing file %s on line %ld", opt_simulate, line_count); } check_validity(); #if 0 if (opt_diploid) update_sp_seqcount(); #endif fclose(fp); }
int find_includes(struct filepointer *filep, struct inclist *file, struct inclist *file_red, int recursion, boolean failOK) { struct inclist *inclistp; char **includedirsp; register char *line; register int type; boolean recfailOK; while ((line = getnextline(filep))) { switch(type = deftype(line, filep, file_red, file, TRUE)) { case IF: doif: type = find_includes(filep, file, file_red, recursion+1, failOK); while ((type == ELIF) || (type == ELIFFALSE) || (type == ELIFGUESSFALSE)) type = gobble(filep, file, file_red); if (type == ELSE) gobble(filep, file, file_red); break; case IFFALSE: case IFGUESSFALSE: doiffalse: if (type == IFGUESSFALSE || type == ELIFGUESSFALSE) recfailOK = TRUE; else recfailOK = failOK; type = gobble(filep, file, file_red); if (type == ELSE) find_includes(filep, file, file_red, recursion+1, recfailOK); else if (type == ELIF) goto doif; else if ((type == ELIFFALSE) || (type == ELIFGUESSFALSE)) goto doiffalse; break; case IFDEF: case IFNDEF: { int isdef = (isdefined(line, file_red, NULL) != NULL); if (type == IFNDEF) isdef = !isdef; if (isdef) { debug(1,(type == IFNDEF ? "line %d: %s !def'd in %s via %s%s\n" : "", filep->f_line, line, file->i_file, file_red->i_file, ": doit")); type = find_includes(filep, file, file_red, recursion+1, failOK); while (type == ELIF || type == ELIFFALSE || type == ELIFGUESSFALSE) type = gobble(filep, file, file_red); if (type == ELSE) gobble(filep, file, file_red); } else { debug(1,(type == IFDEF ? "line %d: %s !def'd in %s via %s%s\n" : "", filep->f_line, line, file->i_file, file_red->i_file, ": gobble")); type = gobble(filep, file, file_red); if (type == ELSE) find_includes(filep, file, file_red, recursion+1, failOK); else if (type == ELIF) goto doif; else if (type == ELIFFALSE || type == ELIFGUESSFALSE) goto doiffalse; } } break; case ELSE: case ELIFFALSE: case ELIFGUESSFALSE: case ELIF: if (!recursion) gobble(filep, file, file_red); case ENDIF: if (recursion) return(type); case DEFINE: define(line, file); break; case UNDEF: if (!*line) { warning("%s", file_red->i_file); if (file_red != file) warning1(" (reading %s)", file->i_file); warning1(", line %d: incomplete undef == \"%s\"\n", filep->f_line, line); break; } undefine(line, file_red); break; case INCLUDE: case INCLUDEDOT: case INCLUDENEXT: case INCLUDENEXTDOT: inclistp = inclistnext; includedirsp = includedirsnext; debug(2,("%s, reading %s, includes %s\n", file_red->i_file, file->i_file, line)); add_include(filep, file, file_red, line, type, failOK); inclistnext = inclistp; includedirsnext = includedirsp; break; case ERROR: case WARNING: warning("%s", file_red->i_file); if (file_red != file) warning1(" (reading %s)", file->i_file); warning1(", line %d: %s\n", filep->f_line, line); break; case PRAGMA: case IDENT: case SCCS: case EJECT: break; case -1: warning("%s", file_red->i_file); if (file_red != file) warning1(" (reading %s)", file->i_file); warning1(", line %d: unknown directive == \"%s\"\n", filep->f_line, line); break; case -2: warning("%s", file_red->i_file); if (file_red != file) warning1(" (reading %s)", file->i_file); warning1(", line %d: incomplete include == \"%s\"\n", filep->f_line, line); break; } } file->i_flags |= FINISHED; debug(2,("finished with %s\n", file->i_file)); return(-1); }
// uses concept of getting parsable tokens seperated by whitespace and '/' // one line of file is parsed at a time, lines seperated by '\n' void ObjFileModel::parsefile() { tokenptr=0; // token pointer points to first element of buffer int tokenstart, tokenlength; xyz tempxyz; xy tempxy; bool success; int line=0; do { line++; // keep track of current line number for error reporting if(!getnexttoken(tokenstart, tokenlength)) continue; // get first token on line, go to next line if first token is \n // ADD FURTHER KEYWORDS HERE TO EXTEND CAPABILITIES if(strncmp(&fbuffer[tokenstart], "v ", 2)==0) // VERTEX POSITION - note the space in the string is needed (see vt, etc) { success=true; // used to see if correct number of tokens left on line for this type of attribute success = success && getnexttoken(tokenstart, tokenlength); tempxyz.x = (float) atof(&fbuffer[tokenstart]); success = success && getnexttoken(tokenstart, tokenlength); tempxyz.y = (float) atof(&fbuffer[tokenstart]); success = success && getnexttoken(tokenstart, tokenlength); tempxyz.z = (float) atof(&fbuffer[tokenstart]); // if not correct number of tokens, display error in debug output if(!success) {char s[100] = "ERROR: Badly formatted vertex, line : "; _itoa(line, &s[strlen(s)], 10); strcat(s, " : "); strcat(s, filename.c_str()); DXTRACE_MSG(s); } position_list.push_back(tempxyz); // add a new element to the list } else if(strncmp(&fbuffer[tokenstart], "vt", 2)==0) // TEXTURE COORDINATES { success=true; success = success && getnexttoken(tokenstart, tokenlength); tempxy.x = (float) atof(&fbuffer[tokenstart]); success = success && getnexttoken(tokenstart, tokenlength); tempxy.y = (float) atof(&fbuffer[tokenstart]); if(!success) {char s[100] = "ERROR: Badly formatted texture coordinate, line : "; _itoa(line, &s[strlen(s)], 10); strcat(s, " : "); strcat(s, filename.c_str()); DXTRACE_MSG(s); } texcoord_list.push_back(tempxy); } else if(strncmp(&fbuffer[tokenstart], "vn", 2)==0) // NORMALS { success=true; success = success && getnexttoken(tokenstart, tokenlength); tempxyz.x = (float) atof(&fbuffer[tokenstart]); success = success && getnexttoken(tokenstart, tokenlength); tempxyz.y = (float) atof(&fbuffer[tokenstart]); success = success && getnexttoken(tokenstart, tokenlength); tempxyz.z = (float) atof(&fbuffer[tokenstart]); if(!success) {char s[100] = "ERROR: Badly formatted normal, line : "; _itoa(line, &s[strlen(s)], 10); strcat(s, " : "); strcat(s, filename.c_str()); DXTRACE_MSG(s); } normal_list.push_back(tempxyz); } else if(strncmp(&fbuffer[tokenstart], "f ", 2)==0) // FACE - only deals with triangles so far { int tempptr = tokenstart + 2; // skip "f " int forwardslashcount=0; bool adjacentslash = false; // this works out how many elements are specified for a face, e.g. // f 1 2 3 -> 0 forward slashes = just position // f 1/1 2/2 3/3 -> 3 slashes = position and texcoords // f 1/1/1 2/2/2 3/3/3 -> 6 slashes = position, texcoords, normals // f 1//1 2//2 3//3 -> 6 slashes with adjacent = position, normals while(fbuffer[tempptr] != '\n') { if(fbuffer[tempptr] == '/') { forwardslashcount++; if(fbuffer[tempptr-1] == '/') adjacentslash=true; } tempptr++; } success=true; // Get 3 sets of indices per face for(int i=0; i<3; i++) { // get vertex index success = success && getnexttoken(tokenstart, tokenlength); pindices.push_back(atoi(&fbuffer[tokenstart])); if(forwardslashcount>=3&& adjacentslash==false) // get texcoord index if required { success = success && getnexttoken(tokenstart, tokenlength); tindices.push_back(atoi(&fbuffer[tokenstart])); } if(forwardslashcount==6 || adjacentslash==true) // get normal index if required { success = success && getnexttoken(tokenstart, tokenlength); nindices.push_back(atoi(&fbuffer[tokenstart])); } } if(!success) {char s[100] = "ERROR: Badly formatted face, line : "; _itoa(line, &s[strlen(s)], 10); strcat(s, " : "); strcat(s, filename.c_str()); DXTRACE_MSG(s); } } } while(getnextline() == true); }
int parse_database() { FILE *f; char buf[128]; int i, lineno; char filename[128]; #ifndef __UNIXOS2__ strcpy(filename, CARD_DATABASE_FILE); #else strcpy(filename, (char*)__XOS2RedirRoot(CARD_DATABASE_FILE)); #endif f = fopen(filename, "r"); if (f == NULL) return -1; lastcard = -1; lineno = 0; for (;;) { if (getnextline(f, buf)) break; lineno++; if (buf[0] == '#') /* Comment. */ continue; if (strncmp(buf, "END", 3) == 0) /* End of database. */ break; if (strncmp(buf, "LINE", 4) == 0 && lastcard>=0) { /* Line of Device comment. */ /* Append to existing lines. */ appendstring(&card[lastcard].lines, buf + 5); continue; } /* * The following keywords require the trailing newline * to be deleted. */ i = strlen(buf); buf[--i] = '\0'; /* remove trailing spaces or tabs */ for(--i; i>=0 && (buf[i] == ' ' || buf[i] == '\011'); i--) ; if (i>=0) buf[i+1] = '\0'; else continue; /* skip empty lines */ if (strncmp(buf, "NAME", 4) == 0) { /* New entry. */ lastcard++; card[lastcard].name = malloc(strlen(buf + 5) + 1); strcpy(card[lastcard].name, buf + 5); card[lastcard].chipset = NULL; card[lastcard].server = NULL; card[lastcard].driver = NULL; card[lastcard].ramdac = NULL; card[lastcard].clockchip = NULL; card[lastcard].dacspeed = NULL; card[lastcard].flags = 0; card[lastcard].lines = ""; continue; } if (lastcard < 0) /* no NAME line found yet */ continue; if (strncmp(buf, "SEE", 3) == 0) { /* Reference to another entry. */ int i; i = lookupcard(buf + 4); if (i == -1) { printf("Error in database, invalid reference: %s.\n", buf + 4); free(card[lastcard].name); lastcard--; continue; } if (card[lastcard].chipset == NULL) card[lastcard].chipset = card[i].chipset; if (card[lastcard].server == NULL) card[lastcard].server = card[i].server; if (card[lastcard].driver == NULL) card[lastcard].driver = card[i].driver; if (card[lastcard].ramdac == NULL) card[lastcard].ramdac = card[i].ramdac; if (card[lastcard].clockchip == NULL) card[lastcard].clockchip = card[i].clockchip; if (card[lastcard].dacspeed == NULL) card[lastcard].dacspeed = card[i].dacspeed; card[lastcard].flags |= card[i].flags; appendstring(&card[lastcard].lines, card[i].lines); continue; } if (strncmp(buf, "CHIPSET", 7) == 0) { /* Chipset description. */ card[lastcard].chipset = malloc(strlen(buf + 8) + 1); strcpy(card[lastcard].chipset, buf + 8); continue; } if (strncmp(buf, "SERVER", 6) == 0) { /* Server identifier. */ card[lastcard].server = malloc(strlen(buf + 7) + 1); strcpy(card[lastcard].server, buf + 7); continue; } if (strncmp(buf, "DRIVER", 6) == 0) { /* Driver identifier. */ card[lastcard].driver = malloc(strlen(buf + 7) + 1); strcpy(card[lastcard].driver, buf + 7); continue; } if (strncmp(buf, "RAMDAC", 6) == 0) { /* Ramdac indentifier. */ card[lastcard].ramdac = malloc(strlen(buf + 7) + 1); strcpy(card[lastcard].ramdac, buf + 7); continue; } if (strncmp(buf, "CLOCKCHIP", 9) == 0) { /* Clockchip indentifier. */ card[lastcard].clockchip = malloc(strlen(buf + 10) + 1); strcpy(card[lastcard].clockchip, buf + 10); card[lastcard].flags |= NOCLOCKPROBE; continue; } if (strncmp(buf, "DACSPEED", 8) == 0) { /* Clockchip indentifier. */ card[lastcard].dacspeed = malloc(strlen(buf + 9) + 1); strcpy(card[lastcard].dacspeed, buf + 9); continue; } if (strncmp(buf, "NOCLOCKPROBE", 12) == 0) { card[lastcard].flags |= NOCLOCKPROBE; continue; } if (strncmp(buf, "UNSUPPORTED", 12) == 0) { card[lastcard].flags |= UNSUPPORTED; continue; } /* test for missing required fields */ if (card[lastcard].driver == NULL) { fprintf(stderr, "Warning DRIVER specification missing " "in Card database entry %s (line %d).\n", card[lastcard].name, lineno); keypress(); card[lastcard].driver = "unknown"; } if (card[lastcard].chipset == NULL) { fprintf(stderr, "Warning CHIPSET specification missing " "in Card database entry %s (line %d).\n", card[lastcard].name, lineno); keypress(); card[lastcard].chipset = "unknown"; } } fclose(f); /* * Add general comments. */ for (i = 0; i <= lastcard; i++) { if (card[i].server && strcmp(card[i].server, "S3") == 0) appendstring(&card[i].lines, s3_comment); if (card[i].chipset && strncmp(card[i].chipset, "CL-GD", 5) == 0) appendstring(&card[i].lines, cirrus_comment); } sort_database(); return 0; }