unsigned int CSVStreamParser :: ColIndexFromName(const string & name ) const { if ( mColMap.size() == 0 ) { ATHROW( "CSV parser has no column map" ); } ColNameMapType::const_iterator it = mColMap.find( name ); if ( it == mColMap.end() ) { ATHROW( "Unknown column name: " << name ); } return it->second; }
bool ASync_Semaphore::trylock() { #if defined(__WINDOWS__) if (m_handle) return (WAIT_TIMEOUT != ::WaitForSingleObjectEx(m_handle, 0, TRUE)); else ATHROW(this, AException::InvalidObject); #elif defined(__LINUX__) if (m_handle) return (0 == sem_trywait(m_handle)); else ATHROW(this, AException::InvalidObject); #endif }
void AOSOutputExecutor::registerOutputGenerator(AOSOutputGeneratorInterface *pGenerator) { if (!pGenerator) ATHROW(this, AException::InvalidParameter); const AString& command = pGenerator->getClass(); { AString str(" Output Generator Registered: "); str.append(command); AOS_DEBUGTRACE(str.c_str(), NULL); } OutputGeneratorContainer::iterator it = m_OutputGenerators.find(command); if (it != m_OutputGenerators.end()) { //a_Command already has this processor m_Services.useLog().add(ASWNL("AOSOutputExecutor::registerOutputGenerator:replacing output generator"), (*it).first, command, ALog::EVENT_WARNING); delete (*it).second; (*it).second = pGenerator; } else { //a_Add new command m_Services.useLog().add(ASWNL("AOSOutputExecutor::registerOutputGenerator"), command, ALog::EVENT_INFO); m_OutputGenerators[command] = pGenerator; } //a_Initialize it pGenerator->init(); //a_Register the module's admin interface pGenerator->adminRegisterObject(m_Services.useAdminRegistry(), ASW("AOSOutputExecutor",17)); }
void connectToServiceDispatch() { AFILE_TRACER_DEBUG_SCOPE("connectToServiceDispatch", NULL); SERVICE_TABLE_ENTRY steServiceTable[2]; steServiceTable[0].lpServiceName = AOS_SERVICE_NAME; steServiceTable[0].lpServiceProc = aosServiceMain; steServiceTable[1].lpServiceName = NULL; //a_Must be NULL steServiceTable[1].lpServiceProc = NULL; //a_Must be NULL //a_Connect to service controller using the current service table AFILE_TRACER_DEBUG_MESSAGE((ASWNL("Calling StartServiceCtrlDispatcher: ")+AOS_SERVICE_NAME).c_str(), NULL); if(!::StartServiceCtrlDispatcher(steServiceTable)) { switch(::GetLastError()) { case ERROR_FAILED_SERVICE_CONTROLLER_CONNECT : AFILE_TRACER_DEBUG_MESSAGE("WARNING: console mode", NULL); case ERROR_INVALID_DATA : AFILE_TRACER_DEBUG_MESSAGE("Invalid data", NULL); ATHROW(NULL, AException::InvalidData); default : AFILE_TRACER_DEBUG_MESSAGE("Unknown error", NULL); } } AFILE_TRACER_DEBUG_MESSAGE("Service dispatcher started", NULL); }
void ASync_CriticalSectionSpinLock::unlock() { if (mp_CriticalSection) ::LeaveCriticalSection(mp_CriticalSection); else ATHROW(this, AException::InvalidObject); }
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); }
bool AFile_Physical::_isNotEof() { if (mp_file) return (!_eof(m_fid)); else ATHROW(this, AException::NotOpen); }
AXmlElement& AXmlElement::addContent( AXmlElement *pnode, const AString& path, // = AConstant::ASTRING_EMPTY bool insertIntoFront // = false ) { if (path.isEmpty() || path.equals(AConstant::ASTRING_SLASH)) { AASSERT(this, m_Content.size() < DEBUG_MAXSIZE_AXmlElement); //Debug only limit AASSERT(this, pnode); pnode->setParent(this); if (insertIntoFront) m_Content.push_front(pnode); else m_Content.push_back(pnode); } else { LIST_AString parts; path.split(parts, '/'); if (!parts.size()) ATHROW(this, AException::InvalidParameter); AXmlElement *pNewParent = _createAndAppend(parts, this, insertIntoFront); AASSERT_EX(this, pNewParent, path); pNewParent->addContent(pnode, AConstant::ASTRING_EMPTY, insertIntoFront); } return *this; }
bool ASync_Mutex::trylock() { if (m_handle) return (WAIT_TIMEOUT != ::WaitForSingleObject(m_handle, 0)); else ATHROW(this, AException::InvalidObject); }
const string & XMLElement :: AttrValue( const string & aname ) const { for ( unsigned int i = 0; i < mAttrs.size(); i++ ) { if ( mAttrs[i].mName == aname ) { return mAttrs[i].mVal; } } ATHROW( "Unknown attribute name " << SQuote( aname ) << Location() ); }
void CSVStreamParser :: MakeColMap( const std::string & cols ) { if ( ALib::IsEmpty( cols ) ) { ATHROW( "No column names available" ); } mColMap.clear(); vector <string> data; LineParser().Parse( cols, data ); for ( unsigned int i = 0; i < data.size(); i++ ) { if ( mColMap.find( data[i] ) != mColMap.end() ) { ATHROW( "Duplicate column name " << data[i] ); } mColMap.insert( std::make_pair( data[i], i ) ); } }
void ALog_AFile::emitCurrentFilename(AOutputBuffer& target) const { if (!mp_File) ATHROW(this, AException::InvalidObject); AFile_Physical *pPhysicalFile = dynamic_cast<AFile_Physical *>(mp_File); if (pPhysicalFile) pPhysicalFile->useFilename().emit(target); }
bool ASync_Event::trylock() { if (m_handle) { ::ResetEvent(m_handle); return (WAIT_TIMEOUT != ::WaitForSingleObject(m_handle, 0)); } else ATHROW(this, AException::InvalidObject); }
void ASync_Semaphore::unlock() { #if defined(__WINDOWS__) if (m_handle) { if (!::ReleaseSemaphore( m_handle, 1, NULL )) ATHROW_LAST_OS_ERROR(this); } else ATHROW(this, AException::InvalidObject); #elif defined(__LINUX__) if (m_handle) { if (sem_post(m_handle)) ATHROW_LAST_OS_ERROR(this); } else ATHROW(this, AException::InvalidObject); #endif }
CSVFileParser :: CSVFileParser( const string & fname, bool igblank, char csvsep ) : mIfstream( 0 ), mParser( 0 ), mFileName( fname ) { mIfstream = new std::ifstream( fname.c_str() ); if ( ! mIfstream->is_open() ) { delete mIfstream; mIfstream = 0; ATHROW( "Cannot open CSV file " << fname << " for input" ); } mParser = new CSVStreamParser( * mIfstream, igblank, csvsep ); }
void AOSAdminRegistry::insert(const AString& name, AOSAdminInterface& object) { if (name.isEmpty()) ATHROW(this, AException::InvalidParameter); if (m_AdminObjects.find(name) != m_AdminObjects.end()) m_Log.add(ASWNL("Admin object already registered"), name, ALog::EVENT_WARNING); //a_Insert/replace m_AdminObjects[name] = &object; }
void ASync_Mutex::lock() { if (m_handle) { if (WAIT_FAILED == ::WaitForSingleObject(m_handle, m_Timeout)) ATHROW_LAST_OS_ERROR(this); } else { ATHROW(this, AException::InvalidObject); } }
bool AFile_Physical::seek(u8 offset, int origin) { if (mp_file) { #ifdef _fseeki64 return (!_fseeki64(mp_file, offset, origin)); #else return (!fseek(mp_file, (long)offset, origin)); #endif } else ATHROW(this, AException::NotOpen); }
u8 AFile_Physical::tell() { if (mp_file) { #ifdef _ftelli64 return _ftelli64(mp_file); #else return (u8)ftell(mp_file); #endif } else ATHROW(this, AException::NotOpen); }
size_t AFile_Physical::_write(const void *buf, size_t size) { 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) { size_t written = ::_write(m_fid, buf, size); if (AConstant::npos == written || written < size) ATHROW_ERRNO(this, AException::UnableToWrite, errno); return written; } else ATHROW(this, AException::NotOpen); }
size_t AFile_Physical::_read(void *buf, size_t size) { 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) { size_t bytesread = ::_read(m_fid, buf, size); if (AConstant::npos == bytesread) ATHROW_ERRNO(this, AException::UnableToRead, errno); return bytesread; } else ATHROW(this, AException::NotOpen); }
void ASync_Semaphore::lock() { #if defined(__WINDOWS__) if (m_handle) { if (WAIT_FAILED == ::WaitForSingleObjectEx( m_handle, INFINITE, TRUE )) ATHROW_LAST_OS_ERROR(this); } else { ATHROW(this, AException::InvalidObject); } #elif defined(__LINUX__) if (m_handle) { if (sem_wait(m_handle)) ATHROW_LAST_OS_ERROR(this); } else { ATHROW(this, AException::InvalidObject); } #endif }
AXmlElement *AXmlElement::_addElement(const AString& path, bool overwrite, bool insertIntoFront) { if (m_Name.isEmpty()) ATHROW_EX(this, AException::InvalidObject, ASWNL("AXmlElement does not have a name")); LIST_AString xparts; path.split(xparts, '/'); if (!xparts.size()) ATHROW(this, AException::InvalidParameter); //a_Check is absolute is used and if root name matches this element if ('/' == path.at(0) && xparts.size() > 0) { //a_xpath starts with /, make sure names match if (isNameEquals(xparts.front())) xparts.pop_front(); else { AString str("Path specified ("); str.append(path); str.append(") is absolute and does not match this element's name: "); str.append(m_Name); ATHROW_EX(this, AException::InvalidPath, str); } } //a_Skipped over root or relative path specified AXmlElement *p = NULL; if (xparts.size() > 0) { if (overwrite) p = _getOrCreate(xparts, this, insertIntoFront); else p = _createAndAppend(xparts, this, insertIntoFront); } else p = this; return p; }
AFragmentSet::AFragmentSet(AFragmentSet::SetType t) { reset(); switch(t) { case UppercaseAlpha : m_Set = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; break; case LowercaseAlpha : m_Set = "abcdefghijklmnopqrstuvwxyz"; break; case Numeric : m_Set = "0123456789"; break; case Alpha : m_Set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; break; case AlphaNumeric : m_Set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; break; case UppercaseAlphaNumeric : m_Set = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; break; case LowercaseAlphaNumeric : m_Set = "abcdefghijklmnopqrstuvwxyz0123456789"; break; default : ATHROW(this, AException::ProgrammingError); // Invalid type break; } }
AOSContext *AOSContextQueue_IsAvailable::_nextContext() { //a_The way the queues work, this method is never used, each queue owns their own set of contexts for select ATHROW(this, AException::ProgrammingError); }
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 XMLElement :: AddAttr( const string & aname, const string & aval ) { if ( HasAttr( aname ) ) { ATHROW( "Duplicate attribute name " << SQuote( aname ) ); } mAttrs.push_back( AttrVal( aname, aval ) ); }
const string & XMLElement :: AttrName( unsigned int i ) const { if ( i >= AttrCount() ) { ATHROW( "Attribute index " << SQuote( Str(i) ) << " out of range" ); } return mAttrs[i].mName; }
const XMLNode * XMLElement :: ChildAt( unsigned int i ) const { if ( i >= ChildCount() ) { ATHROW( "Node index " << SQuote(Str(i)) << " out of range" ); } return mKids[i]; }
void XMLElement :: Error( const std::string & emsg ) const { ATHROW( emsg << Location() ); }