void AXmlElement::emitPath(AOutputBuffer& target) const { if (mp_Parent) mp_Parent->emitPath(target); target.append(AConstant::ASTRING_SLASH); target.append(m_Name); }
/*! AEmittable */ void emit(AOutputBuffer& target) const { target.append('{'); target.append(AString::fromPointer(m_Pointer)); if (m_Ownership) target.append(";owned",6); target.append('}'); }
void ATemplateNodeHandler_DADA::Node::_generateLine(ADadaDataHolder *pddh, MAP_AString_AString& globals, const AString& format, ATemplateContext& context, AOutputBuffer& output) { if (format.isEmpty()) return; AFile_AString file(format); size_t readresult; char c = '\x0'; AString target(1024, 1024), strType(64,128); while (AConstant::npos != file.readUntil(target, S_DELIM_START, true, true)) { file.read(c); readresult = file.readUntil(strType, S_DELIM_END, true, true); if (0 == readresult) { file.readUntilEOF(target); output.append(target); target.clear(); break; } //a_If not EOF process type if (AConstant::npos != readresult) { if (strType.getSize() > 1) { switch (c) { case '%': //a_Process the tag {%type:tag,tag,...} _appendWordType(pddh, globals, strType, target); break; case '$': //a_ {$variable} _appendVariable(pddh, globals, strType, target); break; default: //a_Passthru of unknown tags target.append('{'); target.append(strType); target.append('}'); context.useEventVisitor().addEvent(ASWNL("ATemplateNodeHandler_DADA::Node:") + " unknown type:" + strType, AEventVisitor::EL_WARN); break; } strType.clear(); } } } file.readUntilEOF(target); output.append(target); target.clear(); }
void ATextGenerator::generateRandomWord(AOutputBuffer& target, size_t len /* = 0x1 */) { if (len < 1) return; //a_Lead with consonant --len; AString str, strLast("-", 1); generateRandomString(str, 1, AConstant::CHARSET_LOWERCASE_CONSONANTS); str.makeUpper(); target.append(str); bool boolConsonant = false; while (len > 0) { if (boolConsonant) { //a_Add a consonant do { str.clear(); generateRandomString(str, 1, AConstant::CHARSET_LOWERCASE_CONSONANTS); } while(str.at(0) == strLast.at(0) && ARandomNumberGenerator::get().nextU1() > 220); target.append(str); --len; strLast = str; boolConsonant = false; } else { //a_Add a vowel int twice = 0; do { do { str.clear(); generateRandomString(str, 1, AConstant::CHARSET_LOWERCASE_VOWELS); } while(str.at(0) == strLast.at(0) && ARandomNumberGenerator::get().nextU1() > 130); target.append(str); --len; strLast = str; } while (len > 0 && twice++ < 2 && ARandomNumberGenerator::get().nextU1() > 225); boolConsonant = true; } } }
void ATextGenerator::generateUniqueId(AOutputBuffer& target, size_t size /* = 32 */) { if (size < 16) ATHROW(NULL, AException::InvalidParameter); ARope rope; size_t x = ATime::getTickCount(); rope.append((const char *)&x, sizeof(size_t)); size_t bytesToAdd = size - sizeof(size_t); while(bytesToAdd >= 4) { x = ARandomNumberGenerator::get(ARandomNumberGenerator::Lecuyer).nextU4(); rope.append((const char *)&x, 4); bytesToAdd -= 4; } while(bytesToAdd > 0) { x = ARandomNumberGenerator::get(ARandomNumberGenerator::Lecuyer).nextU1(); rope.append((const char *)&x, 1); --bytesToAdd; } AASSERT(NULL, !bytesToAdd); AString str(rope.getSize() * 2, 256); ATextConverter::encode64(rope, str); AASSERT(&str, str.getSize() >= size); target.append(str, size); }
void ATextGenerator::generateRandomString(AOutputBuffer& target, size_t len, const AString& strCharSet) { for (size_t i = 0x0; i < len; ++i) { target.append(strCharSet.peek(ARandomNumberGenerator::get().nextRange(strCharSet.getSize()))); } }
void ATemplateNodeHandler_MODEL::Node::process(ATemplateContext& context, AOutputBuffer& output) { AAutoPtr<AEventVisitor::ScopedEvent> scoped; if (context.useEventVisitor().isLogging(AEventVisitor::EL_DEBUG)) { scoped.reset(new AEventVisitor::ScopedEvent(context.useEventVisitor(), ASW("ATemplateNodeHandler_MODEL",26), m_BlockData, AEventVisitor::EL_DEBUG)); } if (m_BlockData.isEmpty()) return; AXmlElement *pElement = context.useModel().useRoot().findElement(m_BlockData); if (pElement) { //a_Found object pElement->emitContent(output); } else { ARope rope("<!--Unable to find element for '",32); rope.append(m_BlockData); rope.append("'-->",4); output.append(rope); } }
void ATextGenerator::generateRandomData(AOutputBuffer& target, size_t len) { for (size_t i = 0x0; i < len; ++i) { target.append(ARandomNumberGenerator::get().nextU1()); } }
void AFilename::emitPath(AOutputBuffer& target, AFilename::FTYPE ftype, bool noTrailingSlash) const { char sep = '/'; if (FTYPE_MSDOS == ftype) sep = '\\'; //a_Drive if (m_Drive) { switch (ftype) { case FTYPE_UNIX: break; case FTYPE_CYGWIN: target.append(ASW("/cygdrive/",10)); target.append(m_Drive); break; default: if (!m_RelativePath) { target.append(m_Drive); target.append(':'); } } } //a_Add slash is not a relative path if (!m_RelativePath) target.append(sep); else if (0 == m_PathNames.size()) { //a_Special case, relative and no path, default to . or ./ (depends on noTailingSlash flag) target.append(AConstant::ASTRING_PERIOD); if (!noTrailingSlash) target.append(sep); return; } //a_Path LIST_AString::const_iterator cit = m_PathNames.begin(); while (cit != m_PathNames.end()) { target.append(*cit); ++cit; if (cit != m_PathNames.end() || !noTrailingSlash) target.append(sep); } }
void AXmlElement::_indent(AOutputBuffer& target, int indent) const { while (indent > 0) { target.append(AConstant::ASTRING_TWOSPACES); --indent; } }
void AFilename::emitNoExt(AOutputBuffer& target, AFilename::FTYPE ftype) const { emitPath(target, ftype); size_t pos = m_Filename.rfind('.'); if (AConstant::npos != pos) m_Filename.peek(target, 0, pos); else target.append(m_Filename); }
void ATemplateNodeHandler_LUA::Node::process(ATemplateContext& context, AOutputBuffer& output) { AAutoPtr<AEventVisitor::ScopedEvent> scoped; if (context.useEventVisitor().isLogging(AEventVisitor::EL_DEBUG)) { scoped.reset(new AEventVisitor::ScopedEvent(context.useEventVisitor(), ASW("ATemplateNodeHandler_LUA",24), m_BlockData, AEventVisitor::EL_DEBUG)); } if (m_BlockData.isEmpty()) return; ALuaTemplateContext *pLuaTemplateContext = dynamic_cast<ALuaTemplateContext *>(&context); if (pLuaTemplateContext) { //a_Have a Lua context with its own interpreter if (!pLuaTemplateContext->useLua().execute(m_BlockData, context, output)) { output.append(" block(context)={{{\r\n",21); output.append(m_BlockData); output.append("}}}\r\n",5); context.useEventVisitor().addEvent(output, AEventVisitor::EL_ERROR); } } else { //a_Create a temporary interpreter, this is the less efficient way of doing it ALuaEmbed lua; if (!lua.execute(m_BlockData, context, output)) { output.append(" block(local)={{{\r\n",19); output.append(m_BlockData); output.append("}}}\r\n",5); context.useEventVisitor().addEvent(output, AEventVisitor::EL_ERROR); } } }
void AFilename::emit( AOutputBuffer& target, AFilename::FTYPE ftype, bool noTrailingSlash // = false ) const { //a_Filename if (!m_Filename.isEmpty()) { //a_Path emitPath(target, ftype); //a_Append filename target.append(m_Filename); } else { //a_Check if trailing slash is not needed emitPath(target, ftype, noTrailingSlash); } }
void AFilename::emitDrive(AOutputBuffer& target) const { target.append(m_Drive); }
void ATemplateNodeHandler_DADA::Node::_appendVariable(ADadaDataHolder *pddh, MAP_AString_AString& globals, const AString& strType, AOutputBuffer& target) { AASSERT(this, strType.getSize() > 0); AString strTypeName, strControl; LIST_AString listControlNames; size_t pos = strType.find(':'); if (AConstant::npos != pos) { strType.peek(strTypeName, 0, pos); strTypeName.makeLower(); strType.peek(strControl, pos+1); strControl.makeLower(); strControl.split(listControlNames, ',', AConstant::ASTRING_WHITESPACE); } else { strTypeName.assign(strType); strTypeName.makeLower(); } //a_Find it in the global lookup MAP_AString_AString::iterator it = globals.find(strTypeName); if (it != globals.end()) { AString str((*it).second); LIST_AString::iterator itN = listControlNames.begin(); while (itN != listControlNames.end()) { if (!(*itN).compare("article", 7)) { AString strTemp(str); if (AConstant::npos == AWordUtility::sstr_Vowels.find(str.at(0))) { str.assign("a ", 2); } else { str.assign("an ", 3); } str.append(strTemp); } if (!(*itN).compare("plural", 6)) { AString strTemp; AWordUtility::getPlural(str, strTemp); str.assign(strTemp); } if (!(*itN).compare("uppercase", 9)) { str.makeUpper(); } if (!(*itN).compare("lowercase", 9)) { str.makeLower(); } if (!(*itN).compare("proper", 6)) { str.use(0) = (u1)toupper(str.at(0)); size_t nextStart; size_t nextSpace = str.find(' ', 1); while (AConstant::npos != nextSpace) { nextStart = nextSpace + 1; if (nextStart < str.getSize()) str.use(nextStart) = (u1)toupper(str.at(nextStart)); else break; nextSpace = str.find(' ', nextStart); } } ++itN; } target.append(str); } else { //a_Not found in global map } }
void ATemplateNodeHandler_DADA::Node::_appendWordType(ADadaDataHolder *pddh, MAP_AString_AString& globals, const AString& strType, AOutputBuffer& target) { //a_First remove control tags "TYPE:controltag1,controltag2,..." AString strTypeName, strControl; LIST_AString listControlNames; size_t pos = strType.find(':'); if (AConstant::npos != pos) { strType.peek(strTypeName, 0, pos); strTypeName.makeLower(); strType.peek(strControl, pos+1); strControl.makeLower(); strControl.split(listControlNames, ',', AConstant::ASTRING_WHITESPACE); } else { strTypeName.assign(strType); strTypeName.makeLower(); } ADadaDataHolder::WORDMAP::iterator it = pddh->useWordMap().find(strTypeName); if (pddh->useWordMap().end() != it) { VECTOR_AString& vec = (*it).second; u4 index = ARandomNumberGenerator::get().nextRange((int)vec.size()); AString strSaveVariable; AString str(vec.at(index)); //a_Apply control code actions LIST_AString::iterator itN = listControlNames.begin(); while (itN != listControlNames.end()) { if ('$' == (*itN).at(0, '\x0')) { //a_Add variable to global map (*itN).peek(strSaveVariable, 1); globals[strSaveVariable] = str; strSaveVariable.clear(); } else if (!strTypeName.compare("verb")) { //a_Verb only control if (!(*itN).compare("present", 7)) { if (AConstant::npos != AWordUtility::sstr_Vowels.find(str.last())) { //a_Vowel at the end is removed and replaced with "ing" str.rremove(1); } str.append("ing", 3); } else if (!(*itN).compare("past", 4)) { if (AConstant::npos == AWordUtility::sstr_Vowels.find(str.last())) { str.append("ed", 2); } else { str.append("d", 1); } } } if (!(*itN).compare("article", 7)) { AString strTemp(str); if (AConstant::npos == AWordUtility::sstr_Vowels.find(str.at(0))) { str.assign("a ", 2); } else { str.assign("an ", 3); } str.append(strTemp); } if (!(*itN).compare("plural", 6)) { AString strTemp; AWordUtility::getPlural(str, strTemp); str.assign(strTemp); } if (!(*itN).compare("uppercase", 9)) { str.makeUpper(); } if (!(*itN).compare("lowercase", 9)) { str.makeLower(); } if (!(*itN).compare("proper", 6)) { str.use(0) = (u1)toupper(str.at(0)); size_t nextStart; size_t nextSpace = str.find(' ', 1); while (AConstant::npos != nextSpace) { nextStart = nextSpace + 1; if (nextStart < str.getSize()) str.use(nextStart) = (u1)toupper(str.at(nextStart)); else break; nextSpace = str.find(' ', nextStart); } } ++itN; } target.append(str); } else { //a_Unknown type, pass through as is. target.append('%'); target.append(strType); target.append('%'); } }
void AFilename::emitFileUrl(AOutputBuffer& target) const { target.append(FILE_PREFIX); emit(target, AFilename::FTYPE_DEFAULT); }
void AFragmentSet::emit(AOutputBuffer& target) const { target.append(m_Set.at(m_Offset)); }
void AFragmentCounter::emit(AOutputBuffer& target) const { target.append(m_strOdometer); }
size_t AFile_Physical::access( AOutputBuffer& target, size_t index, //= 0 size_t bytes // = AConstant::npos ) const { AASSERT_EX(this, m_fid >= 0, ASWNL("Probably forgot to call open() or invalid file handle after call to open")); // m_fid == -1? forgot to call open()? if (!mp_file) ATHROW(this, AException::NotOpen); //a_Get original position u8 originalIndex = 0; #ifdef _ftelli64 originalIndex = _ftelli64(mp_file); #else originalIndex = (u8)ftell(mp_file); #endif //a_Seek to new position #ifdef _fseeki64 if (_fseeki64(mp_file, index, 0)) ATHROW_EX(this, AException::InvalidParameter, AString("Unable to seek position: ")+AString::fromSize_t(index)); #else if (fseek(mp_file, (long)index, 0)) ATHROW_EX(this, AException::InvalidParameter, AString("Unable to seek position: ")+AString::fromSize_t(index)); #endif //a_Peek some data const size_t BUFFER_SIZE = 10240; char buffer[BUFFER_SIZE]; size_t totalBytes = 0; while (bytes) { size_t bytesToRead = (bytes > BUFFER_SIZE ? BUFFER_SIZE : bytes); size_t bytesRead = ::_read(m_fid, buffer, bytesToRead); //a_EOF or unavail if (AConstant::npos == bytesRead || AConstant::unavail == bytesRead) return bytesRead; //a_Partial read if (bytesRead < bytesToRead) { totalBytes += bytesRead; break; } size_t written = target.append(buffer, bytesRead); if (AConstant::unavail == written || AConstant::npos == written) { return (totalBytes > 0 ? totalBytes : written); } else { bytes -= bytesRead; totalBytes += bytesRead; } } #ifdef _fseeki64 if (_fseeki64(mp_file, originalIndex, 0)) ATHROW_EX(this, AException::InvalidParameter, AString("Unable to restore seek position: ")+AString::fromSize_t(originalIndex)); #else if (fseek(mp_file, (long)originalIndex, 0)) ATHROW_EX(this, AException::InvalidParameter, AString("Unable to restore seek position: ")+AString::fromSize_t(originalIndex)); #endif return totalBytes; }
void ATextGenerator::generateFromTemplate(AOutputBuffer& target, const AString &strTemplate) { if (strTemplate.isEmpty()) return; //a_If the last is % and one before is not %, then expansion error will occur size_t end = strTemplate.getSize(); if (strTemplate[end - 1] == '%') { if (end < 1) { //a_Force early termination end = 0; } else if (strTemplate[end - 2] != '%') { end--; } } char cEscape; for (size_t x = 0; x < end; ++x) { cEscape = strTemplate[x]; if (cEscape == '%') { if (++x >= end) break; switch(strTemplate[x]) { case '%' : break; case 'z' : target.append(AString::fromInt(si_Counter++)); cEscape = '\x0'; break; case 'n' : cEscape = generateRandomNumeral(); break; case 'N' : if (ARandomNumberGenerator::get().nextRange(100) >= 50) cEscape = '+'; else cEscape = '-'; break; case 'l' : cEscape = generateRandomLowercaseLetter(); break; case 'L' : cEscape = generateRandomUppercaseLetter(); break; case 'r' : cEscape = (char)(ARandomNumberGenerator::get().nextRange(255) + 0x1); break; case 'R' : cEscape = ARandomNumberGenerator::get().nextU1(); break; case 's' : cEscape = generateRandomUppercaseLetter(); if (ARandomNumberGenerator::get().nextRange(100) >= 50) cEscape = (char)tolower(cEscape); break; case 't' : ATime().emitYYYYMMDDHHMMSS(target); cEscape = '\x0'; break; case 'T' : ATime().emitRFCtime(target); cEscape = '\x0'; break; default : //a_Escape character detected, but no valid control character found target.append(cEscape); //a_Keep the sequence untouched cEscape = strTemplate[x]; //a_Current char becomes escape to stay consistent with the add to follow } } if (cEscape != '\x0') target.append(cEscape); } }
void AXmlElement::emit(AOutputBuffer& target, int indent) const { if (!m_Name.isEmpty()) { //a_Element has a name if (indent >= 0) { if (indent) target.append(AConstant::ASTRING_CRLF); //a_End of line for (int i=0; i<indent; ++i) target.append(AConstant::ASTRING_TWOSPACES); //a_Indentation } target.append(AXmlElement::sstr_Start); target.append(m_Name); //a_Display attributes of this element if (m_Attributes.size() > 0) { target.append(AConstant::ASTRING_SPACE); m_Attributes.emit(target); } CONTAINER::const_iterator cit = m_Content.begin(); int iSubElementCount = 0; if (cit != m_Content.end()) { target.append(AXmlElement::sstr_End); while (cit != m_Content.end()) { if ((*cit)->isData()) { (*cit)->emit(target, indent); } else { (*cit)->emit(target, (indent >= 0 ? indent+1 : indent)); ++iSubElementCount; } ++cit; } if (iSubElementCount > 0 && indent >= 0) { target.append(AConstant::ASTRING_CRLF); //a_End of line _indent(target, indent); } target.append(AXmlElement::sstr_StartEnd); target.append(m_Name); target.append(AXmlElement::sstr_End); } else { target.append(AXmlElement::sstr_EndSingular); } } else { //a_Element has no name, just iterate the children if any CONTAINER::const_iterator cit = m_Content.begin(); if (cit != m_Content.end()) { while (cit != m_Content.end()) { (*cit)->emit(target, indent); ++cit; } } } }
void AXmlElement::emitJson( AOutputBuffer& target, int indent // = -1 ) const { if (indent >=0) _indent(target, indent); target.append(m_Name); if (m_Content.size() + m_Attributes.size() == 0) { target.append(":\"\"",3); if (indent >=0) target.append(AConstant::ASTRING_CRLF); return; } target.append(':'); if (!hasElements() && !m_Attributes.size()) { //a_Just content and no attributes or elements target.append('\"'); emitContent(target); target.append('\"'); return; } bool needsBraces = m_Content.size() > 0 || m_Attributes.size() > 0; if (needsBraces) { target.append('{'); if (indent >=0) target.append(AConstant::ASTRING_CRLF); } //a_Add content size_t attrSize = m_Attributes.size(); if (m_Content.size() > 0) { CONTAINER::const_iterator cit = m_Content.begin(); while(cit != m_Content.end()) { (*cit)->emitJson(target, (indent >= 0 ? indent+1 : indent)); ++cit; if (cit != m_Content.end() || attrSize > 0) { target.append(','); if (indent >=0) target.append(AConstant::ASTRING_CRLF); } } } //a_Attributes become name:value pairs if (attrSize > 0) { SET_AString names; m_Attributes.getNames(names); AXmlAttributes::CONTAINER::const_iterator cit = m_Attributes.getAttributeContainer().begin(); const AXmlAttributes::CONTAINER& container = m_Attributes.getAttributeContainer(); while(cit != container.end()) { if (indent >=0) _indent(target, indent+1); target.append((*cit)->getName()); target.append(":\"",2); target.append((*cit)->getValue()); target.append('\"'); ++cit; if (cit != container.end()) { target.append(','); if (indent >= 0) target.append(AConstant::ASTRING_CRLF); } } } if (needsBraces) { if (indent >=0) { target.append(AConstant::ASTRING_CRLF); _indent(target, indent); } target.append('}'); } }
void AFilename::emitPathAndFilename(AOutputBuffer& target) const { emitPath(target); target.append(m_Filename); }
//a_Complely random number from sm__strNumber void ATextGenerator::generateRandomNumber(AOutputBuffer& target, size_t len /* = 0x1 */) { for (u4 i = 0x0; i < len; i++) target.append(generateRandomNumeral()); }
void ATextOdometer::emit(AOutputBuffer& target) const { target.append(mstr_Odometer); }