/* sd_rpl.c Given the .sd file for a given project, sd_rpl creates .rpl files for each stack. These in turn are read by replicat along with the template, soon to become card templates, to create .hif files for each stack. These are then read by hif_nra to create nra instructions. */ #include #include #include #include #include #include "rpldef.h" #if 1 #define SD_DEBUG1 #define SD_DEBUG2 #define SD_DEBUG3 #define SD_DEBUG4 #endif FILE *sdl_fd; char* sdl_fn; #undef MAX_FIELDS #define MAX_FIELDS 100 #define MAX_FIELD_NAME_LENGTH 100 int check_links = 0; /* Don't resolve hyperlinks. Used by rich.c */ int format_lines = 0; /* Don't fill format_text array. Used by rich.c */ unsigned char country[100]; unsigned char sdl_file[100]; extern unsigned char **data_ptr; extern FILE *data_fd; extern unsigned char data_file[50]; FILE *ser_fd; FILE *lnk_fd; FILE *wpg_fd; FILE *toc_fd; char g_debug_flag = 0; int main(argc,argv) int argc; char **argv; { int i,j; int status = 0; unsigned char file_name[100]; unsigned char date_str[100]; FILE *cif_fd; FILE *drw_fd; /* Command line parsing */ if (argc > 1) { for (i=1; i= argc) { printf("sd_rpl error: bad command line. Exiting.\n"); exit(1); } sdl_fn = argv[i+1]; if ((sdl_fd = fopen(sdl_fn,"r")) == NULL) { printf("sd_rpl error: couldn't open file %s. Exiting.\n", argv[i+1]); exit(1); } printf("Opened .sd file: %s\n",argv[i+1]); strcpy(sdl_file,argv[i+1]); for (j=0;sdl_file[j];j++) { if (sdl_file[j] == '.') break; country[j] = sdl_file[j]; } country[j] = '\0'; } } } else { printf("Usage: [-d [debug]] -i .\n"); exit(1); } /* open cross link file .lnk */ strcpy(file_name,country); strcat(file_name,".lnk"); if ((lnk_fd = fopen(file_name,"w+")) == NULL) { printf("sd_rpl error: couldn't open file %s. Exiting.\n", file_name); exit(1); } /* open white pages file .wpg */ strcpy(file_name,country); strcat(file_name,".wpg"); if ((wpg_fd = fopen(file_name,"w")) == NULL) { printf("sd_rpl error: couldn't open file %s. Exiting.\n", file_name); exit(1); } /* open toc region file .rgn */ // strcpy(file_name,country); //strcat(file_name,".rgn"); // if ((toc_fd = fopen(file_name,"w")) == NULL) { // printf("sd_rpl error: couldn't open file %s. Exiting.\n", // file_name); // exit(1); //} /* read_fdf reads the field definition file, which names the input data */ read_fdf(); /* read_rpl reads rpldef, which defines output field names */ read_rpldef(); //read_sdl reads the stack definition file, //which guides the reading of stack data //and the writing of .rpl files read_sdl(); write_rpl(); exit(0); } unsigned char field_names[MAX_FIELDS][MAX_FIELD_NAME_LENGTH]; int field_name_index = 0; int fdf_length = 0; read_fdf() { int len,i; unsigned char fdf_name[20]; unsigned char buf[2*MAX_FIELD_NAME_LENGTH]; FILE *fdf_fd; strcpy(fdf_name,country); strcat(fdf_name,".fdf"); if ((fdf_fd = fopen(fdf_name,"r")) == NULL) { printf("No fdf file for %s. Exiting.\n",country); exit (1); } strcpy(field_names[field_name_index++],"Heading"); strcpy(field_names[field_name_index++],"Subheading"); for (i=0;fgets(buf,2*MAX_FIELD_NAME_LENGTH,fdf_fd);i++) { if (len=strlen(buf)) { if (len > MAX_FIELD_NAME_LENGTH) { printf("Name %s too long in fdf. Exiting.\n", buf); } buf[len-1] = '\0'; strcpy(field_names[field_name_index++], buf); } } fdf_length = i; fclose(fdf_fd); } struct Card { unsigned char name[50]; int used; unsigned char *fields[50]; struct Entry **entries; }; struct Entry { struct Card *card; int used; unsigned char *fields[50]; }; struct rpl { unsigned char fields[50][100]; struct Card **cards; int card_index; }; extern struct rpl *rpl_list[200]; extern int rpl_index; int get_card_index(card_name) unsigned char *card_name; { int c_index; for (c_index = 0;strlen(card_types[c_index].name);c_index++) if (!strcmp(card_types[c_index].name,card_name)) break; if (!strlen(card_types[c_index].name)) { printf("SDL: Couldn't find card type %s. Exiting.\n", card_name); exit(1); } return c_index; } write_rpl() { struct Card *temp_card; struct Entry *temp_entry; int stack_field; unsigned char stack_file[100]; unsigned char process_id[100]; FILE *stack_fd; int type; int i,j; int a,b,c; int errors = 0; stack_field = global_field_index("StackNum"); for (i=0;ifields[stack_field]); strcat(stack_file,".rpl"); if ((stack_fd = fopen(stack_file,"w"))==NULL){ errors++; printf("Warning: Couldn't open output file %s. Trying alternate file name ",stack_file); sprintf(stack_file,"%s.%d",stack_file,getpid()); /* create temp file name based on current process id */ printf("%s.\n",stack_file); if ((stack_fd = fopen(stack_file,"w"))==NULL) { printf("Unable to write to temporary file %s. Exiting ungracefully.\n",stack_file); exit(-1); } printf("Alternate file name %s opened sucessfully.\n",stack_file); } /* write globals */ for (j=0;strlen(global_defs[j]);j++) { fputs(rpl_list[i]->fields[j],stack_fd); fputs("|",stack_fd); } fputs("\n",stack_fd); /* write cards */ a=1; temp_card = rpl_list[i]->cards[a]; while (temp_card && temp_card->used) { # ifdef SD_DEBUG3 printf("write_rpl: card %d\n",a); # endif /* get card type */ type = get_card_index( temp_card->name); fputs("~",stack_fd); fputs(card_types[type].name,stack_fd); fputs("|",stack_fd); for (j=0;strlen(card_types[type].field_names[j]);j++) { if (temp_card->fields[j] != (unsigned char *) 0) fputs(temp_card->fields[j],stack_fd); fputs("|",stack_fd); } fputs("\n",stack_fd); b=0; temp_entry = temp_card->entries[b]; while (temp_entry && temp_entry->used) { /* get card type */ for (j=0;strlen(card_types[type].list_field_names[j]);j++) { if (temp_entry->fields[j] != (unsigned char *) 0) fputs(temp_entry->fields[j],stack_fd); fputs("|",stack_fd); } fputs("\n",stack_fd); b++; temp_entry = temp_card->entries[b]; } a++; temp_card = rpl_list[i]->cards[a]; } fclose(stack_fd); } if (errors) { printf("sd_rpl: There were errors writing to the rpl files. Grep the slog for the string \"Warning\".\n"); } } extern int data_records; read_data() { int i, j, k, len, string_index, head_lines, buf_index,flag,dp_index; unsigned char heading[100]; unsigned char buf[1010]; unsigned char buf2[1010]; unsigned char subheading[100]; unsigned char textline_string[10]; int text_lines; int data_lines; int end_of_file_reached; end_of_file_reached = 0; data_lines = 0; data_records = 0; fgets(buf,1000,data_fd); data_lines++; for (head_lines = 0;;head_lines++) { if (!strlen(buf)) break; buf[strlen(buf)-1] = '\0'; /* parse heading line */ for (buf_index=0,i=0;buf[buf_index] != '|';i++,buf_index++) { if (buf[buf_index] == '\n') { printf("\nImproper heading/subheading line in data file.\n"); printf("Data file: %s, line: %d, char: %d\n",data_file,data_lines,buf_index); printf("Exiting.\n"); exit(1); } heading[i] = buf[buf_index]; } heading[i] = '\0'; buf_index++; for (i=0;buf[buf_index] != '|';i++,buf_index++) { if (buf[buf_index] == '\n') { printf("\nImproper heading/subheading line in data file.\n"); printf("Data file: %s, line: %d, char: %d\n",data_file,data_lines,buf_index); printf("Exiting.\n"); exit(1); } subheading[i] = buf[buf_index]; } subheading[i] = '\0'; /* read location cards */ if (!fgets(buf,1000,data_fd)) break; data_lines++; while (buf[0] == '~') { buf[strlen(buf)-1] = '\0'; data_ptr[data_records][0] = '\0'; strcat(data_ptr[data_records],heading); strcat(data_ptr[data_records],"|"); strcat(data_ptr[data_records],subheading); strcat(data_ptr[data_records],"|"); strcat(data_ptr[data_records],&buf[1]); for (buf_index=0;buf[buf_index] != '|';buf_index++); buf_index++; for (i=0;buf[buf_index] != '|';buf_index++,i++) { textline_string[i] = buf[buf_index]; } textline_string[i] = '\0'; text_lines = atoi(textline_string); for (i=0;i