static int skip_comment(BFile file, COMMENT_TYPE commentType) { int return_value = -1; switch(commentType) { case COMMENT_SLASH_STAR: return_value = skip_slash_star_comment(file); break; case COMMENT_SLASH_SLASH: return_value = skip_slash_slash_comment(file); break; } return return_value; }
int fgetc_skipping_comments_between_tokens( FILE *cur_file, char *cur_file_name ) { int c; if (fgetc_buffer != FLAG_BUFFER_NOT_USED) { c = fgetc_buffer; fgetc_buffer = FLAG_BUFFER_NOT_USED; return(c); } for (c = fgetc(cur_file); TYPE_TRUE; c = fgetc(cur_file)) { switch (c) { case EOF: premature_exit_at_EOF(cur_file_name); break; case '/': fgetc_buffer = fgetc(cur_file); switch (fgetc_buffer) { case EOF: premature_exit_at_EOF(cur_file_name); break; case '*': fprintf(TARGET_INCLUDE_FILE, "/*"); skip_slash_star_comment(cur_file, cur_file_name); fgetc_buffer = FLAG_BUFFER_NOT_USED; break; case '/': fprintf(TARGET_INCLUDE_FILE, "//"); skip_slash_slash_comment(cur_file, cur_file_name); fgetc_buffer = FLAG_BUFFER_NOT_USED; break; default: return('/'); break; } default: return(c); } } }
/* * Gets a char from a C source file, skipping comments */ static int get_char_from_c_file(BFile file) { int iChar = EOF; BOOL done = FALSE; while (!done) { iChar = bfile_get_char(file); switch (iChar) { case EOF: done = TRUE; break; case '\n': if (bfile_is_forward(file)) { done = TRUE; } else { if (skip_slash_slash_comment(file) < 0) { /* no comment */ done = TRUE; } } break; case '/': if (bfile_is_forward(file)) { iChar = bfile_get_char(file); if (iChar == '/') { skip_slash_slash_comment(file); } else if (iChar == '*') { skip_slash_star_comment(file); } else { bfile_backup(file, 1); } } else { /* reversed file */ if ((iChar = bfile_get_char(file)) == '*') { skip_slash_star_comment(file); } else { bfile_backup(file, 1); } } break; default: done = TRUE; break; } } /* while !done */ return iChar; }
int update_include_status( char c, FILE *cur_file, char *cur_file_name ) { switch (include_pos) { case INCLUDE_POS_NONE: if (c == '#') include_pos = INCLUDE_POS_POUND; else if (c == '/') { include_pos = INCLUDE_COMMENT_START; fprintf(TARGET_INCLUDE_FILE, "%c", c); } else fprintf(TARGET_INCLUDE_FILE, "%c", c); break; case INCLUDE_POS_POUND: if (c == 'i') include_pos++; else if (c == '#') { fprintf(TARGET_INCLUDE_FILE, "#"); include_pos = INCLUDE_POS_POUND; } else if (c == '/') { include_pos = INCLUDE_COMMENT_START; fprintf(TARGET_INCLUDE_FILE, "%c", c); } else { fprintf(TARGET_INCLUDE_FILE, "#%c", c); include_pos = INCLUDE_POS_NONE; } break; case INCLUDE_POS_I: if (c == 'n') include_pos++; else if (c == '#') { fprintf(TARGET_INCLUDE_FILE, "#i"); include_pos = INCLUDE_POS_POUND; } else if (c == '/') { include_pos = INCLUDE_COMMENT_START; fprintf(TARGET_INCLUDE_FILE, "%c", c); } else { fprintf(TARGET_INCLUDE_FILE, "#i%c", c); include_pos = INCLUDE_POS_NONE; } break; case INCLUDE_POS_N: if (c == 'c') include_pos++; else if (c == '#') { fprintf(TARGET_INCLUDE_FILE, "#in"); include_pos = INCLUDE_POS_POUND; } else if (c == '/') { include_pos = INCLUDE_COMMENT_START; fprintf(TARGET_INCLUDE_FILE, "%c", c); } else { fprintf(TARGET_INCLUDE_FILE, "#in%c", c); include_pos = INCLUDE_POS_NONE; } break; case INCLUDE_POS_C: if (c == 'l') include_pos++; else if (c == '#') { fprintf(TARGET_INCLUDE_FILE, "#inc"); include_pos = INCLUDE_POS_POUND; } else if (c == '/') { include_pos = INCLUDE_COMMENT_START; fprintf(TARGET_INCLUDE_FILE, "%c", c); } else { fprintf(TARGET_INCLUDE_FILE, "#inc%c", c); include_pos = INCLUDE_POS_NONE; } break; case INCLUDE_POS_L: if (c == 'u') include_pos++; else if (c == '#') { fprintf(TARGET_INCLUDE_FILE, "#incl"); include_pos = INCLUDE_POS_POUND; } else if (c == '/') { include_pos = INCLUDE_COMMENT_START; fprintf(TARGET_INCLUDE_FILE, "%c", c); } else { fprintf(TARGET_INCLUDE_FILE, "#incl%c", c); include_pos = INCLUDE_POS_NONE; } break; case INCLUDE_POS_U: if (c == 'd') include_pos++; else if (c == '#') { fprintf(TARGET_INCLUDE_FILE, "#inclu"); include_pos = INCLUDE_POS_POUND; } else if (c == '/') { include_pos = INCLUDE_COMMENT_START; fprintf(TARGET_INCLUDE_FILE, "%c", c); } else { fprintf(TARGET_INCLUDE_FILE, "#inclu%c", c); include_pos = INCLUDE_POS_NONE; } break; case INCLUDE_POS_D: if (c == 'e') { try_to_include(cur_file, cur_file_name); include_pos = INCLUDE_POS_NONE; } else if (c == '#') { fprintf(TARGET_INCLUDE_FILE, "#includ"); include_pos = INCLUDE_POS_POUND; } else if (c == '/') { include_pos = INCLUDE_COMMENT_START; fprintf(TARGET_INCLUDE_FILE, "%c", c); } else { fprintf(TARGET_INCLUDE_FILE, "#includ%c", c); include_pos = INCLUDE_POS_NONE; } break; case INCLUDE_COMMENT_START: switch (c) { case '*': fprintf(TARGET_INCLUDE_FILE, "*"); skip_slash_star_comment(cur_file, cur_file_name); include_pos = INCLUDE_POS_NONE; break; case '/': fprintf(TARGET_INCLUDE_FILE, "/"); skip_slash_slash_comment(cur_file, cur_file_name); include_pos = INCLUDE_POS_NONE; break; case '#': include_pos = INCLUDE_POS_POUND; break; default: fprintf(TARGET_INCLUDE_FILE, "%c", c); include_pos = INCLUDE_POS_NONE; } break; default: fprintf(RED_OUT, "\nSomething must be wrong.\n"); fflush(RED_OUT); exit(0); break; } }