diff --git a/Result.xml b/Result.xml deleted file mode 100644 index cabf1ed..0000000 --- a/Result.xml +++ /dev/null @@ -1,75 +0,0 @@ - - - - ¾©A88731 - - - - - - - ¾©CX8888 - - - - - - - ¾©H99999 - - - - - - - ¼½FA3215 - - - - - - - - - - ´¨A019W2 - - - - - - ´¨A09X20 - - - - - - - - ´¨A82M83 - - - - - - ´¨A88888 - - - - - - - ´¨AA662F - - - - - - - ´¨AEK882 - - - - - - diff --git a/TestLocation.xml b/TestLocation.xml deleted file mode 100644 index 4c912e9..0000000 --- a/TestLocation.xml +++ /dev/null @@ -1,2496 +0,0 @@ - - - - img_1.png - - - - - - - - - - - - - - - img_10.png - - - - - - - - - - - - - - - img_100.png - - - - - - - - - - - - img_101.png - - - - - - - - img_102.png - - - - - - - - - - img_103.png - - - - - - - - img_104.png - - - - - - - img_105.png - - - - - - - - img_106.png - - - - - - - - img_107.png - - - - - - - - - - - - - - img_108.png - - - - - - - - img_109.png - - - - - - - img_11.png - - - - - - - - - - img_110.png - - - - - - - - img_111.png - - - - - - - - - img_112.png - - - - - - - - - img_113.png - - - - - - - - - - - - - - img_114.png - - - - - - - - - img_115.png - - - - - - - - - - - - - img_116.png - - - - - - - - - img_117.png - - - - - - - - - - - img_118.png - - - - - - - - - - - - img_119.png - - - - - - - - - - - img_12.png - - - - - - - - - img_120.png - - - - - - - - - - - - - img_121.png - - - - - - - - - - img_122.png - - - - - - - - - - - - - img_123.png - - - - - - - - img_124.png - - - - - - - - - - - - img_125.png - - - - - - - - - - - - - img_126.png - - - - - - - - - - - - - - - - - - - img_127.png - - - - - - - - - img_128.png - - - - - - - - - - img_129.png - - - - - - - img_13.png - - - - - - - - img_130.png - - - - - - - - - - - - - - - - - - img_131.png - - - - - - - img_132.png - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - img_133.png - - - - - - - img_134.png - - - - - - - img_135.png - - - - - - - - - - img_136.png - - - - - - - - - img_137.png - - - - - - - - - img_138.png - - - - - - - img_139.png - - - - - - - - - - - - - img_14.png - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - img_140.png - - - - - - - - - - - - - img_141.png - - - - - - - - - img_142.png - - - - - - - img_143.png - - - - - - - img_144.png - - - - - - - - - - img_145.png - - - - - - - img_146.png - - - - - - - img_147.png - - - - - - - - img_148.png - - - - - - - - - - - - - - - img_149.png - - - - - - - - - - img_15.png - - - - - - - - - img_150.png - - - - - - - - img_151.png - - - - - - - img_152.png - - - - - - - - - - - img_153.png - - - - - - - - - img_154.png - - - - - - - - - img_155.png - - - - - - - img_156.png - - - - - - - - - img_157.png - - - - - - - - - - - img_158.png - - - - - - - - - - img_159.png - - - - - - - - - img_16.png - - - - - - - - img_160.png - - - - - - - img_161.png - - - - - - - - - - - - - img_162.png - - - - - - - - - img_163.png - - - - - - - - - - img_164.png - - - - - - - img_165.png - - - - - - - - - - img_166.png - - - - - - - - - - - img_167.png - - - - - - - img_168.png - - - - - - - - img_169.png - - - - - - - - - - - img_17.png - - - - - - - - - - - - - - - - - - - - img_170.png - - - - - - - - - - - - - - - - img_171.png - - - - - - - img_172.png - - - - - - - - - - - img_173.png - - - - - - - - - img_174.png - - - - - - - - img_175.png - - - - - - - - - - - - img_176.png - - - - - - - - img_177.png - - - - - - - - - - - - - - - - - img_178.png - - - - - - - - - - - - - img_179.png - - - - - - - img_18.png - - - - - - - - - - img_180.png - - - - - - - - img_181.png - - - - - - - - - - - img_182.png - - - - - - - - img_183.png - - - - - - - - - - img_184.png - - - - - - - - - - img_185.png - - - - - - - - - img_186.png - - - - - - - - - img_187.png - - - - - - - - img_188.png - - - - - - - - - - img_189.png - - - - - - - - - img_19.png - - - - - - - - - - - - img_190.png - - - - - - - - - - - - - - - - img_191.png - - - - - - - - - img_192.png - - - - - - - - img_193.png - - - - - - - - - - - img_194.png - - - - - - - - - - img_195.png - - - - - - - - - - - - img_196.png - - - - - - - - - - - - - img_197.png - - - - - - - - - - - img_198.png - - - - - - - - - - - img_199.png - - - - - - - - - img_2.png - - - - - - - - - - img_20.png - - - - - - - - - - - - img_200.png - - - - - - - - - img_201.png - - - - - - - - - - - - - - - - img_202.png - - - - - - - img_203.png - - - - - - - - - img_204.png - - - - - - - - - - - - img_205.png - - - - - - - - - - - - - img_206.png - - - - - - - - img_207.png - - - - - - - - - - img_208.png - - - - - - - - img_209.png - - - - - - - - - img_21.png - - - - - - - - - - - - - - - - - - - - - img_210.png - - - - - - - - - img_211.png - - - - - - - - - img_212.png - - - - - - - - - - img_213.png - - - - - - - - - - img_214.png - - - - - - - - - - img_215.png - - - - - - - - - - img_216.png - - - - - - - - - - img_217.png - - - - - - - - - - img_218.png - - - - - - - img_219.png - - - - - - - - - - - img_22.png - - - - - - - - - - - - - - - - img_220.png - - - - - - - - - img_221.png - - - - - - - - - - - img_222.png - - - - - - - - img_223.png - - - - - - - img_224.png - - - - - - - - - - - - - img_225.png - - - - - - - - - - - - - - - - - - img_226.png - - - - - - - img_227.png - - - - - - - - - - img_228.png - - - - - - - img_229.png - - - - - - - - - - - - img_23.png - - - - - - - - - - - - - - - - - - - - - - img_230.png - - - - - - - - - - - - - img_231.png - - - - - - - - - - - - img_232.png - - - - - - - - - img_233.png - - - - - - - - img_24.png - - - - - - - - - img_25.png - - - - - - - - img_26.png - - - - - - - - - - - - - - - - - - - img_27.png - - - - - - - - - - - - - - - - - - img_28.png - - - - - - - - - - img_29.png - - - - - - - - - - - img_3.png - - - - - - - - img_30.png - - - - - - - - - - img_31.png - - - - - - - - - - - - img_32.png - - - - - - - - - - - - - - img_33.png - - - - - - - - - - - - img_34.png - - - - - - - - - img_35.png - - - - - - - - img_36.png - - - - - - - - - img_37.png - - - - - - - - - - img_38.png - - - - - - - - img_39.png - - - - - - - - - - - - - - - - img_4.png - - - - - - - - - - - - - img_40.png - - - - - - - - - - - - - - - - img_41.png - - - - - - - - img_42.png - - - - - - - - - - - img_43.png - - - - - - - - - - img_44.png - - - - - - - - - - - - - - - img_45.png - - - - - - - - - - - - - - - - - - - - - img_46.png - - - - - - - - - img_47.png - - - - - - - - - - - img_48.png - - - - - - - - - - - img_49.png - - - - - - - - - - - - - - - - - - img_5.png - - - - - - - - - - - img_50.png - - - - - - - - - - - img_51.png - - - - - - - - - - - - - - - - img_52.png - - - - - - - - - img_53.png - - - - - - - - - - - - img_54.png - - - - - - - - - - - - img_55.png - - - - - - - - - - - img_56.png - - - - - - - img_57.png - - - - - - - img_58.png - - - - - - - - - - - - - - - img_59.png - - - - - - - - - - img_6.png - - - - - - - - img_60.png - - - - - - - - img_61.png - - - - - - - - - img_62.png - - - - - - - - - - img_63.png - - - - - - - img_64.png - - - - - - - - - - img_65.png - - - - - - - - img_66.png - - - - - - - - - img_67.png - - - - - - - - - - - - img_68.png - - - - - - - - - - - - - - - - - img_69.png - - - - - - - img_7.png - - - - - - - - - - - - - - img_70.png - - - - - - - img_71.png - - - - - - - - img_72.png - - - - - - - - - - - img_73.png - - - - - - - - - - - - img_74.png - - - - - - - - - - img_75.png - - - - - - - - - - - - img_76.png - - - - - - - - img_77.png - - - - - - - - img_78.png - - - - - - - - img_79.png - - - - - - - - img_8.png - - - - - - - - - - - - - - - - - img_80.png - - - - - - - - - - - img_81.png - - - - - - - img_82.png - - - - - - - img_83.png - - - - - - - - - - img_84.png - - - - - - - - - - - - img_85.png - - - - - - - img_86.png - - - - - - - - - - - img_87.png - - - - - - - - - - img_88.png - - - - - - - - - - - img_89.png - - - - - - - - - - img_9.png - - - - - - - - - - - - - - - img_90.png - - - - - - - - img_91.png - - - - - - - - - - - - - - - - - - - - - - - img_92.png - - - - - - - - - - img_93.png - - - - - - - img_94.png - - - - - - - - img_95.png - - - - - - - - img_96.png - - - - - - - - - - img_97.png - - - - - - - - img_98.png - - - - - - - - - - - - - img_99.png - - - - - - - - - - - - - - - - - - diff --git a/accuracy.txt b/accuracy.txt index a3e0066..e1965fe 100644 --- a/accuracy.txt +++ b/accuracy.txt @@ -1373,3 +1373,15 @@ ×ÜͼƬÊý:204, δʶ³öͼƬ:5, ¶¨Î»ÂÊ:97.549% ƽ¾ù×Ö·û²î¾à:0.643216, ÍêÈ«Æ¥ÅäÊý:144, ÍêÈ«Æ¥ÅäÂÊ:72.3618% ×Üʱ¼ä:217Ãë, ƽ¾ùÖ´ÐÐʱ¼ä:1.06373Ãë +2016-06-13 15:12:01 +×ÜͼƬÊý:204, δʶ³öͼƬ:5, ¶¨Î»ÂÊ:97.549% +ƽ¾ù×Ö·û²î¾à:0.643216, ÍêÈ«Æ¥ÅäÊý:144, ÍêÈ«Æ¥ÅäÂÊ:72.3618% +×Üʱ¼ä:324Ãë, ƽ¾ùÖ´ÐÐʱ¼ä:1.58824Ãë +2016-06-13 15:17:44 +×ÜͼƬÊý:8, δʶ³öͼƬ:7, ¶¨Î»ÂÊ:12.5% +ƽ¾ù×Ö·û²î¾à:1, ÍêÈ«Æ¥ÅäÊý:0, ÍêÈ«Æ¥ÅäÂÊ:0% +×Üʱ¼ä:8Ãë, ƽ¾ùÖ´ÐÐʱ¼ä:1Ãë +2016-06-13 16:47:30 +×ÜͼƬÊý:204, δʶ³öͼƬ:5, ¶¨Î»ÂÊ:97.549% +ƽ¾ù×Ö·û²î¾à:0.643216, ÍêÈ«Æ¥ÅäÊý:144, ÍêÈ«Æ¥ÅäÂÊ:72.3618% +×Üʱ¼ä:320Ãë, ƽ¾ùÖ´ÐÐʱ¼ä:1.56863Ãë diff --git a/include/easypr/core/plate_recognize.h.orig b/include/easypr/core/plate_recognize.h.orig new file mode 100644 index 0000000..a8b3faf --- /dev/null +++ b/include/easypr/core/plate_recognize.h.orig @@ -0,0 +1,49 @@ +////////////////////////////////////////////////////////////////////////// +// Name: plate_recognize Header +// Version: 1.0 +// Date: 2014-09-28 +// Author: liuruoze +// Copyright: liuruoze +// Reference: Mastering OpenCV with Practical Computer Vision Projects +// Reference: CSDN Bloger taotao1233 +// Desciption: +// Defines CPlateRecognize +////////////////////////////////////////////////////////////////////////// +#ifndef EASYPR_CORE_PLATERECOGNIZE_H_ +#define EASYPR_CORE_PLATERECOGNIZE_H_ + +#include "easypr/core/plate_detect.h" +#include "easypr/core/chars_recognise.h" + +/*! \namespace easypr + Namespace where all the C++ EasyPR functionality resides +*/ +namespace easypr { + +class CPlateRecognize : public CPlateDetect, public CCharsRecognise { + public: + CPlateRecognize(); + + //! 车牌检测与字符识别 + + int plateRecognize(Mat src, std::vector &licenseVec); + + //! 生活模å¼ä¸Žå·¥ä¸šæ¨¡å¼åˆ‡æ¢ + +<<<<<<< HEAD:include/easypr/plate_recognize.h + // !³µÅÆʶ±ðÄ£¿é + int plateRecognize(Mat src, std::vector &licenseVec, int index); + + //! Éú»îģʽÓ빤ҵģʽÇл» +======= +>>>>>>> 569e7642c2342a31e4358831597f1bedf23258bc:include/easypr/core/plate_recognize.h + inline void setLifemode(bool param) { CPlateDetect::setPDLifemode(param); } + + //! 是å¦å¼€å¯è°ƒè¯•æ¨¡å¼ + + inline void setDebug(bool param) { CPlateDetect::setPDDebug(param); } +}; + +} /* \namespace easypr */ + +#endif // EASYPR_CORE_PLATERECOGNIZE_H_ diff --git a/resources/image/native_test/æµ™A26M71.jpg b/resources/image/native_test/æµ™A26M71.jpg new file mode 100644 index 0000000..b5966ec Binary files /dev/null and b/resources/image/native_test/æµ™A26M71.jpg differ diff --git a/resources/image/native_test/æµ™C01701.jpg b/resources/image/native_test/æµ™C01701.jpg new file mode 100644 index 0000000..06b50ae Binary files /dev/null and b/resources/image/native_test/æµ™C01701.jpg differ diff --git a/resources/image/native_test/粤A82349.jpg b/resources/image/native_test/粤A82349.jpg new file mode 100644 index 0000000..71e1324 Binary files /dev/null and b/resources/image/native_test/粤A82349.jpg differ diff --git a/resources/image/native_test/粤A961F3.jpg b/resources/image/native_test/粤A961F3.jpg new file mode 100644 index 0000000..13b260d Binary files /dev/null and b/resources/image/native_test/粤A961F3.jpg differ diff --git a/resources/image/native_test/粤BA103N.jpg b/resources/image/native_test/粤BA103N.jpg new file mode 100644 index 0000000..429200c Binary files /dev/null and b/resources/image/native_test/粤BA103N.jpg differ diff --git a/resources/image/native_test/粤BDB720.jpg b/resources/image/native_test/粤BDB720.jpg new file mode 100644 index 0000000..19acd42 Binary files /dev/null and b/resources/image/native_test/粤BDB720.jpg differ diff --git a/resources/image/native_test/粤BE609T.jpg b/resources/image/native_test/粤BE609T.jpg new file mode 100644 index 0000000..dd0098d Binary files /dev/null and b/resources/image/native_test/粤BE609T.jpg differ diff --git a/resources/image/native_test/èµ£K11978.jpg b/resources/image/native_test/èµ£K11978.jpg new file mode 100644 index 0000000..113c2fe Binary files /dev/null and b/resources/image/native_test/èµ£K11978.jpg differ diff --git a/src/core/plate_judge.cpp b/src/core/plate_judge.cpp index 830347b..7a8a693 100644 --- a/src/core/plate_judge.cpp +++ b/src/core/plate_judge.cpp @@ -21,7 +21,15 @@ int PlateJudge::plateJudge(const Mat &inMat, int &result) { getHistogramFeatures(inMat, features); float response = svm_->predict(features); - result = (int) response; + + Mat responseArray; + float score = svm_->predict(features, noArray(), cv::ml::StatModel::Flags::RAW_OUTPUT); + + std::cout << "score:" << score << ", " << "response:" << response << std::endl; + + //result = (int) response; + + result = score < 0; return 0; } diff --git a/src/core/plate_recognize.cpp b/src/core/plate_recognize.cpp index 599f21f..d1cf505 100644 --- a/src/core/plate_recognize.cpp +++ b/src/core/plate_recognize.cpp @@ -3,158 +3,86 @@ namespace easypr { -CPlateRecognize::CPlateRecognize() { } - -// !è½¦ç‰Œè¯†åˆ«æ¨¡å— -int CPlateRecognize::plateRecognize(Mat src, std::vector &licenseVec, int index) { - // ³µÅÆ·½¿é¼¯ºÏ - vector plateVec; - - // ½øÐÐÉî¶È¶¨Î»£¬Ê¹ÓÃÑÕÉ«ÐÅÏ¢Óë¶þ´ÎSobel - int resultPD = plateDetect(src, plateVec, getPDDebug(), index); - - if (resultPD == 0) { - int num = plateVec.size(); - int i = 0; - - //ÒÀ´Îʶ±ðÿ¸ö³µÅÆÄڵķûºÅ - for (int j = 0; j < num; j++) { - CPlate item = plateVec[j]; - Mat plate = item.getPlateMat(); - - //»ñÈ¡³µÅÆÑÕÉ« - string plateType = getPlateColor(plate); - - //»ñÈ¡³µÅƺŠ- string plateIdentify = ""; - int resultCR = charsRecognise(plate, plateIdentify, index); - if (resultCR == 0) { - string license = plateType + ":" + plateIdentify; - item.setPlateStr(license); - licenseVec.push_back(item); - } - } - //ÍêÕûʶ±ð¹ý³Ìµ½´Ë½áÊø - - //Èç¹ûÊÇDebugģʽ£¬Ôò»¹ÐèÒª½«¶¨Î»µÄͼƬÏÔʾÔÚԭͼ×óÉÏ½Ç - if (getPDDebug() == true) { - Mat result; - src.copyTo(result); - - for (int j = 0; j < num; j++) { - CPlate item = plateVec[j]; - Mat plate = item.getPlateMat(); - - int height = 36; - int width = 136; - if (height * i + height < result.rows) { - Mat imageRoi = result(Rect(0, 0 + height * i, width, height)); - addWeighted(imageRoi, 0, plate, 1, 0, imageRoi); - } - i++; + CPlateRecognize::CPlateRecognize() { } - RotatedRect minRect = item.getPlatePos(); - Point2f rect_points[4]; - minRect.points(rect_points); + // !³µÅÆʶ±ðÄ£¿é - Scalar lineColor = Scalar(255, 255, 255); + int CPlateRecognize::plateRecognize(Mat src, + std::vector &licenseVec) { - if (item.getPlateLocateType() == SOBEL) lineColor = Scalar(255, 0, 0); + // ³µÅÆ·½¿é¼¯ºÏ - if (item.getPlateLocateType() == COLOR) lineColor = Scalar(0, 255, 0); + std::vector plateVec; - for (int j = 0; j < 4; j++) - line(result, rect_points[j], rect_points[(j + 1) % 4], lineColor, 2, - 8); - } - - //ÏÔʾ¶¨Î»¿òµÄͼƬ - showResult(result); - } - } + // ½øÐÐÉî¶È¶¨Î»£¬Ê¹ÓÃÑÕÉ«ÐÅÏ¢Óë¶þ´ÎSobel - return resultPD; -} + int resultPD = plateDetect(src, plateVec, kDebug, 0); + if (resultPD == 0) { + size_t num = plateVec.size(); + int index = 0; -// !³µÅÆʶ±ðÄ£¿é + //ÒÀ´Îʶ±ðÿ¸ö³µÅÆÄڵķûºÅ -int CPlateRecognize::plateRecognize(Mat src, - std::vector &licenseVec) { - - // 车牌方å—é›†åˆ - - std::vector plateVec; + for (size_t j = 0; j < num; j++) { + CPlate item = plateVec[j]; + Mat plate = item.getPlateMat(); - // 进行深度定ä½ï¼Œä½¿ç”¨é¢œè‰²ä¿¡æ¯ä¸ŽäºŒæ¬¡Sobel + //»ñÈ¡³µÅÆÑÕÉ« - int resultPD = plateDetect(src, plateVec, kDebug, 0); + std::string plateType = getPlateColor(plate); - if (resultPD == 0) { - size_t num = plateVec.size(); - int index = 0; + //»ñÈ¡³µÅƺŠ- //ä¾æ¬¡è¯†åˆ«æ¯ä¸ªè½¦ç‰Œå†…çš„ç¬¦å· + std::string plateIdentify = ""; + int resultCR = charsRecognise(plate, plateIdentify); + if (resultCR == 0) { + std::string license = plateType + ":" + plateIdentify; + licenseVec.push_back(license); + } + } - for (size_t j = 0; j < num; j++) { - CPlate item = plateVec[j]; - Mat plate = item.getPlateMat(); + //ÍêÕûʶ±ð¹ý³Ìµ½´Ë½áÊø - //获å–车牌颜色 + //Èç¹ûÊÇDebugģʽ£¬Ôò»¹ÐèÒª½«¶¨Î»µÄͼƬÏÔʾÔÚԭͼ×óÉÏ½Ç - std::string plateType = getPlateColor(plate); + if (getPDDebug()) { + Mat result; + src.copyTo(result); - //获å–车牌å + for (size_t j = 0; j < num; j++) { + CPlate item = plateVec[j]; + Mat plate = item.getPlateMat(); - std::string plateIdentify = ""; - int resultCR = charsRecognise(plate, plateIdentify); - if (resultCR == 0) { - std::string license = plateType + ":" + plateIdentify; - licenseVec.push_back(license); - } - } + int height = 36; + int width = 136; + if (height * index + height < result.rows) { + Mat imageRoi = result(Rect(0, 0 + height * index, width, height)); + addWeighted(imageRoi, 0, plate, 1, 0, imageRoi); + } + index++; - //å®Œæ•´è¯†åˆ«è¿‡ç¨‹åˆ°æ­¤ç»“æŸ + RotatedRect minRect = item.getPlatePos(); + Point2f rect_points[4]; + minRect.points(rect_points); - //如果是Debug模å¼ï¼Œåˆ™è¿˜éœ€è¦å°†å®šä½çš„å›¾ç‰‡æ˜¾ç¤ºåœ¨åŽŸå›¾å·¦ä¸Šè§ + Scalar lineColor = Scalar(255, 255, 255); - if (getPDDebug()) { - Mat result; - src.copyTo(result); + if (item.getPlateLocateType() == SOBEL) lineColor = Scalar(255, 0, 0); - for (size_t j = 0; j < num; j++) { - CPlate item = plateVec[j]; - Mat plate = item.getPlateMat(); + if (item.getPlateLocateType() == COLOR) lineColor = Scalar(0, 255, 0); - int height = 36; - int width = 136; - if (height * index + height < result.rows) { - Mat imageRoi = result(Rect(0, 0 + height * index, width, height)); - addWeighted(imageRoi, 0, plate, 1, 0, imageRoi); + for (int j = 0; j < 4; j++) + line(result, rect_points[j], rect_points[(j + 1) % 4], lineColor, 2, + 8); } - index++; - RotatedRect minRect = item.getPlatePos(); - Point2f rect_points[4]; - minRect.points(rect_points); + //ÏÔʾ¶¨Î»¿òµÄͼƬ - Scalar lineColor = Scalar(255, 255, 255); - - if (item.getPlateLocateType() == SOBEL) lineColor = Scalar(255, 0, 0); - - if (item.getPlateLocateType() == COLOR) lineColor = Scalar(0, 255, 0); - - for (int j = 0; j < 4; j++) - line(result, rect_points[j], rect_points[(j + 1) % 4], lineColor, 2, - 8); + showResult(result); } - - //显示定ä½æ¡†çš„图片 - - showResult(result); } - } - return resultPD; -} -} + return resultPD; + } +} \ No newline at end of file diff --git a/test/accuracy.hpp b/test/accuracy.hpp index 6ac3b89..0d10be4 100644 --- a/test/accuracy.hpp +++ b/test/accuracy.hpp @@ -9,296 +9,238 @@ using namespace std; -#include "xml\xmlParser.h" - namespace easypr { -namespace demo { - -int accuracyTest(const char* test_path) { - std::shared_ptr kv(new easypr::Kv); - kv->load("etc/chinese_mapping"); + namespace demo { - //int n = xMainNode.nChildNode("image"); + int accuracyTest(const char* test_path) { + std::shared_ptr kv(new easypr::Kv); + kv->load("etc/chinese_mapping"); - //cout << n << endl; - // - ////for (int i = 0; i < n; i++) { - //// XMLNode xNode = xMainNode.getChildNode("image", i); - //// cout << xNode.getChildNode("imageName").getText() << endl; - ////} + auto files = Utils::getFiles(test_path); - ////XMLNode xNode = xMainNode.getChildNode("image", 0); - ////cout << xNode.getChildNode("imageName").getText() << endl; - ////xNode.getChildNode("imageName").updateText("test1"); - // - //XMLNode xNode = xMainNode.addChild("image"); + CPlateRecognize pr; - //cout << xNode.getChildNode("imageName").getText() << endl; - //xNode.getChildNode("imageName").updateText("test1"); + // ÉèÖÃDebugģʽ + pr.setDebug(false); - //char *t = xNode.createXMLString(true); - //printf("%s\n", t); - //free(t); + pr.setLifemode(true); - XMLNode xMainNode = XMLNode::createXMLTopNode("tagset"); - XMLNode::setGlobalOptions(XMLNode::char_encoding_GBK); + // ÉèÖÃÒª´¦ÀíµÄÒ»ÕÅͼƬÖÐ×î¶àÓжàÉÙ³µÅÆ - auto files = Utils::getFiles(test_path); + pr.setMaxPlates(4); - CPlateRecognize pr; + int size = files.size(); - // 设置Debugæ¨¡å¼ + if (0 == size) { + cout << "No File Found in general_test/native_test!" << endl; + return 0; + } - pr.setDebug(false); + cout << "Begin to test the easypr accuracy!" << endl; - pr.setLifemode(true); + // ×ܵIJâÊÔͼƬÊýÁ¿ - // 设置è¦å¤„ç†çš„一张图片中最多有多少车牌 + int count_all = 0; - pr.setMaxPlates(4); + // ´íÎóµÄͼƬÊýÁ¿ - int size = files.size(); + int count_err = 0; - if (0 == size) { - cout << "No File Found in general_test/native_test!" << endl; - return 0; - } + // δʶ±ðµÄͼƬÊýÁ¿ - cout << "Begin to test the easypr accuracy!" << endl; + int count_norecogin = 0; - // æ€»çš„æµ‹è¯•å›¾ç‰‡æ•°é‡ + std::list not_recognized_files; - int count_all = 0; + // ×ܵÄ×Ö·û²î¾à - // é”™è¯¯çš„å›¾ç‰‡æ•°é‡ + float diff_all = 0; - int count_err = 0; + // ƽ¾ù×Ö·û²î¾à - // æœªè¯†åˆ«çš„å›¾ç‰‡æ•°é‡ + float diff_avg = 0; - int count_norecogin = 0; + // ÍêÈ«Æ¥ÅäµÄʶ±ð´ÎÊý - std::list not_recognized_files; + float match_count = 0; - // æ€»çš„å­—ç¬¦å·®è· + // ÍêÈ«Æ¥ÅäµÄʶ±ð´ÎÊýËùռʶ±ðͼƬÖеıÈÀý - float diff_all = 0; + float match_rate = 0; - // å¹³å‡å­—ç¬¦å·®è· + // ¿ªÊ¼ºÍ½áÊøʱ¼ä - float diff_avg = 0; + time_t begin, end; + time(&begin); - // 完全匹é…çš„è¯†åˆ«æ¬¡æ• + for (int i = 0; i < size; i++) { + string filepath = files[i].c_str(); - float match_count = 0; + // EasyPR¿ªÊ¼ÅжϳµÅÆ - // 完全匹é…的识别次数所å è¯†åˆ«å›¾ç‰‡ä¸­çš„æ¯”ä¾ + Mat src = imread(filepath); - float match_rate = 0; + // Èç¹ûÊÇ·ÇͼÏñÎļþ£¬Ö±½Ó¹ýÈ¥ - // 开始和结æŸæ—¶é—´ + if (!src.data) continue; - time_t begin, end; - time(&begin); + cout << "------------------" << endl; - for (int i = 0; i < 10; i++) { - string filepath = files[i].c_str(); + // »ñÈ¡ÕæʵµÄ³µÅÆ - // EasyPRå¼€å§‹åˆ¤æ–­è½¦ç‰ + string plateLicense = Utils::getFileName(filepath); + cout << kv->get("original_plate") << ":" << plateLicense << endl; - Mat src = imread(filepath); + vector plateVec; + int result = pr.plateRecognize(src, plateVec); + if (result == 0) { + int num = plateVec.size(); - // 如果是éžå›¾åƒæ–‡ä»¶ï¼Œç›´æŽ¥è¿‡åŽ + if (num == 0) { + cout << kv->get("empty_plate") << endl; + if (plateLicense != kv->get("empty_plate")) { + not_recognized_files.push_back(plateLicense); + count_norecogin++; + } + } + else if (num > 1) { - if (!src.data) continue; + // ¶à³µÅÆʹÓÃdiff×îСµÄÄǸö¼Ç¼ - cout << "------------------" << endl; + int mindiff = 10000; + for (int j = 0; j < num; j++) { + cout << plateVec[j] << " (" << j + 1 << ")" << endl; + string colorplate = plateVec[j]; - // 获å–çœŸå®žçš„è½¦ç‰ + // ¼ÆËã"À¶ÅÆ:ËÕE7KU22"ÖÐðºÅºóÃæµÄ³µÅÆ´óС" - string plateLicense = Utils::getFileName(filepath); - cout << kv->get("original_plate") << ":" << plateLicense << endl; + vector spilt_plate = Utils::splitString(colorplate, ':'); - XMLNode xNode = xMainNode.addChild("image"); - int result = pr.plateRecognize(src, plateVec); + int size = spilt_plate.size(); + if (size == 2 && spilt_plate[1] != "") { + int diff = utils::levenshtein_distance(plateLicense, + spilt_plate[size - 1]); + if (diff < mindiff) mindiff = diff; + } + } - XMLNode rectangleNodes = xNode.addChild("taggedRectangles"); + cout << kv->get("diff") << ":" << mindiff << kv->get("char") << endl; + if (mindiff == 0) { - vector plateVec; - if (result == 0) { - int num = plateVec.size(); + // ÍêÈ«Æ¥Åä - if (num == 0) { - cout << kv->get("empty_plate") << endl; - if (plateLicense != kv->get("empty_plate")) { - not_recognized_files.push_back(plateLicense); - count_norecogin++; - } - } else if (num > 1) { + match_count++; + } + diff_all = diff_all + mindiff; + } + else { - // 多车牌使用diff最å°çš„那个记录 + // µ¥³µÅÆÖ»¼ÆËãÒ»´Îdiff - int mindiff = 10000; - for (int j = 0; j < num; j++) { - cout << plateVec[j].getPlateStr() << " (" << j + 1 << ")" << endl; + for (int j = 0; j < num; j++) { + cout << plateVec[j] << endl; + string colorplate = plateVec[j]; - XMLNode rectangleNode = rectangleNodes.addChild("taggedRectangle"); - RotatedRect rr = plateVec[j].getPlatePos(); - LocateType locateType = plateVec[j].getPlateLocateType(); - - rectangleNode.addAttribute("x", to_string((int)rr.center.x).c_str()); - rectangleNode.addAttribute("y", to_string((int)rr.center.y).c_str()); - rectangleNode.addAttribute("width", to_string((int)rr.size.width).c_str()); - rectangleNode.addAttribute("height", to_string((int)rr.size.height).c_str()); + // ¼ÆËã"À¶ÅÆ:ËÕE7KU22"ÖÐðºÅºóÃæµÄ³µÅÆ´óС" - rectangleNode.addAttribute("rotation", to_string((int)rr.angle).c_str()); - rectangleNode.addAttribute("locateType", to_string(locateType).c_str()); - //rectangleNode.addText(plateVec[j].getPlateStr().c_str()); - rectangleNode.addAttribute("text", plateVec[j].getPlateStr().c_str()); + vector spilt_plate = Utils::splitString(colorplate, ':'); - string colorplate = plateVec[j].getPlateStr(); + int size = spilt_plate.size(); + if (size == 2 && spilt_plate[1] != "") { + int diff = utils::levenshtein_distance(plateLicense, + spilt_plate[size - 1]); + cout << kv->get("diff") << ":" << diff << kv->get("char") << endl; - // 计算"è“牌:è‹E7KU22"中冒å·åŽé¢çš„车牌大å°" + if (diff == 0) { - vector spilt_plate = Utils::splitString(colorplate, ':'); + // ÍêÈ«Æ¥Åä - int size = spilt_plate.size(); - if (size == 2 && spilt_plate[1] != "") { - int diff = utils::levenshtein_distance(plateLicense, - spilt_plate[size - 1]); - if (diff < mindiff) mindiff = diff; + match_count++; + } + diff_all = diff_all + diff; + } + } } } - - cout << kv->get("diff") << ":" << mindiff << kv->get("char") << endl; - if (mindiff == 0) { - - // å®Œå…¨åŒ¹é… - - match_count++; + else { + cout << kv->get("error_code") << ":" << result << endl; + count_err++; } - diff_all = diff_all + mindiff; - } else { - - // å•è½¦ç‰Œåªè®¡ç®—一次diff - - for (int j = 0; j < num; j++) { - cout << plateVec[j].getPlateStr() << endl; - - XMLNode rectangleNode = rectangleNodes.addChild("taggedRectangle"); - RotatedRect rr = plateVec[j].getPlatePos(); - LocateType locateType = plateVec[j].getPlateLocateType(); - - rectangleNode.addAttribute("x", to_string((int)rr.center.x).c_str()); - rectangleNode.addAttribute("y", to_string((int)rr.center.y).c_str()); - rectangleNode.addAttribute("width", to_string((int)rr.size.width).c_str()); - rectangleNode.addAttribute("height", to_string((int)rr.size.height).c_str()); - - rectangleNode.addAttribute("rotation", to_string((int)rr.angle).c_str()); - rectangleNode.addAttribute("locateType", to_string(locateType).c_str()); - //rectangleNode.addText(plateVec[j].getPlateStr().c_str()); - rectangleNode.addAttribute("text", plateVec[j].getPlateStr().c_str()); + count_all++; + } + time(&end); + + cout << "------------------" << endl; + cout << "Easypr accuracy test end!" << endl; + cout << "------------------" << endl; + cout << endl; + cout << kv->get("summaries") << ":" << endl; + cout << kv->get("sum_pictures") << ":" << count_all << ", "; + cout << kv->get("unrecognized") << ":" << count_norecogin << ", "; + + float count_recogin = float(count_all - (count_err + count_norecogin)); + float count_rate = count_recogin / count_all; + cout << kv->get("locate_rate") << ":" << count_rate * 100 << "% " << endl; + + if (count_recogin > 0) { + diff_avg = diff_all / count_recogin; + } - string colorplate = plateVec[j].getPlateStr(); + if (count_recogin > 0) { + match_rate = match_count / count_recogin * 100; + } - // 计算"è“牌:è‹E7KU22"中冒å·åŽé¢çš„车牌大å°" + cout << kv->get("diff_average") << ":" << diff_avg << ", "; + cout << kv->get("full_match") << ":" << match_count << ", "; + cout << kv->get("full_rate") << ":" << match_rate << "% " << endl; - vector spilt_plate = Utils::splitString(colorplate, ':'); + double seconds = difftime(end, begin); + double avgsec = seconds / double(count_all); - int size = spilt_plate.size(); - if (size == 2 && spilt_plate[1] != "") { - int diff = utils::levenshtein_distance(plateLicense, - spilt_plate[size - 1]); - cout << kv->get("diff") << ":" << diff << kv->get("char") << endl; + cout << kv->get("seconds") << ":" << seconds << kv->get("sec") << ", "; + cout << kv->get("seconds_average") << ":" << avgsec << kv->get("sec") << endl; - if (diff == 0) { + cout << kv->get("unrecognized") << ":" << endl; - // å®Œå…¨åŒ¹é… + for (auto it = not_recognized_files.begin(); it != not_recognized_files.end(); + ++it) { + cout << *it << endl; + } - match_count++; - } - diff_all = diff_all + diff; - } - } + cout << endl; + + cout << "------------------" << endl; + + ofstream myfile("accuracy.txt", ios::app); + if (myfile.is_open()) { + time_t t = time(0); // get time now + struct tm* now = localtime(&t); + char buf[80]; + + strftime(buf, sizeof(buf), "%Y-%m-%d %X", now); + myfile << string(buf) << endl; + + myfile << kv->get("sum_pictures") << ":" << count_all << ", "; + myfile << kv->get("unrecognized") << ":" << count_norecogin << ", "; + myfile << kv->get("locate_rate") << ":" << count_rate * 100 << "% " + << endl; + myfile << kv->get("diff_average") << ":" << diff_avg << ", "; + myfile << kv->get("full_match") << ":" << match_count << ", "; + myfile << kv->get("full_rate") << ":" << match_rate << "% " << endl; + myfile << kv->get("seconds") << ":" << seconds << kv->get("sec") << ", "; + myfile << kv->get("seconds_average") << ":" << avgsec << kv->get("sec") + << endl; + myfile.close(); + } + else { + cout << "Unable to open file"; } - } else { - cout << kv->get("error_code") << ":" << result << endl; - count_err++; + return 0; } - count_all++; - } - time(&end); - - cout << "------------------" << endl; - cout << "Easypr accuracy test end!" << endl; - cout << "------------------" << endl; - cout << endl; - cout << kv->get("summaries") << ":" << endl; - cout << kv->get("sum_pictures") << ":" << count_all << ", "; - cout << kv->get("unrecognized") << ":" << count_norecogin << ", "; - - float count_recogin = float(count_all - (count_err + count_norecogin)); - float count_rate = count_recogin / count_all; - cout << kv->get("locate_rate") << ":" << count_rate * 100 << "% " << endl; - - if (count_recogin > 0) { - diff_avg = diff_all / count_recogin; - } - if (count_recogin > 0) { - match_rate = match_count / count_recogin * 100; } - - cout << kv->get("diff_average") << ":" << diff_avg << ", "; - cout << kv->get("full_match") << ":" << match_count << ", "; - cout << kv->get("full_rate") << ":" << match_rate << "% " << endl; - - double seconds = difftime(end, begin); - double avgsec = seconds / double(count_all); - - cout << kv->get("seconds") << ":" << seconds << kv->get("sec") << ", "; - cout << kv->get("seconds_average") << ":" << avgsec << kv->get("sec") << endl; - - cout << kv->get("unrecognized") << ":" << endl; - - for (auto it = not_recognized_files.begin(); it != not_recognized_files.end(); - ++it) { - cout << *it << endl; - } - - cout << endl; - - cout << "------------------" << endl; - - ofstream myfile("accuracy.txt", ios::app); - if (myfile.is_open()) { - time_t t = time(0); // get time now - struct tm* now = localtime(&t); - char buf[80]; - - strftime(buf, sizeof(buf), "%Y-%m-%d %X", now); - myfile << string(buf) << endl; - - myfile << kv->get("sum_pictures") << ":" << count_all << ", "; - myfile << kv->get("unrecognized") << ":" << count_norecogin << ", "; - myfile << kv->get("locate_rate") << ":" << count_rate * 100 << "% " - << endl; - myfile << kv->get("diff_average") << ":" << diff_avg << ", "; - myfile << kv->get("full_match") << ":" << match_count << ", "; - myfile << kv->get("full_rate") << ":" << match_rate << "% " << endl; - myfile << kv->get("seconds") << ":" << seconds << kv->get("sec") << ", "; - myfile << kv->get("seconds_average") << ":" << avgsec << kv->get("sec") - << endl; - myfile.close(); - } else { - cout << "Unable to open file"; - } - return 0; -} - -} } #endif // EASYPR_ACCURACY_HPP diff --git a/test/xml/AFPL-license.txt b/test/xml/AFPL-license.txt deleted file mode 100644 index 5c0ab93..0000000 --- a/test/xml/AFPL-license.txt +++ /dev/null @@ -1,241 +0,0 @@ -Aladdin Free Public License -(Version 8, November 18, 1999) - -Copyright (C) 1994, 1995, 1997, 1998, 1999 Aladdin Enterprises, -Menlo Park, California, U.S.A. All rights reserved. - - *NOTE:* This License is not the same as any of the GNU Licenses - published by the Free - Software Foundation . Its terms are - substantially different from those of the GNU Licenses. If you are - familiar with the GNU Licenses, please read this license with extra - care. - -Aladdin Enterprises hereby grants to anyone the permission to apply this -License to their own work, as long as the entire License (including the -above notices and this paragraph) is copied with no changes, additions, -or deletions except for changing the first paragraph of Section 0 to -include a suitable description of the work to which the license is being -applied and of the person or entity that holds the copyright in the -work, and, if the License is being applied to a work created in a -country other than the United States, replacing the first paragraph of -Section 6 with an appropriate reference to the laws of the appropriate -country. - - - 0. Subject Matter - -This License applies to the computer program known as "XMLParser library". -The "Program", below, refers to such program. The Program -is a copyrighted work whose copyright is held by Frank Vanden Berghen -(the "Licensor"). - -A "work based on the Program" means either the Program or any derivative -work of the Program, as defined in the United States Copyright Act of -1976, such as a translation or a modification. - -* BY MODIFYING OR DISTRIBUTING THE PROGRAM (OR ANY WORK BASED ON THE -PROGRAM), YOU INDICATE YOUR ACCEPTANCE OF THIS LICENSE TO DO SO, AND ALL -ITS TERMS AND CONDITIONS FOR COPYING, DISTRIBUTING OR MODIFYING THE -PROGRAM OR WORKS BASED ON IT. NOTHING OTHER THAN THIS LICENSE GRANTS YOU -PERMISSION TO MODIFY OR DISTRIBUTE THE PROGRAM OR ITS DERIVATIVE WORKS. -THESE ACTIONS ARE PROHIBITED BY LAW. IF YOU DO NOT ACCEPT THESE TERMS -AND CONDITIONS, DO NOT MODIFY OR DISTRIBUTE THE PROGRAM. * - - - 1. Licenses. - -Licensor hereby grants you the following rights, provided that you -comply with all of the restrictions set forth in this License and -provided, further, that you distribute an unmodified copy of this -License with the Program: - -(a) - You may copy and distribute literal (i.e., verbatim) copies of the - Program's source code as you receive it throughout the world, in any - medium. -(b) - You may modify the Program, create works based on the Program and - distribute copies of such throughout the world, in any medium. - - - 2. Restrictions. - -This license is subject to the following restrictions: - -(a) - Distribution of the Program or any work based on the Program by a - commercial organization to any third party is prohibited if any - payment is made in connection with such distribution, whether - directly (as in payment for a copy of the Program) or indirectly (as - in payment for some service related to the Program, or payment for - some product or service that includes a copy of the Program "without - charge"; these are only examples, and not an exhaustive enumeration - of prohibited activities). The following methods of distribution - involving payment shall not in and of themselves be a violation of - this restriction: - - (i) - Posting the Program on a public access information storage and - retrieval service for which a fee is received for retrieving - information (such as an on-line service), provided that the fee - is not content-dependent (i.e., the fee would be the same for - retrieving the same volume of information consisting of random - data) and that access to the service and to the Program is - available independent of any other product or service. An - example of a service that does not fall under this section is an - on-line service that is operated by a company and that is only - available to customers of that company. (This is not an - exhaustive enumeration.) - (ii) - Distributing the Program on removable computer-readable media, - provided that the files containing the Program are reproduced - entirely and verbatim on such media, that all information on - such media be redistributable for non-commercial purposes - without charge, and that such media are distributed by - themselves (except for accompanying documentation) independent - of any other product or service. Examples of such media include - CD-ROM, magnetic tape, and optical storage media. (This is not - intended to be an exhaustive list.) An example of a distribution - that does not fall under this section is a CD-ROM included in a - book or magazine. (This is not an exhaustive enumeration.) - -(b) - Activities other than copying, distribution and modification of the - Program are not subject to this License and they are outside its - scope. Functional use (running) of the Program is not restricted, - and any output produced through the use of the Program is subject to - this license only if its contents constitute a work based on the - Program (independent of having been made by running the Program). -(c) - You must meet all of the following conditions with respect to any - work that you distribute or publish that in whole or in part - contains or is derived from the Program or any part thereof ("the - Work"): - - (i) - If you have modified the Program, you must cause the Work to - carry prominent notices stating that you have modified the - Program's files and the date of any change. In each source file - that you have modified, you must include a prominent notice that - you have modified the file, including your name, your e-mail - address (if any), and the date and purpose of the change; - (ii) - You must cause the Work to be licensed as a whole and at no - charge to all third parties under the terms of this License; - (iii) - If the Work normally reads commands interactively when run, you - must cause it, at each time the Work commences operation, to - print or display an announcement including an appropriate - copyright notice and a notice that there is no warranty (or - else, saying that you provide a warranty). Such notice must also - state that users may redistribute the Work only under the - conditions of this License and tell the user how to view the - copy of this License included with the Work. (Exceptions: if the - Program is interactive but normally prints or displays such an - announcement only at the request of a user, such as in an "About - box", the Work is required to print or display the notice only - under the same circumstances; if the Program itself is - interactive but does not normally print such an announcement, - the Work is not required to print an announcement.); - (iv) - You must accompany the Work with the complete corresponding - machine-readable source code, delivered on a medium customarily - used for software interchange. The source code for a work means - the preferred form of the work for making modifications to it. - For an executable work, complete source code means all the - source code for all modules it contains, plus any associated - interface definition files, plus the scripts used to control - compilation and installation of the executable code. If you - distribute with the Work any component that is normally - distributed (in either source or binary form) with the major - components (compiler, kernel, and so on) of the operating system - on which the executable runs, you must also distribute the - source code of that component if you have it and are allowed to - do so; - (v) - If you distribute any written or printed material at all with - the Work, such material must include either a written copy of - this License, or a prominent written indication that the Work is - covered by this License and written instructions for printing - and/or displaying the copy of the License on the distribution - medium; - (vi) - You may not impose any further restrictions on the recipient's - exercise of the rights granted herein. - -If distribution of executable or object code is made by offering the -equivalent ability to copy from a designated place, then offering -equivalent ability to copy the source code from the same place counts as -distribution of the source code, even though third parties are not -compelled to copy the source code along with the object code. - - - 3. Reservation of Rights. - -No rights are granted to the Program except as expressly set forth -herein. You may not copy, modify, sublicense, or distribute the Program -except as expressly provided under this License. Any attempt otherwise -to copy, modify, sublicense or distribute the Program is void, and will -automatically terminate your rights under this License. However, parties -who have received copies, or rights, from you under this License will -not have their licenses terminated so long as such parties remain in -full compliance. - - - 4. Other Restrictions. - -If the distribution and/or use of the Program is restricted in certain -countries for any reason, Licensor may add an explicit geographical -distribution limitation excluding those countries, so that distribution -is permitted only in or among countries not thus excluded. In such case, -this License incorporates the limitation as if written in the body of -this License. - - - 5. Limitations. - -* THE PROGRAM IS PROVIDED TO YOU "AS IS," WITHOUT WARRANTY. THERE IS NO -WARRANTY FOR THE PROGRAM, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT -NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. THE -ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH -YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL -NECESSARY SERVICING, REPAIR OR CORRECTION. * - -* IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL LICENSOR, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE -PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY -GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE -USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS -OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR -THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER -PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE -POSSIBILITY OF SUCH DAMAGES. * - - - 6. General. - -This License is governed by the laws of Belgium., excluding choice of -law rules. - -If any part of this License is found to be in conflict with the law, -that part shall be interpreted in its broadest meaning consistent with -the law, and no other parts of the License shall be affected. - -For United States Government users, the Program is provided with -*RESTRICTED RIGHTS*. If you are a unit or agency of the United States -Government or are acquiring the Program for any such unit or agency, the -following apply: - - If the unit or agency is the Department of Defense ("DOD"), the - Program and its documentation are classified as "commercial computer - software" and "commercial computer software documentation" - respectively and, pursuant to DFAR Section 227.7202, the Government - is acquiring the Program and its documentation in accordance with - the terms of this License. If the unit or agency is other than DOD, - the Program and its documentation are classified as "commercial - computer software" and "commercial computer software documentation" - respectively and, pursuant to FAR Section 12.212, the Government is - acquiring the Program and its documentation in accordance with the - terms of this License. diff --git a/test/xml/xmlParser.cpp b/test/xml/xmlParser.cpp deleted file mode 100644 index e0f7f33..0000000 --- a/test/xml/xmlParser.cpp +++ /dev/null @@ -1,2974 +0,0 @@ -/** - **************************************************************************** - *

XML.c - implementation file for basic XML parser written in ANSI C++ - * for portability. It works by using recursion and a node tree for breaking - * down the elements of an XML document.

- * - * @version V2.44 - * @author Frank Vanden Berghen - * - * NOTE: - * - * If you add "#define STRICT_PARSING", on the first line of this file - * the parser will see the following XML-stream: - * some textother text - * as an error. Otherwise, this tring will be equivalent to: - * some textother text - * - * NOTE: - * - * If you add "#define APPROXIMATE_PARSING" on the first line of this file - * the parser will see the following XML-stream: - * - * - * - * as equivalent to the following XML-stream: - * - * - * - * This can be useful for badly-formed XML-streams but prevent the use - * of the following XML-stream (problem is: tags at contiguous levels - * have the same names): - * - * - * - * - * - * - * NOTE: - * - * If you add "#define _XMLPARSER_NO_MESSAGEBOX_" on the first line of this file - * the "openFileHelper" function will always display error messages inside the - * console instead of inside a message-box-window. Message-box-windows are - * available on windows 9x/NT/2000/XP/Vista only. - * - * Copyright (c) 2002, Frank Vanden Berghen - All rights reserved. - * Commercialized by Business-Insight - * See the file "AFPL-license.txt about the licensing terms - * - **************************************************************************** - */ -#ifndef _CRT_SECURE_NO_DEPRECATE -#define _CRT_SECURE_NO_DEPRECATE -#endif -#include "xmlParser.h" -#ifdef _XMLWINDOWS -//#ifdef _DEBUG -//#define _CRTDBG_MAP_ALLOC -//#include -//#endif -#define WIN32_LEAN_AND_MEAN -#include // to have IsTextUnicode, MultiByteToWideChar, WideCharToMultiByte to handle unicode files - // to have "MessageBoxA" to display error messages for openFilHelper -#endif - -#include -#include -#include -#include -#include - -XMLCSTR XMLNode::getVersion() { return _CXML("v2.44"); } -void freeXMLString(XMLSTR t){if(t)free(t);} - -static XMLNode::XMLCharEncoding characterEncoding=XMLNode::char_encoding_UTF8; -static char guessWideCharChars=1, dropWhiteSpace=1, removeCommentsInMiddleOfText=1; - -inline int mmin( const int t1, const int t2 ) { return t1 < t2 ? t1 : t2; } - -// You can modify the initialization of the variable "XMLClearTags" below -// to change the clearTags that are currently recognized by the library. -// The number on the second columns is the length of the string inside the -// first column. -// The "") }, - { _CXML("") }, - { _CXML("") }, - { _CXML("
")    ,5,  _CXML("
") }, -// { _CXML("")}, - { NULL ,0, NULL } -}; - -// You can modify the initialization of the variable "XMLEntities" below -// to change the character entities that are currently recognized by the library. -// The number on the second columns is the length of the string inside the -// first column. Additionally, the syntaxes " " and " " are recognized. -typedef struct { XMLCSTR s; int l; XMLCHAR c;} XMLCharacterEntity; -static XMLCharacterEntity XMLEntities[] = -{ - { _CXML("&" ), 5, _CXML('&' )}, - { _CXML("<" ), 4, _CXML('<' )}, - { _CXML(">" ), 4, _CXML('>' )}, - { _CXML("""), 6, _CXML('\"')}, - { _CXML("'"), 6, _CXML('\'')}, - { NULL , 0, '\0' } -}; - -// When rendering the XMLNode to a string (using the "createXMLString" function), -// you can ask for a beautiful formatting. This formatting is using the -// following indentation character: -#define INDENTCHAR _CXML('\t') - -// The following function parses the XML errors into a user friendly string. -// You can edit this to change the output language of the library to something else. -XMLCSTR XMLNode::getError(XMLError xerror) -{ - switch (xerror) - { - case eXMLErrorNone: return _CXML("No error"); - case eXMLErrorMissingEndTag: return _CXML("Warning: Unmatched end tag"); - case eXMLErrorNoXMLTagFound: return _CXML("Warning: No XML tag found"); - case eXMLErrorEmpty: return _CXML("Error: No XML data"); - case eXMLErrorMissingTagName: return _CXML("Error: Missing start tag name"); - case eXMLErrorMissingEndTagName: return _CXML("Error: Missing end tag name"); - case eXMLErrorUnmatchedEndTag: return _CXML("Error: Unmatched end tag"); - case eXMLErrorUnmatchedEndClearTag: return _CXML("Error: Unmatched clear tag end"); - case eXMLErrorUnexpectedToken: return _CXML("Error: Unexpected token found"); - case eXMLErrorNoElements: return _CXML("Error: No elements found"); - case eXMLErrorFileNotFound: return _CXML("Error: File not found"); - case eXMLErrorFirstTagNotFound: return _CXML("Error: First Tag not found"); - case eXMLErrorUnknownCharacterEntity:return _CXML("Error: Unknown character entity"); - case eXMLErrorCharacterCodeAbove255: return _CXML("Error: Character code above 255 is forbidden in MultiByte char mode."); - case eXMLErrorCharConversionError: return _CXML("Error: unable to convert between WideChar and MultiByte chars"); - case eXMLErrorCannotOpenWriteFile: return _CXML("Error: unable to open file for writing"); - case eXMLErrorCannotWriteFile: return _CXML("Error: cannot write into file"); - - case eXMLErrorBase64DataSizeIsNotMultipleOf4: return _CXML("Warning: Base64-string length is not a multiple of 4"); - case eXMLErrorBase64DecodeTruncatedData: return _CXML("Warning: Base64-string is truncated"); - case eXMLErrorBase64DecodeIllegalCharacter: return _CXML("Error: Base64-string contains an illegal character"); - case eXMLErrorBase64DecodeBufferTooSmall: return _CXML("Error: Base64 decode output buffer is too small"); - }; - return _CXML("Unknown"); -} - -///////////////////////////////////////////////////////////////////////// -// Here start the abstraction layer to be OS-independent // -///////////////////////////////////////////////////////////////////////// - -// Here is an abstraction layer to access some common string manipulation functions. -// The abstraction layer is currently working for gcc, Microsoft Visual Studio 6.0, -// Microsoft Visual Studio .NET, CC (sun compiler) and Borland C++. -// If you plan to "port" the library to a new system/compiler, all you have to do is -// to edit the following lines. -#ifdef XML_NO_WIDE_CHAR -char myIsTextWideChar(const void *b, int len) { return FALSE; } -#else - #if defined (UNDER_CE) || !defined(_XMLWINDOWS) - char myIsTextWideChar(const void *b, int len) // inspired by the Wine API: RtlIsTextUnicode - { -#ifdef sun - // for SPARC processors: wchar_t* buffers must always be alligned, otherwise it's a char* buffer. - if ((((unsigned long)b)%sizeof(wchar_t))!=0) return FALSE; -#endif - const wchar_t *s=(const wchar_t*)b; - - // buffer too small: - if (len<(int)sizeof(wchar_t)) return FALSE; - - // odd length test - if (len&1) return FALSE; - - /* only checks the first 256 characters */ - len=mmin(256,len/sizeof(wchar_t)); - - // Check for the special byte order: - if (*((unsigned short*)s) == 0xFFFE) return TRUE; // IS_TEXT_UNICODE_REVERSE_SIGNATURE; - if (*((unsigned short*)s) == 0xFEFF) return TRUE; // IS_TEXT_UNICODE_SIGNATURE - - // checks for ASCII characters in the UNICODE stream - int i,stats=0; - for (i=0; ilen/2) return TRUE; - - // Check for UNICODE NULL chars - for (i=0; i - static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return wsncasecmp(c1,c2,l);} - static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return wsncmp(c1,c2,l);} - static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return wscasecmp(c1,c2); } - #else - static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncmp(c1,c2,l);} - #ifdef __linux__ - // for gcc/linux - static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return wcsncasecmp(c1,c2,l);} - static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return wcscasecmp(c1,c2); } - #else - #include - // for gcc/non-linux (MacOS X 10.3, FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, AIX 4.3.2, HP-UX 11, IRIX 6.5, OSF/1 5.1, Cygwin, mingw) - static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) - { - wchar_t left,right; - do - { - left=towlower(*c1++); right=towlower(*c2++); - } while (left&&(left==right)); - return (int)left-(int)right; - } - static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) - { - wchar_t left,right; - while(l--) - { - left=towlower(*c1++); right=towlower(*c2++); - if ((!left)||(left!=right)) return (int)left-(int)right; - } - return 0; - } - #endif - #endif - static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)wcsstr(c1,c2); } - static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)wcscpy(c1,c2); } - static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode) - { - char *filenameAscii=myWideCharToMultiByte(filename); - FILE *f; - if (mode[0]==_CXML('r')) f=fopen(filenameAscii,"rb"); - else f=fopen(filenameAscii,"wb"); - free(filenameAscii); - return f; - } - #else - static inline FILE *xfopen(XMLCSTR filename,XMLCSTR mode) { return fopen(filename,mode); } - static inline int xstrlen(XMLCSTR c) { return strlen(c); } - static inline int xstrnicmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncasecmp(c1,c2,l);} - static inline int xstrncmp(XMLCSTR c1, XMLCSTR c2, int l) { return strncmp(c1,c2,l);} - static inline int xstricmp(XMLCSTR c1, XMLCSTR c2) { return strcasecmp(c1,c2); } - static inline XMLSTR xstrstr(XMLCSTR c1, XMLCSTR c2) { return (XMLSTR)strstr(c1,c2); } - static inline XMLSTR xstrcpy(XMLSTR c1, XMLCSTR c2) { return (XMLSTR)strcpy(c1,c2); } - #endif - static inline int _strnicmp(const char *c1,const char *c2, int l) { return strncasecmp(c1,c2,l);} -#endif - - -/////////////////////////////////////////////////////////////////////////////// -// the "xmltoc,xmltob,xmltoi,xmltol,xmltof,xmltoa" functions // -/////////////////////////////////////////////////////////////////////////////// -// These 6 functions are not used inside the XMLparser. -// There are only here as "convenience" functions for the user. -// If you don't need them, you can delete them without any trouble. -#ifdef _XMLWIDECHAR - #ifdef _XMLWINDOWS - // for Microsoft Visual Studio 6.0 and Microsoft Visual Studio .NET and Borland C++ Builder 6.0 - char xmltob(XMLCSTR t,char v){ if (t&&(*t)) return (char)_wtoi(t); return v; } - int xmltoi(XMLCSTR t,int v){ if (t&&(*t)) return _wtoi(t); return v; } - long long xmltol(XMLCSTR t,long long v){ if (t&&(*t)) return _wtoi64(t); return v; } - double xmltof(XMLCSTR t,double v){ if (t&&(*t)) swscanf(t, L"%lf", &v); /*v=_wtof(t);*/ return v; } - #else - #ifdef sun - // for CC - #include - char xmltob(XMLCSTR t,char v){ if (t) return (char)wstol(t,NULL,10); return v; } - int xmltoi(XMLCSTR t,int v){ if (t) return (int)wstol(t,NULL,10); return v; } - long long xmltol(XMLCSTR t,long long v){ if (t) return wstol(t,NULL,10); return v; } - #else - // for gcc - char xmltob(XMLCSTR t,char v){ if (t) return (char)wcstol(t,NULL,10); return v; } - int xmltoi(XMLCSTR t,int v){ if (t) return (int)wcstol(t,NULL,10); return v; } - long long xmltol(XMLCSTR t,long long v){ if (t) return wcstol(t,NULL,10); return v; } - #endif - double xmltof(XMLCSTR t,double v){ if (t&&(*t)) swscanf(t, L"%lf", &v); /*v=_wtof(t);*/ return v; } - #endif -#else - #ifdef _XMLWINDOWS - long long xmltol(XMLCSTR t,long long v){ if (t&&(*t)) return _atoi64(t); return v; } - #else - long long xmltol(XMLCSTR t,long long v){ if (t&&(*t)) return atoll(t); return v; } - #endif - char xmltob(XMLCSTR t,char v){ if (t&&(*t)) return (char)atoi(t); return v; } - int xmltoi(XMLCSTR t,int v){ if (t&&(*t)) return atoi(t); return v; } - double xmltof(XMLCSTR t,double v){ if (t&&(*t)) return atof(t); return v; } -#endif -XMLCSTR xmltoa(XMLCSTR t, XMLCSTR v){ if (t) return t; return v; } -XMLCHAR xmltoc(XMLCSTR t,const XMLCHAR v){ if (t&&(*t)) return *t; return v; } - -///////////////////////////////////////////////////////////////////////// -// the "openFileHelper" function // -///////////////////////////////////////////////////////////////////////// - -// Since each application has its own way to report and deal with errors, you should modify & rewrite -// the following "openFileHelper" function to get an "error reporting mechanism" tailored to your needs. -XMLNode XMLNode::openFileHelper(XMLCSTR filename, XMLCSTR tag) -{ - // guess the value of the global parameter "characterEncoding" - // (the guess is based on the first 200 bytes of the file). - FILE *f=xfopen(filename,_CXML("rb")); - if (f) - { - char bb[205]; - int l=(int)fread(bb,1,200,f); - setGlobalOptions(guessCharEncoding(bb,l),guessWideCharChars,dropWhiteSpace,removeCommentsInMiddleOfText); - fclose(f); - } - - // parse the file - XMLResults pResults; - XMLNode xnode=XMLNode::parseFile(filename,tag,&pResults); - - // display error message (if any) - if (pResults.error != eXMLErrorNone) - { - // create message - char message[2000],*s1=(char*)"",*s3=(char*)""; XMLCSTR s2=_CXML(""); - if (pResults.error==eXMLErrorFirstTagNotFound) { s1=(char*)"First Tag should be '"; s2=tag; s3=(char*)"'.\n"; } -#ifdef _XMLWINDOWS - _snprintf(message,2000, -#else - snprintf(message,2000, -#endif -#ifdef _XMLWIDECHAR - "XML Parsing error inside file '%S'.\n%S\nAt line %i, column %i.\n%s%S%s" -#else - "XML Parsing error inside file '%s'.\n%s\nAt line %i, column %i.\n%s%s%s" -#endif - ,filename,XMLNode::getError(pResults.error),pResults.nLine,pResults.nColumn,s1,s2,s3); - - // display message -#if defined(_XMLWINDOWS) && !defined(UNDER_CE) && !defined(_XMLPARSER_NO_MESSAGEBOX_) - MessageBoxA(NULL,message,"XML Parsing error",MB_OK|MB_ICONERROR|MB_TOPMOST); -#else - printf("%s",message); -#endif - exit(255); - } - return xnode; -} - -///////////////////////////////////////////////////////////////////////// -// Here start the core implementation of the XMLParser library // -///////////////////////////////////////////////////////////////////////// - -// You should normally not change anything below this point. - -#ifndef _XMLWIDECHAR -// If "characterEncoding=ascii" then we assume that all characters have the same length of 1 byte. -// If "characterEncoding=UTF8" then the characters have different lengths (from 1 byte to 4 bytes). -// If "characterEncoding=ShiftJIS" then the characters have different lengths (from 1 byte to 2 bytes). -// This table is used as lookup-table to know the length of a character (in byte) based on the -// content of the first byte of the character. -// (note: if you modify this, you must always have XML_utf8ByteTable[0]=0 ). -static const char XML_utf8ByteTable[256] = -{ - // 0 1 2 3 4 5 6 7 8 9 a b c d e f - 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x00 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x10 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x20 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x30 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x40 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x50 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x70 End of ASCII range - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x80 0x80 to 0xc1 invalid - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x90 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xa0 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xb0 - 1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xc0 0xc2 to 0xdf 2 byte - 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xd0 - 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,// 0xe0 0xe0 to 0xef 3 byte - 4,4,4,4,4,1,1,1,1,1,1,1,1,1,1,1 // 0xf0 0xf0 to 0xf4 4 byte, 0xf5 and higher invalid -}; -static const char XML_legacyByteTable[256] = -{ - 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 -}; -static const char XML_sjisByteTable[256] = -{ - // 0 1 2 3 4 5 6 7 8 9 a b c d e f - 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x00 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x10 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x20 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x30 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x40 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x50 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x70 - 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0x80 0x81 to 0x9F 2 bytes - 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0x90 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xa0 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xb0 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xc0 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0xd0 - 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xe0 0xe0 to 0xef 2 bytes - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 // 0xf0 -}; -static const char XML_gb2312ByteTable[256] = -{ -// 0 1 2 3 4 5 6 7 8 9 a b c d e f - 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x00 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x10 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x20 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x30 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x40 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x50 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x70 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x80 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x90 - 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xa0 0xa1 to 0xf7 2 bytes - 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xb0 - 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xc0 - 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xd0 - 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xe0 - 2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1 // 0xf0 -}; -static const char XML_gbk_big5_ByteTable[256] = -{ - // 0 1 2 3 4 5 6 7 8 9 a b c d e f - 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x00 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x10 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x20 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x30 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x40 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x50 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x60 - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,// 0x70 - 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0x80 0x81 to 0xfe 2 bytes - 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0x90 - 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xa0 - 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xb0 - 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xc0 - 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xd0 - 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,// 0xe0 - 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1 // 0xf0 -}; -static const char *XML_ByteTable=(const char *)XML_utf8ByteTable; // the default is "characterEncoding=XMLNode::encoding_UTF8" -#endif - - -XMLNode XMLNode::emptyXMLNode; -XMLClear XMLNode::emptyXMLClear={ NULL, NULL, NULL}; -XMLAttribute XMLNode::emptyXMLAttribute={ NULL, NULL}; - -// Enumeration used to decipher what type a token is -typedef enum XMLTokenTypeTag -{ - eTokenText = 0, - eTokenQuotedText, - eTokenTagStart, /* "<" */ - eTokenTagEnd, /* "" */ - eTokenEquals, /* "=" */ - eTokenDeclaration, /* "" */ - eTokenClear, - eTokenError -} XMLTokenType; - -// Main structure used for parsing XML -typedef struct XML -{ - XMLCSTR lpXML; - XMLCSTR lpszText; - int nIndex,nIndexMissigEndTag; - enum XMLError error; - XMLCSTR lpEndTag; - int cbEndTag; - XMLCSTR lpNewElement; - int cbNewElement; - int nFirst; -} XML; - -typedef struct -{ - ALLXMLClearTag *pClr; - XMLCSTR pStr; -} NextToken; - -// Enumeration used when parsing attributes -typedef enum Attrib -{ - eAttribName = 0, - eAttribEquals, - eAttribValue -} Attrib; - -// Enumeration used when parsing elements to dictate whether we are currently -// inside a tag -typedef enum XMLStatus -{ - eInsideTag = 0, - eOutsideTag -} XMLStatus; - -XMLError XMLNode::writeToFile(XMLCSTR filename, const char *encoding, char nFormat) const -{ - if (!d) return eXMLErrorNone; - FILE *f=xfopen(filename,_CXML("wb")); - if (!f) return eXMLErrorCannotOpenWriteFile; -#ifdef _XMLWIDECHAR - unsigned char h[2]={ 0xFF, 0xFE }; - if (!fwrite(h,2,1,f)) - { - fclose(f); - return eXMLErrorCannotWriteFile; - } - if ((!isDeclaration())&&((d->lpszName)||(!getChildNode().isDeclaration()))) - { - if (!fwrite(L"\n",sizeof(wchar_t)*40,1,f)) - { - fclose(f); - return eXMLErrorCannotWriteFile; - } - } -#else - if ((!isDeclaration())&&((d->lpszName)||(!getChildNode().isDeclaration()))) - { - if (characterEncoding==char_encoding_UTF8) - { - // header so that windows recognize the file as UTF-8: - unsigned char h[3]={0xEF,0xBB,0xBF}; - if (!fwrite(h,3,1,f)) - { - fclose(f); - return eXMLErrorCannotWriteFile; - } - encoding="utf-8"; - } else if (characterEncoding==char_encoding_ShiftJIS) encoding="SHIFT-JIS"; - - if (!encoding) encoding="ISO-8859-1"; - if (fprintf(f,"\n",encoding)<0) - { - fclose(f); - return eXMLErrorCannotWriteFile; - } - } else - { - if (characterEncoding==char_encoding_UTF8) - { - unsigned char h[3]={0xEF,0xBB,0xBF}; - if (!fwrite(h,3,1,f)) - { - fclose(f); - return eXMLErrorCannotWriteFile; - } - } - } -#endif - int i; - XMLSTR t=createXMLString(nFormat,&i); - if (!fwrite(t,sizeof(XMLCHAR)*i,1,f)) - { - free(t); - fclose(f); - return eXMLErrorCannotWriteFile; - } - if (fclose(f)!=0) - { - free(t); - return eXMLErrorCannotWriteFile; - } - free(t); - return eXMLErrorNone; -} - -// Duplicate a given string. -XMLSTR stringDup(XMLCSTR lpszData, int cbData) -{ - if (lpszData==NULL) return NULL; - - XMLSTR lpszNew; - if (cbData==-1) cbData=(int)xstrlen(lpszData); - lpszNew = (XMLSTR)malloc((cbData+1) * sizeof(XMLCHAR)); - if (lpszNew) - { - memcpy(lpszNew, lpszData, (cbData) * sizeof(XMLCHAR)); - lpszNew[cbData] = (XMLCHAR)NULL; - } - return lpszNew; -} - -XMLSTR ToXMLStringTool::toXMLUnSafe(XMLSTR dest,XMLCSTR source) -{ - XMLSTR dd=dest; - XMLCHAR ch; - XMLCharacterEntity *entity; - while ((ch=*source)) - { - entity=XMLEntities; - do - { - if (ch==entity->c) {xstrcpy(dest,entity->s); dest+=entity->l; source++; goto out_of_loop1; } - entity++; - } while(entity->s); -#ifdef _XMLWIDECHAR - *(dest++)=*(source++); -#else - switch(XML_ByteTable[(unsigned char)ch]) - { - case 4: - if ((!(source[1]))||(!(source[2]))||(!(source[3]))) { *(dest++)='_'; source++; } - else - { - *dest=*source; - dest[1]=source[1]; - dest[2]=source[2]; - dest[3]=source[3]; - dest+=4; source+=4; - } - break; - case 3: - if ((!(source[1]))||(!(source[2]))) { *(dest++)='_'; source++; } - else - { - *dest=*source; - dest[1]=source[1]; - dest[2]=source[2]; - dest+=3; source+=3; - } - break; - case 2: - if (!(source[1])) { *(dest++)='_'; source++; } - else - { - *dest=*source; - dest[1]=source[1]; - dest+=2; source+=2; - } - break; - case 1: *(dest++)=*(source++); - } -#endif -out_of_loop1: - ; - } - *dest=0; - return dd; -} - -// private (used while rendering): -int ToXMLStringTool::lengthXMLString(XMLCSTR source) -{ - int r=0; - XMLCharacterEntity *entity; - XMLCHAR ch; - while ((ch=*source)) - { - entity=XMLEntities; - do - { - if (ch==entity->c) { r+=entity->l; source++; goto out_of_loop1; } - entity++; - } while(entity->s); -#ifdef _XMLWIDECHAR - r++; source++; -#else - ch=XML_ByteTable[(unsigned char)ch]; r+=ch; source+=ch; -#endif -out_of_loop1: - ; - } - return r; -} - -ToXMLStringTool::~ToXMLStringTool(){ freeBuffer(); } -void ToXMLStringTool::freeBuffer(){ if (buf) free(buf); buf=NULL; buflen=0; } -XMLSTR ToXMLStringTool::toXML(XMLCSTR source) -{ - if (!source) - { - if (buflen<1) { buflen=1; buf=(XMLSTR)malloc(sizeof(XMLCHAR)); } - *buf=0; - return buf; - } - int l=lengthXMLString(source)+1; - if (l>buflen) { freeBuffer(); buflen=l; buf=(XMLSTR)malloc(l*sizeof(XMLCHAR)); } - return toXMLUnSafe(buf,source); -} - -// private: -XMLSTR fromXMLString(XMLCSTR s, int lo, XML *pXML) -{ - // This function is the opposite of the function "toXMLString". It decodes the escape - // sequences &, ", ', <, > and replace them by the characters - // &,",',<,>. This function is used internally by the XML Parser. All the calls to - // the XML library will always gives you back "decoded" strings. - // - // in: string (s) and length (lo) of string - // out: new allocated string converted from xml - if (!s) return NULL; - - int ll=0,j; - XMLSTR d; - XMLCSTR ss=s; - XMLCharacterEntity *entity; - while ((lo>0)&&(*s)) - { - if (*s==_CXML('&')) - { - if ((lo>2)&&(s[1]==_CXML('#'))) - { - s+=2; lo-=2; - if ((*s==_CXML('X'))||(*s==_CXML('x'))) { s++; lo--; } - while ((*s)&&(*s!=_CXML(';'))&&((lo--)>0)) s++; - if (*s!=_CXML(';')) - { - pXML->error=eXMLErrorUnknownCharacterEntity; - return NULL; - } - s++; lo--; - } else - { - entity=XMLEntities; - do - { - if ((lo>=entity->l)&&(xstrnicmp(s,entity->s,entity->l)==0)) { s+=entity->l; lo-=entity->l; break; } - entity++; - } while(entity->s); - if (!entity->s) - { - pXML->error=eXMLErrorUnknownCharacterEntity; - return NULL; - } - } - } else - { -#ifdef _XMLWIDECHAR - s++; lo--; -#else - j=XML_ByteTable[(unsigned char)*s]; s+=j; lo-=j; ll+=j-1; -#endif - } - ll++; - } - - d=(XMLSTR)malloc((ll+1)*sizeof(XMLCHAR)); - s=d; - while (ll-->0) - { - if (*ss==_CXML('&')) - { - if (ss[1]==_CXML('#')) - { - ss+=2; j=0; - if ((*ss==_CXML('X'))||(*ss==_CXML('x'))) - { - ss++; - while (*ss!=_CXML(';')) - { - if ((*ss>=_CXML('0'))&&(*ss<=_CXML('9'))) j=(j<<4)+*ss-_CXML('0'); - else if ((*ss>=_CXML('A'))&&(*ss<=_CXML('F'))) j=(j<<4)+*ss-_CXML('A')+10; - else if ((*ss>=_CXML('a'))&&(*ss<=_CXML('f'))) j=(j<<4)+*ss-_CXML('a')+10; - else { free((void*)s); pXML->error=eXMLErrorUnknownCharacterEntity;return NULL;} - ss++; - } - } else - { - while (*ss!=_CXML(';')) - { - if ((*ss>=_CXML('0'))&&(*ss<=_CXML('9'))) j=(j*10)+*ss-_CXML('0'); - else { free((void*)s); pXML->error=eXMLErrorUnknownCharacterEntity;return NULL;} - ss++; - } - } -#ifndef _XMLWIDECHAR - if (j>255) { free((void*)s); pXML->error=eXMLErrorCharacterCodeAbove255;return NULL;} -#endif - (*d++)=(XMLCHAR)j; ss++; - } else - { - entity=XMLEntities; - do - { - if (xstrnicmp(ss,entity->s,entity->l)==0) { *(d++)=entity->c; ss+=entity->l; break; } - entity++; - } while(entity->s); - } - } else - { -#ifdef _XMLWIDECHAR - *(d++)=*(ss++); -#else - switch(XML_ByteTable[(unsigned char)*ss]) - { - case 4: *(d++)=*(ss++); ll--; - case 3: *(d++)=*(ss++); ll--; - case 2: *(d++)=*(ss++); ll--; - case 1: *(d++)=*(ss++); - } -#endif - } - } - *d=0; - return (XMLSTR)s; -} - -#define XML_isSPACECHAR(ch) ((ch==_CXML('\n'))||(ch==_CXML(' '))||(ch== _CXML('\t'))||(ch==_CXML('\r'))) - -// private: -char myTagCompare(XMLCSTR cclose, XMLCSTR copen) -// !!!! WARNING strange convention&: -// return 0 if equals -// return 1 if different -{ - if (!cclose) return 1; - int l=(int)xstrlen(cclose); - if (xstrnicmp(cclose, copen, l)!=0) return 1; - const XMLCHAR c=copen[l]; - if (XML_isSPACECHAR(c)|| - (c==_CXML('/' ))|| - (c==_CXML('<' ))|| - (c==_CXML('>' ))|| - (c==_CXML('=' ))) return 0; - return 1; -} - -// Obtain the next character from the string. -static inline XMLCHAR getNextChar(XML *pXML) -{ - XMLCHAR ch = pXML->lpXML[pXML->nIndex]; -#ifdef _XMLWIDECHAR - if (ch!=0) pXML->nIndex++; -#else - pXML->nIndex+=XML_ByteTable[(unsigned char)ch]; -#endif - return ch; -} - -// Find the next token in a string. -// pcbToken contains the number of characters that have been read. -static NextToken GetNextToken(XML *pXML, int *pcbToken, enum XMLTokenTypeTag *pType) -{ - NextToken result; - XMLCHAR ch; - XMLCHAR chTemp; - int indexStart,nFoundMatch,nIsText=FALSE; - result.pClr=NULL; // prevent warning - - // Find next non-white space character - do { indexStart=pXML->nIndex; ch=getNextChar(pXML); } while XML_isSPACECHAR(ch); - - if (ch) - { - // Cache the current string pointer - result.pStr = &pXML->lpXML[indexStart]; - - // check for standard tokens - switch(ch) - { - // Check for quotes - case _CXML('\''): - case _CXML('\"'): - // Type of token - *pType = eTokenQuotedText; - chTemp = ch; - - // Set the size - nFoundMatch = FALSE; - - // Search through the string to find a matching quote - while((ch = getNextChar(pXML))) - { - if (ch==chTemp) { nFoundMatch = TRUE; break; } - if (ch==_CXML('<')) break; - } - - // If we failed to find a matching quote - if (nFoundMatch == FALSE) - { - pXML->nIndex=indexStart+1; - nIsText=TRUE; - break; - } - -// 4.02.2002 -// if (FindNonWhiteSpace(pXML)) pXML->nIndex--; - - break; - - // Equals (used with attribute values) - case _CXML('='): - *pType = eTokenEquals; - break; - - // Close tag - case _CXML('>'): - *pType = eTokenCloseTag; - break; - - // Check for tag start and tag end - case _CXML('<'): - - { - // First check whether the token is in the clear tag list (meaning it - // does not need formatting). - ALLXMLClearTag *ctag=XMLClearTags; - do - { - if (!xstrncmp(ctag->lpszOpen, result.pStr, ctag->openTagLen)) - { - result.pClr=ctag; - pXML->nIndex+=ctag->openTagLen-1; - *pType=eTokenClear; - return result; - } - ctag++; - } while(ctag->lpszOpen); - - // Peek at the next character to see if we have an end tag 'lpXML[pXML->nIndex]; - - // If we have a tag end... - if (chTemp == _CXML('/')) - { - // Set the type and ensure we point at the next character - getNextChar(pXML); - *pType = eTokenTagEnd; - } - - // If we have an XML declaration tag - else if (chTemp == _CXML('?')) - { - - // Set the type and ensure we point at the next character - getNextChar(pXML); - *pType = eTokenDeclaration; - } - - // Otherwise we must have a start tag - else - { - *pType = eTokenTagStart; - } - break; - } - - // Check to see if we have a short hand type end tag ('/>'). - case _CXML('/'): - - // Peek at the next character to see if we have a short end tag '/>' - chTemp = pXML->lpXML[pXML->nIndex]; - - // If we have a short hand end tag... - if (chTemp == _CXML('>')) - { - // Set the type and ensure we point at the next character - getNextChar(pXML); - *pType = eTokenShortHandClose; - break; - } - - // If we haven't found a short hand closing tag then drop into the - // text process - - // Other characters - default: - nIsText = TRUE; - } - - // If this is a TEXT node - if (nIsText) - { - // Indicate we are dealing with text - *pType = eTokenText; - while((ch = getNextChar(pXML))) - { - if XML_isSPACECHAR(ch) - { - indexStart++; break; - - } else if (ch==_CXML('/')) - { - // If we find a slash then this maybe text or a short hand end tag - // Peek at the next character to see it we have short hand end tag - ch=pXML->lpXML[pXML->nIndex]; - // If we found a short hand end tag then we need to exit the loop - if (ch==_CXML('>')) { pXML->nIndex--; break; } - - } else if ((ch==_CXML('<'))||(ch==_CXML('>'))||(ch==_CXML('='))) - { - pXML->nIndex--; break; - } - } - } - *pcbToken = pXML->nIndex-indexStart; - } else - { - // If we failed to obtain a valid character - *pcbToken = 0; - *pType = eTokenError; - result.pStr=NULL; - } - - return result; -} - -XMLCSTR XMLNode::updateName_WOSD(XMLSTR lpszName) -{ - if (!d) { free(lpszName); return NULL; } - if (d->lpszName&&(lpszName!=d->lpszName)) free((void*)d->lpszName); - d->lpszName=lpszName; - return lpszName; -} - -// private: -XMLNode::XMLNode(struct XMLNodeDataTag *p){ d=p; (p->ref_count)++; } -XMLNode::XMLNode(XMLNodeData *pParent, XMLSTR lpszName, char isDeclaration) -{ - d=(XMLNodeData*)malloc(sizeof(XMLNodeData)); - d->ref_count=1; - - d->lpszName=NULL; - d->nChild= 0; - d->nText = 0; - d->nClear = 0; - d->nAttribute = 0; - - d->isDeclaration = isDeclaration; - - d->pParent = pParent; - d->pChild= NULL; - d->pText= NULL; - d->pClear= NULL; - d->pAttribute= NULL; - d->pOrder= NULL; - - updateName_WOSD(lpszName); -} - -XMLNode XMLNode::createXMLTopNode_WOSD(XMLSTR lpszName, char isDeclaration) { return XMLNode(NULL,lpszName,isDeclaration); } -XMLNode XMLNode::createXMLTopNode(XMLCSTR lpszName, char isDeclaration) { return XMLNode(NULL,stringDup(lpszName),isDeclaration); } - -#define MEMORYINCREASE 50 - -static inline void myFree(void *p) { if (p) free(p); } -static inline void *myRealloc(void *p, int newsize, int memInc, int sizeofElem) -{ - if (p==NULL) { if (memInc) return malloc(memInc*sizeofElem); return malloc(sizeofElem); } - if ((memInc==0)||((newsize%memInc)==0)) p=realloc(p,(newsize+memInc)*sizeofElem); -// if (!p) -// { -// printf("XMLParser Error: Not enough memory! Aborting...\n"); exit(220); -// } - return p; -} - -// private: -XMLElementPosition XMLNode::findPosition(XMLNodeData *d, int index, XMLElementType xxtype) -{ - if (index<0) return -1; - int i=0,j=(int)((index<<2)+xxtype),*o=d->pOrder; while (o[i]!=j) i++; return i; -} - -// private: -// update "order" information when deleting a content of a XMLNode -int XMLNode::removeOrderElement(XMLNodeData *d, XMLElementType t, int index) -{ - int n=d->nChild+d->nText+d->nClear, *o=d->pOrder,i=findPosition(d,index,t); - memmove(o+i, o+i+1, (n-i)*sizeof(int)); - for (;ipOrder=(int)realloc(d->pOrder,n*sizeof(int)); - // but we skip reallocation because it's too time consuming. - // Anyway, at the end, it will be free'd completely at once. - return i; -} - -void *XMLNode::addToOrder(int memoryIncrease,int *_pos, int nc, void *p, int size, XMLElementType xtype) -{ - // in: *_pos is the position inside d->pOrder ("-1" means "EndOf") - // out: *_pos is the index inside p - p=myRealloc(p,(nc+1),memoryIncrease,size); - int n=d->nChild+d->nText+d->nClear; - d->pOrder=(int*)myRealloc(d->pOrder,n+1,memoryIncrease*3,sizeof(int)); - int pos=*_pos,*o=d->pOrder; - - if ((pos<0)||(pos>=n)) { *_pos=nc; o[n]=(int)((nc<<2)+xtype); return p; } - - int i=pos; - memmove(o+i+1, o+i, (n-i)*sizeof(int)); - - while ((pos>2; - memmove(((char*)p)+(pos+1)*size,((char*)p)+pos*size,(nc-pos)*size); - - return p; -} - -// Add a child node to the given element. -XMLNode XMLNode::addChild_priv(int memoryIncrease, XMLSTR lpszName, char isDeclaration, int pos) -{ - if (!lpszName) return emptyXMLNode; - d->pChild=(XMLNode*)addToOrder(memoryIncrease,&pos,d->nChild,d->pChild,sizeof(XMLNode),eNodeChild); - d->pChild[pos].d=NULL; - d->pChild[pos]=XMLNode(d,lpszName,isDeclaration); - d->nChild++; - return d->pChild[pos]; -} - -// Add an attribute to an element. -XMLAttribute *XMLNode::addAttribute_priv(int memoryIncrease,XMLSTR lpszName, XMLSTR lpszValuev) -{ - if (!lpszName) return &emptyXMLAttribute; - if (!d) { myFree(lpszName); myFree(lpszValuev); return &emptyXMLAttribute; } - int nc=d->nAttribute; - d->pAttribute=(XMLAttribute*)myRealloc(d->pAttribute,(nc+1),memoryIncrease,sizeof(XMLAttribute)); - XMLAttribute *pAttr=d->pAttribute+nc; - pAttr->lpszName = lpszName; - pAttr->lpszValue = lpszValuev; - d->nAttribute++; - return pAttr; -} - -// Add text to the element. -XMLCSTR XMLNode::addText_priv(int memoryIncrease, XMLSTR lpszValue, int pos) -{ - if (!lpszValue) return NULL; - if (!d) { myFree(lpszValue); return NULL; } - d->pText=(XMLCSTR*)addToOrder(memoryIncrease,&pos,d->nText,d->pText,sizeof(XMLSTR),eNodeText); - d->pText[pos]=lpszValue; - d->nText++; - return lpszValue; -} - -// Add clear (unformatted) text to the element. -XMLClear *XMLNode::addClear_priv(int memoryIncrease, XMLSTR lpszValue, XMLCSTR lpszOpen, XMLCSTR lpszClose, int pos) -{ - if (!lpszValue) return &emptyXMLClear; - if (!d) { myFree(lpszValue); return &emptyXMLClear; } - d->pClear=(XMLClear *)addToOrder(memoryIncrease,&pos,d->nClear,d->pClear,sizeof(XMLClear),eNodeClear); - XMLClear *pNewClear=d->pClear+pos; - pNewClear->lpszValue = lpszValue; - if (!lpszOpen) lpszOpen=XMLClearTags->lpszOpen; - if (!lpszClose) lpszClose=XMLClearTags->lpszClose; - pNewClear->lpszOpenTag = lpszOpen; - pNewClear->lpszCloseTag = lpszClose; - d->nClear++; - return pNewClear; -} - -// private: -// Parse a clear (unformatted) type node. -char XMLNode::parseClearTag(void *px, void *_pClear) -{ - XML *pXML=(XML *)px; - ALLXMLClearTag pClear=*((ALLXMLClearTag*)_pClear); - int cbTemp=0; - XMLCSTR lpszTemp=NULL; - XMLCSTR lpXML=&pXML->lpXML[pXML->nIndex]; - static XMLCSTR docTypeEnd=_CXML("]>"); - - // Find the closing tag - // Seems the ')) { lpszTemp=pCh; break; } -#ifdef _XMLWIDECHAR - pCh++; -#else - pCh+=XML_ByteTable[(unsigned char)(*pCh)]; -#endif - } - } else lpszTemp=xstrstr(lpXML, pClear.lpszClose); - - if (lpszTemp) - { - // Cache the size and increment the index - cbTemp = (int)(lpszTemp - lpXML); - - pXML->nIndex += cbTemp+(int)xstrlen(pClear.lpszClose); - - // Add the clear node to the current element - addClear_priv(MEMORYINCREASE,cbTemp?stringDup(lpXML,cbTemp):NULL, pClear.lpszOpen, pClear.lpszClose,-1); - return 0; - } - - // If we failed to find the end tag - pXML->error = eXMLErrorUnmatchedEndClearTag; - return 1; -} - -void XMLNode::exactMemory(XMLNodeData *d) -{ - if (d->pOrder) d->pOrder=(int*)realloc(d->pOrder,(d->nChild+d->nText+d->nClear)*sizeof(int)); - if (d->pChild) d->pChild=(XMLNode*)realloc(d->pChild,d->nChild*sizeof(XMLNode)); - if (d->pAttribute) d->pAttribute=(XMLAttribute*)realloc(d->pAttribute,d->nAttribute*sizeof(XMLAttribute)); - if (d->pText) d->pText=(XMLCSTR*)realloc(d->pText,d->nText*sizeof(XMLSTR)); - if (d->pClear) d->pClear=(XMLClear *)realloc(d->pClear,d->nClear*sizeof(XMLClear)); -} - -char XMLNode::maybeAddTxT(void *pa, XMLCSTR tokenPStr) -{ - XML *pXML=(XML *)pa; - XMLCSTR lpszText=pXML->lpszText; - if (!lpszText) return 0; - if (dropWhiteSpace) while (XML_isSPACECHAR(*lpszText)&&(lpszText!=tokenPStr)) lpszText++; - int cbText = (int)(tokenPStr - lpszText); - if (!cbText) { pXML->lpszText=NULL; return 0; } - if (dropWhiteSpace) { cbText--; while ((cbText)&&XML_isSPACECHAR(lpszText[cbText])) cbText--; cbText++; } - if (!cbText) { pXML->lpszText=NULL; return 0; } - XMLSTR lpt=fromXMLString(lpszText,cbText,pXML); - if (!lpt) return 1; - pXML->lpszText=NULL; - if (removeCommentsInMiddleOfText && d->nText && d->nClear) - { - // if the previous insertion was a comment () AND - // if the previous previous insertion was a text then, delete the comment and append the text - int n=d->nChild+d->nText+d->nClear-1,*o=d->pOrder; - if (((o[n]&3)==eNodeClear)&&((o[n-1]&3)==eNodeText)) - { - int i=o[n]>>2; - if (d->pClear[i].lpszOpenTag==XMLClearTags[2].lpszOpen) - { - deleteClear(i); - i=o[n-1]>>2; - n=xstrlen(d->pText[i]); - int n2=xstrlen(lpt)+1; - d->pText[i]=(XMLSTR)realloc((void*)d->pText[i],(n+n2)*sizeof(XMLCHAR)); - if (!d->pText[i]) return 1; - memcpy((void*)(d->pText[i]+n),lpt,n2*sizeof(XMLCHAR)); - free(lpt); - return 0; - } - } - } - addText_priv(MEMORYINCREASE,lpt,-1); - return 0; -} -// private: -// Recursively parse an XML element. -int XMLNode::ParseXMLElement(void *pa) -{ - XML *pXML=(XML *)pa; - int cbToken; - enum XMLTokenTypeTag xtype; - NextToken token; - XMLCSTR lpszTemp=NULL; - int cbTemp=0; - char nDeclaration; - XMLNode pNew; - enum XMLStatus status; // inside or outside a tag - enum Attrib attrib = eAttribName; - - assert(pXML); - - // If this is the first call to the function - if (pXML->nFirst) - { - // Assume we are outside of a tag definition - pXML->nFirst = FALSE; - status = eOutsideTag; - } else - { - // If this is not the first call then we should only be called when inside a tag. - status = eInsideTag; - } - - // Iterate through the tokens in the document - for(;;) - { - // Obtain the next token - token = GetNextToken(pXML, &cbToken, &xtype); - - if (xtype != eTokenError) - { - // Check the current status - switch(status) - { - - // If we are outside of a tag definition - case eOutsideTag: - - // Check what type of token we obtained - switch(xtype) - { - // If we have found text or quoted text - case eTokenText: - case eTokenCloseTag: /* '>' */ - case eTokenShortHandClose: /* '/>' */ - case eTokenQuotedText: - case eTokenEquals: - break; - - // If we found a start tag '<' and declarations 'error = eXMLErrorMissingTagName; - return FALSE; - } - - // If we found a new element which is the same as this - // element then we need to pass this back to the caller.. - -#ifdef APPROXIMATE_PARSING - if (d->lpszName && - myTagCompare(d->lpszName, token.pStr) == 0) - { - // Indicate to the caller that it needs to create a - // new element. - pXML->lpNewElement = token.pStr; - pXML->cbNewElement = cbToken; - return TRUE; - } else -#endif - { - // If the name of the new element differs from the name of - // the current element we need to add the new element to - // the current one and recurse - pNew = addChild_priv(MEMORYINCREASE,stringDup(token.pStr,cbToken), nDeclaration,-1); - - while (!pNew.isEmpty()) - { - // Callself to process the new node. If we return - // FALSE this means we dont have any more - // processing to do... - - if (!pNew.ParseXMLElement(pXML)) return FALSE; - else - { - // If the call to recurse this function - // evented in a end tag specified in XML then - // we need to unwind the calls to this - // function until we find the appropriate node - // (the element name and end tag name must - // match) - if (pXML->cbEndTag) - { - // If we are back at the root node then we - // have an unmatched end tag - if (!d->lpszName) - { - pXML->error=eXMLErrorUnmatchedEndTag; - return FALSE; - } - - // If the end tag matches the name of this - // element then we only need to unwind - // once more... - - if (myTagCompare(d->lpszName, pXML->lpEndTag)==0) - { - pXML->cbEndTag = 0; - } - - return TRUE; - } else - if (pXML->cbNewElement) - { - // If the call indicated a new element is to - // be created on THIS element. - - // If the name of this element matches the - // name of the element we need to create - // then we need to return to the caller - // and let it process the element. - - if (myTagCompare(d->lpszName, pXML->lpNewElement)==0) - { - return TRUE; - } - - // Add the new element and recurse - pNew = addChild_priv(MEMORYINCREASE,stringDup(pXML->lpNewElement,pXML->cbNewElement),0,-1); - pXML->cbNewElement = 0; - } - else - { - // If we didn't have a new element to create - pNew = emptyXMLNode; - - } - } - } - } - break; - - // If we found an end tag - case eTokenTagEnd: - - // If we have node text then add this to the element - if (maybeAddTxT(pXML,token.pStr)) return FALSE; - - // Find the name of the end tag - token = GetNextToken(pXML, &cbTemp, &xtype); - - // The end tag should be text - if (xtype != eTokenText) - { - pXML->error = eXMLErrorMissingEndTagName; - return FALSE; - } - lpszTemp = token.pStr; - - // After the end tag we should find a closing tag - token = GetNextToken(pXML, &cbToken, &xtype); - if (xtype != eTokenCloseTag) - { - pXML->error = eXMLErrorMissingEndTagName; - return FALSE; - } - pXML->lpszText=pXML->lpXML+pXML->nIndex; - - // We need to return to the previous caller. If the name - // of the tag cannot be found we need to keep returning to - // caller until we find a match - if (myTagCompare(d->lpszName, lpszTemp) != 0) -#ifdef STRICT_PARSING - { - pXML->error=eXMLErrorUnmatchedEndTag; - pXML->nIndexMissigEndTag=pXML->nIndex; - return FALSE; - } -#else - { - pXML->error=eXMLErrorMissingEndTag; - pXML->nIndexMissigEndTag=pXML->nIndex; - pXML->lpEndTag = lpszTemp; - pXML->cbEndTag = cbTemp; - } -#endif - - // Return to the caller - exactMemory(d); - return TRUE; - - // If we found a clear (unformatted) token - case eTokenClear: - // If we have node text then add this to the element - if (maybeAddTxT(pXML,token.pStr)) return FALSE; - if (parseClearTag(pXML, token.pClr)) return FALSE; - pXML->lpszText=pXML->lpXML+pXML->nIndex; - break; - - default: - break; - } - break; - - // If we are inside a tag definition we need to search for attributes - case eInsideTag: - - // Check what part of the attribute (name, equals, value) we - // are looking for. - switch(attrib) - { - // If we are looking for a new attribute - case eAttribName: - - // Check what the current token type is - switch(xtype) - { - // If the current type is text... - // Eg. 'attribute' - case eTokenText: - // Cache the token then indicate that we are next to - // look for the equals - lpszTemp = token.pStr; - cbTemp = cbToken; - attrib = eAttribEquals; - break; - - // If we found a closing tag... - // Eg. '>' - case eTokenCloseTag: - // We are now outside the tag - status = eOutsideTag; - pXML->lpszText=pXML->lpXML+pXML->nIndex; - break; - - // If we found a short hand '/>' closing tag then we can - // return to the caller - case eTokenShortHandClose: - exactMemory(d); - pXML->lpszText=pXML->lpXML+pXML->nIndex; - return TRUE; - - // Errors... - case eTokenQuotedText: /* '"SomeText"' */ - case eTokenTagStart: /* '<' */ - case eTokenTagEnd: /* 'error = eXMLErrorUnexpectedToken; - return FALSE; - default: break; - } - break; - - // If we are looking for an equals - case eAttribEquals: - // Check what the current token type is - switch(xtype) - { - // If the current type is text... - // Eg. 'Attribute AnotherAttribute' - case eTokenText: - // Add the unvalued attribute to the list - addAttribute_priv(MEMORYINCREASE,stringDup(lpszTemp,cbTemp), NULL); - // Cache the token then indicate. We are next to - // look for the equals attribute - lpszTemp = token.pStr; - cbTemp = cbToken; - break; - - // If we found a closing tag 'Attribute >' or a short hand - // closing tag 'Attribute />' - case eTokenShortHandClose: - case eTokenCloseTag: - // If we are a declaration element 'lpszText=pXML->lpXML+pXML->nIndex; - - if (d->isDeclaration && - (lpszTemp[cbTemp-1]) == _CXML('?')) - { - cbTemp--; - if (d->pParent && d->pParent->pParent) xtype = eTokenShortHandClose; - } - - if (cbTemp) - { - // Add the unvalued attribute to the list - addAttribute_priv(MEMORYINCREASE,stringDup(lpszTemp,cbTemp), NULL); - } - - // If this is the end of the tag then return to the caller - if (xtype == eTokenShortHandClose) - { - exactMemory(d); - return TRUE; - } - - // We are now outside the tag - status = eOutsideTag; - break; - - // If we found the equals token... - // Eg. 'Attribute =' - case eTokenEquals: - // Indicate that we next need to search for the value - // for the attribute - attrib = eAttribValue; - break; - - // Errors... - case eTokenQuotedText: /* 'Attribute "InvalidAttr"'*/ - case eTokenTagStart: /* 'Attribute <' */ - case eTokenTagEnd: /* 'Attribute error = eXMLErrorUnexpectedToken; - return FALSE; - default: break; - } - break; - - // If we are looking for an attribute value - case eAttribValue: - // Check what the current token type is - switch(xtype) - { - // If the current type is text or quoted text... - // Eg. 'Attribute = "Value"' or 'Attribute = Value' or - // 'Attribute = 'Value''. - case eTokenText: - case eTokenQuotedText: - // If we are a declaration element 'isDeclaration && - (token.pStr[cbToken-1]) == _CXML('?')) - { - cbToken--; - } - - if (cbTemp) - { - // Add the valued attribute to the list - if (xtype==eTokenQuotedText) { token.pStr++; cbToken-=2; } - XMLSTR attrVal=(XMLSTR)token.pStr; - if (attrVal) - { - attrVal=fromXMLString(attrVal,cbToken,pXML); - if (!attrVal) return FALSE; - } - addAttribute_priv(MEMORYINCREASE,stringDup(lpszTemp,cbTemp),attrVal); - } - - // Indicate we are searching for a new attribute - attrib = eAttribName; - break; - - // Errors... - case eTokenTagStart: /* 'Attr = <' */ - case eTokenTagEnd: /* 'Attr = ' */ - case eTokenShortHandClose: /* "Attr = />" */ - case eTokenEquals: /* 'Attr = =' */ - case eTokenDeclaration: /* 'Attr = error = eXMLErrorUnexpectedToken; - return FALSE; - break; - default: break; - } - } - } - } - // If we failed to obtain the next token - else - { - if ((!d->isDeclaration)&&(d->pParent)) - { -#ifdef STRICT_PARSING - pXML->error=eXMLErrorUnmatchedEndTag; -#else - pXML->error=eXMLErrorMissingEndTag; -#endif - pXML->nIndexMissigEndTag=pXML->nIndex; - } - maybeAddTxT(pXML,pXML->lpXML+pXML->nIndex); - return FALSE; - } - } -} - -// Count the number of lines and columns in an XML string. -static void CountLinesAndColumns(XMLCSTR lpXML, int nUpto, XMLResults *pResults) -{ - XMLCHAR ch; - assert(lpXML); - assert(pResults); - - struct XML xml={ lpXML,lpXML, 0, 0, eXMLErrorNone, NULL, 0, NULL, 0, TRUE }; - - pResults->nLine = 1; - pResults->nColumn = 1; - while (xml.nIndexnColumn++; - else - { - pResults->nLine++; - pResults->nColumn=1; - } - } -} - -// Parse XML and return the root element. -XMLNode XMLNode::parseString(XMLCSTR lpszXML, XMLCSTR tag, XMLResults *pResults) -{ - if (!lpszXML) - { - if (pResults) - { - pResults->error=eXMLErrorNoElements; - pResults->nLine=0; - pResults->nColumn=0; - } - return emptyXMLNode; - } - - XMLNode xnode(NULL,NULL,FALSE); - struct XML xml={ lpszXML, lpszXML, 0, 0, eXMLErrorNone, NULL, 0, NULL, 0, TRUE }; - - // Create header element - xnode.ParseXMLElement(&xml); - enum XMLError error = xml.error; - if (!xnode.nChildNode()) error=eXMLErrorNoXMLTagFound; - if ((xnode.nChildNode()==1)&&(xnode.nElement()==1)) xnode=xnode.getChildNode(); // skip the empty node - - // If no error occurred - if ((error==eXMLErrorNone)||(error==eXMLErrorMissingEndTag)||(error==eXMLErrorNoXMLTagFound)) - { - XMLCSTR name=xnode.getName(); - if (tag&&(*tag)&&((!name)||(xstricmp(name,tag)))) - { - xnode=xnode.getChildNode(tag); - if (xnode.isEmpty()) - { - if (pResults) - { - pResults->error=eXMLErrorFirstTagNotFound; - pResults->nLine=0; - pResults->nColumn=0; - } - return emptyXMLNode; - } - } - } else - { - // Cleanup: this will destroy all the nodes - xnode = emptyXMLNode; - } - - - // If we have been given somewhere to place results - if (pResults) - { - pResults->error = error; - - // If we have an error - if (error!=eXMLErrorNone) - { - if (error==eXMLErrorMissingEndTag) xml.nIndex=xml.nIndexMissigEndTag; - // Find which line and column it starts on. - CountLinesAndColumns(xml.lpXML, xml.nIndex, pResults); - } - } - return xnode; -} - -XMLNode XMLNode::parseFile(XMLCSTR filename, XMLCSTR tag, XMLResults *pResults) -{ - if (pResults) { pResults->nLine=0; pResults->nColumn=0; } - FILE *f=xfopen(filename,_CXML("rb")); - if (f==NULL) { if (pResults) pResults->error=eXMLErrorFileNotFound; return emptyXMLNode; } - fseek(f,0,SEEK_END); - int l=(int)ftell(f),headerSz=0; - if (!l) { if (pResults) pResults->error=eXMLErrorEmpty; fclose(f); return emptyXMLNode; } - fseek(f,0,SEEK_SET); - unsigned char *buf=(unsigned char*)malloc(l+4); - l=(int)fread(buf,1,l,f); - fclose(f); - buf[l]=0;buf[l+1]=0;buf[l+2]=0;buf[l+3]=0; -#ifdef _XMLWIDECHAR - if (guessWideCharChars) - { - if (!myIsTextWideChar(buf,l)) - { - XMLNode::XMLCharEncoding ce=XMLNode::char_encoding_legacy; - if ((buf[0]==0xef)&&(buf[1]==0xbb)&&(buf[2]==0xbf)) { headerSz=3; ce=XMLNode::char_encoding_UTF8; } - XMLSTR b2=myMultiByteToWideChar((const char*)(buf+headerSz),ce); - if (!b2) - { - // todo: unable to convert - } - free(buf); buf=(unsigned char*)b2; headerSz=0; - } else - { - if ((buf[0]==0xef)&&(buf[1]==0xff)) headerSz=2; - if ((buf[0]==0xff)&&(buf[1]==0xfe)) headerSz=2; - } - } else - { - if ((buf[0]==0xef)&&(buf[1]==0xff)) headerSz=2; - if ((buf[0]==0xff)&&(buf[1]==0xfe)) headerSz=2; - if ((buf[0]==0xef)&&(buf[1]==0xbb)&&(buf[2]==0xbf)) headerSz=3; - } -#else - if (guessWideCharChars) - { - if (myIsTextWideChar(buf,l)) - { - if ((buf[0]==0xef)&&(buf[1]==0xff)) headerSz=2; - if ((buf[0]==0xff)&&(buf[1]==0xfe)) headerSz=2; - char *b2=myWideCharToMultiByte((const wchar_t*)(buf+headerSz)); - free(buf); buf=(unsigned char*)b2; headerSz=0; - } else - { - if ((buf[0]==0xef)&&(buf[1]==0xbb)&&(buf[2]==0xbf)) headerSz=3; - } - } else - { - if ((buf[0]==0xef)&&(buf[1]==0xff)) headerSz=2; - if ((buf[0]==0xff)&&(buf[1]==0xfe)) headerSz=2; - if ((buf[0]==0xef)&&(buf[1]==0xbb)&&(buf[2]==0xbf)) headerSz=3; - } -#endif - - if (!buf) { if (pResults) pResults->error=eXMLErrorCharConversionError; return emptyXMLNode; } - XMLNode x=parseString((XMLSTR)(buf+headerSz),tag,pResults); - free(buf); - return x; -} - -static inline void charmemset(XMLSTR dest,XMLCHAR c,int l) { while (l--) *(dest++)=c; } -// private: -// Creates an user friendly XML string from a given element with -// appropriate white space and carriage returns. -// -// This recurses through all subnodes then adds contents of the nodes to the -// string. -int XMLNode::CreateXMLStringR(XMLNodeData *pEntry, XMLSTR lpszMarker, int nFormat) -{ - int nResult = 0; - int cb=nFormat<0?0:nFormat; - int cbElement; - int nChildFormat=-1; - int nElementI=pEntry->nChild+pEntry->nText+pEntry->nClear; - int i,j; - if ((nFormat>=0)&&(nElementI==1)&&(pEntry->nText==1)&&(!pEntry->isDeclaration)) nFormat=-2; - - assert(pEntry); - -#define LENSTR(lpsz) (lpsz ? xstrlen(lpsz) : 0) - - // If the element has no name then assume this is the head node. - cbElement = (int)LENSTR(pEntry->lpszName); - - if (cbElement) - { - // "isDeclaration) lpszMarker[nResult++]=_CXML('?'); - xstrcpy(&lpszMarker[nResult], pEntry->lpszName); - nResult+=cbElement; - lpszMarker[nResult++]=_CXML(' '); - - } else - { - nResult+=cbElement+2+cb; - if (pEntry->isDeclaration) nResult++; - } - - // Enumerate attributes and add them to the string - XMLAttribute *pAttr=pEntry->pAttribute; - for (i=0; inAttribute; i++) - { - // "Attrib - cb = (int)LENSTR(pAttr->lpszName); - if (cb) - { - if (lpszMarker) xstrcpy(&lpszMarker[nResult], pAttr->lpszName); - nResult += cb; - // "Attrib=Value " - if (pAttr->lpszValue) - { - cb=(int)ToXMLStringTool::lengthXMLString(pAttr->lpszValue); - if (lpszMarker) - { - lpszMarker[nResult]=_CXML('='); - lpszMarker[nResult+1]=_CXML('"'); - if (cb) ToXMLStringTool::toXMLUnSafe(&lpszMarker[nResult+2],pAttr->lpszValue); - lpszMarker[nResult+cb+2]=_CXML('"'); - } - nResult+=cb+3; - } - if (lpszMarker) lpszMarker[nResult] = _CXML(' '); - nResult++; - } - pAttr++; - } - - if (pEntry->isDeclaration) - { - if (lpszMarker) - { - lpszMarker[nResult-1]=_CXML('?'); - lpszMarker[nResult]=_CXML('>'); - } - nResult++; - if (nFormat!=-1) - { - if (lpszMarker) lpszMarker[nResult]=_CXML('\n'); - nResult++; - } - } else - // If there are child nodes we need to terminate the start tag - if (nElementI) - { - if (lpszMarker) lpszMarker[nResult-1]=_CXML('>'); - if (nFormat>=0) - { - if (lpszMarker) lpszMarker[nResult]=_CXML('\n'); - nResult++; - } - } else nResult--; - } - - // Calculate the child format for when we recurse. This is used to - // determine the number of spaces used for prefixes. - if (nFormat!=-1) - { - if (cbElement&&(!pEntry->isDeclaration)) nChildFormat=nFormat+1; - else nChildFormat=nFormat; - } - - // Enumerate through remaining children - for (i=0; ipOrder[i]; - switch((XMLElementType)(j&3)) - { - // Text nodes - case eNodeText: - { - // "Text" - XMLCSTR pChild=pEntry->pText[j>>2]; - cb = (int)ToXMLStringTool::lengthXMLString(pChild); - if (cb) - { - if (nFormat>=0) - { - if (lpszMarker) - { - charmemset(&lpszMarker[nResult],INDENTCHAR,nFormat+1); - ToXMLStringTool::toXMLUnSafe(&lpszMarker[nResult+nFormat+1],pChild); - lpszMarker[nResult+nFormat+1+cb]=_CXML('\n'); - } - nResult+=cb+nFormat+2; - } else - { - if (lpszMarker) ToXMLStringTool::toXMLUnSafe(&lpszMarker[nResult], pChild); - nResult += cb; - } - } - break; - } - - // Clear type nodes - case eNodeClear: - { - XMLClear *pChild=pEntry->pClear+(j>>2); - // "OpenTag" - cb = (int)LENSTR(pChild->lpszOpenTag); - if (cb) - { - if (nFormat!=-1) - { - if (lpszMarker) - { - charmemset(&lpszMarker[nResult], INDENTCHAR, nFormat+1); - xstrcpy(&lpszMarker[nResult+nFormat+1], pChild->lpszOpenTag); - } - nResult+=cb+nFormat+1; - } - else - { - if (lpszMarker)xstrcpy(&lpszMarker[nResult], pChild->lpszOpenTag); - nResult += cb; - } - } - - // "OpenTag Value" - cb = (int)LENSTR(pChild->lpszValue); - if (cb) - { - if (lpszMarker) xstrcpy(&lpszMarker[nResult], pChild->lpszValue); - nResult += cb; - } - - // "OpenTag Value CloseTag" - cb = (int)LENSTR(pChild->lpszCloseTag); - if (cb) - { - if (lpszMarker) xstrcpy(&lpszMarker[nResult], pChild->lpszCloseTag); - nResult += cb; - } - - if (nFormat!=-1) - { - if (lpszMarker) lpszMarker[nResult] = _CXML('\n'); - nResult++; - } - break; - } - - // Element nodes - case eNodeChild: - { - // Recursively add child nodes - nResult += CreateXMLStringR(pEntry->pChild[j>>2].d, lpszMarker ? lpszMarker + nResult : 0, nChildFormat); - break; - } - default: break; - } - } - - if ((cbElement)&&(!pEntry->isDeclaration)) - { - // If we have child entries we need to use long XML notation for - // closing the element - "blah blah blah" - if (nElementI) - { - // "\0" - if (lpszMarker) - { - if (nFormat >=0) - { - charmemset(&lpszMarker[nResult], INDENTCHAR,nFormat); - nResult+=nFormat; - } - - lpszMarker[nResult]=_CXML('<'); lpszMarker[nResult+1]=_CXML('/'); - nResult += 2; - xstrcpy(&lpszMarker[nResult], pEntry->lpszName); - nResult += cbElement; - - lpszMarker[nResult]=_CXML('>'); - if (nFormat == -1) nResult++; - else - { - lpszMarker[nResult+1]=_CXML('\n'); - nResult+=2; - } - } else - { - if (nFormat>=0) nResult+=cbElement+4+nFormat; - else if (nFormat==-1) nResult+=cbElement+3; - else nResult+=cbElement+4; - } - } else - { - // If there are no children we can use shorthand XML notation - - // "" - // "/>\0" - if (lpszMarker) - { - lpszMarker[nResult]=_CXML('/'); lpszMarker[nResult+1]=_CXML('>'); - if (nFormat != -1) lpszMarker[nResult+2]=_CXML('\n'); - } - nResult += nFormat == -1 ? 2 : 3; - } - } - - return nResult; -} - -#undef LENSTR - -// Create an XML string -// @param int nFormat - 0 if no formatting is required -// otherwise nonzero for formatted text -// with carriage returns and indentation. -// @param int *pnSize - [out] pointer to the size of the -// returned string not including the -// NULL terminator. -// @return XMLSTR - Allocated XML string, you must free -// this with free(). -XMLSTR XMLNode::createXMLString(int nFormat, int *pnSize) const -{ - if (!d) { if (pnSize) *pnSize=0; return NULL; } - - XMLSTR lpszResult = NULL; - int cbStr; - - // Recursively Calculate the size of the XML string - if (!dropWhiteSpace) nFormat=0; - nFormat = nFormat ? 0 : -1; - cbStr = CreateXMLStringR(d, 0, nFormat); - // Alllocate memory for the XML string + the NULL terminator and - // create the recursively XML string. - lpszResult=(XMLSTR)malloc((cbStr+1)*sizeof(XMLCHAR)); - CreateXMLStringR(d, lpszResult, nFormat); - lpszResult[cbStr]=_CXML('\0'); - if (pnSize) *pnSize = cbStr; - return lpszResult; -} - -int XMLNode::detachFromParent(XMLNodeData *d) -{ - XMLNode *pa=d->pParent->pChild; - int i=0; - while (((void*)(pa[i].d))!=((void*)d)) i++; - d->pParent->nChild--; - if (d->pParent->nChild) memmove(pa+i,pa+i+1,(d->pParent->nChild-i)*sizeof(XMLNode)); - else { free(pa); d->pParent->pChild=NULL; } - return removeOrderElement(d->pParent,eNodeChild,i); -} - -XMLNode::~XMLNode() -{ - if (!d) return; - d->ref_count--; - emptyTheNode(0); -} -void XMLNode::deleteNodeContent() -{ - if (!d) return; - if (d->pParent) { detachFromParent(d); d->pParent=NULL; d->ref_count--; } - emptyTheNode(1); -} -void XMLNode::emptyTheNode(char force) -{ - XMLNodeData *dd=d; // warning: must stay this way! - if ((dd->ref_count==0)||force) - { - if (d->pParent) detachFromParent(d); - int i; - XMLNode *pc; - for(i=0; inChild; i++) - { - pc=dd->pChild+i; - pc->d->pParent=NULL; - pc->d->ref_count--; - pc->emptyTheNode(force); - } - myFree(dd->pChild); - for(i=0; inText; i++) free((void*)dd->pText[i]); - myFree(dd->pText); - for(i=0; inClear; i++) free((void*)dd->pClear[i].lpszValue); - myFree(dd->pClear); - for(i=0; inAttribute; i++) - { - free((void*)dd->pAttribute[i].lpszName); - if (dd->pAttribute[i].lpszValue) free((void*)dd->pAttribute[i].lpszValue); - } - myFree(dd->pAttribute); - myFree(dd->pOrder); - myFree((void*)dd->lpszName); - dd->nChild=0; dd->nText=0; dd->nClear=0; dd->nAttribute=0; - dd->pChild=NULL; dd->pText=NULL; dd->pClear=NULL; dd->pAttribute=NULL; - dd->pOrder=NULL; dd->lpszName=NULL; dd->pParent=NULL; - } - if (dd->ref_count==0) - { - free(dd); - d=NULL; - } -} - -XMLNode& XMLNode::operator=( const XMLNode& A ) -{ - // shallow copy - if (this != &A) - { - if (d) { d->ref_count--; emptyTheNode(0); } - d=A.d; - if (d) (d->ref_count) ++ ; - } - return *this; -} - -XMLNode::XMLNode(const XMLNode &A) -{ - // shallow copy - d=A.d; - if (d) (d->ref_count)++ ; -} - -XMLNode XMLNode::deepCopy() const -{ - if (!d) return XMLNode::emptyXMLNode; - XMLNode x(NULL,stringDup(d->lpszName),d->isDeclaration); - XMLNodeData *p=x.d; - int n=d->nAttribute; - if (n) - { - p->nAttribute=n; p->pAttribute=(XMLAttribute*)malloc(n*sizeof(XMLAttribute)); - while (n--) - { - p->pAttribute[n].lpszName=stringDup(d->pAttribute[n].lpszName); - p->pAttribute[n].lpszValue=stringDup(d->pAttribute[n].lpszValue); - } - } - if (d->pOrder) - { - n=(d->nChild+d->nText+d->nClear)*sizeof(int); p->pOrder=(int*)malloc(n); memcpy(p->pOrder,d->pOrder,n); - } - n=d->nText; - if (n) - { - p->nText=n; p->pText=(XMLCSTR*)malloc(n*sizeof(XMLCSTR)); - while(n--) p->pText[n]=stringDup(d->pText[n]); - } - n=d->nClear; - if (n) - { - p->nClear=n; p->pClear=(XMLClear*)malloc(n*sizeof(XMLClear)); - while (n--) - { - p->pClear[n].lpszCloseTag=d->pClear[n].lpszCloseTag; - p->pClear[n].lpszOpenTag=d->pClear[n].lpszOpenTag; - p->pClear[n].lpszValue=stringDup(d->pClear[n].lpszValue); - } - } - n=d->nChild; - if (n) - { - p->nChild=n; p->pChild=(XMLNode*)malloc(n*sizeof(XMLNode)); - while (n--) - { - p->pChild[n].d=NULL; - p->pChild[n]=d->pChild[n].deepCopy(); - p->pChild[n].d->pParent=p; - } - } - return x; -} - -XMLNode XMLNode::addChild(XMLNode childNode, int pos) -{ - XMLNodeData *dc=childNode.d; - if ((!dc)||(!d)) return childNode; - if (!dc->lpszName) - { - // this is a root node: todo: correct fix - int j=pos; - while (dc->nChild) - { - addChild(dc->pChild[0],j); - if (pos>=0) j++; - } - return childNode; - } - if (dc->pParent) { if ((detachFromParent(dc)<=pos)&&(dc->pParent==d)) pos--; } else dc->ref_count++; - dc->pParent=d; -// int nc=d->nChild; -// d->pChild=(XMLNode*)myRealloc(d->pChild,(nc+1),memoryIncrease,sizeof(XMLNode)); - d->pChild=(XMLNode*)addToOrder(0,&pos,d->nChild,d->pChild,sizeof(XMLNode),eNodeChild); - d->pChild[pos].d=dc; - d->nChild++; - return childNode; -} - -void XMLNode::deleteAttribute(int i) -{ - if ((!d)||(i<0)||(i>=d->nAttribute)) return; - d->nAttribute--; - XMLAttribute *p=d->pAttribute+i; - free((void*)p->lpszName); - if (p->lpszValue) free((void*)p->lpszValue); - if (d->nAttribute) memmove(p,p+1,(d->nAttribute-i)*sizeof(XMLAttribute)); else { free(p); d->pAttribute=NULL; } -} - -void XMLNode::deleteAttribute(XMLAttribute *a){ if (a) deleteAttribute(a->lpszName); } -void XMLNode::deleteAttribute(XMLCSTR lpszName) -{ - int j=0; - getAttribute(lpszName,&j); - if (j) deleteAttribute(j-1); -} - -XMLAttribute *XMLNode::updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName,int i) -{ - if (!d) { if (lpszNewValue) free(lpszNewValue); if (lpszNewName) free(lpszNewName); return NULL; } - if (i>=d->nAttribute) - { - if (lpszNewName) return addAttribute_WOSD(lpszNewName,lpszNewValue); - return NULL; - } - XMLAttribute *p=d->pAttribute+i; - if (p->lpszValue&&p->lpszValue!=lpszNewValue) free((void*)p->lpszValue); - p->lpszValue=lpszNewValue; - if (lpszNewName&&p->lpszName!=lpszNewName) { free((void*)p->lpszName); p->lpszName=lpszNewName; }; - return p; -} - -XMLAttribute *XMLNode::updateAttribute_WOSD(XMLAttribute *newAttribute, XMLAttribute *oldAttribute) -{ - if (oldAttribute) return updateAttribute_WOSD((XMLSTR)newAttribute->lpszValue,(XMLSTR)newAttribute->lpszName,oldAttribute->lpszName); - return addAttribute_WOSD((XMLSTR)newAttribute->lpszName,(XMLSTR)newAttribute->lpszValue); -} - -XMLAttribute *XMLNode::updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName,XMLCSTR lpszOldName) -{ - int j=0; - getAttribute(lpszOldName,&j); - if (j) return updateAttribute_WOSD(lpszNewValue,lpszNewName,j-1); - else - { - if (lpszNewName) return addAttribute_WOSD(lpszNewName,lpszNewValue); - else return addAttribute_WOSD(stringDup(lpszOldName),lpszNewValue); - } -} - -int XMLNode::indexText(XMLCSTR lpszValue) const -{ - if (!d) return -1; - int i,l=d->nText; - if (!lpszValue) { if (l) return 0; return -1; } - XMLCSTR *p=d->pText; - for (i=0; i=d->nText)) return; - d->nText--; - XMLCSTR *p=d->pText+i; - free((void*)*p); - if (d->nText) memmove(p,p+1,(d->nText-i)*sizeof(XMLCSTR)); else { free(p); d->pText=NULL; } - removeOrderElement(d,eNodeText,i); -} - -void XMLNode::deleteText(XMLCSTR lpszValue) { deleteText(indexText(lpszValue)); } - -XMLCSTR XMLNode::updateText_WOSD(XMLSTR lpszNewValue, int i) -{ - if (!d) { if (lpszNewValue) free(lpszNewValue); return NULL; } - if (i>=d->nText) return addText_WOSD(lpszNewValue); - XMLCSTR *p=d->pText+i; - if (*p!=lpszNewValue) { free((void*)*p); *p=lpszNewValue; } - return lpszNewValue; -} - -XMLCSTR XMLNode::updateText_WOSD(XMLSTR lpszNewValue, XMLCSTR lpszOldValue) -{ - if (!d) { if (lpszNewValue) free(lpszNewValue); return NULL; } - int i=indexText(lpszOldValue); - if (i>=0) return updateText_WOSD(lpszNewValue,i); - return addText_WOSD(lpszNewValue); -} - -void XMLNode::deleteClear(int i) -{ - if ((!d)||(i<0)||(i>=d->nClear)) return; - d->nClear--; - XMLClear *p=d->pClear+i; - free((void*)p->lpszValue); - if (d->nClear) memmove(p,p+1,(d->nClear-i)*sizeof(XMLClear)); else { free(p); d->pClear=NULL; } - removeOrderElement(d,eNodeClear,i); -} - -int XMLNode::indexClear(XMLCSTR lpszValue) const -{ - if (!d) return -1; - int i,l=d->nClear; - if (!lpszValue) { if (l) return 0; return -1; } - XMLClear *p=d->pClear; - for (i=0; ilpszValue); } - -XMLClear *XMLNode::updateClear_WOSD(XMLSTR lpszNewContent, int i) -{ - if (!d) { if (lpszNewContent) free(lpszNewContent); return NULL; } - if (i>=d->nClear) return addClear_WOSD(lpszNewContent); - XMLClear *p=d->pClear+i; - if (lpszNewContent!=p->lpszValue) { free((void*)p->lpszValue); p->lpszValue=lpszNewContent; } - return p; -} - -XMLClear *XMLNode::updateClear_WOSD(XMLSTR lpszNewContent, XMLCSTR lpszOldValue) -{ - if (!d) { if (lpszNewContent) free(lpszNewContent); return NULL; } - int i=indexClear(lpszOldValue); - if (i>=0) return updateClear_WOSD(lpszNewContent,i); - return addClear_WOSD(lpszNewContent); -} - -XMLClear *XMLNode::updateClear_WOSD(XMLClear *newP,XMLClear *oldP) -{ - if (oldP) return updateClear_WOSD((XMLSTR)newP->lpszValue,(XMLSTR)oldP->lpszValue); - return NULL; -} - -int XMLNode::nChildNode(XMLCSTR name) const -{ - if (!d) return 0; - int i,j=0,n=d->nChild; - XMLNode *pc=d->pChild; - for (i=0; id->lpszName, name)==0) j++; - pc++; - } - return j; -} - -XMLNode XMLNode::getChildNode(XMLCSTR name, int *j) const -{ - if (!d) return emptyXMLNode; - int i=0,n=d->nChild; - if (j) i=*j; - XMLNode *pc=d->pChild+i; - for (; id->lpszName, name)) - { - if (j) *j=i+1; - return *pc; - } - pc++; - } - return emptyXMLNode; -} - -XMLNode XMLNode::getChildNode(XMLCSTR name, int j) const -{ - if (!d) return emptyXMLNode; - if (j>=0) - { - int i=0; - while (j-->0) getChildNode(name,&i); - return getChildNode(name,&i); - } - int i=d->nChild; - while (i--) if (!xstricmp(name,d->pChild[i].d->lpszName)) break; - if (i<0) return emptyXMLNode; - return getChildNode(i); -} - -XMLNode XMLNode::getChildNodeByPath(XMLCSTR _path, char createMissing, XMLCHAR sep) -{ - XMLSTR path=stringDup(_path); - XMLNode x=getChildNodeByPathNonConst(path,createMissing,sep); - if (path) free(path); - return x; -} - -XMLNode XMLNode::getChildNodeByPathNonConst(XMLSTR path, char createIfMissing, XMLCHAR sep) -{ - if ((!path)||(!(*path))) return *this; - XMLNode xn,xbase=*this; - XMLCHAR *tend1,sepString[2]; sepString[0]=sep; sepString[1]=0; - tend1=xstrstr(path,sepString); - while(tend1) - { - *tend1=0; - xn=xbase.getChildNode(path); - if (xn.isEmpty()) - { - if (createIfMissing) xn=xbase.addChild(path); - else { *tend1=sep; return XMLNode::emptyXMLNode; } - } - *tend1=sep; - xbase=xn; - path=tend1+1; - tend1=xstrstr(path,sepString); - } - xn=xbase.getChildNode(path); - if (xn.isEmpty()&&createIfMissing) xn=xbase.addChild(path); - return xn; -} - -XMLElementPosition XMLNode::positionOfText (int i) const { if (i>=d->nText ) i=d->nText-1; return findPosition(d,i,eNodeText ); } -XMLElementPosition XMLNode::positionOfClear (int i) const { if (i>=d->nClear) i=d->nClear-1; return findPosition(d,i,eNodeClear); } -XMLElementPosition XMLNode::positionOfChildNode(int i) const { if (i>=d->nChild) i=d->nChild-1; return findPosition(d,i,eNodeChild); } -XMLElementPosition XMLNode::positionOfText (XMLCSTR lpszValue) const { return positionOfText (indexText (lpszValue)); } -XMLElementPosition XMLNode::positionOfClear(XMLCSTR lpszValue) const { return positionOfClear(indexClear(lpszValue)); } -XMLElementPosition XMLNode::positionOfClear(XMLClear *a) const { if (a) return positionOfClear(a->lpszValue); return positionOfClear(); } -XMLElementPosition XMLNode::positionOfChildNode(XMLNode x) const -{ - if ((!d)||(!x.d)) return -1; - XMLNodeData *dd=x.d; - XMLNode *pc=d->pChild; - int i=d->nChild; - while (i--) if (pc[i].d==dd) return findPosition(d,i,eNodeChild); - return -1; -} -XMLElementPosition XMLNode::positionOfChildNode(XMLCSTR name, int count) const -{ - if (!name) return positionOfChildNode(count); - int j=0; - do { getChildNode(name,&j); if (j<0) return -1; } while (count--); - return findPosition(d,j-1,eNodeChild); -} - -XMLNode XMLNode::getChildNodeWithAttribute(XMLCSTR name,XMLCSTR attributeName,XMLCSTR attributeValue, int *k) const -{ - int i=0,j; - if (k) i=*k; - XMLNode x; - XMLCSTR t; - do - { - x=getChildNode(name,&i); - if (!x.isEmpty()) - { - if (attributeValue) - { - j=0; - do - { - t=x.getAttribute(attributeName,&j); - if (t&&(xstricmp(attributeValue,t)==0)) { if (k) *k=i; return x; } - } while (t); - } else - { - if (x.isAttributeSet(attributeName)) { if (k) *k=i; return x; } - } - } - } while (!x.isEmpty()); - return emptyXMLNode; -} - -// Find an attribute on an node. -XMLCSTR XMLNode::getAttribute(XMLCSTR lpszAttrib, int *j) const -{ - if (!d) return NULL; - int i=0,n=d->nAttribute; - if (j) i=*j; - XMLAttribute *pAttr=d->pAttribute+i; - for (; ilpszName, lpszAttrib)==0) - { - if (j) *j=i+1; - return pAttr->lpszValue; - } - pAttr++; - } - return NULL; -} - -char XMLNode::isAttributeSet(XMLCSTR lpszAttrib) const -{ - if (!d) return FALSE; - int i,n=d->nAttribute; - XMLAttribute *pAttr=d->pAttribute; - for (i=0; ilpszName, lpszAttrib)==0) - { - return TRUE; - } - pAttr++; - } - return FALSE; -} - -XMLCSTR XMLNode::getAttribute(XMLCSTR name, int j) const -{ - if (!d) return NULL; - int i=0; - while (j-->0) getAttribute(name,&i); - return getAttribute(name,&i); -} - -XMLNodeContents XMLNode::enumContents(int i) const -{ - XMLNodeContents c; - if (!d) { c.etype=eNodeNULL; return c; } - if (inAttribute) - { - c.etype=eNodeAttribute; - c.attrib=d->pAttribute[i]; - return c; - } - i-=d->nAttribute; - c.etype=(XMLElementType)(d->pOrder[i]&3); - i=(d->pOrder[i])>>2; - switch (c.etype) - { - case eNodeChild: c.child = d->pChild[i]; break; - case eNodeText: c.text = d->pText[i]; break; - case eNodeClear: c.clear = d->pClear[i]; break; - default: break; - } - return c; -} - -XMLCSTR XMLNode::getName() const { if (!d) return NULL; return d->lpszName; } -int XMLNode::nText() const { if (!d) return 0; return d->nText; } -int XMLNode::nChildNode() const { if (!d) return 0; return d->nChild; } -int XMLNode::nAttribute() const { if (!d) return 0; return d->nAttribute; } -int XMLNode::nClear() const { if (!d) return 0; return d->nClear; } -int XMLNode::nElement() const { if (!d) return 0; return d->nAttribute+d->nChild+d->nText+d->nClear; } -XMLClear XMLNode::getClear (int i) const { if ((!d)||(i>=d->nClear )) return emptyXMLClear; return d->pClear[i]; } -XMLAttribute XMLNode::getAttribute (int i) const { if ((!d)||(i>=d->nAttribute)) return emptyXMLAttribute; return d->pAttribute[i]; } -XMLCSTR XMLNode::getAttributeName (int i) const { if ((!d)||(i>=d->nAttribute)) return NULL; return d->pAttribute[i].lpszName; } -XMLCSTR XMLNode::getAttributeValue(int i) const { if ((!d)||(i>=d->nAttribute)) return NULL; return d->pAttribute[i].lpszValue; } -XMLCSTR XMLNode::getText (int i) const { if ((!d)||(i>=d->nText )) return NULL; return d->pText[i]; } -XMLNode XMLNode::getChildNode (int i) const { if ((!d)||(i>=d->nChild )) return emptyXMLNode; return d->pChild[i]; } -XMLNode XMLNode::getParentNode ( ) const { if ((!d)||(!d->pParent )) return emptyXMLNode; return XMLNode(d->pParent); } -char XMLNode::isDeclaration ( ) const { if (!d) return 0; return d->isDeclaration; } -char XMLNode::isEmpty ( ) const { return (d==NULL); } -XMLNode XMLNode::emptyNode ( ) { return XMLNode::emptyXMLNode; } - -XMLNode XMLNode::addChild(XMLCSTR lpszName, char isDeclaration, XMLElementPosition pos) - { return addChild_priv(0,stringDup(lpszName),isDeclaration,pos); } -XMLNode XMLNode::addChild_WOSD(XMLSTR lpszName, char isDeclaration, XMLElementPosition pos) - { return addChild_priv(0,lpszName,isDeclaration,pos); } -XMLAttribute *XMLNode::addAttribute(XMLCSTR lpszName, XMLCSTR lpszValue) - { return addAttribute_priv(0,stringDup(lpszName),stringDup(lpszValue)); } -XMLAttribute *XMLNode::addAttribute_WOSD(XMLSTR lpszName, XMLSTR lpszValuev) - { return addAttribute_priv(0,lpszName,lpszValuev); } -XMLCSTR XMLNode::addText(XMLCSTR lpszValue, XMLElementPosition pos) - { return addText_priv(0,stringDup(lpszValue),pos); } -XMLCSTR XMLNode::addText_WOSD(XMLSTR lpszValue, XMLElementPosition pos) - { return addText_priv(0,lpszValue,pos); } -XMLClear *XMLNode::addClear(XMLCSTR lpszValue, XMLCSTR lpszOpen, XMLCSTR lpszClose, XMLElementPosition pos) - { return addClear_priv(0,stringDup(lpszValue),lpszOpen,lpszClose,pos); } -XMLClear *XMLNode::addClear_WOSD(XMLSTR lpszValue, XMLCSTR lpszOpen, XMLCSTR lpszClose, XMLElementPosition pos) - { return addClear_priv(0,lpszValue,lpszOpen,lpszClose,pos); } -XMLCSTR XMLNode::updateName(XMLCSTR lpszName) - { return updateName_WOSD(stringDup(lpszName)); } -XMLAttribute *XMLNode::updateAttribute(XMLAttribute *newAttribute, XMLAttribute *oldAttribute) - { return updateAttribute_WOSD(stringDup(newAttribute->lpszValue),stringDup(newAttribute->lpszName),oldAttribute->lpszName); } -XMLAttribute *XMLNode::updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName,int i) - { return updateAttribute_WOSD(stringDup(lpszNewValue),stringDup(lpszNewName),i); } -XMLAttribute *XMLNode::updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName,XMLCSTR lpszOldName) - { return updateAttribute_WOSD(stringDup(lpszNewValue),stringDup(lpszNewName),lpszOldName); } -XMLCSTR XMLNode::updateText(XMLCSTR lpszNewValue, int i) - { return updateText_WOSD(stringDup(lpszNewValue),i); } -XMLCSTR XMLNode::updateText(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue) - { return updateText_WOSD(stringDup(lpszNewValue),lpszOldValue); } -XMLClear *XMLNode::updateClear(XMLCSTR lpszNewContent, int i) - { return updateClear_WOSD(stringDup(lpszNewContent),i); } -XMLClear *XMLNode::updateClear(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue) - { return updateClear_WOSD(stringDup(lpszNewValue),lpszOldValue); } -XMLClear *XMLNode::updateClear(XMLClear *newP,XMLClear *oldP) - { return updateClear_WOSD(stringDup(newP->lpszValue),oldP->lpszValue); } - -char XMLNode::setGlobalOptions(XMLCharEncoding _characterEncoding, char _guessWideCharChars, - char _dropWhiteSpace, char _removeCommentsInMiddleOfText) -{ - guessWideCharChars=_guessWideCharChars; dropWhiteSpace=_dropWhiteSpace; removeCommentsInMiddleOfText=_removeCommentsInMiddleOfText; -#ifdef _XMLWIDECHAR - if (_characterEncoding) characterEncoding=_characterEncoding; -#else - switch(_characterEncoding) - { - case char_encoding_UTF8: characterEncoding=_characterEncoding; XML_ByteTable=XML_utf8ByteTable; break; - case char_encoding_legacy: characterEncoding=_characterEncoding; XML_ByteTable=XML_legacyByteTable; break; - case char_encoding_ShiftJIS: characterEncoding=_characterEncoding; XML_ByteTable=XML_sjisByteTable; break; - case char_encoding_GB2312: characterEncoding=_characterEncoding; XML_ByteTable=XML_gb2312ByteTable; break; - case char_encoding_Big5: - case char_encoding_GBK: characterEncoding=_characterEncoding; XML_ByteTable=XML_gbk_big5_ByteTable; break; - default: return 1; - } -#endif - return 0; -} - -XMLNode::XMLCharEncoding XMLNode::guessCharEncoding(void *buf,int l, char useXMLEncodingAttribute) -{ -#ifdef _XMLWIDECHAR - return (XMLCharEncoding)0; -#else - if (l<25) return (XMLCharEncoding)0; - if (guessWideCharChars&&(myIsTextWideChar(buf,l))) return (XMLCharEncoding)0; - unsigned char *b=(unsigned char*)buf; - if ((b[0]==0xef)&&(b[1]==0xbb)&&(b[2]==0xbf)) return char_encoding_UTF8; - - // Match utf-8 model ? - XMLCharEncoding bestGuess=char_encoding_UTF8; - int i=0; - while (i>2 ]; - *(curr++)=base64EncodeTable[(inbuf[0]<<4)&0x3F]; - *(curr++)=base64Fillchar; - *(curr++)=base64Fillchar; - } else if (eLen==2) - { - j=(inbuf[0]<<8)|inbuf[1]; - *(curr++)=base64EncodeTable[ j>>10 ]; - *(curr++)=base64EncodeTable[(j>> 4)&0x3f]; - *(curr++)=base64EncodeTable[(j<< 2)&0x3f]; - *(curr++)=base64Fillchar; - } - *(curr++)=0; - return (XMLSTR)buf; -} - -unsigned int XMLParserBase64Tool::decodeSize(XMLCSTR data,XMLError *xe) -{ - if (!data) return 0; - if (xe) *xe=eXMLErrorNone; - int size=0; - unsigned char c; - //skip any extra characters (e.g. newlines or spaces) - while (*data) - { -#ifdef _XMLWIDECHAR - if (*data>255) { if (xe) *xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; } -#endif - c=base64DecodeTable[(unsigned char)(*data)]; - if (c<97) size++; - else if (c==98) { if (xe) *xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; } - data++; - } - if (xe&&(size%4!=0)) *xe=eXMLErrorBase64DataSizeIsNotMultipleOf4; - if (size==0) return 0; - do { data--; size--; } while(*data==base64Fillchar); size++; - return (unsigned int)((size*3)/4); -} - -unsigned char XMLParserBase64Tool::decode(XMLCSTR data, unsigned char *buf, int len, XMLError *xe) -{ - if (!data) return 0; - if (xe) *xe=eXMLErrorNone; - int i=0,p=0; - unsigned char d,c; - for(;;) - { - -#ifdef _XMLWIDECHAR -#define BASE64DECODE_READ_NEXT_CHAR(c) \ - do { \ - if (data[i]>255){ c=98; break; } \ - c=base64DecodeTable[(unsigned char)data[i++]]; \ - }while (c==97); \ - if(c==98){ if(xe)*xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; } -#else -#define BASE64DECODE_READ_NEXT_CHAR(c) \ - do { c=base64DecodeTable[(unsigned char)data[i++]]; }while (c==97); \ - if(c==98){ if(xe)*xe=eXMLErrorBase64DecodeIllegalCharacter; return 0; } -#endif - - BASE64DECODE_READ_NEXT_CHAR(c) - if (c==99) { return 2; } - if (c==96) - { - if (p==(int)len) return 2; - if (xe) *xe=eXMLErrorBase64DecodeTruncatedData; - return 1; - } - - BASE64DECODE_READ_NEXT_CHAR(d) - if ((d==99)||(d==96)) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData; return 1; } - if (p==(int)len) { if (xe) *xe=eXMLErrorBase64DecodeBufferTooSmall; return 0; } - buf[p++]=(unsigned char)((c<<2)|((d>>4)&0x3)); - - BASE64DECODE_READ_NEXT_CHAR(c) - if (c==99) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData; return 1; } - if (p==(int)len) - { - if (c==96) return 2; - if (xe) *xe=eXMLErrorBase64DecodeBufferTooSmall; - return 0; - } - if (c==96) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData; return 1; } - buf[p++]=(unsigned char)(((d<<4)&0xf0)|((c>>2)&0xf)); - - BASE64DECODE_READ_NEXT_CHAR(d) - if (d==99 ) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData; return 1; } - if (p==(int)len) - { - if (d==96) return 2; - if (xe) *xe=eXMLErrorBase64DecodeBufferTooSmall; - return 0; - } - if (d==96) { if (xe) *xe=eXMLErrorBase64DecodeTruncatedData; return 1; } - buf[p++]=(unsigned char)(((c<<6)&0xc0)|d); - } -} -#undef BASE64DECODE_READ_NEXT_CHAR - -void XMLParserBase64Tool::alloc(int newsize) -{ - if ((!buf)&&(newsize)) { buf=malloc(newsize); buflen=newsize; return; } - if (newsize>buflen) { buf=realloc(buf,newsize); buflen=newsize; } -} - -unsigned char *XMLParserBase64Tool::decode(XMLCSTR data, int *outlen, XMLError *xe) -{ - if (xe) *xe=eXMLErrorNone; - if (!data) { *outlen=0; return (unsigned char*)""; } - unsigned int len=decodeSize(data,xe); - if (outlen) *outlen=len; - if (!len) return NULL; - alloc(len+1); - if(!decode(data,(unsigned char*)buf,len,xe)){ return NULL; } - return (unsigned char*)buf; -} - diff --git a/test/xml/xmlParser.h b/test/xml/xmlParser.h deleted file mode 100644 index b95ee5d..0000000 --- a/test/xml/xmlParser.h +++ /dev/null @@ -1,732 +0,0 @@ -/****************************************************************************/ -/*! \mainpage XMLParser library - * \section intro_sec Introduction - * - * This is a basic XML parser written in ANSI C++ for portability. - * It works by using recursion and a node tree for breaking - * down the elements of an XML document. - * - * @version V2.44 - * @author Frank Vanden Berghen - * - * Copyright (c) 2002, Frank Vanden Berghen - All rights reserved.
- * Commercialized by Business-Insight
- * See the file AFPL-license.txt about the licensing terms - * - * \section tutorial First Tutorial - * You can follow a simple Tutorial to know the basics... - * - * \section usage General usage: How to include the XMLParser library inside your project. - * - * The library is composed of two files: xmlParser.cpp and - * xmlParser.h. These are the ONLY 2 files that you need when - * using the library inside your own projects. - * - * All the functions of the library are documented inside the comments of the file - * xmlParser.h. These comments can be transformed in - * full-fledged HTML documentation using the DOXYGEN software: simply type: "doxygen doxy.cfg" - * - * By default, the XMLParser library uses (char*) for string representation.To use the (wchar_t*) - * version of the library, you need to define the "_UNICODE" preprocessor definition variable - * (this is usually done inside your project definition file) (This is done automatically for you - * when using Visual Studio). - * - * \section example Advanced Tutorial and Many Examples of usage. - * - * Some very small introductory examples are described inside the Tutorial file - * xmlParser.html - * - * Some additional small examples are also inside the file xmlTest.cpp - * (for the "char*" version of the library) and inside the file - * xmlTestUnicode.cpp (for the "wchar_t*" - * version of the library). If you have a question, please review these additionnal examples - * before sending an e-mail to the author. - * - * To build the examples: - * - linux/unix: type "make" - * - solaris: type "make -f makefile.solaris" - * - windows: Visual Studio: double-click on xmlParser.dsw - * (under Visual Studio .NET, the .dsp and .dsw files will be automatically converted to .vcproj and .sln files) - * - * In order to build the examples you need some additional files: - * - linux/unix: makefile - * - solaris: makefile.solaris - * - windows: Visual Studio: *.dsp, xmlParser.dsw and also xmlParser.lib and xmlParser.dll - * - * \section debugging Debugging with the XMLParser library - * - * \subsection debugwin Debugging under WINDOWS - * - * Inside Visual C++, the "debug versions" of the memory allocation functions are - * very slow: Do not forget to compile in "release mode" to get maximum speed. - * When I had to debug a software that was using the XMLParser Library, it was usually - * a nightmare because the library was sooOOOoooo slow in debug mode (because of the - * slow memory allocations in Debug mode). To solve this - * problem, during all the debugging session, I am now using a very fast DLL version of the - * XMLParser Library (the DLL is compiled in release mode). Using the DLL version of - * the XMLParser Library allows me to have lightening XML parsing speed even in debug! - * Other than that, the DLL version is useless: In the release version of my tool, - * I always use the normal, ".cpp"-based, XMLParser Library (I simply include the - * xmlParser.cpp and - * xmlParser.h files into the project). - * - * The file XMLNodeAutoexp.txt contains some - * "tweaks" that improve substancially the display of the content of the XMLNode objects - * inside the Visual Studio Debugger. Believe me, once you have seen inside the debugger - * the "smooth" display of the XMLNode objects, you cannot live without it anymore! - * - * \subsection debuglinux Debugging under LINUX/UNIX - * - * The speed of the debug version of the XMLParser library is tolerable so no extra - * work.has been done. - * - ****************************************************************************/ - -#ifndef __INCLUDE_XML_NODE__ -#define __INCLUDE_XML_NODE__ - -#include - -#if defined(UNICODE) || defined(_UNICODE) -// If you comment the next "define" line then the library will never "switch to" _UNICODE (wchar_t*) mode (16/32 bits per characters). -// This is useful when you get error messages like: -// 'XMLNode::openFileHelper' : cannot convert parameter 2 from 'const char [5]' to 'const wchar_t *' -// The _XMLWIDECHAR preprocessor variable force the XMLParser library into either utf16/32-mode (the proprocessor variable -// must be defined) or utf8-mode(the pre-processor variable must be undefined). -// #define _XMLWIDECHAR -#endif - -#if defined(WIN32) || defined(UNDER_CE) || defined(_WIN32) || defined(WIN64) || defined(__BORLANDC__) -// comment the next line if you are under windows and the compiler is not Microsoft Visual Studio (6.0 or .NET) or Borland -#define _XMLWINDOWS -#endif - -#ifdef XMLDLLENTRY -#undef XMLDLLENTRY -#endif -#ifdef _USE_XMLPARSER_DLL -#ifdef _DLL_EXPORTS_ -#define XMLDLLENTRY __declspec(dllexport) -#else -#define XMLDLLENTRY __declspec(dllimport) -#endif -#else -#define XMLDLLENTRY -#endif - -// uncomment the next line if you want no support for wchar_t* (no need for the or libraries anymore to compile) -//#define XML_NO_WIDE_CHAR - -#ifdef XML_NO_WIDE_CHAR -#undef _XMLWINDOWS -#undef _XMLWIDECHAR -#endif - -#ifdef _XMLWINDOWS -#include -#else -#define XMLDLLENTRY -#ifndef XML_NO_WIDE_CHAR -#include // to have 'wcsrtombs' for ANSI version - // to have 'mbsrtowcs' for WIDECHAR version -#endif -#endif - -// Some common types for char set portable code -#ifdef _XMLWIDECHAR - #define _CXML(c) L ## c - #define XMLCSTR const wchar_t * - #define XMLSTR wchar_t * - #define XMLCHAR wchar_t -#else - #define _CXML(c) c - #define XMLCSTR const char * - #define XMLSTR char * - #define XMLCHAR char -#endif -#ifndef FALSE - #define FALSE 0 -#endif /* FALSE */ -#ifndef TRUE - #define TRUE 1 -#endif /* TRUE */ - - -/// Enumeration for XML parse errors. -typedef enum XMLError -{ - eXMLErrorNone = 0, - eXMLErrorMissingEndTag, - eXMLErrorNoXMLTagFound, - eXMLErrorEmpty, - eXMLErrorMissingTagName, - eXMLErrorMissingEndTagName, - eXMLErrorUnmatchedEndTag, - eXMLErrorUnmatchedEndClearTag, - eXMLErrorUnexpectedToken, - eXMLErrorNoElements, - eXMLErrorFileNotFound, - eXMLErrorFirstTagNotFound, - eXMLErrorUnknownCharacterEntity, - eXMLErrorCharacterCodeAbove255, - eXMLErrorCharConversionError, - eXMLErrorCannotOpenWriteFile, - eXMLErrorCannotWriteFile, - - eXMLErrorBase64DataSizeIsNotMultipleOf4, - eXMLErrorBase64DecodeIllegalCharacter, - eXMLErrorBase64DecodeTruncatedData, - eXMLErrorBase64DecodeBufferTooSmall -} XMLError; - - -/// Enumeration used to manage type of data. Use in conjunction with structure XMLNodeContents -typedef enum XMLElementType -{ - eNodeChild=0, - eNodeAttribute=1, - eNodeText=2, - eNodeClear=3, - eNodeNULL=4 -} XMLElementType; - -/// Structure used to obtain error details if the parse fails. -typedef struct XMLResults -{ - enum XMLError error; - int nLine,nColumn; -} XMLResults; - -/// Structure for XML clear (unformatted) node (usually comments) -typedef struct XMLClear { - XMLCSTR lpszValue; XMLCSTR lpszOpenTag; XMLCSTR lpszCloseTag; -} XMLClear; - -/// Structure for XML attribute. -typedef struct XMLAttribute { - XMLCSTR lpszName; XMLCSTR lpszValue; -} XMLAttribute; - -/// XMLElementPosition are not interchangeable with simple indexes -typedef int XMLElementPosition; - -struct XMLNodeContents; - -/** @defgroup XMLParserGeneral The XML parser */ - -/// Main Class representing a XML node -/** - * All operations are performed using this class. - * \note The constructors of the XMLNode class are protected, so use instead one of these four methods to get your first instance of XMLNode: - *
    - *
  • XMLNode::parseString
  • - *
  • XMLNode::parseFile
  • - *
  • XMLNode::openFileHelper
  • - *
  • XMLNode::createXMLTopNode (or XMLNode::createXMLTopNode_WOSD)
  • - *
*/ -typedef struct XMLDLLENTRY XMLNode -{ - private: - - struct XMLNodeDataTag; - - /// Constructors are protected, so use instead one of: XMLNode::parseString, XMLNode::parseFile, XMLNode::openFileHelper, XMLNode::createXMLTopNode - XMLNode(struct XMLNodeDataTag *pParent, XMLSTR lpszName, char isDeclaration); - /// Constructors are protected, so use instead one of: XMLNode::parseString, XMLNode::parseFile, XMLNode::openFileHelper, XMLNode::createXMLTopNode - XMLNode(struct XMLNodeDataTag *p); - - public: - static XMLCSTR getVersion();///< Return the XMLParser library version number - - /** @defgroup conversions Parsing XML files/strings to an XMLNode structure and Rendering XMLNode's to files/string. - * @ingroup XMLParserGeneral - * @{ */ - - /// Parse an XML string and return the root of a XMLNode tree representing the string. - static XMLNode parseString (XMLCSTR lpXMLString, XMLCSTR tag=NULL, XMLResults *pResults=NULL); - /**< The "parseString" function parse an XML string and return the root of a XMLNode tree. The "opposite" of this function is - * the function "createXMLString" that re-creates an XML string from an XMLNode tree. If the XML document is corrupted, the - * "parseString" method will initialize the "pResults" variable with some information that can be used to trace the error. - * If you still want to parse the file, you can use the APPROXIMATE_PARSING option as explained inside the note at the - * beginning of the "xmlParser.cpp" file. - * - * @param lpXMLString the XML string to parse - * @param tag the name of the first tag inside the XML file. If the tag parameter is omitted, this function returns a node that represents the head of the xml document including the declaration term (). - * @param pResults a pointer to a XMLResults variable that will contain some information that can be used to trace the XML parsing error. You can have a user-friendly explanation of the parsing error with the "getError" function. - */ - - /// Parse an XML file and return the root of a XMLNode tree representing the file. - static XMLNode parseFile (XMLCSTR filename, XMLCSTR tag=NULL, XMLResults *pResults=NULL); - /**< The "parseFile" function parse an XML file and return the root of a XMLNode tree. The "opposite" of this function is - * the function "writeToFile" that re-creates an XML file from an XMLNode tree. If the XML document is corrupted, the - * "parseFile" method will initialize the "pResults" variable with some information that can be used to trace the error. - * If you still want to parse the file, you can use the APPROXIMATE_PARSING option as explained inside the note at the - * beginning of the "xmlParser.cpp" file. - * - * @param filename the path to the XML file to parse - * @param tag the name of the first tag inside the XML file. If the tag parameter is omitted, this function returns a node that represents the head of the xml document including the declaration term (). - * @param pResults a pointer to a XMLResults variable that will contain some information that can be used to trace the XML parsing error. You can have a user-friendly explanation of the parsing error with the "getError" function. - */ - - /// Parse an XML file and return the root of a XMLNode tree representing the file. A very crude error checking is made. An attempt to guess the Char Encoding used in the file is made. - static XMLNode openFileHelper(XMLCSTR filename, XMLCSTR tag=NULL); - /**< The "openFileHelper" function reports to the screen all the warnings and errors that occurred during parsing of the XML file. - * This function also tries to guess char Encoding (UTF-8, ASCII or SHIT-JIS) based on the first 200 bytes of the file. Since each - * application has its own way to report and deal with errors, you should rather use the "parseFile" function to parse XML files - * and program yourself thereafter an "error reporting" tailored for your needs (instead of using the very crude "error reporting" - * mechanism included inside the "openFileHelper" function). - * - * If the XML document is corrupted, the "openFileHelper" method will: - * - display an error message on the console (or inside a messageBox for windows). - * - stop execution (exit). - * - * I strongly suggest that you write your own "openFileHelper" method tailored to your needs. If you still want to parse - * the file, you can use the APPROXIMATE_PARSING option as explained inside the note at the beginning of the "xmlParser.cpp" file. - * - * @param filename the path of the XML file to parse. - * @param tag the name of the first tag inside the XML file. If the tag parameter is omitted, this function returns a node that represents the head of the xml document including the declaration term (). - */ - - static XMLCSTR getError(XMLError error); ///< this gives you a user-friendly explanation of the parsing error - - /// Create an XML string starting from the current XMLNode. - XMLSTR createXMLString(int nFormat=1, int *pnSize=NULL) const; - /**< The returned string should be free'd using the "freeXMLString" function. - * - * If nFormat==0, no formatting is required otherwise this returns an user friendly XML string from a given element - * with appropriate white spaces and carriage returns. if pnSize is given it returns the size in character of the string. */ - - /// Save the content of an xmlNode inside a file - XMLError writeToFile(XMLCSTR filename, - const char *encoding=NULL, - char nFormat=1) const; - /**< If nFormat==0, no formatting is required otherwise this returns an user friendly XML string from a given element with appropriate white spaces and carriage returns. - * If the global parameter "characterEncoding==encoding_UTF8", then the "encoding" parameter is ignored and always set to "utf-8". - * If the global parameter "characterEncoding==encoding_ShiftJIS", then the "encoding" parameter is ignored and always set to "SHIFT-JIS". - * If "_XMLWIDECHAR=1", then the "encoding" parameter is ignored and always set to "utf-16". - * If no "encoding" parameter is given the "ISO-8859-1" encoding is used. */ - /** @} */ - - /** @defgroup navigate Navigate the XMLNode structure - * @ingroup XMLParserGeneral - * @{ */ - XMLCSTR getName() const; ///< name of the node - XMLCSTR getText(int i=0) const; ///< return ith text field - int nText() const; ///< nbr of text field - XMLNode getParentNode() const; ///< return the parent node - XMLNode getChildNode(int i=0) const; ///< return ith child node - XMLNode getChildNode(XMLCSTR name, int i) const; ///< return ith child node with specific name (return an empty node if failing). If i==-1, this returns the last XMLNode with the given name. - XMLNode getChildNode(XMLCSTR name, int *i=NULL) const; ///< return next child node with specific name (return an empty node if failing) - XMLNode getChildNodeWithAttribute(XMLCSTR tagName, - XMLCSTR attributeName, - XMLCSTR attributeValue=NULL, - int *i=NULL) const; ///< return child node with specific name/attribute (return an empty node if failing) - XMLNode getChildNodeByPath(XMLCSTR path, char createNodeIfMissing=0, XMLCHAR sep='/'); - ///< return the first child node with specific path - XMLNode getChildNodeByPathNonConst(XMLSTR path, char createNodeIfMissing=0, XMLCHAR sep='/'); - ///< return the first child node with specific path. - - int nChildNode(XMLCSTR name) const; ///< return the number of child node with specific name - int nChildNode() const; ///< nbr of child node - XMLAttribute getAttribute(int i=0) const; ///< return ith attribute - XMLCSTR getAttributeName(int i=0) const; ///< return ith attribute name - XMLCSTR getAttributeValue(int i=0) const; ///< return ith attribute value - char isAttributeSet(XMLCSTR name) const; ///< test if an attribute with a specific name is given - XMLCSTR getAttribute(XMLCSTR name, int i) const; ///< return ith attribute content with specific name (return a NULL if failing) - XMLCSTR getAttribute(XMLCSTR name, int *i=NULL) const; ///< return next attribute content with specific name (return a NULL if failing) - int nAttribute() const; ///< nbr of attribute - XMLClear getClear(int i=0) const; ///< return ith clear field (comments) - int nClear() const; ///< nbr of clear field - XMLNodeContents enumContents(XMLElementPosition i) const; ///< enumerate all the different contents (attribute,child,text, clear) of the current XMLNode. The order is reflecting the order of the original file/string. NOTE: 0 <= i < nElement(); - int nElement() const; ///< nbr of different contents for current node - char isEmpty() const; ///< is this node Empty? - char isDeclaration() const; ///< is this node a declaration - XMLNode deepCopy() const; ///< deep copy (duplicate/clone) a XMLNode - static XMLNode emptyNode(); ///< return XMLNode::emptyXMLNode; - /** @} */ - - ~XMLNode(); - XMLNode(const XMLNode &A); ///< to allow shallow/fast copy: - XMLNode& operator=( const XMLNode& A ); ///< to allow shallow/fast copy: - - XMLNode(): d(NULL){}; - static XMLNode emptyXMLNode; - static XMLClear emptyXMLClear; - static XMLAttribute emptyXMLAttribute; - - /** @defgroup xmlModify Create or Update the XMLNode structure - * @ingroup XMLParserGeneral - * The functions in this group allows you to create from scratch (or update) a XMLNode structure. Start by creating your top - * node with the "createXMLTopNode" function and then add new nodes with the "addChild" function. The parameter 'pos' gives - * the position where the childNode, the text or the XMLClearTag will be inserted. The default value (pos=-1) inserts at the - * end. The value (pos=0) insert at the beginning (Insertion at the beginning is slower than at the end).
- * - * REMARK: 0 <= pos < nChild()+nText()+nClear()
- */ - - /** @defgroup creation Creating from scratch a XMLNode structure - * @ingroup xmlModify - * @{ */ - static XMLNode createXMLTopNode(XMLCSTR lpszName, char isDeclaration=FALSE); ///< Create the top node of an XMLNode structure - XMLNode addChild(XMLCSTR lpszName, char isDeclaration=FALSE, XMLElementPosition pos=-1); ///< Add a new child node - XMLNode addChild(XMLNode nodeToAdd, XMLElementPosition pos=-1); ///< If the "nodeToAdd" has some parents, it will be detached from it's parents before being attached to the current XMLNode - XMLAttribute *addAttribute(XMLCSTR lpszName, XMLCSTR lpszValuev); ///< Add a new attribute - XMLCSTR addText(XMLCSTR lpszValue, XMLElementPosition pos=-1); ///< Add a new text content - XMLClear *addClear(XMLCSTR lpszValue, XMLCSTR lpszOpen=NULL, XMLCSTR lpszClose=NULL, XMLElementPosition pos=-1); - /**< Add a new clear tag - * @param lpszOpen default value "" - */ - /** @} */ - - /** @defgroup xmlUpdate Updating Nodes - * @ingroup xmlModify - * Some update functions: - * @{ - */ - XMLCSTR updateName(XMLCSTR lpszName); ///< change node's name - XMLAttribute *updateAttribute(XMLAttribute *newAttribute, XMLAttribute *oldAttribute); ///< if the attribute to update is missing, a new one will be added - XMLAttribute *updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName=NULL,int i=0); ///< if the attribute to update is missing, a new one will be added - XMLAttribute *updateAttribute(XMLCSTR lpszNewValue, XMLCSTR lpszNewName,XMLCSTR lpszOldName);///< set lpszNewName=NULL if you don't want to change the name of the attribute if the attribute to update is missing, a new one will be added - XMLCSTR updateText(XMLCSTR lpszNewValue, int i=0); ///< if the text to update is missing, a new one will be added - XMLCSTR updateText(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue); ///< if the text to update is missing, a new one will be added - XMLClear *updateClear(XMLCSTR lpszNewContent, int i=0); ///< if the clearTag to update is missing, a new one will be added - XMLClear *updateClear(XMLClear *newP,XMLClear *oldP); ///< if the clearTag to update is missing, a new one will be added - XMLClear *updateClear(XMLCSTR lpszNewValue, XMLCSTR lpszOldValue); ///< if the clearTag to update is missing, a new one will be added - /** @} */ - - /** @defgroup xmlDelete Deleting Nodes or Attributes - * @ingroup xmlModify - * Some deletion functions: - * @{ - */ - /// The "deleteNodeContent" function forces the deletion of the content of this XMLNode and the subtree. - void deleteNodeContent(); - /**< \note The XMLNode instances that are referring to the part of the subtree that has been deleted CANNOT be used anymore!!. Unexpected results will occur if you continue using them. */ - void deleteAttribute(int i=0); ///< Delete the ith attribute of the current XMLNode - void deleteAttribute(XMLCSTR lpszName); ///< Delete the attribute with the given name (the "strcmp" function is used to find the right attribute) - void deleteAttribute(XMLAttribute *anAttribute); ///< Delete the attribute with the name "anAttribute->lpszName" (the "strcmp" function is used to find the right attribute) - void deleteText(int i=0); ///< Delete the Ith text content of the current XMLNode - void deleteText(XMLCSTR lpszValue); ///< Delete the text content "lpszValue" inside the current XMLNode (direct "pointer-to-pointer" comparison is used to find the right text) - void deleteClear(int i=0); ///< Delete the Ith clear tag inside the current XMLNode - void deleteClear(XMLCSTR lpszValue); ///< Delete the clear tag "lpszValue" inside the current XMLNode (direct "pointer-to-pointer" comparison is used to find the clear tag) - void deleteClear(XMLClear *p); ///< Delete the clear tag "p" inside the current XMLNode (direct "pointer-to-pointer" comparison on the lpszName of the clear tag is used to find the clear tag) - /** @} */ - - /** @defgroup xmlWOSD ???_WOSD functions. - * @ingroup xmlModify - * The strings given as parameters for the "add" and "update" methods that have a name with - * the postfix "_WOSD" (that means "WithOut String Duplication")(for example "addText_WOSD") - * will be free'd by the XMLNode class. For example, it means that this is incorrect: - * \code - * xNode.addText_WOSD("foo"); - * xNode.updateAttribute_WOSD("#newcolor" ,NULL,"color"); - * \endcode - * In opposition, this is correct: - * \code - * xNode.addText("foo"); - * xNode.addText_WOSD(stringDup("foo")); - * xNode.updateAttribute("#newcolor" ,NULL,"color"); - * xNode.updateAttribute_WOSD(stringDup("#newcolor"),NULL,"color"); - * \endcode - * Typically, you will never do: - * \code - * char *b=(char*)malloc(...); - * xNode.addText(b); - * free(b); - * \endcode - * ... but rather: - * \code - * char *b=(char*)malloc(...); - * xNode.addText_WOSD(b); - * \endcode - * ('free(b)' is performed by the XMLNode class) - * @{ */ - static XMLNode createXMLTopNode_WOSD(XMLSTR lpszName, char isDeclaration=FALSE); ///< Create the top node of an XMLNode structure - XMLNode addChild_WOSD(XMLSTR lpszName, char isDeclaration=FALSE, XMLElementPosition pos=-1); ///< Add a new child node - XMLAttribute *addAttribute_WOSD(XMLSTR lpszName, XMLSTR lpszValue); ///< Add a new attribute - XMLCSTR addText_WOSD(XMLSTR lpszValue, XMLElementPosition pos=-1); ///< Add a new text content - XMLClear *addClear_WOSD(XMLSTR lpszValue, XMLCSTR lpszOpen=NULL, XMLCSTR lpszClose=NULL, XMLElementPosition pos=-1); ///< Add a new clear Tag - - XMLCSTR updateName_WOSD(XMLSTR lpszName); ///< change node's name - XMLAttribute *updateAttribute_WOSD(XMLAttribute *newAttribute, XMLAttribute *oldAttribute); ///< if the attribute to update is missing, a new one will be added - XMLAttribute *updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName=NULL,int i=0); ///< if the attribute to update is missing, a new one will be added - XMLAttribute *updateAttribute_WOSD(XMLSTR lpszNewValue, XMLSTR lpszNewName,XMLCSTR lpszOldName); ///< set lpszNewName=NULL if you don't want to change the name of the attribute if the attribute to update is missing, a new one will be added - XMLCSTR updateText_WOSD(XMLSTR lpszNewValue, int i=0); ///< if the text to update is missing, a new one will be added - XMLCSTR updateText_WOSD(XMLSTR lpszNewValue, XMLCSTR lpszOldValue); ///< if the text to update is missing, a new one will be added - XMLClear *updateClear_WOSD(XMLSTR lpszNewContent, int i=0); ///< if the clearTag to update is missing, a new one will be added - XMLClear *updateClear_WOSD(XMLClear *newP,XMLClear *oldP); ///< if the clearTag to update is missing, a new one will be added - XMLClear *updateClear_WOSD(XMLSTR lpszNewValue, XMLCSTR lpszOldValue); ///< if the clearTag to update is missing, a new one will be added - /** @} */ - - /** @defgroup xmlPosition Position helper functions (use in conjunction with the update&add functions - * @ingroup xmlModify - * These are some useful functions when you want to insert a childNode, a text or a XMLClearTag in the - * middle (at a specified position) of a XMLNode tree already constructed. The value returned by these - * methods is to be used as last parameter (parameter 'pos') of addChild, addText or addClear. - * @{ */ - XMLElementPosition positionOfText(int i=0) const; - XMLElementPosition positionOfText(XMLCSTR lpszValue) const; - XMLElementPosition positionOfClear(int i=0) const; - XMLElementPosition positionOfClear(XMLCSTR lpszValue) const; - XMLElementPosition positionOfClear(XMLClear *a) const; - XMLElementPosition positionOfChildNode(int i=0) const; - XMLElementPosition positionOfChildNode(XMLNode x) const; - XMLElementPosition positionOfChildNode(XMLCSTR name, int i=0) const; ///< return the position of the ith childNode with the specified name if (name==NULL) return the position of the ith childNode - /** @} */ - - /// Enumeration for XML character encoding. - typedef enum XMLCharEncoding - { - char_encoding_error=0, - char_encoding_UTF8=1, - char_encoding_legacy=2, - char_encoding_ShiftJIS=3, - char_encoding_GB2312=4, - char_encoding_Big5=5, - char_encoding_GBK=6 // this is actually the same as Big5 - } XMLCharEncoding; - - /** \addtogroup conversions - * @{ */ - - /// Sets the global options for the conversions - static char setGlobalOptions(XMLCharEncoding characterEncoding=XMLNode::char_encoding_UTF8, char guessWideCharChars=1, - char dropWhiteSpace=1, char removeCommentsInMiddleOfText=1); - /**< The "setGlobalOptions" function allows you to change four global parameters that affect string & file - * parsing. First of all, you most-probably will never have to change these 3 global parameters. - * - * @param guessWideCharChars If "guessWideCharChars"=1 and if this library is compiled in WideChar mode, then the - * XMLNode::parseFile and XMLNode::openFileHelper functions will test if the file contains ASCII - * characters. If this is the case, then the file will be loaded and converted in memory to - * WideChar before being parsed. If 0, no conversion will be performed. - * - * @param guessWideCharChars If "guessWideCharChars"=1 and if this library is compiled in ASCII/UTF8/char* mode, then the - * XMLNode::parseFile and XMLNode::openFileHelper functions will test if the file contains WideChar - * characters. If this is the case, then the file will be loaded and converted in memory to - * ASCII/UTF8/char* before being parsed. If 0, no conversion will be performed. - * - * @param characterEncoding This parameter is only meaningful when compiling in char* mode (multibyte character mode). - * In wchar_t* (wide char mode), this parameter is ignored. This parameter should be one of the - * three currently recognized encodings: XMLNode::encoding_UTF8, XMLNode::encoding_ascii, - * XMLNode::encoding_ShiftJIS. - * - * @param dropWhiteSpace In most situations, text fields containing only white spaces (and carriage returns) - * are useless. Even more, these "empty" text fields are annoying because they increase the - * complexity of the user's code for parsing. So, 99% of the time, it's better to drop - * the "empty" text fields. However The XML specification indicates that no white spaces - * should be lost when parsing the file. So to be perfectly XML-compliant, you should set - * dropWhiteSpace=0. A note of caution: if you set "dropWhiteSpace=0", the parser will be - * slower and your code will be more complex. - * - * @param removeCommentsInMiddleOfText To explain this parameter, let's consider this code: - * \code - * XMLNode x=XMLNode::parseString("foobarchu","a"); - * \endcode - * If removeCommentsInMiddleOfText=0, then we will have: - * \code - * x.getText(0) -> "foo" - * x.getText(1) -> "bar" - * x.getText(2) -> "chu" - * x.getClear(0) --> "" - * x.getClear(1) --> "" - * \endcode - * If removeCommentsInMiddleOfText=1, then we will have: - * \code - * x.getText(0) -> "foobar" - * x.getText(1) -> "chu" - * x.getClear(0) --> "" - * \endcode - * - * \return "0" when there are no errors. If you try to set an unrecognized encoding then the return value will be "1" to signal an error. - * - * \note Sometime, it's useful to set "guessWideCharChars=0" to disable any conversion - * because the test to detect the file-type (ASCII/UTF8/char* or WideChar) may fail (rarely). */ - - /// Guess the character encoding of the string (ascii, utf8 or shift-JIS) - static XMLCharEncoding guessCharEncoding(void *buffer, int bufLen, char useXMLEncodingAttribute=1); - /**< The "guessCharEncoding" function try to guess the character encoding. You most-probably will never - * have to use this function. It then returns the appropriate value of the global parameter - * "characterEncoding" described in the XMLNode::setGlobalOptions. The guess is based on the content of a buffer of length - * "bufLen" bytes that contains the first bytes (minimum 25 bytes; 200 bytes is a good value) of the - * file to be parsed. The XMLNode::openFileHelper function is using this function to automatically compute - * the value of the "characterEncoding" global parameter. There are several heuristics used to do the - * guess. One of the heuristic is based on the "encoding" attribute. The original XML specifications - * forbids to use this attribute to do the guess but you can still use it if you set - * "useXMLEncodingAttribute" to 1 (this is the default behavior and the behavior of most parsers). - * If an inconsistency in the encoding is detected, then the return value is "0". */ - /** @} */ - - private: - // these are functions and structures used internally by the XMLNode class (don't bother about them): - - typedef struct XMLNodeDataTag // to allow shallow copy and "intelligent/smart" pointers (automatic delete): - { - XMLCSTR lpszName; // Element name (=NULL if root) - int nChild, // Number of child nodes - nText, // Number of text fields - nClear, // Number of Clear fields (comments) - nAttribute; // Number of attributes - char isDeclaration; // Whether node is an XML declaration - '' - struct XMLNodeDataTag *pParent; // Pointer to parent element (=NULL if root) - XMLNode *pChild; // Array of child nodes - XMLCSTR *pText; // Array of text fields - XMLClear *pClear; // Array of clear fields - XMLAttribute *pAttribute; // Array of attributes - int *pOrder; // order of the child_nodes,text_fields,clear_fields - int ref_count; // for garbage collection (smart pointers) - } XMLNodeData; - XMLNodeData *d; - - char parseClearTag(void *px, void *pa); - char maybeAddTxT(void *pa, XMLCSTR tokenPStr); - int ParseXMLElement(void *pXML); - void *addToOrder(int memInc, int *_pos, int nc, void *p, int size, XMLElementType xtype); - int indexText(XMLCSTR lpszValue) const; - int indexClear(XMLCSTR lpszValue) const; - XMLNode addChild_priv(int,XMLSTR,char,int); - XMLAttribute *addAttribute_priv(int,XMLSTR,XMLSTR); - XMLCSTR addText_priv(int,XMLSTR,int); - XMLClear *addClear_priv(int,XMLSTR,XMLCSTR,XMLCSTR,int); - void emptyTheNode(char force); - static inline XMLElementPosition findPosition(XMLNodeData *d, int index, XMLElementType xtype); - static int CreateXMLStringR(XMLNodeData *pEntry, XMLSTR lpszMarker, int nFormat); - static int removeOrderElement(XMLNodeData *d, XMLElementType t, int index); - static void exactMemory(XMLNodeData *d); - static int detachFromParent(XMLNodeData *d); -} XMLNode; - -/// This structure is given by the function XMLNode::enumContents. -typedef struct XMLNodeContents -{ - /// This dictates what's the content of the XMLNodeContent - enum XMLElementType etype; - /**< should be an union to access the appropriate data. Compiler does not allow union of object with constructor... too bad. */ - XMLNode child; - XMLAttribute attrib; - XMLCSTR text; - XMLClear clear; - -} XMLNodeContents; - -/** @defgroup StringAlloc String Allocation/Free functions - * @ingroup xmlModify - * @{ */ -/// Duplicate (copy in a new allocated buffer) the source string. -XMLDLLENTRY XMLSTR stringDup(XMLCSTR source, int cbData=-1); -/**< This is - * a very handy function when used with all the "XMLNode::*_WOSD" functions (\link xmlWOSD \endlink). - * @param cbData If !=0 then cbData is the number of chars to duplicate. New strings allocated with - * this function should be free'd using the "freeXMLString" function. */ - -/// to free the string allocated inside the "stringDup" function or the "createXMLString" function. -XMLDLLENTRY void freeXMLString(XMLSTR t); // {free(t);} -/** @} */ - -/** @defgroup atoX ato? like functions - * @ingroup XMLParserGeneral - * The "xmlto?" functions are equivalents to the atoi, atol, atof functions. - * The only difference is: If the variable "xmlString" is NULL, than the return value - * is "defautValue". These 6 functions are only here as "convenience" functions for the - * user (they are not used inside the XMLparser). If you don't need them, you can - * delete them without any trouble. - * - * @{ */ -XMLDLLENTRY char xmltob(XMLCSTR xmlString,char defautValue=0); -XMLDLLENTRY int xmltoi(XMLCSTR xmlString,int defautValue=0); -XMLDLLENTRY long long xmltol(XMLCSTR xmlString,long long defautValue=0); -XMLDLLENTRY double xmltof(XMLCSTR xmlString,double defautValue=.0); -XMLDLLENTRY XMLCSTR xmltoa(XMLCSTR xmlString,XMLCSTR defautValue=_CXML("")); -XMLDLLENTRY XMLCHAR xmltoc(XMLCSTR xmlString,const XMLCHAR defautValue=_CXML('\0')); -/** @} */ - -/** @defgroup ToXMLStringTool Helper class to create XML files using "printf", "fprintf", "cout",... functions. - * @ingroup XMLParserGeneral - * @{ */ -/// Helper class to create XML files using "printf", "fprintf", "cout",... functions. -/** The ToXMLStringTool class helps you creating XML files using "printf", "fprintf", "cout",... functions. - * The "ToXMLStringTool" class is processing strings so that all the characters - * &,",',<,> are replaced by their XML equivalent: - * \verbatim &, ", ', <, > \endverbatim - * Using the "ToXMLStringTool class" and the "fprintf function" is THE most efficient - * way to produce VERY large XML documents VERY fast. - * \note If you are creating from scratch an XML file using the provided XMLNode class - * you must not use the "ToXMLStringTool" class (because the "XMLNode" class does the - * processing job for you during rendering).*/ -typedef struct XMLDLLENTRY ToXMLStringTool -{ -public: - ToXMLStringTool(): buf(NULL),buflen(0){} - ~ToXMLStringTool(); - void freeBuffer();///Level3 Disabled WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) - %OPENCV%\include;$(SolutionDir)include + $(OPENCV)\include;$(SolutionDir)include 4819 diff --git a/vcprojs/demo.vcxproj.filters b/vcprojs/demo.vcxproj.filters index 480fa94..9b86c99 100644 --- a/vcprojs/demo.vcxproj.filters +++ b/vcprojs/demo.vcxproj.filters @@ -1,42 +1,14 @@  - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;hm;inl;inc;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - + - - æºæ–‡ä»¶ - - - - - 头文件 - - - 头文件 - - - 头文件 - - - 头文件 - + + + - æºæ–‡ä»¶ - - \ No newline at end of file diff --git a/vcprojs/demo.vcxproj.filters.orig b/vcprojs/demo.vcxproj.filters.orig new file mode 100644 index 0000000..3eb2049 --- /dev/null +++ b/vcprojs/demo.vcxproj.filters.orig @@ -0,0 +1,48 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + æºæ–‡ä»¶ + + + + + 头文件 + + + 头文件 + + + 头文件 + + + 头文件 + + + +<<<<<<< HEAD:Demo.vcxproj.filters + + æºæ–‡ä»¶ + + + æºæ–‡ä»¶ + +======= + +>>>>>>> 569e7642c2342a31e4358831597f1bedf23258bc:vcprojs/demo.vcxproj.filters + + \ No newline at end of file diff --git a/vcprojs/libeasypr.vcxproj b/vcprojs/libeasypr.vcxproj index 712d44c..99a736b 100644 --- a/vcprojs/libeasypr.vcxproj +++ b/vcprojs/libeasypr.vcxproj @@ -86,7 +86,7 @@ Level3 Disabled true - %OPENCV%\include;$(SolutionDir)include + $(OPENCV)\include;$(SolutionDir)include 4819 _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)