/// /// Writes a word of data into the specified bin. /// /// @param path The path to load. /// @return Whether the bin was loaded successfully. /// bool bins_write(freed_bstring name, uint16_t word) { struct ldbin* bin = list_seek(&ldbins.bins, name.ref); if (bin == NULL) { bautodestroy(name); return false; } bin_write(bin, word); return true; }
void ChoiceTable::dump(const std::string& fname) const { std::ofstream stream(fname.c_str(),std::ios::out | std::ios::binary); if (stream) { bin_write(stream,1.0f); // version bin_write(stream,_i_size); // sizes bin_write(stream,_r_size); for (ChoiceListArray::const_iterator itcr = _data.begin(); itcr != _data.end(); ++itcr){ for (ChoiceListVector::const_iterator itcc = itcr->begin(); itcc != itcr->end(); ++itcc){ // size of a record bin_write(stream,itcc->size()); for (ChoiceList::const_iterator itc = itcc->begin(); itc != itcc->end(); ++itc){ // values of a record bin_write(stream,*itc); } } } } }
/// /// Loads a new bin by reading a linker object from file. /// /// Adds a new bin by reading in a linker object stored on disk and /// automatically handling loading bytes and linker information into /// the bin. /// /// @param path The path to load. /// @return Whether the bin was loaded successfully. /// bool bins_load(freed_bstring path) { uint16_t offset = 0, store; struct lprov_entry* required = NULL; struct lprov_entry* provided = NULL; struct lprov_entry* adjustment = NULL; struct lprov_entry* section = NULL; struct lprov_entry* output = NULL; struct ldbin* bin; FILE* in; char* test; // Open the input file. in = fopen(path.ref->data, "rb"); if (in == NULL) { // Handle the error. return false; } // Is this the object format? test = malloc(strlen(ldata_objfmt) + 1); memset(test, 0, strlen(ldata_objfmt) + 1); fread(test, 1, strlen(ldata_objfmt), in); fseek(in, 1, SEEK_CUR); if (strcmp(test, ldata_objfmt) != 0) { // Handle the error. return false; } free(test); // Load only the provided label entries into memory. objfile_load(path.ref->data, in, &offset, &provided, &required, &adjustment, §ion, &output); // Add the new bin. bin = bins_add(path, provided, required, adjustment, section, output); // Copy all of the input file's data into the output // file, word by word. while (true) { // Read a word into the bin. The reason that // we break inside the loop is that we are reading // two bytes at a time and thus the EOF flag doesn't // get set until we actually attempt to read past // the end-of-file. If we don't do this, we get a // double read of the same data. fread(&store, sizeof(uint16_t), 1, in); if (feof(in)) break; bin_write(bin, store); } // Close the file. fclose(in); // TODO: Free the list data in the struct lprov_entry // pointers (since it's cloned by the bin system). return true; }