📄 sof_05.cc
字号:
} cur_pos_d += BUFFER_SIZE * sizeof(byte); // clear out the buffer and initialize pointer // MemoryManager::memset(entry_buffer, 0, BUFFER_SIZE); ptr = 0; } // build the binary entry // entry_buffer[ptr + SYMBOL_OFFSET_NEXT] = (byte)size; MemoryManager::memcpy(&entry_buffer[ptr + SYMBOL_OFFSET_IND], &ind, sizeof(int32)); MemoryManager::memcpy(&entry_buffer[ptr + SYMBOL_OFFSET_REFS], &refs, sizeof(int32)); MemoryManager::memcpy(&entry_buffer[ptr + SYMBOL_OFFSET_NAME], name_buf, slen); entry_buffer[ptr + SYMBOL_OFFSET_NAME + slen] = (byte)NULL; // compute the checksum // cksm_d.compute(&entry_buffer[ptr + SYMBOL_OFFSET_IND], 2 * sizeof(int32) + slen); // set the last size pointer for the next time around // last_size = &entry_buffer[ptr + INDEX_OFFSET_NEXT]; // increment pointer // ptr += size; } } // if there are entries left over, write them out // if (ptr > 0) { if (fp_d.write(entry_buffer, sizeof(byte), ptr) != ptr) { return Error::handle(name(), L"writeTable", Error::WRITE, __FILE__, __LINE__); } cur_pos_d += ptr * sizeof(byte); } // exit gracefully // return true;}// method: isSof//// arguments: none//// return: a boolean value indicating status//// check if the file is an Sof file. if it is, all values from the header are// automatically read.//boolean Sof::isSof() { // initialize strings // cname_d.clear(); version_d.clear(); magic_d.clear(); // make sure the file is open // if (!fp_d.isReadable()) { return false; } // read the first line from the file // SysString buf; fp_d.rewind(); fp_d.get(buf); // fetch the header // SysChar delim_beg, delim_end; // try to get the first delimiter char // long index0 = buf.firstNotSpace(); long index1 = -1; if (index0 < 0) { return false; } delim_beg.assign(buf(index0)); // try to get the class name (Sof) // index0 = buf.firstNotSpace(index0 + 1); index1 = buf.firstSpace(index0); if ((index0 < 0) || (index1 < 0)) { return false; } buf.substr(cname_d, index0, index1 - index0); // try to get the version // index0 = buf.firstNotSpace(index1 + 1); index1 = buf.firstSpace(index0); if ((index0 < 0) || (index1 < 0)) { return false; } buf.substr(version_d, index0, index1 - index0); // try to get either the delimiter or the magic string // index0 = buf.firstNotSpace(index1 + 1); if (index0 < 0) { return false; } // if this index is the last character on the line, then we are in // a text file and no magic string was specified. // if (buf.firstNotSpace(index0 + 1) < 0) { file_type_d = File::TEXT; delim_end = buf(index0); } // else this better be a binary sof file // else { file_type_d = File::BINARY; index1 = buf.firstSpace(index0); if ((index1 < 0) || ((index1 - index0) != BSTR_LEN)) { return false; } buf.substr(magic_d, index0, BSTR_LEN); index0 = buf.firstNotSpace(index1 + 1); if (index0 < 0) { return false; } // the delimiter should be the last character on the line // if (buf.firstNotSpace(index0 + 1) < 0) { delim_end = buf(index0); } else { return false; } // check byte mode // int32 x = 0; float32 y = 0; byte magic_buf[sizeof(x) + 1]; magic_d.getBuffer((byte*)magic_buf, sizeof(x) + 1); MemoryManager::memcpy(&x, magic_buf, sizeof(x)); MemoryManager::memcpy(&y, magic_buf, sizeof(y)); if (x == BCODE_NATIVE) { fp_d.setBMode(File::NATIVE); } else if (x == BCODE_SWAP) { fp_d.setBMode(File::SWAP); } else { return false; } // check floating point encoding // if ((y != IEEE_FLOAT_NATIVE) && (y != IEEE_FLOAT_SWAP)) { return false; } // read in the end of data pointer // int32 val; if (fp_d.read(&val, sizeof(int32), 1) != 1) { return false; } end_of_data_d = val; // sanity check on end_of_data_d // if (end_of_data_d < 1) { return false; } // read in the checksum // uint16 checksum; if (fp_d.read(&checksum, sizeof(uint16), 1) != 1) { return false; } file_checksum_d = checksum; if (debug_level_d >= Integral::DETAILED) { SysString output; output.assign(file_checksum_d); output.insert(L"read file_checksum_d = ", 0); Console::put(output); } } // save the position // end_of_header_d = fp_d.tell(); cur_pos_d = end_of_header_d; // provide some debug information // if (debug_level_d > Integral::DETAILED) { debug(L"isSof"); } // check the class name and version // if (!cname_d.eq(CLASS_NAME)) { return false; } if (!version_d.eq(VERSION)) { return false; } // make sure the delimiters are equal // if (delim_beg != delim_end) { return false; } else { delimiter_d = delim_beg; } // exit gracefully // return true;}// method: isSof//// arguments:// const SysString& fname: (input) filename//// return: a boolean value indicating status//// check if this file is an Sof file//boolean Sof::isSof(const SysString& fname_a) { // we need an Sof object // Sof sof; // open the file // if (!sof.fp_d.open(fname_a)) { return false; } // check if it is sof // if (!sof.isSof()) { // close the file pointers and return false // sof.fp_d.close(); return false; } // close the file pointer // sof.fp_d.close(); // exit gracefully // return true;}// method: update//// arguments: none//// return: a boolean value indicating status//// this method updates the header and index//boolean Sof::update() { // local variables // SysString buf; buf.assign(L" "); buf(0) = delimiter_d; buf.concat(cname_d); buf.concat(L" "); buf.concat(version_d); // start from the beginning of the file // fp_d.rewind(); // text files are simpler // if (file_type_d == File::TEXT) { // concatenate the last delimiter and write string // buf.concat(L" \n"); buf(buf.length() - 2) = delimiter_d; fp_d.put(buf); fp_d.flush(); end_of_header_d = ftell(); return true; } // binary file, build magic string from bmode // int32 x[2]; File::BMODE byte_mode = fp_d.getBMode(); File::BMODE mach_byte_mode = fp_d.getMachineByteOrder(); if (byte_mode == mach_byte_mode) { x[0] = BCODE_NATIVE; } else { x[0] = BCODE_SWAP; } x[1] = (long)0; magic_d.assign((byte*)&x); buf.concat(L" "); buf.concat(magic_d); // concatenate the last delimiter and write string // buf.concat(L" \n"); buf(buf.length() - 2) = delimiter_d; fp_d.put(buf); int32 val = (int32)end_of_data_d; if (fp_d.write(&val, sizeof(int32), 1) != 1) { return Error::handle(name(), L"update", Error::WRITE, __FILE__, __LINE__); } // write out a null checksum (we'll update it after we write the // index and table) // uint16 valc = 0; if (fp_d.write(&valc, sizeof(uint16), 1) != 1) { return Error::handle(name(), L"update", Error::WRITE, __FILE__, __LINE__); } // set end of header, possibly set end of data // end_of_header_d = fp_d.tell(); cur_pos_d = end_of_header_d; if (end_of_data_d < end_of_header_d) { end_of_data_d = end_of_header_d; } // if end of data exists, seek to it // else { // seek to the end of the data // if (!fseek(end_of_data_d, File::POS)) { return Error::handle(name(), L"update", Error::SEEK, __FILE__, __LINE__); } } // clear the checksum object so it can accumulate a new checksum // cksm_d.clear(Integral::RETAIN); // write out the symbol table // if (debug_level_d > Integral::BRIEF) { SysString output; output.assign((long)end_of_data_d); output.insert(L"<Sof::update> writing table to position ", 0); Console::put(output); } if (!writeTable()) { return Error::handle(name(), L"update", SofSymbolTable::ERR, __FILE__, __LINE__); } // update the index at the end of the file // if (debug_level_d > Integral::BRIEF) { SysString output; output.assign((long)ftell()); output.insert(L"<Sof::update> writing index to position ", 0); Console::put(output); } if (!writeIndex()) { return Error::handle(name(), L"update", SofList::ERR, __FILE__, __LINE__); } // truncate the file if necessary // long end_of_file = ftell(); if (!fseek(0, File::POS_PLUS_END)) { return Error::handle(name(), L"update", Error::SEEK, __FILE__, __LINE__); } if (ftell() > (end_of_file + 1)) { // give debug message // if (debug_level_d > Integral::BRIEF) { SysString output; SysString numeric; output.assign(L"<Sof::update> truncating file ["); numeric.assign(ftell()); output.concat(numeric); output.concat(L" - "); numeric.assign(end_of_file); output.concat(numeric); output.concat(L"]"); Console::put(output); } // truncate file // if (!fp_d.truncate(end_of_file + 1)) { return Error::handle(name(), L"update", Error::IO, __FILE__, __LINE__); } } // write out the checksum // if (!fseek(end_of_header_d - sizeof(uint16), File::POS)) { return Error::handle(name(), L"update", Error::SEEK, __FILE__, __LINE__); } valc = cksm_d.get(); if (fp_d.write(&valc, sizeof(uint16), 1) != 1) { return Error::handle(name(), L"update", Error::WRITE, __FILE__, __LINE__); } if (debug_level_d >= Integral::DETAILED) { SysString output; output.assign(valc); output.insert(L"writing checksum = ", 0); Console::put(output); } // seek back to the beginning of the current object, if set // if (cur_data_d >= end_of_header_d) { rewind(); } fp_d.flush(); // exit gracefully // return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -