forked from johnrsibert/ADMB_XML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADMB_XMLDoc.cpp
1019 lines (854 loc) · 27 KB
/
ADMB_XMLDoc.cpp
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
// $Id: $
/** \file ADMB_XMLDoc.cpp for class ADMB_XMLDoc. Based on the XMLDocument2 class by Johnoel Ancheta.
\author John Sibert
*/
#include "ADMB_XMLDoc.h"
#include <stdexcept>
//#include "trace.h"
//#include <gdbprintlib.h>
using namespace std;
const char* ADMB_XMLDoc::dataS = (const char*)"CON";
const char* ADMB_XMLDoc::paramS = (const char*)"VAR";
const char* ADMB_XMLDoc::statS = (const char*)"STAT";
ADMB_XMLDoc::ADMB_XMLDoc()
{
doc = NULL;
context = NULL;
RootNode = NULL;
xmlInitParser();
}
ADMB_XMLDoc::ADMB_XMLDoc(const adstring& drn, const adstring& prg, const adstring& id, const adstring filen)
{
allocate(drn, prg, id, filen);
}
void ADMB_XMLDoc::allocate(const adstring& drn, const adstring& prg, const adstring& id, const adstring filen)
{
doc = xmlNewDoc(BAD_CAST "1.0");
//file << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" << endl;
XMLFileName = (const char*)filen;
DocRootName = (const char*)drn;
RootNode = xmlNewNode(NULL, BAD_CAST DocRootName.c_str());
//file << "<?xml-stylesheet type=\"text/xsl\" href=\"ADMB.xsl\"?>" << endl;
//xmlNewProp(ss, BAD_CAST "type",BAD_CAST "text/xsl");
//xmlNewProp(ss, BAD_CAST "href",BAD_CAST "ADMB.xsl");
xmlNodePtr ss = xmlNewPI(BAD_CAST "xml-stylesheet",
BAD_CAST "type=\"text/xsl\" href=\"ADMB.xsl\"");
xmlNewProp(RootNode, BAD_CAST "id", BAD_CAST (const char*)id);
xmlDocSetRootElement(doc, RootNode);
xmlAddPrevSibling(RootNode,ss);
xmlNodePtr CNode = xmlNewComment (BAD_CAST "a simple comment in the previos node");
xmlAddPrevSibling(RootNode,CNode);
/*
if (1)
{
xmlSaveFormatFileEnc(BAD_CAST XMLFileName.c_str(),doc,"UTF-8", 1);
exit(1);
}
*/
}
/** Terminates the document tree
*/
ADMB_XMLDoc::~ADMB_XMLDoc()
{
//Free the global variables that may
//have been allocated by the parser.
xmlCleanupParser();
}
/* * Write the contents of doc to a file.
* @param parfile character string containing filename to write the xml tree.
*/
int ADMB_XMLDoc::write(void)
{
int nbyte = 0;
nbyte = xmlSaveFormatFileEnc((const char*)XMLFileName.c_str(),doc, "UTF-8", 1);
return nbyte;
}
int ADMB_XMLDoc::createXMLcomment(const adstring& t)
{
xmlNodePtr node = xmlNewComment (BAD_CAST (const char*)t);
//xmlAddPrevSibling(RootNode,node);
xmlAddChild(RootNode,node);
return ((node == NULL));
}
int ADMB_XMLDoc::createXMLelement(const adstring& name, const double t, const adstring& title)
{
// create name node for variable
xmlNodePtr node = createNameNode(name, ADMB_XMLDoc::dataS);
// create title node and add to name node
createTitleNode(node,title);
// create value node and add to name node
createValueNode(node,t);
// add name node to root
xmlNodePtr nnode = xmlAddChild(RootNode,node);
return ((nnode == NULL));
}
int ADMB_XMLDoc::createXMLelement(const data_int& _t, const adstring& title)
{
ADUNCONST(data_int, t);
adstring name(t.get_name());
xmlNodePtr node = createNameNode(name, ADMB_XMLDoc::dataS);
createTitleNode(node,title);
createValueNode(node,(int)t);
xmlNodePtr nnode = xmlAddChild(RootNode,node);
return ((nnode == NULL));
}
int ADMB_XMLDoc::createXMLelement(const adstring& name,const data_int& _t, const adstring& title)
{
ADUNCONST(data_int, t);
xmlNodePtr node = createNameNode(name, ADMB_XMLDoc::dataS);
createTitleNode(node,title);
createValueNode(node,(int)t);
xmlNodePtr nnode = xmlAddChild(RootNode,node);
return ((nnode == NULL));
}
int ADMB_XMLDoc::createXMLelement(const data_matrix& _t, const adstring& title)
{
ADUNCONST(data_matrix, t)
adstring name(t.get_name());
xmlNodePtr node = createNameNode(name, ADMB_XMLDoc::dataS);
createTitleNode(node,title);
const int i1 = t.colmin();
const int i2 = t.colmax();
const int j1 = t.rowmin();
const int j2 = t.rowmax();
createIndexNode(node,j1,j2,i1,i2);
createValueNode(node,t);
xmlNodePtr nnode = xmlAddChild(RootNode,node);
return ((nnode == NULL));
}
int ADMB_XMLDoc::createXMLelement(const param_init_bounded_number& _t, const adstring& title)
{
ADUNCONST(param_init_bounded_number, t);
adstring name(t.get_name());
xmlNodePtr node = createNameNode(name, ADMB_XMLDoc::paramS);
createTitleNode(node,title);
createValueNode(node,value(t));
//file << createBoundsTag(t.get_minb(),t.get_maxb());
createBoundsNode(node,t.get_minb(),t.get_maxb());
//file << createActiveTag(active(t));
createActiveNode(node, active(t));
//file << createPhaseTag(t.phase_start);
createPhaseNode(node,t.phase_start);
xmlNodePtr nnode = xmlAddChild(RootNode,node);
return ((nnode == NULL));
}
int ADMB_XMLDoc::createXMLelement(const adstring& name, const param_init_bounded_number_matrix& _t, const adstring& title, const int M)
{
ADUNCONST(param_init_bounded_number_matrix, t)
xmlNodePtr node = createNameNode(name, ADMB_XMLDoc::paramS);
createTitleNode(node,title);
//const int j1 = t.colmin();
//const int j2 = t.colmax();
//const int i1 = t.rowmin();
//const int i2 = t.rowmax();
const int i1 = 1;
const int i2 = M;
dmatrix vt(i1,i2); //,j1,j2);
dmatrix maxb(i1,i2);
dmatrix minb(i1,i2);
imatrix phase(i1,i2);
imatrix active(i1,i2);
int j1,j2;
for (int i = i1; i <= i2; i++)
{
j1 = t(i).indexmin();
j2 = t(i).indexmax();
vt(i).allocate(j1,j2);
maxb(i).allocate(j1,j2);
minb(i).allocate(j1,j2);
phase(i).allocate(j1,j2);
active(i).allocate(j1,j2);
for (int j = j1; j <= j2; j++)
{
vt(i,j) = value(t(i,j));
maxb(i,j) = t(i,j).get_maxb();
minb(i,j) = t(i,j).get_minb();
phase(i,j) = t(i,j).phase_start;
active(i,j) = (phase(i,j) > 0);
}
}
createValueNode(node,vt);
createIndexNode(node,i1,i2,j1,j2);
createBoundsNode(node,minb,maxb);
createPhaseNode(node,phase);
createActiveNode(node,active);
xmlNodePtr nnode = xmlAddChild(RootNode,node);
return ((nnode == NULL));
}
xmlNodePtr ADMB_XMLDoc::createNameNode(const adstring& name, const char* category)
{
xmlNodePtr node = xmlNewNode(NULL, BAD_CAST (const char*)name);
if (category)
xmlNewProp(node, BAD_CAST "category", BAD_CAST category);
return node;
}
int ADMB_XMLDoc::createTitleNode(xmlNodePtr parent, const adstring& title)
{
xmlNodePtr tNode = xmlNewNode(NULL, BAD_CAST "title");
xmlNodeAddContent(tNode,BAD_CAST (const char*)title);
xmlAddChild(parent,tNode);
return ((tNode==NULL));
}
int ADMB_XMLDoc::createValueNode(xmlNodePtr parent, const adstring& t)
{
xmlNodePtr tNode = xmlNewChild(parent,NULL, BAD_CAST "value",
BAD_CAST (const char*)t);
return ((tNode==NULL));
}
int ADMB_XMLDoc::createValueNode(xmlNodePtr parent, const int t)
{
ostringstream ostr;
ostr << t;
// create value node and add to name node
xmlNodePtr vNode = xmlNewNode(NULL, BAD_CAST "value");
xmlNodeAddContent(vNode, BAD_CAST (const char*)ostr.str().c_str());
xmlAddChild(parent,vNode);
return ((vNode==NULL));
}
int ADMB_XMLDoc::createValueNode(xmlNodePtr parent, const double t)
{
ostringstream ostr;
ostr << setprecision(16) << t;
// create value node and add to name node
xmlNodePtr vNode = xmlNewNode(NULL, BAD_CAST "value");
xmlNodeAddContent(vNode, BAD_CAST (const char*)ostr.str().c_str());
xmlAddChild(parent,vNode);
return ((vNode==NULL));
}
/*
int ADMB_XMLDoc::createValueNode(xmlNodePtr parent, const param_init_bounded_number& t)
{
ostringstream ostr;
ostr << setprecision(16) << value(t);
// create value node and add to name node
xmlNodePtr vNode = xmlNewNode(NULL, BAD_CAST "value");
xmlNodeAddContent(vNode, BAD_CAST (const char*)ostr.str().c_str());
xmlAddChild(parent,vNode);
return ((vNode==NULL));
}
*/
int ADMB_XMLDoc::createValueNode(xmlNodePtr parent, const dvector& t)
{
ostringstream ostr;
ostr << setprecision(16) << t;
xmlNodePtr vNode = xmlNewChild(parent,NULL, BAD_CAST "value",
BAD_CAST (const char*)ostr.str().c_str());
return ((vNode==NULL));
}
int ADMB_XMLDoc::createValueNode(xmlNodePtr parent, const dmatrix& t)
{
ostringstream ostr;
ostr << setprecision(16) << t;
xmlNodePtr vNode = xmlNewChild(parent,NULL, BAD_CAST "value",
BAD_CAST (const char*)ostr.str().c_str());
return ((vNode==NULL));
}
int ADMB_XMLDoc::createValueNode(xmlNodePtr parent, const adstring_array& _t)
{
ADUNCONST(adstring_array, t)
const int i1 = t.indexmin();
const int i2 = t.indexmax();
ostringstream ostr;
for (int i = i1; i <= i2; i++)
{
ostr << t(i);
if (i < i2)
ostr << " ";
}
xmlNodePtr vNode = xmlNewChild(parent,NULL, BAD_CAST "value",
BAD_CAST (const char*)ostr.str().c_str());
return ((vNode==NULL));
}
int ADMB_XMLDoc::createValueNode(xmlNodePtr parent, const ivector& t)
{
ostringstream ostr;
ostr << t;
xmlNodePtr vNode = xmlNewChild(parent,NULL, BAD_CAST "value",
BAD_CAST (const char*)ostr.str().c_str());
return ((vNode==NULL));
}
int ADMB_XMLDoc::createValueNode(xmlNodePtr parent, const imatrix& t)
{
ostringstream ostr;
ostr << t;
xmlNodePtr vNode = xmlNewChild(parent,NULL, BAD_CAST "value",
BAD_CAST (const char*)ostr.str().c_str());
return ((vNode==NULL));
}
int ADMB_XMLDoc::createBoundsNode(xmlNodePtr parent, const double min, const double max)
{
xmlNodePtr bNode = xmlNewNode(NULL, BAD_CAST "bounds");
xmlNodePtr mNode = xmlNewNode(NULL, BAD_CAST "min");
ostringstream ostr;
ostr << min << ends;
xmlNodeAddContent(mNode, BAD_CAST (const char*)ostr.str().c_str());
xmlAddChild(bNode,mNode);
mNode = xmlNewNode(NULL, BAD_CAST "max");
// "rewind" the ostr, but don't forget the << ends
ostr.seekp(ios_base::beg);
ostr << max << ends;
xmlNodeAddContent(mNode, BAD_CAST (const char*)ostr.str().c_str());
xmlAddChild(bNode,mNode);
xmlAddChild(parent,bNode);
return ((bNode==NULL));
}
int ADMB_XMLDoc::createBoundsNode(xmlNodePtr parent, const dvector& min, const dvector& max)
{
xmlNodePtr node = createNameNode("bounds",NULL);
ostringstream ostr;
ostr << min << ends;
xmlNewChild(node, NULL, BAD_CAST "min", BAD_CAST ostr.str().c_str());
ostr.seekp(ios_base::beg);
ostr << max << ends;
xmlNewChild(node, NULL, BAD_CAST "max", BAD_CAST ostr.str().c_str());
xmlNodePtr tNode = xmlAddChild(parent,node);
return ((tNode==NULL));
}
int ADMB_XMLDoc::createBoundsNode(xmlNodePtr parent, const dmatrix& min, const dmatrix& max)
{
xmlNodePtr node = createNameNode("bounds",NULL);
ostringstream ostr;
ostr << min << ends;
xmlNewChild(node, NULL, BAD_CAST "min", BAD_CAST ostr.str().c_str());
ostr.seekp(ios_base::beg);
ostr << max << ends;
xmlNewChild(node, NULL, BAD_CAST "max", BAD_CAST ostr.str().c_str());
xmlNodePtr tNode = xmlAddChild(parent,node);
return ((tNode==NULL));
}
int ADMB_XMLDoc::createActiveNode(xmlNodePtr parent, const int t)
{
xmlNodePtr node = xmlNewNode(NULL, BAD_CAST "active");
ostringstream ostr;
ostr << t;
xmlNodeAddContent(node, BAD_CAST (const char*)ostr.str().c_str());
xmlAddChild(parent,node);
return ((node==NULL));
}
int ADMB_XMLDoc::createActiveNode(xmlNodePtr parent, const ivector& t)
{
ostringstream ostr;
ostr << t;
xmlNodePtr node = xmlNewChild(parent, NULL, BAD_CAST "active",
BAD_CAST (const char*)ostr.str().c_str());
return ((node==NULL));
}
int ADMB_XMLDoc::createActiveNode(xmlNodePtr parent, const imatrix& t)
{
ostringstream ostr;
ostr << t;
xmlNodePtr node = xmlNewChild(parent, NULL, BAD_CAST "active",
BAD_CAST (const char*)ostr.str().c_str());
return ((node==NULL));
}
int ADMB_XMLDoc::createPhaseNode(xmlNodePtr parent, const int t)
{
xmlNodePtr node = xmlNewNode(NULL, BAD_CAST "phase");
ostringstream ostr;
ostr << t;
xmlNodeAddContent(node, BAD_CAST (const char*)ostr.str().c_str());
xmlAddChild(parent,node);
return ((node==NULL));
}
int ADMB_XMLDoc::createPhaseNode(xmlNodePtr parent, const ivector& t)
{
ostringstream ostr;
ostr << t;
xmlNodePtr node = xmlNewChild(parent,NULL, BAD_CAST "phase",
BAD_CAST (const char*)ostr.str().c_str());
return ((node==NULL));
}
int ADMB_XMLDoc::createPhaseNode(xmlNodePtr parent, const imatrix& t)
{
ostringstream ostr;
ostr << t;
xmlNodePtr node = xmlNewChild(parent,NULL, BAD_CAST "phase",
BAD_CAST (const char*)ostr.str().c_str());
return ((node==NULL));
}
int ADMB_XMLDoc::createXMLelement(const objective_function_value& _t)
{
ADUNCONST(objective_function_value, t)
adstring name(t.get_name());
xmlNodePtr node = createNameNode(name, ADMB_XMLDoc::statS);
createTitleNode(node, adstring("Fit Statistics"));
ostringstream ostr;
ostr << initial_params::nvarcalc() << ends;
xmlNewChild(node, NULL, BAD_CAST "n", BAD_CAST ostr.str().c_str());
createValueNode(node,value(t));
ostr.seekp(ios_base::beg);
ostr << t.fun_without_pen << ends;
xmlNewChild(node, NULL, BAD_CAST "fun_without_pen", BAD_CAST ostr.str().c_str());
ostr.seekp(ios_base::beg);
ostr << setprecision(16) << t.gmax << ends;
xmlNewChild(node, NULL, BAD_CAST "gmax", BAD_CAST ostr.str().c_str());
xmlNodePtr tNode = xmlAddChild(RootNode,node);
return ((tNode == NULL));
}
int ADMB_XMLDoc::createXMLelement(const adstring& name, const param_init_bounded_number_vector& _t, const adstring& title)
{
ADUNCONST(param_init_bounded_number_vector, t)
const int i1 = t.indexmin();
const int i2 = t.indexmax();
dvector minb(i1,i2);
dvector maxb(i1,i2);
ivector phase(i1,i2);
ivector activef(i1,i2);
dvector vt(i1,i2);
for (int i = i1; i <= i2; i++)
{
minb(i) = t(i).get_minb();
maxb(i) = t(i).get_maxb();
phase(i) = t(i).phase_start;
activef(i) = active(t(i));
vt(i) = value(t(i));
}
xmlNodePtr node = createNameNode(name, ADMB_XMLDoc::paramS);
createTitleNode(node,title);
createValueNode(node,vt);
createIndexNode(node,i1,i2);
createBoundsNode(node,minb,maxb);
createActiveNode(node,activef);
createPhaseNode(node,phase);
xmlNodePtr tnode = xmlAddChild(RootNode,node);
return ((tnode == NULL));
}
int ADMB_XMLDoc::createXMLelement(const adstring& name, const adstring_array& _t, const adstring& title)
{
ADUNCONST(adstring_array, t)
const int i1 = t.indexmin();
const int i2 = t.indexmax();
xmlNodePtr node = createNameNode(name, ADMB_XMLDoc::dataS);
createTitleNode(node,title);
createValueNode(node,t);
createIndexNode(node,i1,i2);
xmlNodePtr tnode = xmlAddChild(RootNode,node);
return ((tnode == NULL));
}
int ADMB_XMLDoc::createXMLelement(const adstring& name, const ivector& t, const adstring& title)
{
const int i1 = t.indexmin();
const int i2 = t.indexmax();
xmlNodePtr node = createNameNode(name, ADMB_XMLDoc::paramS);
createTitleNode(node,title);
createValueNode(node,t);
createIndexNode(node,i1,i2);
xmlNodePtr tnode = xmlAddChild(RootNode,node);
return ((tnode == NULL));
}
int ADMB_XMLDoc::createXMLelement(const adstring& name, const imatrix& _t, const adstring& title)
{
ADUNCONST(imatrix, t)
xmlNodePtr node = createNameNode(name, ADMB_XMLDoc::dataS);
createTitleNode(node,title);
const int i1 = t.colmin();
const int i2 = t.colmax();
const int j1 = t.rowmin();
const int j2 = t.rowmax();
createValueNode(node,t);
createIndexNode(node,j1,j2,i1,i2);
xmlNodePtr nnode = xmlAddChild(RootNode,node);
return ((nnode == NULL));
}
int ADMB_XMLDoc::createXMLelement(const param_init_bounded_vector& _t, const adstring& title)
{
ADUNCONST(param_init_bounded_vector, t)
adstring name(t.get_name());
const int i1 = t.indexmin();
const int i2 = t.indexmax();
xmlNodePtr node = createNameNode(name, ADMB_XMLDoc::paramS);
createTitleNode(node,title);
createValueNode(node,value(t));
createIndexNode(node,i1,i2);
createBoundsNode(node,t.get_minb(),t.get_maxb());
createActiveNode(node,active(t));
createPhaseNode(node,t.phase_start);
xmlNodePtr tnode = xmlAddChild(RootNode,node);
return ((tnode == NULL));
}
int ADMB_XMLDoc::createIndexNode(xmlNodePtr parent, const int lb, const int ub)
{
xmlNodePtr node = createNameNode("index",NULL);
ostringstream ostr;
ostr << lb << ends;
xmlNewChild(node, NULL, BAD_CAST "lb", BAD_CAST ostr.str().c_str());
ostr.seekp(ios_base::beg);
ostr << ub << ends;
xmlNewChild(node, NULL, BAD_CAST "ub", BAD_CAST ostr.str().c_str());
xmlNodePtr tnode = xmlAddChild(parent,node);
return ((tnode == NULL));
}
int ADMB_XMLDoc::createIndexNode(xmlNodePtr parent, const int nrl, const int nrh,
const int ncl, const int nch)
{
// imat.cpp: imatrix::imatrix(int nrl, int nrh, int ncl, int nch)
xmlNodePtr node = createNameNode("index",NULL);
ostringstream ostr;
ostr << nrl << ends;
xmlNewChild(node, NULL, BAD_CAST "nrl", BAD_CAST ostr.str().c_str());
ostr.seekp(ios_base::beg);
ostr << nrh << ends;
xmlNewChild(node, NULL, BAD_CAST "nrh", BAD_CAST ostr.str().c_str());
ostr.seekp(ios_base::beg);
ostr << ncl << ends;
xmlNewChild(node, NULL, BAD_CAST "ncl", BAD_CAST ostr.str().c_str());
ostr.seekp(ios_base::beg);
ostr << nch << ends;
xmlNewChild(node, NULL, BAD_CAST "nch", BAD_CAST ostr.str().c_str());
xmlNodePtr tnode = xmlAddChild(parent,node);
return ((tnode == NULL));
}
int ADMB_XMLDoc::createXMLelement(const adstring& name, const adstring& t, const adstring& title)
{
xmlNodePtr node = createNameNode(name, ADMB_XMLDoc::dataS);
createTitleNode(node,title);
createValueNode(node,t);
xmlNodePtr tnode = xmlAddChild(RootNode,node);
return ((tnode == NULL));
}
int ADMB_XMLDoc::createXMLelement(const adstring& name, const int t, const adstring& title)
{
xmlNodePtr node = createNameNode(name, ADMB_XMLDoc::dataS);
createTitleNode(node,title);
createValueNode(node,t);
xmlNodePtr tnode = xmlAddChild(RootNode,node);
return ((tnode == NULL));
}
/**
Read and parse and xml file. Reports errors for ill-formed xml tree.
\param parfile XMLFileName
*/
int ADMB_XMLDoc::read(const char* parfile)
{
if (parfile == 0) {
return -1;
}
doc = xmlParseFile(parfile);
if (doc == NULL)
{
cerr << "Error parsing " << parfile << endl;
//xmlErrorPtr ErrorPtr1 = xmlGetLastError();
ad_exit(1);
}
XMLFileName = string((char*)(doc->URL));
DocRootName = string((char*)(doc->last->name));
context = xmlXPathNewContext(doc);
if (context == NULL)
{
ad_exit(1);
}
return 1;
}
/**
\param name Variable name of devarialbe to fetch
\return Value of name in xml node
*/
double ADMB_XMLDoc::getDouble(const string& name) const
{
double d = 0.0;
string s = getContentString(name,"value");
if (!s.empty()) {
d = strtod(s.c_str(),0);
}
else {
throw runtime_error("Error: XPath=\"" + name + "\" does not exist.\n");
}
return d;
}
int ADMB_XMLDoc::getInt(const string& name) const
{
int d = 0;
string s = getContentString(name,"value");
if (!s.empty()) {
d = atoi(s.c_str());
}
else {
throw runtime_error("Error: XPath=\"" + name + "\" does not exist.\n");
}
return d;
}
adstring ADMB_XMLDoc::getString(const string& name) const
{
adstring d;
string s = getContentString(name,"value");
if (!s.empty()) {
d = adstring(s.c_str());
}
else {
throw runtime_error("Error: XPath=\"" + name + "\" does not exist.\n");
}
return d;
}
/*
<fleet_names category="CON">
<title>Names of fleets to use in the model</title>
<value> fjpl jppl pgpl sbpl jpps</value>
</fleet_names>
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}
*/
/** Extract adstring_array from xml document.
This function causes a segment violation on return.
The problem seems to be passing the array.
\param name Name of the variable.
\parm lb Upper bound of array.
\parm ub Lower bound of array.
\return Reference to adstring_array&
*/
adstring_array ADMB_XMLDoc::getADStringArray(const string& name, const int lb, const int ub) const
{
string s = getContentString(name,"value");
adstring_array a(lb,ub);
istringstream istr(s);
for (int i = lb; i <= ub; i++)
{
istr >> a(i);
}
return a;
}
void ADMB_XMLDoc::getADStringArray(const string& name, adstring_array* p) const
{
string s = getContentString(name,"value");
char * pch = NULL;
pch = strtok((char*)s.c_str()," ");
const int lb = p->indexmin();
const int ub = p->indexmax();
for (int i = lb; ((i <= ub)&&(pch != NULL)); i++)
{
adstring tmp(pch);
(*p)(i) = tmp; //adstring((const char*)pch);
pch = strtok (NULL, " ");
}
}
ivector ADMB_XMLDoc::getIvector(const string& name) const
{
string xpath = "/"+DocRootName + "/" + name + "/index/";
string s = getContentString(xpath+"lb");
int lb = atoi(s.c_str());
s = getContentString(xpath+"ub");
int ub = atoi(s.c_str());
ivector d = getIvector(name, lb, ub);
return d;
}
ivector ADMB_XMLDoc::getIvector(const string& name, const int lb, const int ub) const
{
string s = getContentString(name,"value");
istringstream istr(s);
ivector d(lb,ub);
istr >> d;
return d;
}
dvector ADMB_XMLDoc::getDvector(const string& name) const
{
string xpath = "/"+DocRootName + "/" + name + "/index/";
string s = getContentString(xpath+"lb");
int lb = atoi(s.c_str());
s = getContentString(xpath+"ub");
int ub = atoi(s.c_str());
dvector d = getDvector(name, lb, ub);
return d;
}
dvector ADMB_XMLDoc::getDvector(const string& name, const int lb, const int ub) const
{
string s = getContentString(name,"value");
istringstream istr(s);
dvector d(lb,ub);
istr >> d;
return d;
}
/*
double ADMB_XMLDoc::getMinNumberMatrixBounds(const string& name) const
{
string xpath;
string s;
xpath = "/"+DocRootName + "/" + name + "/index/";
s = getContentString(xpath+"nrl");
int nrl = atoi(s.c_str());
s = getContentString(xpath+"nrh");
int nrh = atoi(s.c_str());
s = getContentString(xpath+"ncl");
int ncl = atoi(s.c_str());
s = getContentString(xpath+"nch");
int nch = atoi(s.c_str());
dmatrix m(nrl, nrh, ncl, nch);
xpath = "/"+DocRootName + "/" + name + "/bounds/min";
s = getContentString(xpath);
istringstream istr(s);
istr >> m;
double b = min(m);
return(b);
}
double ADMB_XMLDoc::getMaxNumberMatrixBounds(const string& name) const
{
string xpath;
string s;
xpath = "/"+DocRootName + "/" + name + "/index/";
s = getContentString(xpath+"nrl");
int nrl = atoi(s.c_str());
s = getContentString(xpath+"nrh");
int nrh = atoi(s.c_str());
s = getContentString(xpath+"ncl");
int ncl = atoi(s.c_str());
s = getContentString(xpath+"nch");
int nch = atoi(s.c_str());
dmatrix m(nrl, nrh, ncl, nch);
xpath = "/"+DocRootName + "/" + name + "/bounds/max";
s = getContentString(xpath);
istringstream istr(s);
istr >> m;
double b = max(m);
return(b);
}
*/
/*
<index>
<nrl>1</nrl>
<nrh>80</nrh>
<ncl>1</ncl>
<nch>40</nch>
</index>
*/
imatrix ADMB_XMLDoc::getImatrix(const string& name) const
{
string xpath = "/"+DocRootName + "/" + name + "/index/";
string s = getContentString(xpath+"nrl");
int nrl = atoi(s.c_str());
s = getContentString(xpath+"nrh");
int nru = atoi(s.c_str());
// you must call istringstream::clear()
// in order to reuse the istringstream instance
istringstream istr;
s = getContentString(xpath+"ncl");
int ncl = atoi(s.c_str());
//istr.str(s);
//ivector ncl(nrl,nru);
//istr >> ncl;
s = getContentString(xpath+"nch");
int nch = atoi(s.c_str());
//istr.clear();
//istr.str(s);
//ivector nch(nrl,nru);
//istr >> nch;
imatrix m(nrl, nru, ncl, nch);
s = getContentString(name,"value");
istr.clear();
istr.str(s);
istr >> m;
return m;
}
/*
<index>
<nrl>1</nrl>
<nrh>1</nrh>
<ncl>1</ncl>
<nch>19</nch>
</index>
*/
dmatrix ADMB_XMLDoc::getDmatrix(const string& name) const
{
string xpath = "/"+DocRootName + "/" + name + "/index/";
string s = getContentString(xpath+"nrl");
int nrl = atoi(s.c_str());
s = getContentString(xpath+"nrh");
int nrh = atoi(s.c_str());
// you must call istringstream::clear()
// in order to reuse the istringstream instance
istringstream istr;
s = getContentString(xpath+"ncl");
int ncl = atoi(s.c_str());
//istr.str(s);
//ivector ncl(nrl,nru);
//istr >> ncl;
s = getContentString(xpath+"nch");
int nch = atoi(s.c_str());
//istr.clear();
//istr.str(s);
//ivector nch(nrl,nru);
//istr >> nch;
dmatrix m(nrl, nrh, ncl, nch);
s = getContentString(name,"value");
istr.clear();
istr.str(s);
istr >> m;
return m;
}
/**
* @param xname absolute element xpath
*/
string ADMB_XMLDoc::getContentString(const string& xpathfull) const
{
string value;
xmlXPathObject* xpathObj = NULL;
xpathObj = xmlXPathEvalExpression((xmlChar*)xpathfull.c_str(), context);
if (xpathObj != 0)
{
if (xpathObj->type == XPATH_NODESET) {
xmlNodeSet* xmlnodeset = xpathObj->nodesetval;
const int max = xmlnodeset->nodeNr;
for (int i = 0; i < max; i++) {
xmlNode* xmlnode = xmlnodeset->nodeTab[i];
if (xmlnode != 0 && xmlnode->children)
{
value += (char*)xmlnode->children->content;
}
}
}
}
if (value.empty())
{
cerr << "Error: Unable to extract XPath, \"" << xpathfull
<< "\", from XML tree in file " << XMLFileName << endl;
ad_exit(1);
}
return value;
}
/**
\param name Element name
\param tag A specific tag, i. e., "value"
*/
string ADMB_XMLDoc::getContentString(const string& name, const string& tag) const
{
string value;
string xpathfull = "/"+DocRootName + "/" + name + "/" + tag;
value = getContentString(xpathfull);
return value;
}
/**
* @param xname absolute element xpath
*/
string* ADMB_XMLDoc::getContentStringArray(const string& xpathfull) const
{
string* value = NULL;
xmlXPathObject* xpathObj = xmlXPathEvalExpression((xmlChar*)xpathfull.c_str(), context);
if (xpathObj == 0) {
return value;
}