MCPcopy Create free account
hub / github.com/DescentDevelopers/Descent3 / PPic_BuildDatabases

Function PPic_BuildDatabases

Descent3/PilotPicsAPI.cpp:490–598  ·  view source on GitHub ↗

----------------------------------------------- PPic_BuildDatabase Purpose: Builds up some databases for fast access based off the index file. -----------------------------------------------

Source from the content-addressed store, hash-verified

488// based off the index file.
489// -----------------------------------------------
490bool PPic_BuildDatabases(void) {
491 CFILE *file = PilotPic_database_index_handle;
492 if (!file)
493 return false;
494
495 // first rewind to the beginning and find out how many pilots we have
496 // -------------------------------------------------------------------
497 cf_Rewind(file);
498
499 PilotPic_count = cf_ReadInt(file);
500 if (PilotPic_count < 0) {
501 // hmm a negative!
502 mprintf(0, "PPIC: Invalid number of pilot pictures (%d)\n", PilotPic_count);
503 Int3();
504 PilotPic_count = 0;
505 cf_Rewind(file);
506 return false;
507 }
508 if (PilotPic_count > 65535) {
509 // too many!!!!
510 mprintf(0, "PPIC: Invalid number of pilot pictures (%d)\n", PilotPic_count);
511 Int3();
512 PilotPic_count = 0;
513 cf_Rewind(file);
514 return false;
515 }
516
517 // allocate all the memory we're going to need
518 // -------------------------------------------
519 Pilot_id_to_offset = (tPilotPicIdOffset *)mem_malloc(sizeof(tPilotPicIdOffset) * PilotPic_count);
520 Sorted_Pilot_id_to_offset = (uint16_t *)mem_malloc(sizeof(uint16_t) * PilotPic_count);
521 if (!Pilot_id_to_offset) {
522 // out of memory!!!
523 mprintf(0, "PPIC: Out of memory allocating index database\n");
524 Int3();
525 PilotPic_count = 0;
526 cf_Rewind(file);
527 return false;
528 }
529
530 // now it is time to go through the index file and build the databases
531 // -------------------------------------------------------------------
532 int count;
533 char name_buffer[256];
534
535 for (count = 0; count < 27; count++)
536 Alphabet_to_offset[count] = -1;
537
538 for (count = 0; count < PilotPic_count; count++) {
539
540 // get the current file position, we'll need it
541 int file_pos;
542 file_pos = cftell(file);
543
544 // first read in the pilot name
545 uint8_t name_size;
546 name_size = cf_ReadByte(file);
547 cf_ReadBytes((uint8_t *)name_buffer, name_size, file);

Callers 1

PPic_InitDatabaseFunction · 0.85

Calls 7

cf_RewindFunction · 0.85
cf_ReadIntFunction · 0.85
cftellFunction · 0.85
cf_ReadByteFunction · 0.85
cf_ReadBytesFunction · 0.85
cf_ReadShortFunction · 0.85
cfseekFunction · 0.85

Tested by

no test coverage detected