void switching(char *hotel_current, char *hotel_name) { char hotel_filename[NAME_MAXIMUM + 1]; FILE *hotel_file; hotel hotel_new; if(!get_name("Hotel Name", hotel_filename, hotel_name)) return; to_filename(hotel_filename); strcat(hotel_filename, NAME_EXTENSION); hotel_file = fopen(hotel_filename, "rb"); if(!hotel_file){ error_print("File not opened!"); return; } fread(&hotel_new, sizeof(hotel_new), 1, hotel_file); if(feof(hotel_file) || ferror(hotel_file)){ error_print("Data not read!"); goto cleanup; } strcpy(hotel_current, to_name(hotel_new.name)); cleanup: fclose(hotel_file); }
static int physfs_searcher(lua_State *L) // module name -> nil or loader function { to_filename(L, 1); const char * filename = lua_tostring(L, 2); if(PHYSFS_exists(filename)) { // call physfs_loadfile with filename as argument lua_pushcfunction(L, seed_loadfile); lua_insert(L, 2); lua_call(L, 1, 1); } else { lua_pushfstring(L, "\n\tno physfs file '%s'", filename); } return 1; }
int main(int argc, char **argv) { printf("cpk_replace 0.3\n"); if (argc <= 2) fprintf(stderr,"Incorrect program usage\nusage: %s cpk files...\n",argv[0]); CHECK_ERRNO(argc <= 2, "argc"); /* create file list */ int new_file_count = argc-2; struct cpk_file file[new_file_count]; for (int i = 0; i < new_file_count; i++) { file[i].location = argv[i+2]; file[i].filename = to_filename(file[i].location); file[i].orig_offset = 0; file[i].offset_diff = 0; file[i].orig_size = 0; file[i].new_size = 0; file[i].index = 0; file[i].found = 0; file[i].copied = 0; #ifdef DEBUG printf("(Debug)input file location: %s\n(Debug)input file name: %s\n",file[i].location,file[i].filename); #endif } /* open file */ FILE *infile = fopen(argv[1], "rb"); CHECK_ERRNO(!infile, "fopen"); /* get file size */ CHECK_ERRNO(fseek(infile, 0 , SEEK_END) != 0, "fseek"); long file_length = ftell(infile); CHECK_ERRNO(file_length == -1, "ftell"); rewind(infile); analyze_CPK(infile, file_length, file, new_file_count); exit(EXIT_SUCCESS); }
void bill(char *hotel_current, char *guest) { int i, service_id, bill_amount; char hotel_filename[NAME_MAXIMUM + 1], guest_name[NAME_MAXIMUM + 1]; FILE *hotel_file, *service_file; fpos_t write_position; hotel hotel_new; service service_new; room room_new; if(!*hotel_current){ error_print("No hotel selected! Use\n\tswitch <hotel name>"); return; } to_filename(strcpy(hotel_filename, hotel_current)); strcat(hotel_filename, NAME_EXTENSION); hotel_file = fopen(hotel_filename, "rb+"); if(!hotel_file){ error_print("File not opened!"); return; } service_file = fopen(SERVICE_FILENAME, "rb"); if(!service_file){ error_print("Services file unavailable!"); fclose(hotel_file); return; } if(!get_name("Guest Name", guest_name, guest)) goto cleanup; fread(&hotel_new, sizeof(hotel_new), 1, hotel_file); if(feof(hotel_file) || ferror(hotel_file)){ error_print("Data not read!"); goto cleanup; } for(i = 0; i < hotel_new.rooms; ++i){ fgetpos(hotel_file, &write_position); fread(&room_new, sizeof(room_new), 1, hotel_file); if(ferror(hotel_file)){ error_print("Data not read!"); goto cleanup; } if(feof(hotel_file)) break; if(!strcmp(room_new.guest, guest_name)) break; } if(i == hotel_new.rooms || feof(hotel_file)){ error_print("Guest not found!"); goto cleanup; } if(!room_new.number_of_services) goto cleanup; printf( "Bill for %s\n" "+----+------+------------------------------------------------------+\n" "| ID | Cost | Description |\n" "+----+------+------------------------------------------------------+\n" , to_name(room_new.guest) ); bill_amount = 0; for(i = 0; i < room_new.number_of_services; ++i){ for(service_id = 1; ; ++service_id){ fread(&service_new, sizeof(service_new), 1, service_file); if(feof(service_file) || ferror(service_file)){ error_print("Data not read!"); goto cleanup; } if(service_id != room_new.service_list[i]) continue; printf( "| %2d | %4d | %52.52s |\n", service_id, service_new.cost, to_name(service_new.description) ); bill_amount += service_new.cost; break; } rewind(service_file); } room_new.number_of_services = 0; fsetpos(hotel_file, &write_position); fwrite(&room_new, sizeof(room_new), 1, hotel_file); if(ferror(hotel_file)){ error_print("Data not written!"); goto cleanup; } printf( "+----+------+------------------------------------------------------+\n" "| | %4d | Total |\n" "+----+------+------------------------------------------------------+\n" , bill_amount ); cleanup: fclose(hotel_file); fclose(service_file); }