// FindWhitespace bool COpcTextReader::FindWhitespace(COpcText& cToken) { OPC_ASSERT(m_szBuf != NULL); OPC_ASSERT(m_uLength != 0); UINT uPosition = 0; // skip leading non-whitespace if (cToken.GetSkipLeading()) { for (UINT ii = 0; ii < m_uEndOfData; ii++) { if (CheckForHalt(cToken, ii)) { return false; } if (iswspace(m_szBuf[ii])) { uPosition = ii; break; } } } // check if there is still data left to read. if (uPosition >= m_uEndOfData) { return false; } // read until a non-whitespace. for (UINT ii = uPosition; ii < m_uEndOfData; ii++) { if (CheckForHalt(cToken, ii)) { break; } if (!iswspace(m_szBuf[ii])) { break; } } // check for empty token if (ii == uPosition) { return false; } // copy token. CopyData(cToken, uPosition, ii); return true; }
// FindToken bool COpcTextReader::FindToken(COpcText& cToken) { OPC_ASSERT(m_szBuf != NULL); OPC_ASSERT(m_uLength != 0); switch (cToken.GetType()) { case COpcText::Literal: return FindLiteral(cToken); case COpcText::NonWhitespace: return FindNonWhitespace(cToken); case COpcText::Whitespace: return FindWhitespace(cToken); case COpcText::Delimited: return FindDelimited(cToken); } return false; }
// FindDelimited bool COpcTextReader::FindDelimited(COpcText& cToken) { OPC_ASSERT(m_szBuf != NULL); OPC_ASSERT(m_uLength != 0); // skip leading whitespace UINT uPosition = SkipWhitespace(cToken); // check if there is still data left to read. if (uPosition >= m_uEndOfData) { return false; } // read until a delimiter. for (UINT ii = uPosition; ii < m_uEndOfData; ii++) { // check if search halted. if (CheckForHalt(cToken, ii)) { return false; } // check if delimiter found. if (CheckForDelim(cToken, ii)) { // copy token - empty tokens are valid. CopyData(cToken, uPosition, ii); return true; } } // check for end of data - true if EOF is a delim. if (ii >= m_uEndOfData) { cToken.SetEof(); if (cToken.GetEofDelim()) { CopyData(cToken, uPosition, ii); return true; } } return false; }
// FindLiteral bool COpcTextReader::FindLiteral(COpcText& cToken) { OPC_ASSERT(m_szBuf != NULL); OPC_ASSERT(m_uLength != 0); LPCWSTR szText = cToken.GetText(); UINT uLength = (szText != NULL)?wcslen(szText):0; // check for trivial case if (uLength == 0) { return false; } UINT uPosition = SkipWhitespace(cToken); // check if there is enough data. if (uLength > (m_uEndOfData - uPosition)) { return false; } for (UINT ii = uPosition; ii < m_uEndOfData-uLength+1; ii++) { // check if search halted. if (CheckForHalt(cToken, ii)) { return false; } // compare text at current position. if (IsEqual(cToken, m_szBuf+ii, szText, uLength)) { CopyData(cToken, ii, ii+uLength); return true; } // stop search if leading unmatching characters are not ignored. if (!cToken.GetSkipLeading()) { break; } } return false; }
// FindEnclosed bool COpcTextReader::FindEnclosed(COpcText& cToken) { OPC_ASSERT(m_szBuf != NULL); OPC_ASSERT(m_uLength != 0); WCHAR zStart = 0; WCHAR zEnd = 0; cToken.GetBounds(zStart, zEnd); // skip leading whitespace UINT uPosition = SkipWhitespace(cToken); // check if there is still data left to read. if (uPosition >= m_uEndOfData) { return false; } // read until finding the start delimiter, for (UINT ii = uPosition; ii < m_uEndOfData; ii++) { // check if search halted. if (CheckForHalt(cToken, ii)) { return false; } // check for start character. if (IsEqual(cToken, m_szBuf[ii], zStart)) { uPosition = ii; break; } } // check if there is still data left to read. if (ii >= m_uEndOfData) { return false; } // read until finding the end delimiter, for (ii = uPosition+1; ii < m_uEndOfData; ii++) { // check if search halted. if (CheckForHalt(cToken, ii)) { return false; } // check for end character. if (IsEqual(cToken, m_szBuf[ii], zStart)) { // ignore if character is escaped. if (cToken.GetAllowEscape() && (uPosition < ii-1)) { if (m_szBuf[ii-1] == L'\\') { continue; } } // copy token - empty tokens are valid. CopyData(cToken, uPosition+1, ii); return true; } } return false; }
// Insert COpcBrowseElement* COpcBrowseElement::Insert(const COpcString& cPath) { COpcString cName = cPath; COpcString cSubPath = OPC_EMPTY_STRING; // check if multiple levels have been specified. do { UINT uIndex = cName.Find(GetSeparator()); if (uIndex == -1) { break; } cSubPath = cName.SubStr(uIndex + GetSeparator().GetLength()); cName = cName.SubStr(0, uIndex); if (!cName.IsEmpty()) { break; } cName = cSubPath; } while (!cSubPath.IsEmpty()); // invalid path specified. if (cName.IsEmpty()) { return NULL; } // find out if node already exists. COpcBrowseElement* pNode = NULL; OPC_POS pos = m_cChildren.GetHeadPosition(); while (pos != NULL) { pNode = m_cChildren.GetNext(pos); if (pNode->m_cName == cName) { // return existing node. if (cSubPath.IsEmpty()) { return pNode; } // insert sub-path into existing node. return pNode->Insert(cSubPath); } } // create new node. pNode = CreateInstance(); pNode->m_cName = cName; OPC_ASSERT(!pNode->m_cName.IsEmpty()); COpcBrowseElement* pChild = pNode; if (!cSubPath.IsEmpty()) { pChild = pNode->Insert(cSubPath); if (pChild == NULL) { delete pNode; return NULL; } } m_cChildren.AddTail(pNode); return pChild; }
// Destructor COpcThreadPool::~COpcThreadPool() { // check that stop was called prior to destruction. OPC_ASSERT(m_cQueue.GetCount() == 0); }
int main( int argc, const char* argv[] ) { #ifdef WIN32 _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); #endif int ret=-1; time_t start_time=time(NULL); FILE *file=NULL; int writer_indent=0; pbool_t reader_mce=PTRUE; const char *fileName=NULL; for(int i=1;i<argc;i++) { if ((0==xmlStrcmp(_X("--understands"), _X(argv[i])) || 0==xmlStrcmp(_X("-u"), _X(argv[i]))) && i+1<argc) { i++; // skip namespace, registered later when parser was created. } else if ((0==xmlStrcmp(_X("--out"), _X(argv[i])) || 0==xmlStrcmp(_X("--out"), _X(argv[i]))) && i+1<argc && NULL==file) { const char *filename=argv[++i]; file=fopen(filename, "w"); } else if (0==xmlStrcmp(_X("--indent"), _X(argv[i]))) { writer_indent=1; } else if (0==xmlStrcmp(_X("--raw"), _X(argv[i]))) { reader_mce=PFALSE; } else if (NULL==fileName) { fileName=argv[i]; } else { fprintf(stderr, "IGNORED: %s\n", argv[i]); } } xmlTextWriter *writer=xmlNewTextWriterFile(file); if (NULL==fileName || NULL==writer) { printf("mcepp [--understands NAMESPACE] [--out FILENAME] [--indent] [--raw] [FILENAME | - ]\n\n"); printf("Sample: mcepp sample.xml\n"); } else { xmlInitParser(); xmlTextWriterSetIndent(writer, writer_indent); mceTextReader_t mceTextReader; mceTextReaderInit(&mceTextReader, ('-'==fileName[0] && 0==fileName[1]?xmlReaderForFd(0, NULL, NULL, 0):xmlReaderForFile(fileName, NULL, 0))); mceTextReaderDisableMCE(&mceTextReader, !reader_mce); for(int i=1;i<argc;i++) { if ((0==xmlStrcmp(_X("--understands"), _X(argv[i])) || 0==xmlStrcmp(_X("-u"), _X(argv[i]))) && i+1<argc) { const xmlChar *ns=_X(argv[++i]); mceTextReaderUnderstandsNamespace(&mceTextReader, ns); } } if (-1==mceTextReaderDump(&mceTextReader, writer, PFALSE)) { ret=mceTextReaderGetError(&mceTextReader); } else { ret=0; } mceTextReaderCleanup(&mceTextReader); xmlCleanupParser(); } if (NULL!=writer) xmlFreeTextWriter(writer); if (NULL!=file) fclose(file); time_t end_time=time(NULL); fprintf(stderr, "time %.2lfsec\n", difftime(end_time, start_time)); #ifdef WIN32 OPC_ASSERT(!_CrtDumpMemoryLeaks()); #endif return ret; }