/************************************************************************************************************ * Function name : clear_assignment * * * * INPUT : char *filename * * unsigned int exponent * * int bit_min - from old assignment file * * int bit_max * * int bit_min_new - new bit_min,what was factored to--if 0,reached bit_max * * OUTPUT : * * * * 0 - OK * * 3 - clear_assignment : cannot open file <filename> * * 4 - clear_assignment : cannot open file "__worktodo__.tmp" * * 5 - clear_assignment : assignment not found * * 6 - clear_assignment : cannot rename temporary workfile to regular workfile * * * * If bit_min_new is zero then the specified assignment will be cleared. If bit_min_new is greater than * * zero the specified assignment will be modified * ************************************************************************************************************/ enum ASSIGNMENT_ERRORS clear_assignment(char *filename, unsigned int exponent, int bit_min, int bit_max, int bit_min_new) { int found = FALSE; FILE *f_in, *f_out; LINE_BUFFER line; // line buffer char *tail = NULL; // points to tail material in line, if non-null enum PARSE_WARNINGS value; unsigned int line_to_drop = UINT_MAX; unsigned int current_line; struct ASSIGNMENT assignment; // the found assignment.... f_in = fopen_and_lock(filename, "r"); if (NULL == f_in) return CANT_OPEN_WORKFILE; f_out = fopen_and_lock("__worktodo__.tmp", "w"); if (NULL == f_out) { unlock_and_fclose(f_in); return CANT_OPEN_TEMPFILE; } if ((bit_min_new > bit_min) && (bit_min_new < bit_max)) // modify only line_to_drop = UINT_MAX; else { current_line =0; while (END_OF_FILE != (value = parse_worktodo_line(f_in,&assignment,&line,&tail)) ) { current_line++; if (NO_WARNING == value) { if( (exponent == assignment.exponent) && (bit_min == assignment.bit_min) && (bit_max == assignment.bit_max)) // make final decision { if (line_to_drop > current_line) line_to_drop = current_line; break; } else { line_to_drop = current_line+1; // found different assignment, can drop no earlier than next line } } else if ((BLANK_LINE == value) && (UINT_MAX == line_to_drop)) line_to_drop = current_line+1; } } errno = 0; if (fseek(f_in,0L,SEEK_SET)) { unlock_and_fclose(f_in); f_in = fopen_and_lock(filename, "r"); if (NULL == f_in) { unlock_and_fclose(f_out); return CANT_OPEN_WORKFILE; } } found = FALSE; current_line = 0; while (END_OF_FILE != (value = parse_worktodo_line(f_in,&assignment,&line,&tail)) ) { current_line++; if ((NO_WARNING != value) || found) { if ((found) || (current_line < line_to_drop)) fprintf(f_out, "%s", line); } else // assignment on the line, so we may need to print it.. { found =( (exponent == assignment.exponent) && (bit_min == assignment.bit_min) && (bit_max == assignment.bit_max) ); if (!found) { fprintf(f_out,"%s",line); } else // we have the assignment... { if ((bit_min_new > bit_min) && (bit_min_new < bit_max)) { fprintf(f_out,"Factor=" ); if (strlen(assignment.assignment_key) != 0) fprintf(f_out,"%s,", assignment.assignment_key); fprintf(f_out,"%u,%u,%u",exponent, bit_min_new, bit_max); if (tail != NULL) fprintf(f_out,"%s",tail); } } } } // while..... unlock_and_fclose(f_out); unlock_and_fclose(f_in); if (!found) return ASSIGNMENT_NOT_FOUND; if(remove(filename) != 0) return CANT_RENAME; if(rename("__worktodo__.tmp", filename) != 0) return CANT_RENAME; return OK; }
/************************************************************************************************************ * Function name : clear_assignment * * * * INPUT : char *filename * * int exponent * * OUTPUT : * * * * 0 - OK * * 3 - clear_assignment : cannot open file <filename> * * 4 - clear_assignment : cannot open file "__worktodo__.tmp" * * 5 - clear_assignment : assignment not found * * 6 - clear_assignment : cannot rename temporary workfile to regular workfile * * * ************************************************************************************************************/ enum ASSIGNMENT_ERRORS clear_assignment(char *filename, int exponent) { int found = 0; FILE *f_in, *f_out; LINE_BUFFER line; // line buffer char *tail = NULL; // points to tail material in line, if non-null enum PARSE_WARNINGS value; unsigned int line_to_drop = UINT_MAX; unsigned int current_line; struct ASSIGNMENT assignment; // the found assignment.... f_in = _fopen(filename, "r"); if (NULL == f_in) { fprintf(stderr, "Can't open workfile %s\n\n", filename); return CANT_OPEN_WORKFILE; } f_out = _fopen("__worktodo__.tmp", "w"); if (NULL == f_out) { fclose(f_in); fprintf(stderr, "Can't open tmp workfile\n\n"); return CANT_OPEN_TEMPFILE; } current_line = 0; while (END_OF_FILE != (value = parse_worktodo_line(f_in, &assignment, &line, &tail))) { current_line++; #ifdef EBUG printf("Current_line = %d\n", current_line); #endif if (NO_WARNING == value) { if ((exponent == assignment.exponent)) // make final decision { if (line_to_drop > current_line) line_to_drop = current_line; #ifdef EBUG printf("Made final decision. linetodrop = %d, current = %d\n", line_to_drop, current_line); #endif break; } else { line_to_drop = current_line + 1; // found different assignment, can drop no earlier than next line } } else if ((BLANK_LINE == value) && (UINT_MAX == line_to_drop)) line_to_drop = current_line + 1; } #ifdef EBUG printf("Broken the first while loop, linetodrop = %d\n", line_to_drop); #endif errno = 0; if (fseek(f_in, 0L, SEEK_SET)) { fclose(f_in); f_in = _fopen(filename, "r"); if (NULL == f_in) { fclose(f_out); fprintf(stderr, "Can't open workfile %s\n\n", filename); return CANT_OPEN_WORKFILE; } } found = 0; current_line = 0; while (END_OF_FILE != (value = parse_worktodo_line(f_in, &assignment, &line, &tail))) { current_line++; if ((NO_WARNING != value) || found) { if ((found) || (current_line < line_to_drop)) fprintf(f_out, "%s\n", line); } else // assignment on the line, so we may need to print it.. { found = (exponent == assignment.exponent); if (!found) { fprintf(f_out, "%s\n", line); } else // we have the assignment... { // Do nothing; we don't print this to the temp file, 'cause we're trying to delete it :) } } } // while..... fclose(f_in); fclose(f_out); if (!found) { fprintf(stderr, "Assignment M%d completed but not found in workfile\n\n", exponent); return ASSIGNMENT_NOT_FOUND; } if (remove(filename) != 0) { fprintf(stderr, "Cannot remove old workfile\n\n"); return CANT_RENAME; } if (rename("__worktodo__.tmp", filename) != 0) { fprintf(stderr, "Cannot move tmp workfile to regular workfile\n\n"); return CANT_RENAME; } return OK; }
/************************************************************************************************************ * Function name : get_next_assignment * * * * INPUT : char *filename * * unsigned int *exponent * * int *bit_min * * int *bit_max * * char *assignment_key[100]; * OUTPUT : * * * * 0 - OK * * 1 - get_next_assignment : cannot open file * * 2 - get_next_assignment : no valid assignment found * ************************************************************************************************************/ enum ASSIGNMENT_ERRORS get_next_assignment(char *filename, unsigned int *exponent, unsigned int *bit_min, unsigned int *bit_max, LINE_BUFFER *key, int verbosity) { FILE *f_in; enum PARSE_WARNINGS value; struct ASSIGNMENT assignment; char *tail; LINE_BUFFER line; unsigned int linecount=0; // first, make sure we have an up-to-date worktodo file process_add_file(filename); f_in = fopen_and_lock(filename, "r"); if(f_in == NULL) { printf("Can't open workfile %s\n", filename); return CANT_OPEN_FILE; // nothing to open... } for(;;) { linecount++; value = parse_worktodo_line(f_in,&assignment,&line,&tail); if ((BLANK_LINE == value) || (NONBLANK_LINE == value)) continue; if (NO_WARNING == value) { if (valid_assignment(assignment.exponent,assignment.bit_min, assignment.bit_max, verbosity)) break; value = INVALID_DATA; } if (END_OF_FILE == value) break; if(verbosity >= 1) { printf("WARNING: ignoring line %u in \"%s\"! Reason: ", linecount, filename); switch(value) { case LONG_LINE: printf("line is too long\n"); break; case NO_FACTOR_EQUAL: printf("doesn't begin with Factor=\n");break; case INVALID_FORMAT: printf("invalid format\n");break; case INVALID_DATA: printf("invalid data\n");break; default: printf("unknown error on >%s<",line); break; } } } unlock_and_fclose(f_in); if (NO_WARNING == value) { *exponent = assignment.exponent; *bit_min = assignment.bit_min; *bit_max = assignment.bit_max; if (key!=NULL)strcpy(*key,assignment.assignment_key); return OK; } else return VALID_ASSIGNMENT_NOT_FOUND; }
/************************************************************************************************************ * Function name : get_next_assignment * * * * INPUT : char *filename * * int *exponent * * int* fft_length * * char *hex_key[132]; * * OUTPUT : * * * * 0 - OK * * 1 - get_next_assignment : cannot open file * * 2 - get_next_assignment : no valid assignment found * ************************************************************************************************************/ enum ASSIGNMENT_ERRORS get_next_assignment(char *filename, int *exponent, int* fft_length, int* tf_depth, int* ll_saved, LINE_BUFFER *key, int* b1, int* b2) { struct ASSIGNMENT assignment; FILE *f_in; enum PARSE_WARNINGS value; char *tail; LINE_BUFFER line; unsigned int linecount = 0; #ifdef EBUG printf("Starting GNA. Called with *fft = %d.\n", *fft_length); #endif assignment.fft_length = 0; f_in = _fopen(filename, "r"); if (NULL == f_in) { // printf("Can't open workfile %s in %s\n", filename, getcwd(line,sizeof(LINE_BUFFER)) ); fprintf(stderr, "Can't open workfile %s\n\n", filename); return CANT_OPEN_FILE; // nothing to open... } do { linecount++; value = parse_worktodo_line(f_in, &assignment, &line, &tail); if ((BLANK_LINE == value) || (NONBLANK_LINE == value)) continue; if (NO_WARNING == value) { if (valid_assignment(assignment.exponent, assignment.fft_length)) break; value = INVALID_DATA; } if (END_OF_FILE == value) break; fprintf(stderr, "Warning: ignoring line %u: \"%s\" in \"%s\". Reason: ", linecount, line, filename); switch (value) { case LONG_LINE: fprintf(stderr, "line is too long.\n"); break; case NO_TEST_EQUAL: fprintf(stderr, "doesn't begin with Test= or DoubleCheck=.\n"); break; case INVALID_FORMAT: fprintf(stderr, "invalid format.\n"); break; case INVALID_DATA: fprintf(stderr, "invalid data.\n"); break; default: fprintf(stderr, "unknown error.\n"); break; } // if (LONG_LINE != value) // return 2; } while (1); fclose(f_in); if (NO_WARNING == value) { *exponent = assignment.exponent; if (assignment.fft_length > 0) *fft_length = assignment.fft_length; *tf_depth = assignment.tf_depth; if (assignment.type == PFACTOR) *ll_saved = assignment.ll_saved; if (assignment.type == PMINUS1) { *b1 = assignment.b1; *b2 = assignment.b2; } #ifdef EBUG printf("Struct fft is %d, *fft_length is %d\n", assignment.fft_length, *fft_length); #endif if (key != NULL) strcopy(*key, assignment.hex_key, MAX_LINE_LENGTH + 1); return OK; } else fprintf(stderr, "No valid assignment found.\n\n"); return VALID_ASSIGNMENT_NOT_FOUND; }