~bon-ami/ezcommon/main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
/***************************************************************************
                          ezcommon  -  description
                             -------------------
    begin                : Dec. 26, 2004
    email                : bon_ami_@hotmail.com
 ***************************************************************************/
/***************************************************************************
 *                                                                         *
 *   Explicit Distribution Declaration                                     *
 *   This program is not welcomed to be modified or used by, or, if        *
 *   possible, redistributed to people who discriminate against people     *
 *   based solely on race, gender or sexual orientation.                   *
 *                                                                         *
 ***************************************************************************/
/***************************************************************************
   Copyright 2010 Allen Tse

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 ***************************************************************************/
/*
 *	source code of EZ Project common functionalities
 */

#ifdef WIN32
#define HAVE_CONIO_H
#define HAVE__GETCH
#else
#include "config.h"
#endif

extern "C" {
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#else
#ifdef HAVE_CONIO_H
#include <conio.h>
#endif
#endif
#include <stdio.h>
#include <unistd.h>
}
#include <cstdio>
#include <iostream>
#include <string>
#include <list>
#include <map>

#include "ezcommon.h"
#include "tinyxml.h"

namespace ezproject {

/*	ezoi's private implementation	*/
class ezoip
{
private:
	std::istream *is;	//input file stream
	std::ostream *os;	//output file stream
	std::ostream *es;	//error-output file stream
#ifdef HAVE_TERMIOS_H
	struct termios *tm;	//terminal attributes for standard input
#endif
	std::string mod_nm;	//module name for all output

#ifdef HAVE_TERMIOS_H
	erttp st(void);	//get old and set new standard Input attributes
	void rst(void);	//restore old standard Input attributes
#endif

public:
	ezoip(void) : is(NULL), os(NULL), es(NULL)
#ifdef HAVE_TERMIOS_H
		, tm(NULL)
#endif
		{};
	~ezoip(void);

	void file(ezoi::efltp type, std::ios *strm);
	//set file stream for I/O
	void mod(const std::string &ModuleName);
	//set prefix for all output
	void dsp(const ezoi::eoilvl level, const std::string &content);
	//output something
	void ind(const std::string &Indication);
	//output something almost in raw
	void inpt(const std::string &Indication, std::string &Result);
	//input something
	void inpt(const std::string &Indication, char &Result);
	//input something
};


/*	ezoip	*/
ezoip::~ezoip(void) {
#ifdef HAVE_TERMIOS_H
       free(tm);
#endif
}

void ezoip::file(ezoi::efltp type, std::ios *strm)
{
	switch(type)
	{
		case ezoi::FLTP_INPUT:
			is = dynamic_cast<std::istream*>(strm);
			break;
		case ezoi::FLTP_OUTPUT:
			os = dynamic_cast<std::ostream*>(strm);
			break;
		case ezoi::FLTP_ERROR:
			es = dynamic_cast<std::ostream*>(strm);
	}
}

void ezoip::mod(const std::string &ModuleName)
{
	mod_nm = ModuleName;
}

#ifdef HAVE_TERMIOS_H
erttp ezoip::st(void)
{
	erttp ret = RTTP_CRER;

	if ((NULL == is) && (NULL == tm))
	{
		if (NULL != (tm = (struct termios *)
			malloc(sizeof(struct termios))))
		{
			struct termios t;

			if (0 == tcgetattr(STDIN_FILENO, &t))
			{
				if (NULL != memcpy(tm, &t, sizeof(t)))
				{
					ret = RTTP_RGHT;
					if (0 == (tm->c_lflag & ICANON))
					{
						tm->c_lflag &= ~ICANON;
						tcsetattr(STDIN_FILENO,
							TCSANOW, tm);
					}
					else
					{
						free(tm); tm = NULL;
					}
				}
			}
		}
	}
	else
		ret = RTTP_NOER;
	if (RTTP_CRER == ret)
	{
		free(tm);
		tm = NULL;
	}

	return ret;
}

void ezoip::rst(void)
{
	if ((tm != NULL) && (NULL == is))
		tcsetattr(STDIN_FILENO, 0, tm);
}
#endif	/* HAVE_TERMIOS_H */

void ezoip::dsp(const ezoi::eoilvl lvl, const std::string &cnt)
{
	std::string decorated(mod_nm);
	switch (lvl)
	{
		case ezoi::OILVL_FATAL:
			decorated.append(" FATAL: ");
			decorated.append(cnt);
			decorated.append("!");
			((NULL == es) ? std::cerr : *es) << decorated << std::endl;
			if (es != os)
				((NULL == os) ? std::cout : *os) << decorated << std::endl;
			break;
		case ezoi::OILVL_WARN:
			decorated.append(" Warning! ");
			decorated.append(cnt);
			((NULL == es) ? std::cerr : *es) << decorated << std::endl;
			if (es != os)
				((NULL == os) ? std::cout : *os) << decorated << std::endl;
			break;
		case ezoi::OILVL_INFO:
			((NULL == os) ? std::cout : *os)
				<< mod_nm << " Info: "
				<< cnt << std::endl;
			break;
		case ezoi::OILVL_NA:
			((NULL == os) ? std::cout : *os)
				<< mod_nm << ' ' << cnt;
			break;
	}
}

void ezoip::ind(const std::string &ind)
{
	if (NULL == os)
		std::cout << mod_nm << ' '
			<< ind << std::flush;
	else
		dsp(ezoi::OILVL_NA, ind);
}

void ezoip::inpt(const std::string &indi, std::string &ret)
{
	ind(indi);
	getline((NULL == is) ? std::cin : *is, ret);
}

void ezoip::inpt(const std::string &indi, char &ret)
{
	std::ostream *osb = os;

	os = NULL;	//show it on screen
	ind(indi);
	os = osb;	//restore input

#ifdef HAVE_TERMIOS_H
	st();
	ret = getchar();
	if ('\n' != ret)	//finish the line
		std::cout << std::endl;
	rst();
#else
#ifdef HAVE__GETCH
	ret = _getch();
	if ('\n' != ret)	//finish the line
		std::cout << std::endl;
#else
#ifdef HAVE_GETCH
	ret = getch();
	if ('\n' != ret)	//finish the line
		std::cout << std::endl;
#else
	ret = getchar();
	/* trash whatever not the first */
	if (ret != '\n')
		while (getchar() != '\n')
			;
#endif	/* _getch */
#endif	/* getch */
#endif	/* termios */
}


/*	ezoi	*/
/*	-structors	*/
ezoi::ezoi(void)
{
	m_p = new ezoip;
	for (char i = 0; i < FLTP_MAX; i++)
	{
		fStream[i] = NULL;
		fBuf[i] = NULL;
	}
}

ezoi::~ezoi(void)
{
	delete m_p;
	for (char i = 0; i < FLTP_MAX; i++)
	{
		if (fBuf[i])
		{
			delete fBuf[i];
			delete fStream[i];
		}
	}
}

/*	other interfaces	*/
erttp ezoi::file(int IOTypes, const char *FileName)
{
	if (!FileName || (strlen(FileName) <= 0) || (IOTypes < 0))
		return RTTP_CRER;

	std::list<efltp> ioTypes;

	if (IOTypes & ezoi::FLTP_INPUT)
		ioTypes.push_back(ezoi::FLTP_INPUT);
	if (IOTypes & ezoi::FLTP_OUTPUT)
		ioTypes.push_back(ezoi::FLTP_OUTPUT);
	if (IOTypes & ezoi::FLTP_ERROR)
		ioTypes.push_back(ezoi::FLTP_ERROR);
	if (ioTypes.empty())
		return RTTP_CRER;

	std::list<efltp>::iterator it;
	for (it = ioTypes.begin(); it != ioTypes.end(); it++)
	{
		if (fBuf[*it])
		{
			delete fBuf[*it];
			fBuf[*it] = NULL;
			delete fStream[*it];
			fStream[*it] = NULL;
		}
	}
	it = ioTypes.begin();

	fBuf[*it] = new std::filebuf();
	if (fBuf[*it])
	{
#define OPENSTREAM(IOS, STREAM) \
	if (fBuf[*it]->open(FileName, std::ios::IOS) != NULL) \
		fStream[*it] = new std::STREAM(fBuf[*it]); \
	else return RTTP_MNER;

		switch(*it)
		{
			case FLTP_INPUT:
				OPENSTREAM(in, istream);
				break;
			case FLTP_OUTPUT:
				OPENSTREAM(out, ostream);
				break;
			case FLTP_ERROR:
				OPENSTREAM(out, ostream);
		}
	}
	if (fStream[*it])
	{
		for (; it != ioTypes.end(); it++)
			m_p->file(*it, fStream[*it]);
		return RTTP_RGHT;
	}
	else
		return RTTP_CRER;
}

void ezoi::mod(const char *ModuleName)
{
	if (ModuleName)
		m_p->mod(ModuleName);
}

void ezoi::dsp(ezoi::eoilvl lvl, const char *cnt)
{
	if (cnt)
		m_p->dsp(lvl, cnt);
}

void ezoi::mem(void)
{
	m_p->dsp(ezoi::OILVL_FATAL, "error in allocating memory");
}

void ezoi::ind(const char *ind)
{
	if (ind)
		m_p->ind(ind);
}

void ezoi::inpt(const char *ind, char **rst)
{
	if (ind && rst)
	{
		std::string str;

		m_p->inpt(ind, str);
		free(*rst);
		*rst = strdup(str.c_str());
	}
}

void ezoi::inpt(const char *ind, char &rst)
{
	if (ind)
		m_p->inpt(ind, rst);
}


/*	ezcfg's private implementation	*/
class ezcfgp
{
private:
	TiXmlDocument *doc;
	TiXmlNode *node;
	static const char * const defaultVersion;
	static const char * const defaultRoot;
	struct positionData_type
	{
		TiXmlNode *node;
		ezcfg::leveltp level;
	};
	typedef std::map<std::string, positionData_type*> position_type;
	position_type positions;
	typedef position_type::value_type position_value_type;
	TiXmlAttribute *attribute;
	ezcfg::edectp dectp;	// current declaration attribute
	ezcfg::leveltp levelCurr;
	void clearPositions(void);

public:
	ezcfgp(const char *root, const char *comment, const char *version,
		const char *encoding, const char *standalone);	// for output
	ezcfgp(const char *FileName);	// for input
	~ezcfgp();

	erttp next(bool noDeeper);	// return value:
	//RTTP_CRER: no more element; RTTP_MNER: one level up;
	//RTTP_NOER: one level down; RTTP_RGHT: one element ahead
	//RTTP_NA: no element at all
	erttp more(erttp, enum ezcfg::eenttp);
		// parameter & return value are similar to
		// next()'s return value
	erttp remove();
	void home();	//goto the initial place
	bool save(const char *FileName);
	enum ezcfg::eenttp getType();
	bool cdata(bool *setting = NULL);
	void getValue(std::string &name);
	void setValue(const std::string &name);
	void getDeclarationAttribute(TiXmlDeclaration *,
		const char *&val, enum ezcfg::edectp);
	enum ezcfg::edectp  getDeclarationAttributes(std::string &val,
		enum ezcfg::edectp = ezcfg::DECTP_NA);
	void getAttribute(std::string &name, std::string &value,
		bool fromBeginning);
	void setAttribute(const std::string &name, const std::string &value);
	void removeAttribute(const std::string &name);
	void setPosition(std::string &position);
	void removePosition(std::string &position);
	bool getPosition(std::string &position);
	ezcfg::leveltp level(ezcfg::leveltp);
};

/*	ezcfgp	*/
const char * const ezcfgp::defaultVersion = "1.0";
const char * const ezcfgp::defaultRoot = "EZ_Project";
/*	-structors	*/
ezcfgp::ezcfgp(const char *root, const char *comment, const char *version,
	const char *encoding, const char *standalone)
{
	doc = new TiXmlDocument();
	node = NULL;
	levelCurr = ezcfg::levelNA;
	if (doc)
	{
		if (NULL == root)
			root = defaultRoot;
		if (NULL == version)
			version = defaultVersion;
		if (NULL == encoding)
			encoding = "";
		if (NULL == standalone)
			standalone = "";

		TiXmlDeclaration * decl = new TiXmlDeclaration(version,
			encoding, standalone);

		if (decl)
		{
			TiXmlComment *cmt = NULL;

			if (comment != NULL)
				cmt = new TiXmlComment(comment);
			TiXmlElement * element = new TiXmlElement(root);

			if (element)
			{
				doc->LinkEndChild(decl);
				if (cmt != NULL)
					doc->LinkEndChild(cmt);
				doc->LinkEndChild(element);
				node = element;
				levelCurr = ezcfg::levelFirst;
			}
			else
				delete decl;
		}
	}
	attribute = NULL;
}

ezcfgp::ezcfgp(const char *FileName)
{
	doc = new TiXmlDocument(FileName);
	if (!doc || !doc->LoadFile())
	{
		delete doc;
		doc = NULL;
		node = NULL;
	}
	else
		node = doc->GetDocument();
	attribute = NULL;
	levelCurr = ezcfg::levelNA;
}

ezcfgp::~ezcfgp()
{
	delete doc;
	clearPositions();
}

/*	other interfaces	*/
/*
return value:	RTTP_CRER: no more element; RTTP_MNER: level(s) up;
		RTTP_NOER: one level down; RTTP_RGHT: one element ahead
*/
erttp ezcfgp::next(bool noDeeper)
{
	if (doc)
	{
		attribute = NULL;
		dectp = ezcfg::DECTP_NA;
		if (!node || (node == doc->GetDocument()))
		{
			node = doc->FirstChild();
			if (node)
			{
				levelCurr = ezcfg::levelFirst;
				return RTTP_RGHT;
			}
		}
		else
		{
			if (!noDeeper && !node->NoChildren())
			{
				node = node->FirstChild();
				levelCurr++;
				return RTTP_NOER;
			}
			else
			{
				if (!node->NextSibling())
				{
					do
					{
						node = node->Parent();
						levelCurr--;
						if (node && (node->Type() != TiXmlNode::DOCUMENT))
						{
							if (node->NextSibling())
							{
								node = node->NextSibling();
								return RTTP_MNER;
							}
						}
						else
						{
							levelCurr = ezcfg::levelNA;
							break;
						}
					} while(true);
				}
				else
				{
					node = node->NextSibling();
					return RTTP_RGHT;
				}
			}
		}
	}

	return RTTP_CRER;
}

erttp ezcfgp::more(erttp level, enum ezcfg::eenttp type)
{
	erttp ret = RTTP_CRER;

	if (doc && node && (type > ezcfg::ENTTP_DECLARATION))
	{
		TiXmlNode * newNode = NULL;

		switch (type)
		{
			case ezcfg::ENTTP_NA:
			case ezcfg::ENTTP_NONE:
			case ezcfg::ENTTP_DOC:
			case ezcfg::ENTTP_DECLARATION:
				break;
			case ezcfg::ENTTP_ELEMENT:
				newNode = new TiXmlElement("");
				break;
			case ezcfg::ENTTP_TEXT:
				newNode = new TiXmlText("");
				break;
			case ezcfg::ENTTP_COMMENT:
				newNode = new TiXmlComment;
		}

		if (newNode)
		{
			TiXmlNode *parent, *child, *newNodeResult = NULL;

			switch (level)
			{
				case RTTP_MNER:
					parent = node->Parent();
					if (parent && (TiXmlNode::ELEMENT ==parent->Type())
						&& (parent->ToElement() != NULL))
					{	// put everything under one root element
						parent = parent->ToElement()->Parent();
						if (parent && (parent->Type() != TiXmlNode::DOCUMENT))
						{
							newNodeResult = parent->InsertAfterChild(node, *newNode);
							if (newNodeResult)
							{
								node = newNodeResult;
								levelCurr--;
								ret = RTTP_MNER;
							}
							break;
						}
						else	/* current node's parent is root.
								put the new node right after
								the current node. */
							level = RTTP_RGHT;
					}
					/* else, current node is root,
						put the new node underneath.
						fall through */
				case RTTP_RGHT:
					if (RTTP_RGHT == level)
					{
						parent = node->Parent();
						if (parent && (parent->Type() != TiXmlNode::DOCUMENT))
						{
							newNodeResult = parent->InsertAfterChild(node, *newNode);
							if (newNodeResult)
							{
								node = newNodeResult;
								ret = RTTP_RGHT;
							}
							break;
						}
						/* else, current node is root,
							put the new node underneath.
							fall through */
					}
				case RTTP_NOER:
					child = node->FirstChild();
					if (child)
						newNodeResult = node->InsertBeforeChild(child, *newNode);
					else
					{
						node->LinkEndChild(newNode);
						newNodeResult = newNode;
					}
					if (newNodeResult)
					{
						node = newNodeResult;
						levelCurr++;
						ret = RTTP_NOER;
					}
					break;
				default:
					;
			}
			if (ret != RTTP_NOER)
				delete newNode;
		}
	}

	return ret;
}

void ezcfgp::removePosition(std::string &position)
{
	position_type::iterator it;

	if (position.empty())
	{
		for(it = positions.begin(); it != positions.end(); ++it)
			if ((it->second != NULL) && (it->second->node == node))
			{
				delete it->second;
				positions.erase(it);
				break;
			}
	}
	else
	{
		it = positions.find(position);
		if (it != positions.end())
		{
			delete it->second;
			positions.erase(it);
		}
	}
}

erttp ezcfgp::remove()
{
	erttp ret = RTTP_CRER;

	if (node)
	{
		TiXmlNode *parent = node->Parent();
		TiXmlNode *removal = node;

		if (parent/* && (parent->Type() != TiXmlNode::DOCUMENT)*/)
		{
			ret = next(true);
			parent->RemoveChild(removal);

			position_type::iterator it;
			for(it = positions.begin(); it != positions.end(); ++it)
				if ((it->second != NULL) && (it->second->node == removal))
				{
					delete it->second;
					positions.erase(it);
					break;
				}
		}
	}

	return ret;
}

void ezcfgp::home()
{
	node = NULL;
	levelCurr = ezcfg::levelNA;
}

bool ezcfgp::save(const char * filename)
{
	if (doc != NULL)
	{
		if (filename != NULL)
			doc->SetValue(filename);
		return doc->SaveFile();
	}
	return false;
}

enum ezcfg::eenttp ezcfgp::getType()
{
	enum ezcfg::eenttp type = ezcfg::ENTTP_NA;

	if (!doc)
		type = ezcfg::ENTTP_NONE;
	else if (node)
		switch (node->Type())
		{
			case TiXmlNode::ELEMENT:
				type = ezcfg::ENTTP_ELEMENT;
				break;
			case TiXmlNode::COMMENT:
				type = ezcfg::ENTTP_COMMENT;
				break;
			case TiXmlNode::DECLARATION:
				type = ezcfg::ENTTP_DECLARATION;
				break;
			case TiXmlNode::DOCUMENT:
				type = ezcfg::ENTTP_DOC;
				break;
			case TiXmlNode::TEXT:
				type = ezcfg::ENTTP_TEXT;
				break;
			case TiXmlNode::UNKNOWN:
			default:
				;
		}
    else
        type = ezcfg::ENTTP_DOC;

	return type;
}

bool ezcfgp::cdata(bool *setting)
{
	bool ret = false;

	if (ezcfg::ENTTP_TEXT == getType())
	{
		TiXmlText *nodex = node->ToText();
		if (nodex != NULL)
		{
			if (setting != NULL)
				nodex->SetCDATA(*setting);
			ret = nodex->CDATA();
		}
	}

	return ret;
}

void ezcfgp::getValue(std::string &name)
{
	if (node)
	{
		const char *namep = node->Value();

		if (namep)
		{
			name = namep;
			return;
		}
	}
	name.clear();
}

void ezcfgp::setValue(const std::string &name)
{
	if (node)
		node->SetValue(
#ifdef TIXML_USE_STL
			name
#else
			name.c_str()
#endif 
		);
}

void ezcfgp::getDeclarationAttribute(TiXmlDeclaration *dec,
	const char *&val, enum ezcfg::edectp)
{
	switch (dectp)
	{
		case ezcfg::DECTP_VERSION:
			val = dec->Version();
			break;
		case ezcfg::DECTP_ENCODING:
			val = dec->Encoding();
			break;
		case ezcfg::DECTP_STANDALONE:
			val = dec->Standalone();
			break;
		default:
			;
	}
}

enum ezcfg::edectp ezcfgp::getDeclarationAttributes(std::string &val,
	enum ezcfg::edectp type)
{
	val.clear();
	if (node && (TiXmlNode::DECLARATION == node->Type()))
	{
		TiXmlDeclaration *dec = node->ToDeclaration();
		const char *result = NULL;
		int i = static_cast<int>(dectp);

		switch (type)
		{
			case ezcfg::DECTP_NA:
				for (i++; (i != ezcfg::DECTP_COUNT)
					&& (NULL == result); i++)
					getDeclarationAttribute(dec, result,
						dectp = static_cast<ezcfg::edectp>(i));
				break;
			case ezcfg::DECTP_COUNT:
				break;
			default:
				getDeclarationAttribute(dec, result, type);
		}
		if (result)
			val = result;
	}
	return ezcfg::DECTP_COUNT;
}

void ezcfgp::getAttribute(std::string &name, std::string &value,
	bool fromBeginning)
{
	if (node)
	{
		TiXmlElement *ele = node->ToElement();
		if (ele)
		{
			if (!attribute || fromBeginning)
				attribute = ele->FirstAttribute();
			else
				attribute = attribute->Next();
			if (attribute)
			{
				const char *namep = attribute->Name();

				if (namep)
				{
					if (name.empty() || (namep == name))
					{
						name = namep;

						const char *valuep
							= attribute->Value();

						if (valuep)
							value = attribute->Value();
						else
							value.clear();
					}
					else
						getAttribute(name, value, false);
				}
				else
					getAttribute(name, value, false);
				return;
			}
		}
	}
	name.clear();
}

void ezcfgp::setAttribute(const std::string &name, const std::string &value)
{
	if (node)
	{
		TiXmlElement *ele = node->ToElement();
		if (ele)
		{
			ele->SetAttribute(
#ifdef TIXML_USE_STL
				name, value
#else
				name.c_str(), value.c_str()
#endif
			);
			attribute = ele->LastAttribute();
		}
	}
}

void ezcfgp::removeAttribute(const std::string &name)
{
	if (node)
	{
		TiXmlElement *ele = node->ToElement();
		if (ele)
		{
			if (name.empty())
			{
				TiXmlAttribute *attr = NULL;
				do
				{
					attr = ele->FirstAttribute();
					if (NULL == attr)
						break;
					ele->RemoveAttribute(attr->Name());
				} while(true);
			}
			else
				ele->RemoveAttribute(
#ifdef TIXML_USE_STL
					name
#else
					name.c_str()
#endif
				);
			attribute = NULL;
		}
	}
}

void ezcfgp::clearPositions(void)
{
	position_type::iterator it;
	for(it = positions.begin(); it != positions.end(); ++it)
		delete it->second;
	positions.clear();
}

void ezcfgp::setPosition(std::string &position)
{
	if (node)
	{
		positionData_type *data = new positionData_type;
		data->node = node;
		data->level = levelCurr;
		positions.insert(position_value_type(position, data));
	}
	else
		clearPositions();
}

bool ezcfgp::getPosition(std::string &position)
{
	position_type::const_iterator it = positions.find(position);

	if (it != positions.end())
	{
		node = it->second->node;
		levelCurr = it->second->level;
		return true;
	}
	else
		return false;
}

ezcfg::leveltp ezcfgp::level(ezcfg::leveltp lvl)
{
	ezcfg::leveltp ret = levelCurr;
	if (ezcfg::levelNA != lvl)
	{
		do
		{
			switch (next(false))
			{
				case RTTP_CRER:
					ret = ezcfg::levelNA;
					break;
				case RTTP_MNER:
					ret = levelCurr;
					break;
				case RTTP_NOER:
				case RTTP_RGHT:
					break;
				case RTTP_NA:
					;
			}
		} while((ezcfg::levelNA != ret) && (ret > lvl)); 
	}
	return ret;
}

/*	ezcfg	*/
/*	-structors	*/
ezcfg::ezcfg()
{
	m_p = new ezcfgp(NULL, NULL, NULL, NULL, NULL);
}

ezcfg::ezcfg(const char *root, const char *version, const char *encoding,
	const char *standalone, const char *comment)
{
	m_p = new ezcfgp(root, comment, version, encoding, standalone);
}

ezcfg::ezcfg(const char *FileName)
{
	if (FileName && strlen(FileName))
		m_p = new ezcfgp(FileName);
	else
		ezcfg();
}

ezcfg::~ezcfg(void)
{
	delete m_p;
}

/*	easy memory operations	*/
void ezcfg::free(void *m)
{
	::free(m);
}

void ezcfg::moveString(std::string &str, char *&val)
{
	if (val != NULL)
	{
		str = val;
		free(val);
		val = NULL;
	}
}

/*	other interfaces	*/
erttp ezcfg::next(bool noDeeper)
{
	if (m_p)
		return m_p->next(noDeeper);
	else
		return RTTP_CRER;
}

erttp ezcfg::more(erttp level, eenttp type)
{
	return m_p ? m_p->more(level, type) : RTTP_CRER;
}

erttp ezcfg::remove()
{
	return m_p ? m_p->remove() : RTTP_CRER;
}

const char *ezcfg::moveFile(const char *oldName, const char *newName)
{
	FILE *fp = fopen(oldName, "rb");

	if (fp != NULL)
	{
		fclose(fp);
		fp = fopen(newName, "rb");
		if (NULL == fp)
		{
			if (0 == rename(oldName, newName))
				return newName;
		}
		else
			fclose(fp);
	}
	else
		return oldName;
	return NULL;
}

bool ezcfg::save(const char *FileName, const char *RenamePattern,
	trytimetp TryTimes)
{
	if ((FileName != NULL) && (strlen(FileName) > 0)
		&& (RenamePattern != NULL) && (strlen(RenamePattern) > 0))
	{	// do not overwrite, if in existence
		const char *resultName = NULL;
		std::string pattern(RenamePattern);
		std::string::size_type locP = pattern.find('%');
	
		if (locP != std::string::npos)
		{
			std::string head = pattern.substr(0, locP);
			std::string::size_type locD = pattern.find('d', locP);

			if (locD != std::string::npos)
			{	// *%STHd*
				unsigned char digits = 0;
				std::string tail = pattern.substr(locD);

				tail.erase(0, 1);	// remove 'd'
				if ((locD - locP) > 1)
				{
					std::string mid = pattern.substr(locP + 1,
						locD - locP - 1);
					if (std::string::npos
						== mid.find_first_not_of("0123456789"))
						// *%Xd*
						digits = atoi(mid.c_str());
					else	// *%STHd*
						assert(false);
				}
				// else, *%d*

				// construct consecutive names
				trytimetp maxDigits = 0;
				const char *fmtStr = NULL;

				if (digits != 0)
				{
					maxDigits = digits;
					fmtStr = "%s%0*d%s";
				}
				else
				{	// calculate max digits number
					trytimetp digitsTemp = TryTimes;
					while (digitsTemp > 0)
					{
						maxDigits++;
						digitsTemp /= 10;
					}
					fmtStr = "%s%0.*u%s";
					// unsigned int has no digits after
					// point, so the digit parameter
					// is ignored
				}

				int bufSize = head.length() + tail.length()
					+ maxDigits;
				char *NewName = (char *)malloc(bufSize);

				for (trytimetp i = 1; (i <= TryTimes)
					&& (NULL == resultName); i++)
				{
					sprintf(NewName, fmtStr,
						head.c_str(), maxDigits,
						i, tail.c_str());
					resultName = moveFile(FileName,
						NewName);
				}
			}
			else
				assert(false);
		}
		else
			resultName = moveFile(FileName, RenamePattern);
		if (NULL == resultName)
			return false;
	}
	return m_p ? m_p->save(FileName) : false;
}

ezcfg::eenttp ezcfg::getType()
{
	if (m_p)
		return m_p->getType();
	return ENTTP_NONE;
}

bool ezcfg::getCdata()
{
	if (m_p)
		return m_p->cdata();
	return false;
}

void ezcfg::setCdata(bool setting)
{
	if (m_p)
		m_p->cdata(&setting);
}

void ezcfg::getValue(char *&name)
{
	std::string str;

	free(name);
	name = NULL;
	if (m_p)
	{
		m_p->getValue(str);
		if (str.length() > 0)
			name = strdup(str.c_str());
	}
}

void ezcfg::setValue(const char *name)
{
	if (name && m_p)
	{
		std::string str(name);
		m_p->setValue(str);
	}
}

enum ezcfg::edectp ezcfg::getDeclarationAttributes(char *&val, enum edectp type)
{
	edectp ret = DECTP_NA;
	std::string str;

	free(val);
	val = NULL;
	if (m_p)
	{
		ret = m_p->getDeclarationAttributes(str, type);
		if (str.length() > 0)
			val = strdup(str.c_str());
		else
			ret = DECTP_NA;
	}

	return ret;
}

void ezcfg::getAttribute(char *&name, char *&value, bool fromBeginning)
{
	std::string nm, vl;

	moveString(nm, name);
	moveString(vl, value);
	if (m_p)
	{
		m_p->getAttribute(nm, vl, fromBeginning);
		if (nm.length() > 0)
		{
			name = strdup(nm.c_str());
			value = strdup(vl.c_str());
		}
	}
}

void ezcfg::getAttribute(const char *&name, char *&value, bool fromBeginning)
{
	std::string vl;

	moveString(vl, value);
	if (m_p)
	{
		std::string nm(name);
		m_p->getAttribute(nm, vl, fromBeginning);
		if (nm.length() > 0)
			value = strdup(vl.c_str());
		else
			name = NULL;
	}
}

void ezcfg::setAttribute(const char *name, const char * value)
{
	if (name && value && m_p)
	{
		std::string strName(name), strValue(value);
		m_p->setAttribute(strName, strValue);
	}
}

void ezcfg::removeAttribute(const char *name)
{
	if (name && m_p)
	{
		std::string str(name);
		m_p->removeAttribute(str);
	}
}

void ezcfg::setBookmark(const char *position)
{
	if (position && m_p)
	{
		std::string s(position);
		m_p->setPosition(s);
	}
}

void ezcfg::removeBookmark(const char *position)
{
	if (m_p)
	{
		std::string s((position != NULL) ? position : "");
		m_p->removePosition(s);
	}
}

bool ezcfg::getBookmark(const char *position)
{
	if (m_p)
	{
		if (position)
		{
			std::string s(position);
			return m_p->getPosition(s);
		}
		else
		{
			m_p->home();
			return true;
		}
	}
	else
		return false;
}

ezcfg::leveltp ezcfg::level(leveltp lvl)
{
	if (m_p)
		return m_p->level(lvl);
	return levelNA;
}

}	/*	namespace ezproject	*/