bool KMapData::initialize(StreamInterface& si) { this->finalize(); DWORD width, height; if(si.ReadData(&width, sizeof(width)) != sizeof(width)) return false; if(si.ReadData(&height, sizeof(height)) != sizeof(height)) return false; ASSERT(width <= MAX_MAP_WIDTH); ASSERT(height <= MAX_MAP_HEIGHT); ASSERT(width%REGION_WIDTH == 0); ASSERT(height%REGION_HEIGHT == 0); int len = width * height * sizeof(KCellData); KCellData* cells = (KCellData*)malloc_k(len); if(si.ReadData(cells, len) != len) { free_k(cells); return false; } m_width = width; m_height = height; m_cells = cells; m_refCount = new int(1); return true; }
bool KMapData::save(StreamInterface& fo) { DWORD width = m_width; DWORD height = m_height; if(fo.WriteData(&width, sizeof(width)) != sizeof(width)) return false; if(fo.WriteData(&height, sizeof(height)) != sizeof(height)) return false; int len = m_width*m_height*sizeof(KCellData); if(fo.WriteData(m_cells, len) != len) return false; fo.flush(); return true; }
BOOL XMLDomParser::Parse(StreamInterface& s, XMLElementNode& root) { this->Reset(); size_t pos = s.tell(); DWORD flag = 0; int_r n2 = s.ReadData(&flag, 3); if(n2 == 3 && flag == utf8_signature) { const char* encoding = "UTF-8"; if(stricmp(encoding, m_encoding.c_str()) && m_parser) { XML_ParserFree(m_parser); m_parser = NULL; } m_encoding = encoding; } else { s.Seek(pos); } if(!m_parser) { if(m_encoding.empty()) m_encoding = "GB2312"; m_parser = XML_ParserCreate(m_encoding.c_str()); if(!m_parser) return FALSE; XML_SetUserData(m_parser, this); XML_SetElementHandler(m_parser, &StartElementHandler, &EndElementHandler); XML_SetCharacterDataHandler(m_parser, &CharacterDataHandler); } if(root.m_tagName.empty()) root.SetTagName("XMLRoot"); m_pRootNode = &root; m_stack.push(m_pRootNode); //const char* orglocale = NULL; //if(m_encoding.icompare("GB2312") == 0) // orglocale = setLocaleToGbk(); //else if(g_encoding == encode_ansii && m_encoding.icompare("UTF-8") == 0) // orglocale = setLocaleToGbk(); int_r n; char cBuffer[2048]; while(n = s.ReadData(&cBuffer, 2048), n>=0) { BOOL isFinal = (n < 2048); XML_Status status = XML_Parse(m_parser, cBuffer, (int)n, isFinal); if(status == XML_STATUS_ERROR) { //if(orglocale) setlocale(LC_CTYPE, orglocale); sprintf_k(cBuffer, sizeof(cBuffer), "%s at line:%d col:%d", XML_ErrorString(XML_GetErrorCode(m_parser)), XML_GetCurrentLineNumber(m_parser), XML_GetCurrentColumnNumber(m_parser)); m_errMsg = cBuffer; return FALSE; } if(isFinal) break; } //if(orglocale) setlocale(LC_CTYPE, orglocale); return TRUE; }
BOOL XMLElementNode::WriteTo(StreamInterface& s, int level) const { for(int i=0; i<level; i++) s.WriteData("\t", 1); s.WriteData("<", 1); s.WriteData(m_tagName.c_str(), m_tagName.size()); if(m_attrs.Size()) s.WriteData(" ", 1); for(int i=0; i<m_attrs.Size(); i++) { const XMLAttribute* pattr = m_attrs.GetAttribute(i); if(i) s.WriteData(" ", 1); s.WriteData(pattr->m_name.c_str(), pattr->m_name.size()); s.WriteData("=", 1); s.WriteData("\"", 1); s.WriteData(pattr->m_value.c_str(), pattr->m_value.Size()); s.WriteData("\"", 1); } if(!m_children.Size()) { s.WriteData("/>", 2); return TRUE; } s.WriteData(">", 1); if(m_children.Size() == 1 && m_children.GetNode(0)->IsTextNode()) { m_children.GetNode(0)->WriteTo(s); // const StringValue* pVal = this->GetText(); // s.WriteData(pVal->c_str(), pVal->Size()); s.WriteData("</", 2); s.WriteData(m_tagName.c_str(), m_tagName.size()); s.WriteData(">", 1); return TRUE; } s.WriteData("\n", 1); for(int i=0; i<m_children.Size(); i++) { const XMLDomNode* pNode = m_children.GetNode(i); pNode->WriteTo(s, level+1); s.WriteData("\n", 1); } for(int i=0; i<level; i++) s.WriteData("\t", 1); s.WriteData("</", 2); s.WriteData(m_tagName.c_str(), m_tagName.size()); s.WriteData(">", 1); return TRUE; }
extern "C" int readstream_k(void* s, size_t* from, char* buf, size_t len) { StreamInterface* si = (StreamInterface*)s; if(*from == (size_t)-1) { size_t pos = si->tell(); char szBinary[4]; si->ReadData(szBinary, sizeof(szBinary)); if(!strncmp(szBinary,LUA_SIGNATURE,sizeof(szBinary))) *from = encode_count + 1; si->Seek(pos); if(*from != encode_count + 1) { DWORD flag = 0; pos = si->tell(); int_r n = si->ReadData(&flag, 3); if(n == 3 && flag == utf8_signature) { *from = (size_t)encode_utf8; } else { *from = (size_t)encode_ansii; si->Seek(pos); } } } if(*from == (size_t)g_encoding || *from == encode_count + 1) { int n = (int)si->ReadData(buf, len); if(n < 1) return 0; return n; } else { size_t pos = si->tell(); char buf2[LUAL_BUFFERSIZE]; int n = (int)si->ReadData(buf2, sizeof(buf2)); if(n < 1) return 0; const char* src = buf2; int srclen = n; char* dst = buf; int dstlen = (int)len; KCharset* cs = KCharset::getLocalCharset(); if(g_encoding == encode_ansii) n = cs->from_utf8(&src, &srclen, &dst, &dstlen); else if(g_encoding == encode_utf8) n = cs->to_utf8(&src, &srclen, &dst, &dstlen); else return 0; int rbytes = (int)(src - &buf2[0]); int wbytes = (int)(dst - buf); if(wbytes < 1) return 0; if(srclen > 0) si->Seek(pos + rbytes); return wbytes; } }