Пример #1
0
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);
}
Пример #2
0
void testFileIO()
{
  AFile_AString strfile("Buffer09123456789Test0123456789");
  ARope rope;

  rope.read(strfile, AConstant::npos);
  AString str;
  rope.emit(str);

  std::cout << str << std::endl;

  strfile.clear();
  strfile.emit(rope);

  std::cout << strfile.useAString() << std::endl;
}
Пример #3
0
/*!
Uses the model to emit content for a path

thisfunction("<path to the AXmlElement to emit>", separate)

lua namespace: model
lua param: Path to emit
lua param: if non-nil then each element found will be returned by itself
lua return: Content at the given path or nil if element does not exist
*/
static int alibrary_Objects_Model_emitContentFromPath(lua_State *L)
{
    ATemplateContext *pLuaContext = static_cast<ATemplateContext *>(L->acontext);
    AASSERT(NULL, pLuaContext);

    size_t len = AConstant::npos;
    const char *s = luaL_checklstring(L, 1, &len);
    const AString& xmlpath = AString::wrap(s, len);

    int mode = 0;
    if (lua_gettop(L) > 1)
        mode = luaL_checkint(L, 2);

    AXmlElement::CONST_CONTAINER nodes;
    size_t ret = pLuaContext->useModel().useRoot().find(xmlpath, nodes);

    if (mode)
    {
        //a_Return each as separate values
        AString str;
        for (AXmlElement::CONST_CONTAINER::const_iterator cit = nodes.begin(); cit != nodes.end(); ++cit)
        {
            (*cit)->emitContent(str);
            lua_pushlstring(L, str.c_str(), str.getSize());
            str.clear();
        }
        return (int)ret;
    }
    else
    {
        if (ret > 0)
        {
            //a_Return content concatinated
            ARope rope;
            for (AXmlElement::CONST_CONTAINER::const_iterator cit = nodes.begin(); cit != nodes.end(); ++cit)
                (*cit)->emitContent(rope);

            const AString& str = rope.toAString();
            lua_pushlstring(L, str.c_str(), str.getSize());

            return 1;
        }
        else
            return 0;
    }
}
Пример #4
0
int main(int argc, char* argv[])
{
  try
  {
    //a_XML dump
    AFile_AString stringfile("<root><c_0><gc_00/><gc_01>5</gc_01></c_0><c_1><gc_10/><gc_11/></c_1></root>");
    AXmlElement element;
    element.fromAFile(stringfile);
    ARope rope;
    element.emitXml(rope, 0);
  //  element.debugDump();
    std::cout << rope.toString() << std::endl;
  }
  catch(AException& ex)
  {
    std::cerr << ex.what() << std::endl;
  }
  return 0;
}
Пример #5
0
AOSContext *AOSContextManager::_newContext(AFile_Socket *pSocket)
{
  AOSContext *pContext = (AOSContext *)m_FreeStore.popFront();
  
  if (!pContext)
    return new AOSContext(pSocket, m_Services);
  else
  {
    pContext->reset(pSocket);
    if (pContext->useEventVisitor().isLoggingEvent())
    {
      ARope rope;
      rope.append(" - ",3);
      rope.append(pSocket->getSocketInfo().m_address);
      rope.append(':');
      rope.append(AString::fromInt(pSocket->getSocketInfo().m_port));
      pContext->useEventVisitor().useName().append(rope);
    }
    return pContext;
  }
}
Пример #6
0
void testCheckpoints()
{
  AEventVisitor visitor(ASWNL("My Visitor's Name"), AEventVisitor::EL_WARN);
  visitor.startEvent(ASWNL("main: Starting the test"));
  visitor.startEvent(ASWNL("main: Adding first line"));
  visitor.startEvent(ASWNL("main: Replacing the initial line"), AEventVisitor::EL_WARN);
  visitor.startEvent(ASWNL("main: Setting second line"));
  visitor.startEvent(ASWNL("main: Debug event state"), AEventVisitor::EL_DEBUG);
  visitor.startEvent(ASWNL("main: Adding first error"), AEventVisitor::EL_ERROR);
  visitor.startEvent(ASWNL("main: Oops, another error"), AEventVisitor::EL_ERROR);
  visitor.startEvent(ASWNL("main: Last event"));
  visitor.endEvent();

  std::cout << "----Textual current----" << std::endl;
  ARope rope;
  visitor.emit(rope);
  std::cout << rope << std::endl;
  
  std::cout << "----Textual debug----" << std::endl;
  rope.clear();
  visitor.emit(rope, AEventVisitor::EL_DEBUG);
  std::cout << rope << std::endl;

  std::cout << "----Textual only errors----" << std::endl;
  rope.clear();
  visitor.emit(rope, AEventVisitor::EL_ERROR);
  std::cout << rope << std::endl;

  std::cout << "----Textual only events and errors----" << std::endl;
  rope.clear();
  visitor.emit(rope, AEventVisitor::EL_EVENT);
  std::cout << rope << std::endl;

  std::cout << "----XML----" << std::endl;
  AFile_IOStream iii;
  AXmlDocument doc("AEventVisitor");
  visitor.emitXml(doc.useRoot());
  doc.emit(iii, 0);
  std::cout << std::endl;
}
Пример #7
0
void __insert(AObjectContainer& ns)
{
  AFile_AString f("<inserted><var a=1 b=2 c=3/><mixed>foo<bar/>baz</mixed></inserted>");
  AXmlElement element;
  element.fromAFile(f);
  ARope rope;
  element.emitXml(rope);
//  element.debugDump(std::cout, 0);
//  std::cout << rope << std::endl;

  AObjectContainer nsNew;
  nsNew.fromElement(element);
  rope.clear();
  element.clear();
  nsNew.publish(element);
//  nsNew.debugDump(std::cout, 0);

  element.debugDump(std::cout, 0);
  element.emitXml(rope);
//  std::cout << rope << std::endl;

  ns.insert("/incoming/", nsNew.clone());
//  ns.debugDump(std::cout, 0);
}
Пример #8
0
AOSContext::ReturnCode AOSModule_Template::execute(AOSContext& context, const AXmlElement& params)
{
  const AXmlElement *pNode = params.findElement(ASW("template",8));
  AAutoPtr<ATemplate> pTemplate(NULL, false);
  AAutoPtr<AFile> pFile(NULL, false);
  if (pNode)
  {
    //a_Element contains script
    pTemplate.reset(m_Services.createTemplate(), true);
    
    //a_Parse template
    AFile_AString strfile;
    pNode->emitContent(strfile);
    pTemplate->fromAFile(strfile);
  }
  else
  {
    //a_Filename provided, use the cache
    pNode = params.findElement(AOS_BaseModules_Constants::FILENAME);
    if (pNode)
    {
      AString relativePath;
      pNode->emitContent(relativePath);

      //a_File to be used (may need caching for it, but for now keep it dynamic)
      AFilename filename(m_Services.useConfiguration().getAosBaseDataDirectory(), true);
      filename.join(relativePath, false);
      if (ACacheInterface::NOT_FOUND == m_Services.useCacheManager().getTemplate(context, filename, pTemplate))
      {
        //a_Not found, return error
        ARope rope;
        rope.append(getClass());
        rope.append(": Unable to find a template file: ",34);
        rope.append(filename);
        context.useEventVisitor().startEvent(rope, AEventVisitor::EL_ERROR);
        return AOSContext::RETURN_ERROR;
      }
    }
    else
    {
      context.addError(getClass(), ASWNL("Unable to find module/template nor module/filename, Template module did not execute, params"));
      return AOSContext::RETURN_ERROR;  //a_Did not find either module/template or module/filename
    }
  }  

  //a_Process and save output
  ARope ropeOutput;
  pTemplate->process(context.useLuaTemplateContext(), ropeOutput);
  
  //a_Add template to debug
  if (context.getDumpContextLevel() > 0)
  {
    AString str("debug/",6);
    str.append(getClass());
    str.append("/template",9);
    AXmlElement& base = context.useModel().addElement(str);
    pTemplate->emitXml(base);
  }

  //a_Insert output into outpath (if any)
  pNode = params.findElement(AOS_BaseModules_Constants::PATH);
  if (pNode)
  {
    AString xmlpath;
    pNode->emitContent(xmlpath);
    if (!xmlpath.isEmpty())
    {
      //a_Add output as CDATA
      context.useModel().addElement(xmlpath).addData(ropeOutput, AXmlElement::ENC_CDATADIRECT);
    }
  }
  else
  {
    context.useEventVisitor().addEvent(ASWNL("Unable to find module/path, output from template discarded"), AEventVisitor::EL_WARN);
  }
  return AOSContext::RETURN_OK;
}
Пример #9
0
void AXmlElement::toAFile(AFile& file) const
{
  ARope rope;
  emit(rope);
  rope.write(file);
}