// // M_ReadFile // // Reads a file, it will allocate storage via Z_Malloc for it and return // the buffer and the size. QWORD M_ReadFile(std::string filename, BYTE **buffer) { FILE *handle; QWORD count, length; BYTE *buf; handle = fopen(filename.c_str(), "rb"); if (handle == NULL) { Printf(PRINT_HIGH, "Could not open file %s for reading\n", filename.c_str()); return false; } length = M_FileLength(handle); buf = (BYTE *)Z_Malloc (length, PU_STATIC, NULL); count = fread(buf, 1, length, handle); fclose (handle); if (count != length) { Printf(PRINT_HIGH, "Failed while reading from file %s\n", filename.c_str()); return false; } *buffer = buf; return length; }
// // WadDirectory::addSingleFile // // haleyjd 10/27/12: Load a single file into the wad directory. // bool WadDirectory::addSingleFile(openwad_t &openData, wfileadd_t &addInfo, int startlump) { filelump_t singleinfo; lumpinfo_t *lump_p; singleinfo.filepos = 0; singleinfo.size = static_cast<int>(M_FileLength(openData.handle)); M_ExtractFileBase(openData.filename, singleinfo.name); lump_p = reAllocLumpInfo(1, startlump); lump_p->type = lumpinfo_t::lump_direct; // haleyjd: lump type lump_p->size = static_cast<size_t>(singleinfo.size); lump_p->source = source; // haleyjd: source id // setup for direct file IO lump_p->direct.file = openData.handle; lump_p->direct.position = static_cast<size_t>(singleinfo.filepos); lump_p->li_namespace = addInfo.li_namespace; // killough 4/17/98 strncpy(lump_p->name, singleinfo.name, 8); incrementSource(openData); return true; }
int M_ReadFile(char *name, byte **buffer) { FILE *handle; int count, length; byte *buf; handle = fopen(name, "rb"); if (handle == NULL) I_Error ("Couldn't read file %s", name); // find the size of the file by seeking to the end and // reading the current position length = M_FileLength(handle); buf = Z_Malloc (length, PU_STATIC, NULL); count = fread(buf, 1, length, handle); fclose (handle); if (count < length) I_Error ("Couldn't read file %s", name); *buffer = buf; return length; }
// // W_ReadChunk // // denis - for wad downloading // unsigned W_ReadChunk (const char *file, unsigned offs, unsigned len, void *dest, unsigned &filelen) { FILE *fp = fopen(file, "rb"); unsigned read = 0; if(fp) { filelen = M_FileLength(fp); fseek(fp, offs, SEEK_SET); read = fread(dest, 1, len, fp); fclose(fp); } else filelen = 0; return read; }
static wad_file_t *W_StdC_OpenFile(char *path) { stdc_wad_file_t *result; FILE *fstream; fstream = fopen(path, "rb"); if (fstream == NULL) return NULL; // Create a new stdc_wad_file_t to hold the file handle. result = Z_Malloc(sizeof(stdc_wad_file_t), PU_STATIC, 0); result->wad.file_class = &stdc_wad_file; result->wad.mapped = NULL; result->wad.length = M_FileLength(fstream); result->fstream = fstream; return &result->wad; }
std::string W_AddFile (std::string filename) { wadinfo_t header; lumpinfo_t* lump_p; size_t i; FILE *handle; size_t length; size_t startlump; size_t res; filelump_t* fileinfo; filelump_t singleinfo; FixPathSeparator (filename); std::string name = filename; M_AppendExtension (name, ".wad"); // open the file if ( (handle = fopen (filename.c_str(), "rb")) == NULL) { Printf (PRINT_HIGH, " couldn't open %s\n", filename.c_str()); return ""; } Printf (PRINT_HIGH, "adding %s\n", filename.c_str()); startlump = numlumps; res = fread (&header, sizeof(header), 1, handle); header.identification = LELONG(header.identification); if (header.identification != IWAD_ID && header.identification != PWAD_ID) { // raw lump file fileinfo = &singleinfo; singleinfo.filepos = 0; singleinfo.size = M_FileLength(handle); M_ExtractFileBase (filename, name); numlumps++; Printf (PRINT_HIGH, " (single lump)\n", header.numlumps); } else { // WAD file header.numlumps = LELONG(header.numlumps); header.infotableofs = LELONG(header.infotableofs); length = header.numlumps*sizeof(filelump_t); if(length > (unsigned)M_FileLength(handle)) { Printf (PRINT_HIGH, " bad number of lumps for %s\n", filename.c_str()); fclose(handle); return ""; } fileinfo = (filelump_t *)Z_Malloc (length, PU_STATIC, 0); fseek (handle, header.infotableofs, SEEK_SET); res = fread (fileinfo, length, 1, handle); numlumps += header.numlumps; Printf (PRINT_HIGH, " (%d lumps)\n", header.numlumps); } // Fill in lumpinfo lumpinfo = (lumpinfo_t *)Realloc (lumpinfo, numlumps*sizeof(lumpinfo_t)); if (!lumpinfo) I_Error ("Couldn't realloc lumpinfo"); lump_p = &lumpinfo[startlump]; for (i=startlump ; i<numlumps ; i++,lump_p++, fileinfo++) { lump_p->handle = handle; lump_p->position = LELONG(fileinfo->filepos); lump_p->size = LELONG(fileinfo->size); strncpy (lump_p->name, fileinfo->name, 8); // W_CheckNumForName needs all lump names in upper case std::transform(lump_p->name, lump_p->name+8, lump_p->name, toupper); } return W_MD5(filename); }
// // W_AddFile // // All files are optional, but at least one file must be found // (PWAD, if all required lumps are present). // Files with a .wad extension are wadlink files with multiple lumps. // Other files are single lumps with the base filename for the lump name. // // Map reloads are supported through WAD reload so no need for vanilla tilde // reload hack here // std::string W_AddFile(std::string filename) { FILE* handle; filelump_t* fileinfo; FixPathSeparator(filename); if ( (handle = fopen(filename.c_str(), "rb")) == NULL) { Printf(PRINT_HIGH, "couldn't open %s\n", filename.c_str()); return ""; } Printf(PRINT_HIGH, "adding %s", filename.c_str()); size_t newlumps; wadinfo_t header; fread(&header, sizeof(header), 1, handle); header.identification = LELONG(header.identification); if (header.identification != IWAD_ID && header.identification != PWAD_ID) { // raw lump file std::string lumpname; M_ExtractFileBase(filename, lumpname); fileinfo = new filelump_t[1]; fileinfo->filepos = 0; fileinfo->size = M_FileLength(handle); std::transform(lumpname.c_str(), lumpname.c_str() + 8, fileinfo->name, toupper); newlumps = 1; Printf(PRINT_HIGH, " (single lump)\n"); } else { // WAD file header.numlumps = LELONG(header.numlumps); header.infotableofs = LELONG(header.infotableofs); size_t length = header.numlumps * sizeof(filelump_t); if (length > (unsigned)M_FileLength(handle)) { Printf(PRINT_HIGH, "\nbad number of lumps for %s\n", filename.c_str()); fclose(handle); return ""; } fileinfo = new filelump_t[header.numlumps]; fseek(handle, header.infotableofs, SEEK_SET); fread(fileinfo, length, 1, handle); // convert from little-endian to target arch and capitalize lump name for (int i = 0; i < header.numlumps; i++) { fileinfo[i].filepos = LELONG(fileinfo[i].filepos); fileinfo[i].size = LELONG(fileinfo[i].size); std::transform(fileinfo[i].name, fileinfo[i].name + 8, fileinfo[i].name, toupper); } newlumps = header.numlumps; Printf(PRINT_HIGH, " (%d lumps)\n", header.numlumps); } W_AddLumps(handle, fileinfo, newlumps, false); delete [] fileinfo; return W_MD5(filename); }
static void LoadResponseFile(int argv_index) { FILE *handle; int size; char *infile; char *file; char *response_filename; char **newargv; int newargc; int i, k; response_filename = myargv[argv_index] + 1; // Read the response file into memory handle = fopen(response_filename, "rb"); if (handle == NULL) { printf ("\nNo such response file!"); exit(1); } printf("Found response file %s!\n", response_filename); size = M_FileLength(handle); // Read in the entire file // Allocate one byte extra - this is in case there is an argument // at the end of the response file, in which case a '\0' will be // needed. file = malloc(size + 1); if (fread(file, 1, size, handle) < size) { I_Error("Failed to read entire response file"); } fclose(handle); // Create new arguments list array newargv = malloc(sizeof(char *) * MAXARGVS); newargc = 0; memset(newargv, 0, sizeof(char *) * MAXARGVS); // Copy all the arguments in the list up to the response file for (i=0; i<argv_index; ++i) { newargv[i] = myargv[i]; ++newargc; } infile = file; k = 0; while(k < size) { // Skip past space characters to the next argument while(k < size && isspace(infile[k])) { ++k; } if (k >= size) { break; } // If the next argument is enclosed in quote marks, treat // the contents as a single argument. This allows long filenames // to be specified. if (infile[k] == '\"') { // Skip the first character(") ++k; newargv[newargc++] = &infile[k]; // Read all characters between quotes while (k < size && infile[k] != '\"' && infile[k] != '\n') { ++k; } if (k >= size || infile[k] == '\n') { I_Error("Quotes unclosed in response file '%s'", response_filename); } // Cut off the string at the closing quote infile[k] = '\0'; ++k; } else { // Read in the next argument until a space is reached newargv[newargc++] = &infile[k]; while(k < size && !isspace(infile[k])) { ++k; } // Cut off the end of the argument at the first space infile[k] = '\0'; ++k; } } // Add arguments following the response file argument for (i=argv_index + 1; i<myargc; ++i) { newargv[newargc] = myargv[i]; ++newargc; } myargv = newargv; myargc = newargc; #if 0 // Disabled - Vanilla Doom does not do this. // Display arguments printf("%d command-line args:\n", myargc); for (k=1; k<myargc; k++) { printf("'%s'\n", myargv[k]); } #endif }