/****************************************************************************** NAME: addline.c FUNCTION: Searches the given directory for all files beginning with "io". Opens and reads this file, copying each line to a temporary file. When it detects the line: #define xxxxxxx_vect _VECTOR(##) it precedes this line with the line: #define xxxxxxx_vect_num ## using the same ## number. Finally removes the original file and renames the temporary file with the same name. AUTHOR: Ron Kreymborg Jennaron Resarch April, 2008 ******************************************************************************/ #include #include #include #include #include #include #define TRUE 1 #define FALSE 0 #define TEMP_NAME "tempxxxx.tmp" int main(int argc, char* argv[]) { char folderName[1024]; DIR *dp; struct dirent *ep; char fileName[1024]; char buffer[512]; FILE* istream; FILE* ostream; size_t bytes; char* token; char line[512]; char temp[1024]; char name[512]; char vector[512]; size_t number; if (argc != 2) { printf("Command is: addline 'folder_name'\n"); return 1; } // Get the folder name. // if ((bytes = strlen(argv[1])) == 0) { printf("Zero length folder name."); exit(1); } strcpy(folderName, argv[1]); printf("\nIn folder: %s\n", folderName); printf("Converting files...\n"); // Work through all files in this directory. // strcat(folderName, "/"); dp = opendir(folderName); if (dp != NULL) { while ((ep = readdir(dp))) { // Only look in files that start with "io" and have a ".h" // extension. // int flag = TRUE; strcpy(fileName, ep->d_name); strncpy(temp, fileName, 2); temp[2] = '\0'; strupr(temp); if (strcmp(temp, "IO") != 0) { flag = FALSE; } else { strcpy(temp, fileName); strupr(temp); if (strstr(temp, ".H") == NULL) { flag = FALSE; } else { // Search the input file to be sure it has not already // been converted. // if ((istream = fopen(fileName, "r")) == NULL) { printf("Cannot find input file %s.\n", fileName); flag = FALSE; } while (fgets(line, 500, istream)) { if (strstr(line, "_vect_num") != NULL) { flag = FALSE; break; } } fclose(istream); } } if (flag) { flag = FALSE; printf(" %s\n", fileName); // Not previously converted. Proceed with the conversion... // istream = fopen(fileName, "r"); if ((ostream = fopen(TEMP_NAME, "w")) == NULL) { printf("Cannot open output file.\n"); return 1; } while (fgets(line, 500, istream)) { strcpy(buffer, line); token = strtok(buffer, " \t"); if (strcmp(token, "#define") == 0) { token = strtok(NULL, " \t"); if (token) { strcpy(name, token); token = strtok(NULL, " \t"); if (token) { strcpy(vector, token); if (strlen(vector) >= 10) { strncpy(temp, vector, 7); temp[7] = '\0'; strupr(temp); if (strcmp(temp, "_VECTOR") == 0) { // This is an interrupt vector definition. // Discover if it is a "_vect" definition. // if ((token = strstr(name, "_vect")) != NULL) { // It is. Extract the vector number. // strncpy(temp, &vector[8], 2); if (temp[1] == ')') { temp[1] = '\0'; } else { temp[2] = '\0'; } number = atoi(temp); // vector number strcat(name, "_num"); // name is now XXXX_vect_num sprintf(buffer, "#define %s ", name); strcpy(&buffer[40], temp); strcat(buffer, "\n"); fputs(buffer, ostream); // write new line flag = TRUE; } } } } } } fputs(line, ostream); // write existing line } fclose(istream); fclose(ostream); // If a change was made, delete the old file and rename the temporary. // if (flag) { remove(fileName); if (rename(TEMP_NAME, fileName)) { printf("Could not rename: %s\n", fileName); return 1; } } } } } return 0; }