00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifdef _FEDM_XML_SUPPORT
00036
00037
00038
00039 #include "FEDM_XmlBase.h"
00040
00041
00042 #if !defined(_WIN32_WCE)
00043 #include <sys/stat.h>
00044 #endif
00045
00046 #ifdef _FEDM_LINUX
00047 #include <string.h>
00048 #endif
00049
00050 #if _MSC_VER >= 1400
00051 #pragma warning(disable : 4996)
00052 #endif
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 FEDM_XMLBase::FEDM_XMLBase()
00070 {
00071 m_iLastError = 0;
00072 m_sXmlStream = L"";
00073 m_file = NULL;
00074
00075 m_wsXmlVer = L"";
00076 m_wsEncoding = L"";
00077 m_wsStandalone = L"";
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 FEDM_XMLBase::~FEDM_XMLBase()
00091 {
00092 DeleteXmlTree();
00093
00094 if(m_file != NULL)
00095 fclose(m_file);
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 void FEDM_XMLBase::DeleteXmlTree()
00109 {
00110 FEDM_XML_TREE_LIST_ITOR Itor;
00111
00112 for(Itor = m_XmlTree.begin(); Itor != m_XmlTree.end(); Itor++)
00113 {
00114 if((*Itor) != NULL)
00115 {
00116 DeleteTagItem(*(Itor));
00117 }
00118 }
00119
00120 m_XmlTree.clear();
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 int FEDM_XMLBase::DeleteTagItem(FEDM_XML_TREEITEM* item)
00135 {
00136 FEDM_CHK5(item);
00137
00138 #if defined(_MSC_VER) && defined(_FEDM_TRACE) && defined(_DEBUG)
00139 char buf[256];
00140 #endif
00141 FEDM_XML_TREE_LIST_ITOR TreeItor;
00142 FEDM_XML_ATTRIBUTE_LIST_ITOR AttrItor;
00143
00144 for(TreeItor = item->aChildList.begin(); TreeItor != item->aChildList.end(); TreeItor++)
00145 {
00146 if((*TreeItor) != NULL)
00147 {
00148 DeleteTagItem(*TreeItor);
00149 }
00150 }
00151
00152 item->aChildList.clear();
00153
00154 for(AttrItor = item->aAttribList.begin(); AttrItor != item->aAttribList.end(); AttrItor++)
00155 {
00156 #if defined(_MSC_VER) && defined(_FEDM_TRACE) && defined(_DEBUG)
00157 sprintf(buf, "%S=%S", (*AttrItor)->sAttribute.c_str(), (*AttrItor)->sValue.c_str());
00158 TRACE("deleting attribute: %s\n", buf);
00159 ::Sleep(0);
00160 #endif
00161 (*AttrItor)->sAttribute.erase();
00162 (*AttrItor)->sValue.erase();
00163
00164 delete *AttrItor;
00165 }
00166
00167 #if defined(_MSC_VER) && defined(_FEDM_TRACE) && defined(_DEBUG)
00168 swscanf(item->sTag.c_str(), L"%S", buf);
00169 TRACE("deleting tag: %s\n", buf);
00170 ::Sleep(0);
00171 #endif
00172 delete item;
00173
00174 return FEDM_OK;
00175 }
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 int FEDM_XMLBase::QueryDocType(char* sFileName, char* sDocType, char* sDocVersion)
00195 {
00196 int iBack = 0;
00197 unsigned int i = 0;
00198 FEDM_XML_TREEITEM* root = NULL;
00199 FEDM_XML_TREEITEM* parent = NULL;
00200 FEDM_XML_TREEITEM* child = NULL;
00201
00202
00203 iBack = OpenDoc(sFileName, "r");
00204 if(iBack)
00205 return iBack;
00206
00207
00208 if(IsXmlDoc())
00209 {
00210 CloseDoc();
00211 return m_iLastError;
00212 }
00213
00214
00215 if(HasOBIDTag())
00216 {
00217 CloseDoc();
00218 return m_iLastError;
00219 }
00220
00221
00222 if(ReadDoc())
00223 {
00224 CloseDoc();
00225 return m_iLastError;
00226 }
00227
00228 CloseDoc();
00229
00230
00231 root = FindTag(FEDM_XML_TAG_OBID_UC);
00232 if(root == NULL)
00233 return m_iLastError;
00234 AddTagItem(NULL, root);
00235
00236
00237 child = FindTag(FEDM_XML_TAG_FILE_HDR_UC);
00238 if(child == NULL)
00239 {
00240 DeleteXmlTree();
00241 return m_iLastError;
00242 }
00243 AddTagItem(root, child);
00244 parent = child;
00245
00246
00247 child = FindTag(FEDM_XML_TAG_DOC_TYPE_UC);
00248 if(child == NULL)
00249 {
00250 DeleteXmlTree();
00251 return m_iLastError;
00252 }
00253 AddTagItem(parent, child);
00254 m_wsDocType = child->sValue;
00255
00256 #if defined(__BORLANDC__) || defined(_MSC_VER)
00257 for (i=0; i<child->sValue.length(); i++)
00258 sDocType[i] = (char)(child->sValue.c_str())[i];
00259 sDocType[i] = '\0';
00260 #elif defined(_FEDM_SUPPORT_UCLINUX)
00261 FEDM_SWSCANF(child->sValue.c_str(), sDocType);
00262 #else
00263 swscanf(child->sValue.c_str(), L"%s", sDocType);
00264 #endif
00265
00266
00267 child = FindTag(FEDM_XML_TAG_DOC_VERSION_UC);
00268 if(child == NULL)
00269 {
00270 DeleteXmlTree();
00271 return m_iLastError;
00272 }
00273 m_wsDocVer = child->sValue;
00274
00275 #if defined(__BORLANDC__) || defined(_MSC_VER)
00276 swscanf(child->sValue.c_str(), L"%S", sDocVersion);
00277 #elif defined(_FEDM_SUPPORT_UCLINUX)
00278 FEDM_SWSCANF(child->sValue.c_str(), sDocVersion);
00279 #else
00280 swscanf(child->sValue.c_str(), L"%s", sDocVersion);
00281 #endif
00282
00283 delete child;
00284 DeleteXmlTree();
00285
00286 FEDM_RETURN(FEDM_OK);
00287 }
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302 int FEDM_XMLBase::QueryDocType(char* sFileName, const char* sRootTag, char* sDocType, char* sDocVersion)
00303 {
00304 int iBack = 0;
00305 unsigned int i = 0;
00306 wchar_t wcRootTag[64];
00307 FEDM_XML_TREEITEM* root = NULL;
00308 FEDM_XML_TREEITEM* parent = NULL;
00309 FEDM_XML_TREEITEM* child = NULL;
00310
00311
00312 iBack = OpenDoc(sFileName, "r");
00313 if(iBack)
00314 return iBack;
00315
00316
00317 if(IsXmlDoc())
00318 {
00319 CloseDoc();
00320 return m_iLastError;
00321 }
00322
00323
00324 if(ReadDoc())
00325 {
00326 CloseDoc();
00327 return m_iLastError;
00328 }
00329
00330 CloseDoc();
00331
00332
00333 if(strlen(sRootTag) > 0)
00334 {
00335 #if defined(__BORLANDC__)
00336 swprintf(wcRootTag, L"%S", sRootTag);
00337 #elif defined(_MSC_VER)
00338 #if defined(_WIN32_WCE)
00339 swprintf(wcRootTag, L"%S", sRootTag);
00340 #else
00341 #if _MSC_VER <= 1200
00342 swprintf(wcRootTag, L"%S", sRootTag);
00343 #else
00344 swprintf(wcRootTag, 64, L"%S", sRootTag);
00345 #endif
00346 #endif
00347 #else
00348 swprintf(wcRootTag, 64, L"%S", sRootTag);
00349 #endif
00350 root = FindTag(wcRootTag);
00351 if(root == NULL)
00352 return m_iLastError;
00353 AddTagItem(NULL, root);
00354 }
00355 else
00356 {
00357 root = new FEDM_XML_TREEITEM;
00358 AddTagItem(NULL, root);
00359 }
00360
00361
00362 child = FindTag(FEDM_XML_TAG_FILE_HDR_UC);
00363 if(child == NULL)
00364 {
00365 DeleteXmlTree();
00366 return m_iLastError;
00367 }
00368 AddTagItem(root, child);
00369 parent = child;
00370
00371
00372 child = FindTag(FEDM_XML_TAG_DOC_TYPE_UC);
00373 if(child == NULL)
00374 {
00375 DeleteXmlTree();
00376 return m_iLastError;
00377 }
00378 AddTagItem(parent, child);
00379 m_wsDocType = child->sValue;
00380
00381 #if defined(__BORLANDC__) || defined(_MSC_VER)
00382 for (i=0; i<child->sValue.length(); i++)
00383 sDocType[i] = (char)(child->sValue.c_str())[i];
00384 sDocType[i] = '\0';
00385 #elif defined(_FEDM_SUPPORT_UCLINUX)
00386 FEDM_SWSCANF(child->sValue.c_str(), sDocType);
00387 #else
00388 swscanf(child->sValue.c_str(), L"%s", sDocType);
00389 #endif
00390
00391
00392 child = FindTag(FEDM_XML_TAG_DOC_VERSION_UC);
00393 if(child == NULL)
00394 {
00395 DeleteXmlTree();
00396 return m_iLastError;
00397 }
00398 m_wsDocVer = child->sValue;
00399
00400 #if defined(__BORLANDC__) || defined(_MSC_VER)
00401 swscanf(child->sValue.c_str(), L"%S", sDocVersion);
00402 #elif defined(_FEDM_SUPPORT_UCLINUX)
00403 FEDM_SWSCANF(child->sValue.c_str(), sDocVersion);
00404 #else
00405 swscanf(child->sValue.c_str(), L"%s", sDocVersion);
00406 #endif
00407
00408 delete child;
00409 DeleteXmlTree();
00410
00411 FEDM_RETURN(FEDM_OK);
00412 }
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436 int FEDM_XMLBase::OpenDoc(char* sFileName, char* sMode, bool bAddCopyright)
00437 {
00438 FEDM_CHK5(sFileName);
00439 FEDM_CHK5(sMode);
00440
00441 int iBack = 0;
00442
00443 m_file = fopen(sFileName, sMode);
00444 if(m_file == NULL)
00445 FEDM_RETURN(FEDM_ERROR_OPEN_FILE);
00446
00447 #if defined(_MSC_VER) && defined(_FEDM_TRACE) && defined(_DEBUG)
00448 TRACE("open xml doc: %s\n", sFileName);
00449 ::Sleep(0);
00450 #endif
00451
00452
00453 if(strcmp("w", sMode) == 0)
00454 {
00455 m_sXmlStream = L"<?xml version=";
00456 m_sXmlStream += L"\"";
00457 m_sXmlStream += FEDM_XML_STRING_XML_VERSION_UC;
00458 m_sXmlStream += L"\"";
00459 m_sXmlStream += L" encoding=";
00460 m_sXmlStream += L"\"";
00461 m_sXmlStream += FEDM_XML_STRING_XML_ENCODING_UC;
00462 m_sXmlStream += L"\"";
00463 m_sXmlStream += L" standalone=";
00464 m_sXmlStream += L"\"";
00465 m_sXmlStream += FEDM_XML_STRING_XML_STANDALONE_UC;
00466 m_sXmlStream += L"\"";
00467 m_sXmlStream += L"?>";
00468 m_sXmlStream += L"\n";
00469
00470 if(bAddCopyright)
00471 {
00472 m_sXmlStream += m_sXmlComment;
00473 }
00474 }
00475 else
00476 {
00477
00478 #if defined(_FEDM_WINDOWS)
00479 #if defined(_WIN32_WCE)
00480 CFileStatus status;
00481 CString strFileName = sFileName;
00482 if(CFile::GetStatus(strFileName, status))
00483 {
00484 m_sXmlStream.reserve((unsigned int)status.m_size + 1);
00485 }
00486 #else
00487 struct _stat buf;
00488 iBack = _stat(sFileName, &buf);
00489
00490 if(iBack == 0)
00491 {
00492
00493 m_sXmlStream.reserve(buf.st_size + 1);
00494 }
00495 #endif
00496 #endif
00497 #if defined(_FEDM_LINUX)
00498 struct stat buf;
00499 iBack = stat(sFileName, &buf);
00500
00501 if(iBack == 0)
00502 {
00503
00504 m_sXmlStream.reserve(buf.st_size + 1);
00505 }
00506 #endif
00507 }
00508
00509 FEDM_RETURN(FEDM_OK);
00510 }
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522 int FEDM_XMLBase::CloseDoc()
00523 {
00524 if(m_file != NULL)
00525 {
00526 #if defined(_MSC_VER) && defined(_FEDM_TRACE) && defined(_DEBUG)
00527 TRACE("close xml doc\n");
00528 ::Sleep(0);
00529 #endif
00530 fclose(m_file);
00531 }
00532
00533 m_file = NULL;
00534
00535 return FEDM_OK;
00536 }
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548 int FEDM_XMLBase::IsXmlDoc()
00549 {
00550 if(m_file == NULL)
00551 FEDM_RETURN(FEDM_ERROR_NULL_POINTER);
00552
00553 char cBuffer[17];
00554 char* cSubStr;
00555 size_t iLen = 0;
00556
00557
00558 if(fseek(m_file, 0, SEEK_SET))
00559 FEDM_RETURN(FEDM_ERROR_READ_FILE);
00560
00561
00562
00563 iLen = fread(cBuffer, sizeof(char), 16, m_file);
00564 if(iLen < 16)
00565 FEDM_RETURN(FEDM_XML_ERROR_NO_XML_FILE);
00566
00567 cBuffer[16] = '\0';
00568
00569
00570 cSubStr = strstr(cBuffer, "<?xml");
00571 if(cSubStr == NULL)
00572 FEDM_RETURN(FEDM_XML_ERROR_NO_XML_FILE);
00573
00574 cSubStr[5] = '\0';
00575
00576 if(strcmp("<?xml", cSubStr) == 0)
00577 FEDM_RETURN(FEDM_OK);
00578
00579
00580 FEDM_RETURN(FEDM_XML_ERROR_NO_XML_FILE);
00581 }
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593 int FEDM_XMLBase::HasOBIDTag()
00594 {
00595 if(m_file == NULL)
00596 FEDM_RETURN(FEDM_ERROR_NULL_POINTER);
00597
00598 char cBuffer[128];
00599 char* cSubStr = NULL;
00600 size_t iLen = 0;
00601
00602
00603 if(fseek(m_file, 0, SEEK_SET))
00604 FEDM_RETURN(FEDM_ERROR_READ_FILE);
00605
00606
00607 iLen = fread(cBuffer, sizeof(char), 128, m_file);
00608 while(iLen > 0)
00609 {
00610 cSubStr = strstr(cBuffer, "<OBID>");
00611 if(cSubStr != NULL)
00612 break;
00613
00614 iLen = fread(cBuffer, sizeof(char), 128, m_file);
00615 }
00616
00617 if(cSubStr == NULL)
00618 FEDM_RETURN(FEDM_XML_ERROR_NO_OBID_TAG);
00619
00620 cSubStr[6] = '\0';
00621
00622 if(strcmp("<OBID>", cSubStr) == 0)
00623 FEDM_RETURN(FEDM_OK);
00624
00625
00626 FEDM_RETURN(FEDM_XML_ERROR_NO_OBID_TAG);
00627 }
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639 int FEDM_XMLBase::HasRootTag(const char* szOemRootTag)
00640 {
00641 if(m_file == NULL)
00642 FEDM_RETURN(FEDM_ERROR_NULL_POINTER);
00643
00644 char cBuffer[128];
00645 char* cSubStr = NULL;
00646 size_t iLen = 0;
00647 string sRootTag = "<";
00648 sRootTag += szOemRootTag;
00649 sRootTag += ">";
00650
00651
00652 if(fseek(m_file, 0, SEEK_SET))
00653 FEDM_RETURN(FEDM_ERROR_READ_FILE);
00654
00655
00656 iLen = fread(cBuffer, sizeof(char), 128, m_file);
00657 while(iLen > 0)
00658 {
00659 cSubStr = strstr(cBuffer, sRootTag.c_str());
00660 if(cSubStr != NULL)
00661 break;
00662
00663 iLen = fread(cBuffer, sizeof(char), 128, m_file);
00664 }
00665
00666 if(cSubStr == NULL)
00667 FEDM_RETURN(FEDM_XML_ERROR_TAG_NOT_FOUND);
00668
00669 cSubStr[sRootTag.size()] = '\0';
00670
00671 if(strcmp(sRootTag.c_str(), cSubStr) == 0)
00672 FEDM_RETURN(FEDM_OK);
00673
00674
00675 FEDM_RETURN(FEDM_XML_ERROR_TAG_NOT_FOUND);
00676 }
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688 int FEDM_XMLBase::ReadDoc()
00689 {
00690 bool bFirstRead = true;
00691 char cBuffer[101];
00692 wchar_t wcBuffer[101];
00693 size_t iRead = 0;
00694
00695 if(m_file == NULL)
00696 FEDM_RETURN(FEDM_ERROR_NULL_POINTER);
00697
00698
00699 if(fseek(m_file, 0, SEEK_SET))
00700 FEDM_RETURN(FEDM_ERROR_READ_FILE);
00701
00702
00703 m_sXmlStream.erase();
00704
00705
00706 while(! feof(m_file))
00707 {
00708 iRead = fread(cBuffer, sizeof(char), 100, m_file);
00709 if(iRead <= 0)
00710 {
00711 int err = ferror(m_file);
00712 if(err)
00713 {
00714 FEDM_RETURN(FEDM_ERROR_READ_FILE);
00715 }
00716 else
00717 {
00718 }
00719 }
00720
00721 cBuffer[iRead] = '\0';
00722 #if defined(__BORLANDC__) || defined(_MSC_VER)
00723 swprintf(wcBuffer, L"%S", cBuffer);
00724 #else
00725 swprintf(wcBuffer, 101, L"%s", cBuffer);
00726 #endif
00727 m_sXmlStream += wcBuffer;
00728
00729 if(bFirstRead)
00730 {
00731 bFirstRead = false;
00732 ReadXmlHeader(cBuffer);
00733 }
00734 }
00735
00736
00737 DeleteXmlTree();
00738
00739 FEDM_RETURN(FEDM_OK);
00740 }
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752 int FEDM_XMLBase::LoadStream(wstring sXmlStream)
00753 {
00754
00755 m_sXmlStream.erase();
00756
00757 m_sXmlStream = sXmlStream;
00758
00759 FEDM_RETURN(FEDM_OK);
00760 }
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772 int FEDM_XMLBase::WriteDoc()
00773 {
00774 if(m_file == NULL)
00775 FEDM_RETURN(FEDM_ERROR_NULL_POINTER);
00776
00777 FEDM_XML_TREE_LIST_ITOR Itor;
00778
00779
00780 for(Itor = m_XmlTree.begin(); Itor != m_XmlTree.end(); Itor++)
00781 {
00782 if((*Itor) != NULL)
00783 WriteTagStream(*Itor);
00784 }
00785
00786
00787 int iBack = fwprintf(m_file, m_sXmlStream.c_str());
00788
00789
00790 m_sXmlStream.erase();
00791
00792
00793 DeleteXmlTree();
00794
00795 if(iBack <= 0)
00796 FEDM_RETURN(FEDM_ERROR_WRITE_FILE);
00797
00798 FEDM_RETURN(FEDM_OK);
00799 }
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811 wstring FEDM_XMLBase::WriteStream()
00812 {
00813 FEDM_XML_TREE_LIST_ITOR Itor;
00814
00815
00816 m_sXmlStream.erase();
00817
00818
00819 for(Itor = m_XmlTree.begin(); Itor != m_XmlTree.end(); Itor++)
00820 {
00821 if((*Itor) != NULL)
00822 WriteTagStream(*Itor);
00823 }
00824
00825
00826
00827 DeleteXmlTree();
00828
00829 return m_sXmlStream;
00830 }
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843 int FEDM_XMLBase::WriteTagStream(FEDM_XML_TREEITEM* item)
00844 {
00845 FEDM_CHK5(item);
00846
00847 int iPos = 0;
00848 wstring wStr;
00849 FEDM_XML_TREE_LIST_ITOR TreeItor;
00850 FEDM_XML_ATTRIBUTE_LIST_ITOR AttrItor;
00851
00852
00853 m_sXmlStream += L"<";
00854 m_sXmlStream += item->sTag;
00855
00856 #if defined(_MSC_VER) && defined(_FEDM_TRACE) && defined(_DEBUG)
00857 char buf[256];
00858 swscanf(item->sTag.c_str(), L"%S", buf);
00859 TRACE("writing tag: %s\n", buf);
00860 ::Sleep(0);
00861 #endif
00862
00863
00864 for(AttrItor = item->aAttribList.begin(); AttrItor != item->aAttribList.end(); AttrItor++)
00865 {
00866 m_sXmlStream += L" ";
00867 m_sXmlStream += (*AttrItor)->sAttribute;
00868 m_sXmlStream += L"=";
00869 m_sXmlStream += L"\"";
00870 m_sXmlStream += (*AttrItor)->sValue;
00871 m_sXmlStream += L"\"";
00872
00873 #if defined(_MSC_VER) && defined(_FEDM_TRACE) && defined(_DEBUG)
00874 char buf[256];
00875 sprintf(buf, "%S=%S", (*AttrItor)->sAttribute.c_str(), (*AttrItor)->sValue.c_str());
00876 TRACE("writing attribute: %s\n", buf);
00877 ::Sleep(0);
00878 #endif
00879 }
00880
00881
00882 if(item->bEmpty)
00883 m_sXmlStream += L"/";
00884 m_sXmlStream += L">";
00885 if(item->bNewLine)
00886 m_sXmlStream += L"\n";
00887
00888
00889 for(TreeItor = item->aChildList.begin(); TreeItor != item->aChildList.end(); TreeItor++)
00890 {
00891 if((*TreeItor) != NULL)
00892 WriteTagStream(*TreeItor);
00893 }
00894
00895
00896 if(! item->bEmpty)
00897 {
00898
00899 iPos = (int)item->sValue.find(L"&");
00900 while(iPos >= 0)
00901 {
00902 item->sValue.replace(iPos, 1, L"&");
00903 iPos = (int)item->sValue.find(L"&", iPos+1);
00904 }
00905
00906 iPos = (int)item->sValue.find(L"<");
00907 while(iPos >= 0)
00908 {
00909 item->sValue.replace(iPos, 1, L"<");
00910 iPos = (int)item->sValue.find(L"<", iPos+1);
00911 }
00912
00913 iPos = (int)item->sValue.find(L">");
00914 while(iPos >= 0)
00915 {
00916 item->sValue.replace(iPos, 1, L">");
00917 iPos = (int)item->sValue.find(L">", iPos+1);
00918 }
00919
00920 iPos = (int)item->sValue.find(L"\"");
00921 while(iPos >= 0)
00922 {
00923 item->sValue.replace(iPos, 1, L""");
00924 iPos = (int)item->sValue.find(L"\"", iPos+1);
00925 }
00926
00927 iPos = (int)item->sValue.find(L"'");
00928 while(iPos >= 0)
00929 {
00930 item->sValue.replace(iPos, 1, L"'");
00931 iPos = (int)item->sValue.find(L"'", iPos+1);
00932 }
00933
00934 m_sXmlStream += item->sValue;
00935 }
00936
00937
00938 if(! item->bEmpty)
00939 {
00940 m_sXmlStream += L"</";
00941 m_sXmlStream += item->sTag;
00942 m_sXmlStream += L">";
00943 }
00944
00945 m_sXmlStream += L"\n";
00946
00947 FEDM_RETURN(FEDM_OK);
00948 }
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968 FEDM_XML_TREEITEM* FEDM_XMLBase::BuildTag(wstring sTag, bool bNewLine, bool bEmpty)
00969 {
00970 if(sTag.empty())
00971 {
00972 SetLastError(FEDM_ERROR_PARAMETER);
00973 return NULL;
00974 }
00975
00976 FEDM_XML_TREEITEM* item = new FEDM_XML_TREEITEM;
00977
00978 if(item == NULL)
00979 {
00980 SetLastError(FEDM_ERROR_NO_MORE_MEM);
00981 return NULL;
00982 }
00983
00984 item->sTag = sTag;
00985 item->bNewLine = bNewLine;
00986 item->bEmpty = bEmpty;
00987
00988 #if defined(_MSC_VER) && defined(_FEDM_TRACE) && defined(_DEBUG)
00989 char buf[256];
00990 swscanf(sTag.c_str(), L"%S", buf);
00991 TRACE("build tag: %s\n", buf);
00992 ::Sleep(0);
00993 #endif
00994
00995 SetLastError(FEDM_OK);
00996 return item;
00997 }
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010 int FEDM_XMLBase::AddTagValue(FEDM_XML_TREEITEM* item, wstring sValue)
01011 {
01012 FEDM_CHK5(item);
01013
01014 if(sValue.empty())
01015 {
01016 item->bEmpty = true;
01017 FEDM_RETURN(FEDM_OK);
01018 }
01019
01020 item->sValue = sValue;
01021 item->bEmpty = false;
01022 #if defined(_MSC_VER) && defined(_FEDM_TRACE) && defined(_DEBUG)
01023 char buf[256];
01024 sprintf(buf, "%S", item->sValue.c_str());
01025 TRACE("add tag value: %s\n", buf);
01026 ::Sleep(0);
01027 #endif
01028
01029 FEDM_RETURN(FEDM_OK);
01030 }
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044 int FEDM_XMLBase::AddTagAttrib(FEDM_XML_TREEITEM* item, wstring sAttrib, wstring sValue)
01045 {
01046 FEDM_CHK5(item);
01047
01048 if(sAttrib.empty())
01049 FEDM_RETURN(FEDM_ERROR_PARAMETER);
01050
01051 FEDM_XML_ATTRIBUTE* pAttribute = new FEDM_XML_ATTRIBUTE;
01052
01053 if(pAttribute == NULL)
01054 FEDM_RETURN(FEDM_ERROR_NO_MORE_MEM);
01055
01056 pAttribute->sAttribute = sAttrib;
01057 pAttribute->sValue = sValue;
01058
01059 item->aAttribList.push_back(pAttribute);
01060 #if defined(_MSC_VER) && defined(_FEDM_TRACE) && defined(_DEBUG)
01061 char buf[256];
01062 sprintf(buf, "%S=%S", pAttribute->sAttribute.c_str(), pAttribute->sValue.c_str());
01063 TRACE("add tag attribute: %s\n", buf);
01064 ::Sleep(0);
01065 #endif
01066
01067 FEDM_RETURN(FEDM_OK);
01068 }
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081 int FEDM_XMLBase::AddTagItem(FEDM_XML_TREEITEM* pParent, FEDM_XML_TREEITEM* pChild)
01082 {
01083 FEDM_CHK5(pChild);
01084
01085 #if defined(_MSC_VER) && defined(_FEDM_TRACE) && defined(_DEBUG)
01086 char buf[256];
01087 #endif
01088
01089 if(pParent == NULL)
01090 {
01091
01092
01093 if(m_XmlTree.empty())
01094 {
01095 m_XmlTree.push_back(pChild);
01096 #if defined(_MSC_VER) && defined(_FEDM_TRACE) && defined(_DEBUG)
01097 sprintf(buf, "%S", pChild->sTag.c_str());
01098 TRACE("add root tag: %s\n", buf);
01099 ::Sleep(0);
01100 #endif
01101 }
01102 else
01103 FEDM_RETURN(FEDM_ERROR_PARAMETER);
01104 }
01105 else
01106 {
01107
01108 pParent->aChildList.push_back(pChild);
01109 #if defined(_MSC_VER) && defined(_FEDM_TRACE) && defined(_DEBUG)
01110 sprintf(buf, "%S", pChild->sTag.c_str());
01111 TRACE("add child tag: %s\n", buf);
01112 ::Sleep(0);
01113 #endif
01114 }
01115
01116 FEDM_RETURN(FEDM_OK);
01117 }
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137 FEDM_XML_TREEITEM* FEDM_XMLBase::FindTag(wstring sTag,
01138 bool bInside,
01139 bool bSavePos,
01140 bool bNext)
01141 {
01142 if(m_sXmlStream.empty())
01143 {
01144 SetLastError(FEDM_ERROR_NO_DATA);
01145 return NULL;
01146 }
01147
01148
01149 static int iPosStart = 0;
01150 static int iPosEnd = 0;
01151 static int iTagLen = 0;
01152
01153 bool bAttrib = true;
01154 int iPos0 = 0;
01155 int iPos1 = 0;
01156 int iPos2 = 0;
01157 int iPos3 = 0;
01158 int iPos4 = 0;
01159 int iPos5 = 0;
01160 wstring s0, s1, s2, s3, s4;
01161 FEDM_XML_TREEITEM* item = NULL;
01162 FEDM_XML_ATTRIBUTE* attrib = NULL;
01163
01164 #if defined(_MSC_VER) && defined(_FEDM_TRACE) && defined(_DEBUG)
01165 char buf[256];
01166 #endif
01167
01168
01169 s0 = L"<";
01170 s0 += sTag;
01171 s0 += L"/>";
01172
01173
01174 s1 = L"<";
01175 s1 += sTag;
01176
01177
01178 s2 = L"</";
01179 s2 += sTag;
01180 s2 += L">";
01181
01182
01183 item = new FEDM_XML_TREEITEM;
01184 if(item == NULL)
01185 {
01186 SetLastError(FEDM_ERROR_NO_MORE_MEM);
01187 return NULL;
01188 }
01189 item->sTag = sTag;
01190 #if defined(_MSC_VER) && defined(_FEDM_TRACE) && defined(_DEBUG)
01191 sprintf(buf, "%S", sTag.c_str());
01192 TRACE("new tag: %s\n", buf);
01193 ::Sleep(0);
01194 #endif
01195
01196
01197 if(bNext)
01198 {
01199 iPosStart += iTagLen;
01200 }
01201
01202
01203 if(bInside)
01204 {
01205
01206 iPos0 = (int)m_sXmlStream.find(s0.c_str(), iPosStart);
01207 if(iPos0 > 0 && iPos0 > iPosStart && iPos0 < iPosEnd)
01208 {
01209 item->bEmpty = true;
01210 SetLastError(FEDM_OK);
01211 return item;
01212 }
01213
01214
01215 iPos1 = (int)m_sXmlStream.find(s1.c_str(), iPosStart);
01216
01217 if(iPos1 < 0 || iPos1 < iPosStart || iPos1 > iPosEnd)
01218 {
01219 SetLastError(FEDM_XML_ERROR_TAG_NOT_FOUND);
01220 DeleteTagItem(item);
01221 return NULL;
01222 }
01223
01224
01225 iPos2 = (int)m_sXmlStream.find(s2.c_str(), iPos1);
01226 if(iPos2 < 0 || iPos2 < iPosStart || iPos2 > iPosEnd)
01227 {
01228
01229
01230 s2 = L"/>";
01231
01232 iPos2 = (int)m_sXmlStream.find(s2.c_str(), iPos1);
01233
01234 if(iPos2 < 0 || iPos2 < iPosStart || iPos2 > iPosEnd)
01235 {
01236 SetLastError(FEDM_XML_ERROR_TAG_NOT_FOUND);
01237 DeleteTagItem(item);
01238 return NULL;
01239 }
01240
01241
01242 iPos3 = (int)m_sXmlStream.find(L"<", iPos1, iPos2-iPos1);
01243 if(iPos3 > 0 && (iPos3 < iPos1 || iPos3 > iPos2))
01244 {
01245 SetLastError(FEDM_XML_ERROR_DOC_NOT_WELL_FORMED);
01246 DeleteTagItem(item);
01247 return NULL;
01248 }
01249
01250 item->bEmpty = true;
01251
01252
01253 if(iPos2 - iPos1 - s1.size() == 0)
01254 {
01255 SetLastError(FEDM_OK);
01256 return item;
01257 }
01258 }
01259 else
01260 {
01261 item->bEmpty = false;
01262 }
01263 }
01264 else
01265 {
01266
01267 if(bNext)
01268 {
01269
01270 iPos0 = (int)m_sXmlStream.find(s0.c_str(), iPosStart);
01271 if(iPos0 > 0 && iPos0 > iPosStart && iPos0 < iPosEnd)
01272 {
01273 item->bEmpty = true;
01274 SetLastError(FEDM_OK);
01275 return item;
01276 }
01277
01278
01279 iPos1 = (int)m_sXmlStream.find(s1.c_str(), iPosStart);
01280 }
01281 else
01282 {
01283
01284 iPos0 = (int)m_sXmlStream.find(s0.c_str());
01285 if(iPos0 > 0)
01286 {
01287 item->bEmpty = true;
01288 SetLastError(FEDM_OK);
01289 return item;
01290 }
01291
01292
01293 iPos1 = (int)m_sXmlStream.find(s1.c_str());
01294 }
01295
01296 if(iPos1 < 0 || iPos1 == (int)m_sXmlStream.size())
01297 {
01298 SetLastError(FEDM_XML_ERROR_TAG_NOT_FOUND);
01299 DeleteTagItem(item);
01300 return NULL;
01301 }
01302
01303
01304 iPos2 = (int)m_sXmlStream.find(s2, iPos1);
01305 if(iPos2 < 0 || iPos2 == (int)m_sXmlStream.size())
01306 {
01307
01308
01309 s2 = L"/>";
01310
01311 iPos2 = (int)m_sXmlStream.find(s2, iPos1);
01312
01313 if(iPos2 < 0 || iPos2 == (int)m_sXmlStream.size())
01314 {
01315 SetLastError(FEDM_XML_ERROR_TAG_NOT_FOUND);
01316 DeleteTagItem(item);
01317 return NULL;
01318 }
01319
01320
01321 iPos3 = (int)m_sXmlStream.find(L"<", iPos1, iPos2-iPos1);
01322 if(iPos3 > 0 && (iPos3 < iPos1 || iPos3 > iPos2))
01323 {
01324 SetLastError(FEDM_XML_ERROR_DOC_NOT_WELL_FORMED);
01325 DeleteTagItem(item);
01326 return NULL;
01327 }
01328
01329 item->bEmpty = true;
01330
01331
01332 if(iPos2 - iPos1 - s1.size() == 0)
01333 {
01334 SetLastError(FEDM_OK);
01335 return item;
01336 }
01337 }
01338 else
01339 {
01340 item->bEmpty = false;
01341 }
01342 }
01343
01344
01345 s3 = m_sXmlStream.at(iPos1 + s1.size());
01346 if(s3 == L" ")
01347 {
01348
01349 iPos3 = (int)m_sXmlStream.find_first_of(L">", iPos1);
01350
01351
01352 s3 = m_sXmlStream.substr(iPos1 + s1.size() + 1, iPos3 - iPos1 - s1.size() - 1);
01353
01354
01355 bAttrib = true;
01356 iPos4 = 0;
01357 while(iPos4 < (int)s3.size())
01358 {
01359 if(bAttrib)
01360 {
01361 iPos5 = (int)s3.find_first_of(L"=", iPos4);
01362 if(iPos5 < 0 || iPos5 >= (int)s3.size())
01363 {
01364 SetLastError(FEDM_XML_ERROR_DOC_NOT_WELL_FORMED);
01365 break;
01366 }
01367
01368
01369 attrib = new FEDM_XML_ATTRIBUTE;
01370 if(attrib == NULL)
01371 {
01372 SetLastError(FEDM_ERROR_NO_MORE_MEM);
01373 break;
01374 }
01375
01376
01377 if(s3[iPos4] == L' ')
01378 attrib->sAttribute = s3.substr(iPos4+1, iPos5-iPos4-1);
01379 else
01380 attrib->sAttribute = s3.substr(iPos4, iPos5-iPos4);
01381
01382 bAttrib = false;
01383 iPos4 = iPos5 + 1;
01384 }
01385 else
01386 {
01387 if(s3[iPos4] == L'\"')
01388 {
01389 iPos4++;
01390 iPos5 = (int)s3.find_first_of(L"\"", iPos4);
01391 if(iPos5 <= 0 || iPos5 >= (int)s3.size())
01392 {
01393 SetLastError(FEDM_XML_ERROR_DOC_NOT_WELL_FORMED);
01394 break;
01395 }
01396
01397
01398 attrib->sValue = s3.substr(iPos4, iPos5-iPos4);;
01399
01400
01401 item->aAttribList.push_back(attrib);
01402 bAttrib = true;
01403 iPos4 = iPos5 + 1;
01404 #if defined(_MSC_VER) && defined(_FEDM_TRACE) && defined(_DEBUG)
01405 sprintf(buf, "%S=%S", attrib->sAttribute.c_str(), attrib->sValue.c_str());
01406 TRACE("new tag attribute: %s\n", buf);
01407 ::Sleep(0);
01408 #endif
01409 }
01410 else
01411 {
01412 delete attrib;
01413 SetLastError(FEDM_XML_ERROR_DOC_NOT_WELL_FORMED);
01414 break;
01415 }
01416 }
01417 }
01418
01419 if(!item->bEmpty)
01420 {
01421
01422 s3 = m_sXmlStream.at(iPos3 + 1);
01423 if(s3 == L"\n")
01424 {
01425 item->bNewLine = true;
01426 }
01427 else if(s3 != L"<")
01428 {
01429 item->bNewLine = false;
01430 if(iPos2 - iPos3 - 1 > 0)
01431 {
01432
01433 item->sValue = m_sXmlStream.substr(iPos3 + 1, iPos2 - iPos3 - 1);
01434
01435
01436 int iPos = (int)item->sValue.find(L"&");
01437 while(iPos >= 0)
01438 {
01439 item->sValue.erase(iPos+1, 4);
01440 iPos = (int)item->sValue.find(L"&", iPos+1);
01441 }
01442
01443 iPos = (int)item->sValue.find(L"<");
01444 while(iPos >= 0)
01445 {
01446 item->sValue.replace(iPos, 1, L"<");
01447 iPos = (int)item->sValue.find(L"<", iPos+1);
01448 }
01449
01450 iPos = (int)item->sValue.find(L">");
01451 while(iPos >= 0)
01452 {
01453 item->sValue.replace(iPos, 1, L">");
01454 iPos = (int)item->sValue.find(L">", iPos+1);
01455 }
01456
01457 iPos = (int)item->sValue.find(L""");
01458 while(iPos >= 0)
01459 {
01460 item->sValue.replace(iPos, 1, L"\"");
01461 iPos = (int)item->sValue.find(L""", iPos+1);
01462 }
01463
01464 iPos = (int)item->sValue.find(L"'");
01465 while(iPos >= 0)
01466 {
01467 item->sValue.replace(iPos, 1, L"'");
01468 iPos = (int)item->sValue.find(L"'", iPos+1);
01469 }
01470 }
01471 }
01472 }
01473 }
01474 else if(s3 == L">")
01475 {
01476
01477 s3 = m_sXmlStream.at(iPos1 + s1.size() + 1);
01478 if(s3 == L"\n")
01479 {
01480 item->bNewLine = true;
01481 }
01482 else if(s3 != L"<")
01483 {
01484 item->bNewLine = false;
01485 if(iPos2 - iPos1 - s1.size() - 1 > 0)
01486 {
01487
01488 item->sValue = m_sXmlStream.substr(iPos1 + s1.size() + 1, iPos2 - iPos1 - s1.size() - 1);
01489
01490
01491 int iPos = (int)item->sValue.find(L"&");
01492 while(iPos >= 0)
01493 {
01494 item->sValue.erase(iPos+1, 4);
01495 iPos = (int)item->sValue.find(L"&", iPos+1);
01496 }
01497
01498 iPos = (int)item->sValue.find(L"<");
01499 while(iPos >= 0)
01500 {
01501 item->sValue.replace(iPos, 1, L"<");
01502 iPos = (int)item->sValue.find(L"<", iPos+1);
01503 }
01504
01505 iPos = (int)item->sValue.find(L">");
01506 while(iPos >= 0)
01507 {
01508 item->sValue.replace(iPos, 1, L">");
01509 iPos = (int)item->sValue.find(L">", iPos+1);
01510 }
01511
01512 iPos = (int)item->sValue.find(L""");
01513 while(iPos >= 0)
01514 {
01515 item->sValue.replace(iPos, 1, L"\"");
01516 iPos = (int)item->sValue.find(L""", iPos+1);
01517 }
01518
01519 iPos = (int)item->sValue.find(L"'");
01520 while(iPos >= 0)
01521 {
01522 item->sValue.replace(iPos, 1, L"'");
01523 iPos = (int)item->sValue.find(L"'", iPos+1);
01524 }
01525 }
01526 }
01527 }
01528 else if(s3 == L"/")
01529 {
01530
01531 item->bEmpty = true;
01532 }
01533 else
01534 {
01535 SetLastError(FEDM_XML_ERROR_DOC_NOT_WELL_FORMED);
01536 DeleteTagItem(item);
01537 return NULL;
01538 }
01539
01540 if(bSavePos)
01541 {
01542 if(iPos1 > 0 && iPos2-iPos1 > 0)
01543 {
01544 iPosStart = iPos1;
01545 iPosEnd = iPos2;
01546 iTagLen = (int)s1.size();
01547 }
01548 }
01549
01550 SetLastError(FEDM_OK);
01551 return item;
01552 }
01553
01554
01555
01556
01557
01558
01559
01560
01561
01562
01563
01564
01565
01566
01567
01568
01569 FEDM_XML_TREEITEM* FEDM_XMLBase::FindTag( wstring sTag,
01570 unsigned int uiTagLevel,
01571 bool bNext)
01572 {
01573 if(m_sXmlStream.empty())
01574 {
01575 SetLastError(FEDM_ERROR_NO_DATA);
01576 return NULL;
01577 }
01578
01579 if(uiTagLevel >= 15)
01580 {
01581 SetLastError(FEDM_ERROR_ARRAY_BOUNDARY);
01582 return NULL;
01583 }
01584
01585
01586 static int iPosStart[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
01587 static int iPosEnd[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
01588
01589 bool bRoot = false;
01590 bool bAttrib = true;
01591 int iPos0 = 0;
01592 int iPos1 = 0;
01593 int iPos2 = 0;
01594 int iPos3 = 0;
01595 int iPos4 = 0;
01596 int iPos5 = 0;
01597
01598 unsigned int uiLevel = 0;
01599 unsigned int i = 0;
01600
01601 int iNextPosStart = 0;
01602 int iNextPos = 0;
01603
01604 wstring s0, s1, s2, s3, s4;
01605 FEDM_XML_TREEITEM* item = NULL;
01606 FEDM_XML_ATTRIBUTE* attrib = NULL;
01607
01608
01609 s0 = L"<";
01610 s0 += sTag;
01611 s0 += L"/>";
01612
01613
01614 s1 = L"<";
01615 s1 += sTag;
01616
01617
01618 s2 = L"</";
01619 s2 += sTag;
01620 s2 += L">";
01621
01622
01623 item = new FEDM_XML_TREEITEM;
01624 if(item == NULL)
01625 {
01626 SetLastError(FEDM_ERROR_NO_MORE_MEM);
01627 return NULL;
01628 }
01629 item->sTag = sTag;
01630
01631 if(uiTagLevel == 0)
01632 {
01633
01634 bRoot = true;
01635 for(i=0; i<15; i++)
01636 {
01637 iPosStart[i] = 0;
01638 iPosEnd[i] = 0;
01639 }
01640 }
01641
01642
01643 if(bNext && !bRoot)
01644 {
01645 for(i=uiTagLevel; i<15; i++)
01646 {
01647 if(iPosStart[i] > 0)
01648 {
01649 iPosStart[i] = iPosEnd[uiTagLevel] + 1;
01650 }
01651 }
01652
01653 if(iPosStart[uiTagLevel] > iPosEnd[uiTagLevel])
01654 iPosEnd[uiTagLevel] = iPosEnd[uiTagLevel-1];
01655
01656
01657 iNextPosStart = iPosStart[uiTagLevel-1] + 1;
01658 if(iPosStart[uiTagLevel] > iPosStart[uiTagLevel-1])
01659 iNextPosStart = iPosStart[uiTagLevel];
01660
01661 }
01662 else
01663 {
01664 if(uiTagLevel == 0)
01665 iNextPosStart = iPosStart[0];
01666 else
01667 iNextPosStart = iPosStart[uiTagLevel-1];
01668 }
01669
01670 if(uiTagLevel == 0)
01671 {
01672 uiTagLevel++;
01673 iPosEnd[0] = (int)m_sXmlStream.size();
01674 }
01675
01676
01677
01678 iPos0 = (int)m_sXmlStream.find(s0.c_str(), iNextPosStart);
01679 if(iPos0 > 0 && iPos0 > iNextPosStart && iPos0 < iPosEnd[uiTagLevel-1])
01680 {
01681 item->bEmpty = true;
01682 iPosEnd[uiTagLevel] = iPos0;
01683 SetLastError(FEDM_OK);
01684 return item;
01685 }
01686
01687
01688 iPos1 = (int)m_sXmlStream.find(s1.c_str(), iNextPosStart);
01689
01690 if(iPos1 < 0 || iPos1 < iNextPosStart || iPos1 > iPosEnd[uiTagLevel-1])
01691 {
01692 iPosEnd[uiTagLevel] = iPosStart[uiTagLevel]++;
01693 SetLastError(FEDM_XML_ERROR_TAG_NOT_FOUND);
01694 DeleteTagItem(item);
01695 return NULL;
01696 }
01697
01698
01699 iPos2 = (int)m_sXmlStream.find(L"/>", iPos1+1);
01700 if(iPos2 > iPos1)
01701 {
01702 iPos3 = (int)m_sXmlStream.find(L"<", iPos1+1);
01703 if(iPos3 > iPos2)
01704 {
01705 item->bEmpty = true;
01706 iPosEnd[uiTagLevel] = iPos2;
01707
01708
01709 s3 = m_sXmlStream.at(iPos1 + s1.size());
01710 if(s3 == L" ")
01711 {
01712
01713 iPos3 = (int)m_sXmlStream.find_first_of(L">", iPos1);
01714
01715
01716 s3 = m_sXmlStream.substr(iPos1 + s1.size() + 1, iPos3 - iPos1 - s1.size() - 1);
01717
01718
01719 bAttrib = true;
01720 iPos4 = 0;
01721 while(iPos4 < (int)s3.size())
01722 {
01723 if(bAttrib)
01724 {
01725 iPos5 = (int)s3.find_first_of(L"=", iPos4);
01726 if(iPos5 < 0 || iPos5 >= (int)s3.size())
01727 {
01728 SetLastError(FEDM_XML_ERROR_DOC_NOT_WELL_FORMED);
01729 break;
01730 }
01731
01732
01733 attrib = new FEDM_XML_ATTRIBUTE;
01734 if(attrib == NULL)
01735 {
01736 SetLastError(FEDM_XML_ERROR_DOC_NOT_WELL_FORMED);
01737 break;
01738 }
01739
01740
01741 if(s3[iPos4] == L' ')
01742 attrib->sAttribute = s3.substr(iPos4+1, iPos5-iPos4-1);
01743 else
01744 attrib->sAttribute = s3.substr(iPos4, iPos5-iPos4);
01745
01746 bAttrib = false;
01747 iPos4 = iPos5 + 1;
01748 }
01749 else
01750 {
01751 if(s3[iPos4] == L'\"')
01752 {
01753 iPos4++;
01754 iPos5 = (int)s3.find_first_of(L"\"", iPos4);
01755 if(iPos5 <= 0 || iPos5 >= (int)s3.size())
01756 {
01757 SetLastError(FEDM_XML_ERROR_DOC_NOT_WELL_FORMED);
01758 break;
01759 }
01760
01761
01762 attrib->sValue = s3.substr(iPos4, iPos5-iPos4);;
01763
01764
01765 item->aAttribList.push_back(attrib);
01766 bAttrib = true;
01767 iPos4 = iPos5 + 1;
01768 }
01769 else
01770 {
01771 delete attrib;
01772 SetLastError(FEDM_XML_ERROR_DOC_NOT_WELL_FORMED);
01773 break;
01774 }
01775 }
01776 }
01777 }
01778
01779 SetLastError(FEDM_OK);
01780 return item;
01781 }
01782 }
01783
01784
01785 iPos2 = (int)m_sXmlStream.find(s2.c_str(), iPos1);
01786
01787 if(iPos2 > 0)
01788 {
01789
01790 s3 = L"<";
01791 iPos3 = (int)m_sXmlStream.find(s3.c_str(), iPos1 + 1);
01792 if(iPos3 > 0 && iPos3 < iPos2)
01793 {
01794
01795
01796 iNextPos = iPos1 + (int)s1.size() + 1;
01797 uiLevel = uiTagLevel;
01798 while(iPos2 > 0 && iPos2 < iPosEnd[0])
01799 {
01800
01801 iPos3 = (int)m_sXmlStream.find(s0.c_str(), iNextPos);
01802 if(iPos3 > 0 && iPos3 < iPos2)
01803 iNextPos = iPos3 + (int)s0.size();
01804
01805
01806 iPos3 = (int)m_sXmlStream.find(s1.c_str(), iNextPos);
01807 if(iPos3 > 0 && iPos3 < iPos2)
01808 {
01809
01810 s3 = m_sXmlStream.at(iPos3 + s1.size());
01811 if(s3 != L" " && s3 != L"/" && s3 != L">")
01812 {
01813 if(uiLevel == uiTagLevel)
01814 break;
01815 }
01816
01817
01818 uiLevel++;
01819 iNextPos = iPos3 + (int)s1.size();
01820 }
01821 else
01822 {
01823 if(uiLevel == uiTagLevel)
01824 break;
01825 }
01826
01827
01828 iPos2 = (int)m_sXmlStream.find(s2.c_str(), iPos2 + s2.size());
01829 if(iPos2 > 0 && iPos2 < iPosEnd[uiTagLevel-1])
01830 {
01831
01832 uiLevel--;
01833 }
01834 else
01835 {
01836 if(uiLevel == uiTagLevel)
01837 break;
01838 }
01839
01840 }
01841
01842 }
01843
01844 }
01845
01846 if(iPos2 < 0 || iPos2 < iNextPosStart || iPos2 > iPosEnd[uiTagLevel-1])
01847 {
01848
01849
01850 s2 = L"/>";
01851
01852 iPos2 = (int)m_sXmlStream.find(s2.c_str(), iPos1);
01853
01854 if(iPos2 < 0 || iPos2 < iNextPosStart || iPos2 > iPosEnd[uiTagLevel-1])
01855 {
01856 iPosEnd[uiTagLevel] = iPosStart[uiTagLevel] + 1;
01857 SetLastError(FEDM_XML_ERROR_TAG_NOT_FOUND);
01858 DeleteTagItem(item);
01859 return NULL;
01860 }
01861
01862
01863 iPos3 = (int)m_sXmlStream.find(L"<", iPos1, iPos2-iPos1);
01864 if(iPos3 > 0 && (iPos3 < iPos1 || iPos3 > iPos2))
01865 {
01866 SetLastError(FEDM_XML_ERROR_DOC_NOT_WELL_FORMED);
01867 DeleteTagItem(item);
01868 return NULL;
01869 }
01870
01871 item->bEmpty = true;
01872
01873
01874 if(iPos2 - iPos1 - s1.size() == 0)
01875 {
01876 iPosEnd[uiTagLevel] = iPos2;
01877 SetLastError(FEDM_OK);
01878 return item;
01879 }
01880 }
01881 else
01882 {
01883 item->bEmpty = false;
01884 }
01885
01886
01887 s3 = m_sXmlStream.at(iPos1 + s1.size());
01888 if(s3 == L" ")
01889 {
01890
01891 iPos3 = (int)m_sXmlStream.find_first_of(L">", iPos1);
01892
01893
01894 s3 = m_sXmlStream.substr(iPos1 + s1.size() + 1, iPos3 - iPos1 - s1.size() - 1);
01895
01896
01897 bAttrib = true;
01898 iPos4 = 0;
01899 while(iPos4 < (int)s3.size())
01900 {
01901 if(bAttrib)
01902 {
01903 iPos5 = (int)s3.find_first_of(L"=", iPos4);
01904 if(iPos5 < 0 || iPos5 >= (int)s3.size())
01905 {
01906 SetLastError(FEDM_XML_ERROR_DOC_NOT_WELL_FORMED);
01907 break;
01908 }
01909
01910
01911 attrib = new FEDM_XML_ATTRIBUTE;
01912 if(attrib == NULL)
01913 {
01914 SetLastError(FEDM_ERROR_NO_MORE_MEM);
01915 break;
01916 }
01917
01918
01919 if(s3[iPos4] == L' ')
01920 attrib->sAttribute = s3.substr(iPos4+1, iPos5-iPos4-1);
01921 else
01922 attrib->sAttribute = s3.substr(iPos4, iPos5-iPos4);
01923
01924 bAttrib = false;
01925 iPos4 = iPos5 + 1;
01926 }
01927 else
01928 {
01929 if(s3[iPos4] == L'\"')
01930 {
01931 iPos4++;
01932 iPos5 = (int)s3.find_first_of(L"\"", iPos4);
01933 if(iPos5 <= 0 || iPos5 >= (int)s3.size())
01934 {
01935 SetLastError(FEDM_XML_ERROR_DOC_NOT_WELL_FORMED);
01936 break;
01937 }
01938
01939
01940 attrib->sValue = s3.substr(iPos4, iPos5-iPos4);;
01941
01942
01943 item->aAttribList.push_back(attrib);
01944 bAttrib = true;
01945 iPos4 = iPos5 + 1;
01946 }
01947 else
01948 {
01949 delete attrib;
01950 SetLastError(FEDM_XML_ERROR_DOC_NOT_WELL_FORMED);
01951 break;
01952 }
01953 }
01954 }
01955
01956 if(!item->bEmpty)
01957 {
01958
01959 s3 = m_sXmlStream.at(iPos3 + 1);
01960 if(s3 == L"\n")
01961 {
01962 item->bNewLine = true;
01963 }
01964 else if(s3 != L"<")
01965 {
01966 item->bNewLine = false;
01967 if(iPos2 - iPos3 - 1 > 0)
01968 {
01969
01970 item->sValue = m_sXmlStream.substr(iPos3 + 1, iPos2 - iPos3 - 1);
01971
01972
01973 int iPos = (int)item->sValue.find(L"&");
01974 while(iPos >= 0)
01975 {
01976 item->sValue.erase(iPos+1, 4);
01977 iPos = (int)item->sValue.find(L"&", iPos+1);
01978 }
01979
01980 iPos = (int)item->sValue.find(L"<");
01981 while(iPos >= 0)
01982 {
01983 item->sValue.replace(iPos, 1, L"<");
01984 iPos = (int)item->sValue.find(L"<", iPos+1);
01985 }
01986
01987 iPos = (int)item->sValue.find(L">");
01988 while(iPos >= 0)
01989 {
01990 item->sValue.replace(iPos, 1, L">");
01991 iPos = (int)item->sValue.find(L">", iPos+1);
01992 }
01993
01994 iPos = (int)item->sValue.find(L""");
01995 while(iPos >= 0)
01996 {
01997 item->sValue.replace(iPos, 1, L"\"");
01998 iPos = (int)item->sValue.find(L""", iPos+1);
01999 }
02000
02001 iPos = (int)item->sValue.find(L"'");
02002 while(iPos >= 0)
02003 {
02004 item->sValue.replace(iPos, 1, L"'");
02005 iPos = (int)item->sValue.find(L"'", iPos+1);
02006 }
02007 }
02008 }
02009 }
02010 }
02011 else if(s3 == L">")
02012 {
02013
02014 s3 = m_sXmlStream.at(iPos1 + s1.size() + 1);
02015 if(s3 == L"\n")
02016 {
02017 item->bNewLine = true;
02018 }
02019 else if(s3 != L"<")
02020 {
02021 item->bNewLine = false;
02022 if(iPos2 - iPos1 - s1.size() - 1 > 0)
02023 {
02024
02025 item->sValue = m_sXmlStream.substr(iPos1 + s1.size() + 1, iPos2 - iPos1 - s1.size() - 1);
02026
02027
02028 int iPos = (int)item->sValue.find(L"&");
02029 while(iPos >= 0)
02030 {
02031 item->sValue.erase(iPos+1, 4);
02032 iPos = (int)item->sValue.find(L"&", iPos+1);
02033 }
02034
02035 iPos = (int)item->sValue.find(L"<");
02036 while(iPos >= 0)
02037 {
02038 item->sValue.replace(iPos, 1, L"<");
02039 iPos = (int)item->sValue.find(L"<", iPos+1);
02040 }
02041
02042 iPos = (int)item->sValue.find(L">");
02043 while(iPos >= 0)
02044 {
02045 item->sValue.replace(iPos, 1, L">");
02046 iPos = (int)item->sValue.find(L">", iPos+1);
02047 }
02048
02049 iPos = (int)item->sValue.find(L""");
02050 while(iPos >= 0)
02051 {
02052 item->sValue.replace(iPos, 1, L"\"");
02053 iPos = (int)item->sValue.find(L""", iPos+1);
02054 }
02055
02056 iPos = (int)item->sValue.find(L"'");
02057 while(iPos >= 0)
02058 {
02059 item->sValue.replace(iPos, 1, L"'");
02060 iPos = (int)item->sValue.find(L"'", iPos+1);
02061 }
02062 }
02063 }
02064 }
02065 else if(s3 == L"/")
02066 {
02067
02068 item->bEmpty = true;
02069 }
02070 else
02071 {
02072 SetLastError(FEDM_XML_ERROR_DOC_NOT_WELL_FORMED);
02073 DeleteTagItem(item);
02074 return NULL;
02075 }
02076
02077 if(iPos1 > 0 && iPos2-iPos1 > 0)
02078 {
02079 if(bRoot)
02080 {
02081 iPosStart[0]= iPos1;
02082 iPosEnd[0] = iPos2;
02083 }
02084 else
02085 {
02086 iPosStart[uiTagLevel] = iPos1;
02087 iPosEnd[uiTagLevel] = iPos2;
02088 }
02089 }
02090
02091 SetLastError(FEDM_OK);
02092 return item;
02093 }
02094
02095
02096
02097
02098
02099
02100
02101
02102
02103
02104
02105
02106
02107
02108
02109
02110
02111
02112
02113
02114
02115 FEDM_XML_TREEITEM* FEDM_XMLBase::FindTag(wstring sTag,
02116 wstring sAttribName,
02117 bool bInside,
02118 bool bSavePos)
02119 {
02120 bool bNext = false;
02121
02122 while(true)
02123 {
02124 FEDM_XML_TREEITEM* item = FindTag(sTag, bInside, bSavePos, bNext);
02125
02126 if(item == NULL)
02127 {
02128 SetLastError(FEDM_XML_ERROR_NO_TAG_ATTRIBUTE);
02129 return NULL;
02130 }
02131
02132 for(int i=0; i<(int)item->aAttribList.size(); ++i)
02133 {
02134 if(item->aAttribList[i]->sAttribute == sAttribName)
02135 {
02136 SetLastError(FEDM_OK);
02137 return item;
02138 }
02139 }
02140
02141 DeleteTagItem(item);
02142
02143 bNext = true;
02144 }
02145
02146 SetLastError(FEDM_XML_ERROR_NO_TAG_ATTRIBUTE);
02147 return NULL;
02148 }
02149
02150
02151
02152
02153
02154
02155
02156
02157
02158
02159
02160
02161
02162
02163
02164
02165
02166
02167 FEDM_XML_TREEITEM* FEDM_XMLBase::FindTag( wstring sTag,
02168 wstring sAttribName,
02169 unsigned int uiTagLevel,
02170 bool bNext)
02171 {
02172 while(true)
02173 {
02174 FEDM_XML_TREEITEM* item = FindTag(sTag, uiTagLevel, bNext);
02175
02176 if(item == NULL)
02177 {
02178 SetLastError(FEDM_XML_ERROR_NO_TAG_ATTRIBUTE);
02179 return NULL;
02180 }
02181
02182 for(int i=0; i<(int)item->aAttribList.size(); ++i)
02183 {
02184 if(item->aAttribList[i]->sAttribute == sAttribName)
02185 {
02186 SetLastError(FEDM_OK);
02187 return item;
02188 }
02189 }
02190
02191 DeleteTagItem(item);
02192
02193 bNext = true;
02194 }
02195
02196 SetLastError(FEDM_XML_ERROR_NO_TAG_ATTRIBUTE);
02197 return NULL;
02198 }
02199
02200
02201
02202
02203
02204
02205
02206
02207
02208
02209
02210
02211
02212
02213
02214
02215
02216
02217
02218
02219
02220
02221 FEDM_XML_TREEITEM* FEDM_XMLBase::FindTag(wstring sTag,
02222 wstring sAttribName,
02223 wstring sAttribValue,
02224 bool bInside,
02225 bool bSavePos)
02226 {
02227 bool bNext = false;
02228
02229 while(true)
02230 {
02231 FEDM_XML_TREEITEM* item = FindTag(sTag, bInside, bSavePos, bNext);
02232
02233 if(item == NULL)
02234 {
02235 SetLastError(FEDM_XML_ERROR_NO_TAG_ATTRIBUTE);
02236 return NULL;
02237 }
02238
02239 for(int i=0; i<(int)item->aAttribList.size(); ++i)
02240 {
02241 if( item->aAttribList[i]->sAttribute == sAttribName &&
02242 item->aAttribList[i]->sValue == sAttribValue)
02243 {
02244 SetLastError(FEDM_OK);
02245 return item;
02246 }
02247 }
02248
02249 DeleteTagItem(item);
02250
02251 bNext = true;
02252 }
02253
02254 SetLastError(FEDM_XML_ERROR_NO_TAG_ATTRIBUTE);
02255 return NULL;
02256 }
02257
02258
02259
02260
02261
02262
02263
02264
02265
02266
02267
02268
02269
02270
02271
02272
02273
02274
02275
02276
02277
02278
02279
02280
02281
02282
02283
02284
02285
02286
02287
02288
02289
02290
02291
02292
02293
02294
02295
02296
02297
02298
02299
02300
02301
02302
02303
02304
02305
02306
02307
02308
02309
02310
02311
02312
02313
02314
02315
02316
02317
02318
02319
02320
02321
02322
02323
02324
02325
02326
02327
02328
02329
02330
02331
02332
02333
02334
02335
02336
02337
02338
02339
02340
02341
02342 int FEDM_XMLBase::GetTagValue(FEDM_XML_TREEITEM* item, string& sValue)
02343 {
02344 FEDM_CHK5(item);
02345
02346 if(item->sValue.empty())
02347 FEDM_RETURN(FEDM_XML_ERROR_NO_TAG_VALUE);
02348
02349 char cBuffer[256];
02350
02351
02352 if(item->sValue.size() > 256)
02353 FEDM_RETURN(FEDM_ERROR_STRING_LENGTH);
02354
02355 #if defined(__BORLANDC__) || defined(_MSC_VER)
02356 sprintf(cBuffer, "%S", item->sValue.c_str());
02357 #else
02358 sprintf(cBuffer, "%S", item->sValue.c_str());
02359 #endif
02360 sValue = cBuffer;
02361
02362 FEDM_RETURN(FEDM_OK);
02363 }
02364
02365
02366
02367
02368
02369
02370
02371
02372
02373
02374
02375 int FEDM_XMLBase::GetTagAttribCount(FEDM_XML_TREEITEM* item)
02376 {
02377 FEDM_CHK5(item);
02378
02379 return (int)item->aAttribList.size();
02380 }
02381
02382
02383
02384
02385
02386
02387
02388
02389
02390
02391
02392
02393
02394
02395 int FEDM_XMLBase::GetTagAttrib(FEDM_XML_TREEITEM* item, int iIndex, wstring& wsAttrib, wstring& wsValue)
02396 {
02397 FEDM_CHK5(item);
02398
02399 if((int)item->aAttribList.size() <= iIndex)
02400 FEDM_RETURN(FEDM_XML_ERROR_NO_TAG_ATTRIBUTE);
02401
02402 wsAttrib = item->aAttribList[iIndex]->sAttribute;
02403 wsValue = item->aAttribList[iIndex]->sValue;
02404
02405 FEDM_RETURN(FEDM_OK);
02406 }
02407
02408
02409
02410
02411
02412
02413
02414
02415
02416
02417
02418
02419
02420
02421 int FEDM_XMLBase::GetTagAttrib(FEDM_XML_TREEITEM* item, int iIndex, string& sAttrib, string& sValue)
02422 {
02423 FEDM_CHK5(item);
02424
02425 if((int)item->aAttribList.size() <= iIndex)
02426 FEDM_RETURN(FEDM_XML_ERROR_NO_TAG_ATTRIBUTE);
02427
02428 char cBuffer[256];
02429
02430
02431 if(item->aAttribList[iIndex]->sAttribute.size() > 256)
02432 FEDM_RETURN(FEDM_ERROR_STRING_LENGTH);
02433
02434
02435 if(item->aAttribList[iIndex]->sValue.size() > 256)
02436 FEDM_RETURN(FEDM_ERROR_STRING_LENGTH);
02437
02438 #if defined(__BORLANDC__) || defined(_MSC_VER)
02439 sprintf(cBuffer, "%S", item->aAttribList[iIndex]->sAttribute.c_str());
02440 #else
02441 sprintf(cBuffer, "%S", item->aAttribList[iIndex]->sAttribute.c_str());
02442 #endif
02443 sAttrib = cBuffer;
02444
02445 #if defined(__BORLANDC__) || defined(_MSC_VER)
02446 sprintf(cBuffer, "%S", item->aAttribList[iIndex]->sValue.c_str());
02447 #else
02448 sprintf(cBuffer, "%S", item->aAttribList[iIndex]->sValue.c_str());
02449 #endif
02450 sValue = cBuffer;
02451
02452 FEDM_RETURN(FEDM_OK);
02453 }
02454
02455
02456
02457
02458
02459
02460
02461
02462
02463
02464
02465
02466 int FEDM_XMLBase::ReadXmlHeader(char* cBuffer)
02467 {
02468 bool bVersion = false;
02469 bool bEncoding = false;
02470 bool bStandalone = false;
02471 char* cToken;
02472 wchar_t wcBuffer[101];
02473
02474
02475 cToken = strtok(cBuffer, "<? =\">");
02476 while(cToken)
02477 {
02478 if(strcmp(cToken, "version")==0)
02479 {
02480 bVersion = true;
02481 cToken = strtok(NULL, "<? =\">");
02482 continue;
02483 }
02484
02485 if(strcmp(cToken, "encoding")==0)
02486 {
02487 bEncoding = true;
02488 cToken = strtok(NULL, "<? =\">");
02489 continue;
02490 }
02491
02492 if(strcmp(cToken, "standalone")==0)
02493 {
02494 bStandalone = true;
02495 cToken = strtok(NULL, "<? =\">");
02496 continue;
02497 }
02498
02499 if(bVersion)
02500 {
02501 if(strlen(cBuffer) <= 100)
02502 {
02503 #if defined(__BORLANDC__) || defined(_MSC_VER)
02504 swprintf(wcBuffer, L"%S", cToken);
02505 #else
02506 swprintf(wcBuffer, 101, L"%s", cToken);
02507 #endif
02508 m_wsXmlVer = wcBuffer;
02509 }
02510
02511 bVersion = false;
02512 }
02513
02514 if(bEncoding)
02515 {
02516 if(strlen(cBuffer) <= 100)
02517 {
02518 #if defined(__BORLANDC__) || defined(_MSC_VER)
02519 swprintf(wcBuffer, L"%S", cToken);
02520 #else
02521 swprintf(wcBuffer, 101, L"%s", cToken);
02522 #endif
02523 m_wsEncoding = wcBuffer;
02524 }
02525
02526 bEncoding = false;
02527 }
02528
02529 if(bStandalone)
02530 {
02531 if(strlen(cBuffer) <= 100)
02532 {
02533 #if defined(__BORLANDC__) || defined(_MSC_VER)
02534 swprintf(wcBuffer, L"%S", cToken);
02535 #else
02536 swprintf(wcBuffer, 101, L"%s", cToken);
02537 #endif
02538 m_wsStandalone = wcBuffer;
02539 }
02540
02541 bStandalone = false;
02542 }
02543
02544 cToken = strtok(NULL, "<? =\">");
02545 }
02546
02547 if( m_wsXmlVer.size() == 0 ||
02548 m_wsEncoding.size() == 0 ||
02549 m_wsStandalone.size() == 0 )
02550 {
02551 FEDM_RETURN(FEDM_XML_ERROR_NO_XML_FILE);
02552 }
02553
02554 FEDM_RETURN(FEDM_OK);
02555 }
02556
02557 void FEDM_XMLBase::SetLastError(int iError)
02558 {
02559 m_iLastError = iError;
02560 #if defined(_MSC_VER) && defined(_FEDM_TRACE) && defined(_DEBUG)
02561 if(iError != 0)
02562 TRACE("Error in FEDM_XMLBase: %d\n", iError);
02563 #endif
02564 }
02565
02566 int FEDM_XMLBase::GetLastError()
02567 {
02568 return m_iLastError;
02569 }
02570
02571
02572
02573
02574
02575
02576 #endif // #ifdef _FEDM_XML_SUPPORT