void load_materials(void) { bool fMatch; closeReserve(); FILE *fp = fopen(MATERIAL_FILE, "r"); if(!fp) return; // no materials to load. while(true) { const char *word = feof( fp ) ? "End" : fread_word( fp ); fMatch = FALSE; if(!str_cmp(word, "End")) { break; } if(!str_cmp(word, "MAT")) { MAT_TYPE *mat = new_mat(); while(true) { word = feof( fp ) ? "End" : fread_word(fp); if(!str_cmp(word, "End")) { bug(Format("Prematurely reached end of materials data file; data corrupted!"), 0); fclose(fp); openReserve(); return; } bool finished = false; switch ( UPPER(word[0]) ) { case '*': fMatch = TRUE; fread_to_eol( fp ); break; case 'E': if(!str_cmp(word, "ENDMAT")) { mat->next = mat_list; mat_list = mat; matValue++; finished = true; break; } case 'V': KEY("VAL", mat->assignedValue, fread_number(fp) ); break; case 'N': KEYS( "NAME", mat->name, fread_string(fp) ); break; } if(finished) break; } } } fclose(fp); openReserve(); }
void append_note(NOTE_DATA *pnote) { FILE *fp; char *name; NOTE_DATA **list; NOTE_DATA *last; switch(pnote->type) { default: return; case NOTE_NOTE: name = NOTE_FILE; list = ¬e_list; break; case NOTE_IDEA: name = IDEA_FILE; list = &idea_list; break; case NOTE_PENALTY: name = PENALTY_FILE; list = &penalty_list; break; case NOTE_NEWS: name = NEWS_FILE; list = &news_list; break; case NOTE_CHANGES: name = CHANGES_FILE; list = &changes_list; break; } if (*list == NULL) *list = pnote; else { for ( last = *list; last->next != NULL; last = last->next); last->next = pnote; } closeReserve(); if ( ( fp = fopen(name, "a" ) ) == NULL ) { perror(name); } else { fprintf( fp, "Sender %s~\n", pnote->sender); fprintf( fp, "Date %s~\n", pnote->date); fprintf( fp, "Stamp %ld\n", pnote->date_stamp); fprintf( fp, "To %s~\n", pnote->to_list); fprintf( fp, "Subject %s~\n", pnote->subject); fprintf( fp, "Text\n%s~\n", pnote->text); fclose( fp ); } openReserve(); }
void save_notes(int type) { FILE *fp; char *name; NOTE_DATA *pnote; switch (type) { default: return; case NOTE_NOTE: name = NOTE_FILE; pnote = note_list; break; case NOTE_IDEA: name = IDEA_FILE; pnote = idea_list; break; case NOTE_PENALTY: name = PENALTY_FILE; pnote = penalty_list; break; case NOTE_NEWS: name = NEWS_FILE; pnote = news_list; break; case NOTE_CHANGES: name = CHANGES_FILE; pnote = changes_list; break; } closeReserve(); if ( ( fp = fopen( name, "w" ) ) == NULL ) { perror( name ); } else { for ( ; pnote != NULL; pnote = pnote->next ) { fprintf( fp, "Sender %s~\n", pnote->sender); fprintf( fp, "Date %s~\n", pnote->date); fprintf( fp, "Stamp %ld\n", pnote->date_stamp); fprintf( fp, "To %s~\n", pnote->to_list); fprintf( fp, "Subject %s~\n", pnote->subject); fprintf( fp, "Text\n%s~\n", pnote->text); } fclose( fp ); openReserve(); return; } }
void save_materials(void) { closeReserve(); FILE *fp = fopen(MATERIAL_FILE, "w"); Assert(fp, "unable to open the materials file"); // handle our materials MAT_TYPE *mat, *mat_next; for(mat = mat_list; mat; mat = mat_next) { mat_next = mat->next; fprintf(fp, "MAT\n"); fprintf(fp, "NAME %s~\n", mat->name); fprintf(fp, "VAL %d\n", mat->assignedValue); fprintf(fp, "ENDMAT\n"); } fprintf(fp, "End\n"); fclose(fp); openReserve(); }
void save_wizlist(void) { WIZ_DATA *pwiz; FILE *fp; bool found = FALSE; closeReserve(); if ( ( fp = fopen( WIZ_FILE, "w" ) ) == NULL ) { perror( WIZ_FILE ); } for (pwiz = wiz_list; pwiz != NULL; pwiz = pwiz->next) { found = TRUE; fprintf(fp,"%s %d\n",pwiz->name,pwiz->level); } fclose(fp); openReserve(); if (!found) unlink(WIZ_FILE); }