ko_int32_t KOStateRoomDecoder::decode(const string p_strStatFile, const ko_stat_s& p_objStatHeader) { if (p_strStatFile.empty()) { m_int32State = -1; return KO_ERROR; } //file not exists if (!FileUtils::FileExists(p_strStatFile)) { m_int32State = -1; return KO_ERROR; } //decode the data //1. open the file and read data FILE* fp = fopen(p_strStatFile.c_str(), "rb"); if (fp == NULL) { m_int32State = -1; return KO_ERROR; } ko_int32_t size = p_objStatHeader.size;//note: the real size of the structure ko_uint8_t *buffer = (ko_uint8_t *)malloc(size); ko_int32_t bytes = 0; ko_int32_t offset = 0; //read while ((bytes = fread(buffer + offset, sizeof(ko_uint8_t), size - offset, fp)) > 0 && !feof(fp)) { printf("read %d bytes from data file!\n", bytes); offset += bytes; if (offset >= size) break; } if (offset != size) return KO_ERROR;//cannot decode, quit for later trying //2. decode all the fields if (KO_OK != decodeContent(buffer, size, p_objStatHeader)) { free(buffer); fclose(fp); m_int32State = -1; return KO_ERROR; } free(buffer); fclose(fp); m_int32State = 1; return KO_OK; }
//Decode ko_stat_room_s data file ko_int32_t KOStateRoomDecoder::decode(const string p_strStatFile) { if (p_strStatFile.empty()) { m_int32State = -1; return KO_ERROR; } //file not exists if (!FileUtils::FileExists(p_strStatFile)) { m_int32State = -1; return KO_ERROR; } //decode the data //1. open the file and read data FILE* fp = fopen(p_strStatFile.c_str(), "rb"); if (fp == NULL) { printf("error in open file %s", p_strStatFile.c_str()); return KO_ERROR; } ko_int32_t size = sizeof(m_objRoomStat)*sizeof(ko_uint8_t); ko_uint8_t *buffer = (ko_uint8_t *)malloc(size); ko_int32_t bytes = 0; ko_int32_t offset = 0; //read while((bytes=fread(buffer+offset, sizeof(ko_uint8_t), size-offset, fp)) > 0 && !feof(fp)) { printf("read %d bytes from data file!\n", bytes); offset += bytes; if(offset >= size) break; } //2. decode all the fields if (KO_OK != decodeContent(buffer, size)) { free(buffer); fclose(fp); m_int32State = -1; return KO_ERROR; } free(buffer); fclose(fp); m_int32State = 1; return KO_OK; }
int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s <pathname>\n", argv[0]); exit(1); } char *contentPath = argv[1]; u8* contentBuffer; size_t contentSize; createContentBuffer(contentPath, &contentBuffer, &contentSize); for(int i=0; i<10; ++i) { loadContent(contentPath, contentBuffer, contentSize); decodeContent(contentBuffer, contentSize); } }
ko_int32_t KOStateRoomDecoder::decode(const ko_uint8_t* content, const ko_int32_t size, const ko_stat_s& p_objStatHeader) { return decodeContent(content, size, p_objStatHeader); }
void MessageTransfer::decodeContent(framing::Buffer& buffer) { decodeContent(buffer, buffer.available()); }