SAGA API Version 9.13
Loading...
Searching...
No Matches
metadata.cpp
Go to the documentation of this file.
1
3// //
4// SAGA //
5// //
6// System for Automated Geoscientific Analyses //
7// //
8// Application Programming Interface //
9// //
10// Library: SAGA_API //
11// //
12//-------------------------------------------------------//
13// //
14// metadata.cpp //
15// //
16// Copyright (C) 2009 by Olaf Conrad //
17// //
18//-------------------------------------------------------//
19// //
20// This file is part of 'SAGA - System for Automated //
21// Geoscientific Analyses'. //
22// //
23// This library is free software; you can redistribute //
24// it and/or modify it under the terms of the GNU Lesser //
25// General Public License as published by the Free //
26// Software Foundation, either version 2.1 of the //
27// License, or (at your option) any later version. //
28// //
29// This library is distributed in the hope that it will //
30// be useful, but WITHOUT ANY WARRANTY; without even the //
31// implied warranty of MERCHANTABILITY or FITNESS FOR A //
32// PARTICULAR PURPOSE. See the GNU Lesser General Public //
33// License for more details. //
34// //
35// You should have received a copy of the GNU Lesser //
36// General Public License along with this program; if //
37// not, see <http://www.gnu.org/licenses/>. //
38// //
39//-------------------------------------------------------//
40// //
41// contact: Olaf Conrad //
42// Institute of Geography //
43// University of Hamburg //
44// Germany //
45// //
46// e-mail: oconrad@saga-gis.org //
47// //
49
50//---------------------------------------------------------
51#include <wx/xml/xml.h>
52#include <wx/wfstream.h>
53#include <wx/sstream.h>
54#include <wx/mstream.h>
55#include <wx/protocol/http.h>
56
57#include "metadata.h"
58#include "table.h"
59
60
62// //
63// //
64// //
66
67//---------------------------------------------------------
69{
70 _On_Construction();
71}
72
74{
75 return( true );
76}
77
78//---------------------------------------------------------
80{
81 _On_Construction();
82
83 Create(MetaData);
84}
85
87{
88 return( Assign(MetaData) );
89}
90
91//---------------------------------------------------------
92CSG_MetaData::CSG_MetaData(const CSG_String &File, const SG_Char *Extension)
93{
94 _On_Construction();
95
96 Create(File, Extension);
97}
98
99bool CSG_MetaData::Create(const CSG_String &File, const SG_Char *Extension)
100{
101 return( Load(File, Extension) );
102}
103
104//---------------------------------------------------------
106{
107 _On_Construction();
108
109 Create(Stream);
110}
111
113{
114 return( Load(Stream) );
115}
116
117//---------------------------------------------------------
119{
120 _On_Construction();
121
122 m_pParent = pParent;
123}
124
125//---------------------------------------------------------
126void CSG_MetaData::_On_Construction(void)
127{
128 m_pParent = NULL;
129
131}
132
133//---------------------------------------------------------
138
139//---------------------------------------------------------
141{
142 CSG_MetaData **m_pChildren = (CSG_MetaData **)m_Children.Get_Array();
143
144 for(int i=0; i<Get_Children_Count(); i++)
145 {
146 delete(m_pChildren[i]);
147 }
148
149 m_Children.Destroy();
150
151// m_pParent = NULL;
152
153// m_Name .Clear();
154// m_Content .Clear();
155
156 m_Prop_Names .Clear();
157 m_Prop_Values.Clear();
158}
159
160
162// //
164
165//---------------------------------------------------------
167{
168 return( Ins_Child(-1) );
169}
170
172{
173 return( Ins_Child(Name, -1) );
174}
175
177{
178 return( Ins_Child(Name, Content, -1) );
179}
180
181CSG_MetaData * CSG_MetaData::Add_Child(const CSG_String &Name, double Content)
182{
183 return( Ins_Child(Name, SG_Get_String(Content, -16), -1) );
184}
185
187{
188 return( Ins_Child(Name, CSG_String::Format(SG_T("%d"), Content), -1) );
189}
190
192{
193 return( Ins_Child(Name, CSG_String::Format(SG_T("%lld"), Content), -1) );
194}
195
196CSG_MetaData * CSG_MetaData::Add_Child(const CSG_MetaData &MetaData, bool bAddChildren)
197{
198 return( Ins_Child(MetaData, -1, bAddChildren) );
199}
200
201
203// //
205
206//---------------------------------------------------------
208{
209 if( !m_Children.Inc_Array() )
210 {
211 return( NULL );
212 }
213
214 CSG_MetaData **pChildren = (CSG_MetaData **)m_Children.Get_Array();
215
216 if( Position < 0 || Position >= Get_Children_Count() )
217 {
218 Position = Get_Children_Count() - 1;
219 }
220
221 for(int i=Get_Children_Count()-1; i>Position; i--)
222 {
223 pChildren[i] = pChildren[i - 1];
224 }
225
226 return( pChildren[Position] = new CSG_MetaData(this) );
227}
228
229//---------------------------------------------------------
230CSG_MetaData * CSG_MetaData::Ins_Child(const CSG_String &Name, const CSG_String &Content, int Position)
231{
232 CSG_MetaData *pChild = Ins_Child(Position);
233
234 if( pChild )
235 {
236 pChild->m_Name = Name;
237 pChild->m_Content = Content;
238 }
239
240 return( pChild );
241}
242
244{
245 return( Ins_Child(Name, CSG_String(""), Position) );
246}
247
248CSG_MetaData * CSG_MetaData::Ins_Child(const CSG_String &Name, double Content, int Position)
249{
250 return( Ins_Child(Name, SG_Get_String(Content, -16), Position) );
251}
252
253CSG_MetaData * CSG_MetaData::Ins_Child(const CSG_String &Name, int Content, int Position)
254{
255 return( Ins_Child(Name, CSG_String::Format(SG_T("%d"), Content), Position) );
256}
257
258CSG_MetaData * CSG_MetaData::Ins_Child(const CSG_String &Name, sLong Content, int Position)
259{
260 return( Ins_Child(Name, CSG_String::Format(SG_T("%lld"), Content), Position) );
261}
262
263CSG_MetaData * CSG_MetaData::Ins_Child(const CSG_MetaData &MetaData, int Position, bool bAddChildren)
264{
265 CSG_MetaData *pChild = Ins_Child(Position);
266
267 if( pChild )
268 {
269 pChild->Assign(MetaData, bAddChildren);
270 }
271
272 return( pChild );
273}
274
275
277// //
279
280//---------------------------------------------------------
281bool CSG_MetaData::Mov_Child(int from_Index, int to_Index)
282{
283 if( from_Index < 0 || from_Index >= Get_Children_Count()
284 || to_Index < 0 || to_Index >= Get_Children_Count() )
285 {
286 return( false );
287 }
288
289 if( from_Index != to_Index )
290 {
291 CSG_MetaData **pChildren = (CSG_MetaData **)m_Children.Get_Array();
292 CSG_MetaData *pChild = pChildren[from_Index];
293
294 if( from_Index < to_Index )
295 {
296 for(int i=from_Index; i<to_Index; i++)
297 {
298 pChildren[i] = pChildren[i + 1];
299 }
300 }
301 else // if( from_Index > to_Index )
302 {
303 for(int i=from_Index; i>to_Index; i--)
304 {
305 pChildren[i] = pChildren[i - 1];
306 }
307 }
308
309 pChildren[to_Index] = pChild;
310 }
311
312 return( true );
313}
314
315
317// //
319
320//---------------------------------------------------------
322{
323 if( Index >= 0 && Index < Get_Children_Count() )
324 {
325 CSG_MetaData **pChildren = (CSG_MetaData **)m_Children.Get_Array();
326
327 delete(pChildren[Index]);
328
329 for(int i=Index, j=Index+1; j<Get_Children_Count(); i++, j++)
330 {
331 pChildren[i] = pChildren[j];
332 }
333
334 m_Children.Dec_Array();
335
336 return( true );
337 }
338
339 return( false );
340}
341
342//---------------------------------------------------------
344{
345 if( Name.Find('.') > 0 )
346 {
347 CSG_MetaData *pParent = Get_Child(Name.BeforeLast('.'));
348
349 if( pParent )
350 {
351 return( pParent->Del_Child(Name.AfterLast('.')) );
352 }
353 }
354
355 return( Del_Child(_Get_Child(Name)) );
356}
357
358//---------------------------------------------------------
360{
361 if( &MetaData != this )
362 {
363 for(int i=0; i<MetaData.Get_Children_Count(); i++)
364 {
365 Add_Child(MetaData[i], true);
366 }
367 }
368
369 return( true );
370}
371
372//---------------------------------------------------------
380//---------------------------------------------------------
381bool CSG_MetaData::Del_Children(int Depth, const SG_Char *Name)
382{
383 if( Depth < 0 )
384 {
385 // nop
386 }
387 else if( Name && *Name )
388 {
389 for(int i=Get_Children_Count()-1; i>=0; i--)
390 {
391 if( Get_Child(i)->Get_Name().CmpNoCase(Name) )
392 {
393 Get_Child(i)->Del_Children(Depth, Name);
394 }
395 else if( Depth > 0 )
396 {
397 Get_Child(i)->Del_Children(Depth - 1, Name);
398 }
399 else
400 {
401 Del_Child(i);
402 }
403 }
404 }
405 else if( Depth > 0 )
406 {
407 for(int i=0; i<Get_Children_Count(); i++)
408 {
409 Get_Child(i)->Del_Children(Depth - 1, Name);
410 }
411 }
412 else
413 {
414 for(int i=0; i<Get_Children_Count(); i++)
415 {
416 delete(Get_Child(i));
417 }
418
419 m_Children.Destroy();
420 }
421
422 return( true );
423}
424
425//---------------------------------------------------------
427{
428 if( Name.Find('.') > 0 )
429 {
430 CSG_MetaData *pEntry = Get_Child(Name.BeforeFirst('.'));
431
432 if( pEntry )
433 {
434 return( pEntry->Get_Child(Name.AfterFirst('.')) );
435 }
436 }
437
438 return( Get_Child(_Get_Child(Name)) );
439}
440
441//---------------------------------------------------------
443{
444 if( JSON_Pointer.Find('/') > 0 )
445 {
446 CSG_String Child_Name = JSON_Pointer.BeforeFirst('/');
447 Child_Name.Replace("~0", "~");
448 Child_Name.Replace("~1", "/");
449 int Index = -1;
450 CSG_MetaData *pEntry = Child_Name.asInt(Index) ? Get_Child(Index) : Get_Child(Child_Name);
451 if( pEntry )
452 {
453 return( pEntry->Get_Child_by_JSON_Pointer(JSON_Pointer.AfterFirst('/')) );
454 }
455 }
456 else
457 {
458 int Index = -1;
459 return( JSON_Pointer.asInt(Index) ? Get_Child(Index) : Get_Child(_Get_Child(JSON_Pointer)) );
460 }
461 return( nullptr );
462}
463
464//---------------------------------------------------------
465int CSG_MetaData::_Get_Child(const CSG_String &Name) const
466{
467 for(int i=0; i<Get_Children_Count(); i++)
468 {
469 if( Name.CmpNoCase(Get_Child(i)->Get_Name()) == 0 )
470 {
471 return( i );
472 }
473 }
474
475 return( -1 );
476}
477
478
480// //
482
483//---------------------------------------------------------
484bool CSG_MetaData::Cmp_Name(const CSG_String &String, bool bNoCase) const
485{
486 return( bNoCase ? !m_Name.CmpNoCase(String) : !m_Name.Cmp(String) );
487}
488
489
491// //
493
494//---------------------------------------------------------
496{
497 if( Name.is_Empty() )
498 {
499 return( m_Content );
500 }
501
502 CSG_MetaData *pEntry = Get_Child(Name);
503
504 return( pEntry ? pEntry->Get_Content().c_str() : NULL );
505}
506
507//---------------------------------------------------------
508bool CSG_MetaData::Get_Content(const CSG_String &Name, CSG_String &Value) const
509{
510 const SG_Char *cString = Name.is_Empty() ? Get_Content().c_str() : Get_Content(Name);
511
512 if( cString )
513 {
514 Value = cString;
515
516 return( true );
517 }
518
519 return( false );
520}
521
522//---------------------------------------------------------
523bool CSG_MetaData::Get_Content(const CSG_String &Name, double &Value) const { CSG_String s; return( Get_Content(Name, s) && s.asDouble (Value) ); }
524bool CSG_MetaData::Get_Content(const CSG_String &Name, int &Value) const { CSG_String s; return( Get_Content(Name, s) && s.asInt (Value) ); }
525bool CSG_MetaData::Get_Content(const CSG_String &Name, sLong &Value) const { CSG_String s; return( Get_Content(Name, s) && s.asLongLong(Value) ); }
526
527
529// //
531
532//---------------------------------------------------------
533void CSG_MetaData::Fmt_Content(const char *Format, ...)
534{
535 wxString s; va_list argptr;
536
537#ifdef _SAGA_LINUX
538 wxString _Format(Format); _Format.Replace("%s", "%ls"); // workaround as we only use wide characters since wx 2.9.4 so interpret strings as multibyte
539 va_start(argptr, _Format);
540 s.PrintfV(_Format, argptr);
541#else
542 va_start(argptr, Format);
543 s.PrintfV(Format, argptr);
544#endif
545
546 m_Content = CSG_String(&s);
547
548 va_end(argptr);
549}
550
551//---------------------------------------------------------
552void CSG_MetaData::Fmt_Content(const wchar_t *Format, ...)
553{
554 wxString s; va_list argptr;
555
556#ifdef _SAGA_LINUX
557 wxString _Format(Format); _Format.Replace("%s", "%ls"); // workaround as we only use wide characters since wx 2.9.4 so interpret strings as multibyte
558 va_start(argptr, _Format);
559 s.PrintfV(_Format, argptr);
560#else
561 va_start(argptr, Format);
562 s.PrintfV(Format, argptr);
563#endif
564
565 m_Content = CSG_String(&s);
566
567 va_end(argptr);
568}
569
570//---------------------------------------------------------
571bool CSG_MetaData::Cmp_Content(const CSG_String &String, bool bNoCase) const
572{
573 return( bNoCase ? !m_Content.CmpNoCase(String) : !m_Content.Cmp(String) );
574}
575
576
578// //
580
581//---------------------------------------------------------
582bool CSG_MetaData::Add_Property(const CSG_String &Name, const CSG_String &Value)
583{
584 if( !Value.is_Empty() &&_Get_Property(Name) < 0 )
585 {
586 m_Prop_Names .Add(Name );
587 m_Prop_Values.Add(Value);
588
589 return( true );
590 }
591
592 return( false );
593}
594
595bool CSG_MetaData::Add_Property(const CSG_String &Name, double Value)
596{
597 return( Add_Property(Name, CSG_String::Format(SG_T("%f"), Value)) );
598}
599
600bool CSG_MetaData::Add_Property(const CSG_String &Name, int Value)
601{
602 return( Add_Property(Name, CSG_String::Format(SG_T("%d"), Value)) );
603}
604
606{
607 return( Add_Property(Name, CSG_String::Format(SG_T("%lld"), Value)) );
608}
609
610//---------------------------------------------------------
612{
613 for(int i=0; i<Get_Property_Count(); i++)
614 {
615 if( !Get_Property_Name(i).CmpNoCase(Name) )
616 {
617 return( Del_Property(i) );
618 }
619 }
620
621 return( false );
622}
623
625{
626 if( i >= 0 && i < Get_Property_Count() )
627 {
628 m_Prop_Names .Del(i);
629 m_Prop_Values.Del(i);
630
631 return( true );
632 }
633
634 return( false );
635}
636
637//---------------------------------------------------------
638bool CSG_MetaData::Set_Property(const CSG_String &Name, const CSG_String &Value, bool bAddIfNotExists)
639{
640 int Index = _Get_Property(Name);
641
642 if( Index >= 0 )
643 {
644 m_Prop_Values[Index] = Value;
645
646 return( true );
647 }
648 else if( bAddIfNotExists )
649 {
650 m_Prop_Names .Add(Name);
651 m_Prop_Values.Add(Value);
652
653 return( true );
654 }
655
656 return( false );
657}
658
659bool CSG_MetaData::Set_Property(const CSG_String &Name, double Value, bool bAddIfNotExists)
660{
661 return( Set_Property(Name, CSG_String::Format(SG_T("%f"), Value, bAddIfNotExists)) );
662}
663
664bool CSG_MetaData::Set_Property(const CSG_String &Name, int Value, bool bAddIfNotExists)
665{
666 return( Set_Property(Name, CSG_String::Format(SG_T("%d"), Value, bAddIfNotExists)) );
667}
668
669bool CSG_MetaData::Set_Property(const CSG_String &Name, sLong Value, bool bAddIfNotExists)
670{
671 return( Set_Property(Name, CSG_String::Format(SG_T("%lld"), Value, bAddIfNotExists)) );
672}
673
674//---------------------------------------------------------
675bool CSG_MetaData::Get_Property(const CSG_String &Name, CSG_String &Value) const
676{
677 const SG_Char *cString = Get_Property(Name);
678
679 if( cString )
680 {
681 Value = cString;
682
683 return( true );
684 }
685
686 return( false );
687}
688
689bool CSG_MetaData::Get_Property(const CSG_String &Name, double &Value) const { CSG_String s; return( Get_Property(Name, s) && s.asDouble (Value) ); }
690bool CSG_MetaData::Get_Property(const CSG_String &Name, int &Value) const { CSG_String s; return( Get_Property(Name, s) && s.asInt (Value) ); }
691bool CSG_MetaData::Get_Property(const CSG_String &Name, sLong &Value) const { CSG_String s; return( Get_Property(Name, s) && s.asLongLong(Value) ); }
692
693//---------------------------------------------------------
694bool CSG_MetaData::Cmp_Property(const CSG_String &Name, const CSG_String &String, bool bNoCase) const
695{
696 CSG_String s;
697
698 return( Get_Property(Name, s) && (bNoCase ? !s.CmpNoCase(String) : !s.Cmp(String)) );
699}
700
701//---------------------------------------------------------
702int CSG_MetaData::_Get_Property(const CSG_String &Name) const
703{
704 for(int i=0; i<m_Prop_Names.Get_Count(); i++)
705 {
706 if( Name.CmpNoCase(m_Prop_Names[i]) == 0 )
707 {
708 return( i );
709 }
710 }
711
712 return( -1 );
713}
714
715
717// //
719
720//---------------------------------------------------------
722{
723 CSG_String s;
724
725 if( Flags == 0 )
726 {
727 for(int i=0; i<Get_Children_Count(); i++)
728 {
729 s += Get_Child(i)->Get_Name() + ":\t" + Get_Child(i)->Get_Content() + "\n";
730 }
731 }
732 else
733 {
734 wxXmlDocument XML; wxXmlNode *pRoot = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, Get_Name().c_str());
735
736 XML.SetRoot(pRoot);
737
738 Save(pRoot);
739
740 wxStringOutputStream Stream;
741
742 XML.Save(Stream);
743
744 s = &Stream.GetString();
745
746 if( Flags == 2 ) // remove <xml>
747 {
748 s = s.AfterFirst('\n');
749 }
750 }
751
752 return( s );
753}
754
755//---------------------------------------------------------
757{
758 CSG_Table t;
759
760 t.Add_Field("NAME" , SG_DATATYPE_String);
761 t.Add_Field("VALUE", SG_DATATYPE_String);
762
763 for(int i=0; i<Get_Children_Count(); i++)
764 {
766
767 r->Set_Value(0, Get_Child(i)->Get_Name());
768 r->Set_Value(1, Get_Child(i)->Get_Content());
769 }
770
771 return( t );
772}
773
774
776// //
778
779//---------------------------------------------------------
780bool CSG_MetaData::Assign(const CSG_MetaData &MetaData, bool bAddChildren)
781{
782 if( &MetaData != this )
783 {
784 Destroy();
785
786 Set_Name (MetaData.Get_Name ());
787 Set_Content(MetaData.Get_Content());
788
789 for(int i=0; i<MetaData.Get_Property_Count(); i++)
790 {
791 Add_Property(MetaData.Get_Property_Name(i), MetaData.Get_Property(i));
792 }
793
794 if( bAddChildren )
795 {
796 Add_Children(MetaData);
797 }
798 }
799
800 return( true );
801}
802
803
805// //
807
808//---------------------------------------------------------
809bool CSG_MetaData::Load(const CSG_String &File, const SG_Char *Extension)
810{
811 Destroy();
812
813 //-----------------------------------------------------
814 if( File.Find("http://") == 0 )
815 {
816 CSG_String s(File.Right(File.Length() - CSG_String("http://").Length()));
817
818 return( Load_HTTP(s.BeforeFirst('/'), s.AfterFirst('/')) );
819 }
820
821 //-----------------------------------------------------
822 CSG_String _File(SG_File_Make_Path("", File, Extension));
823
824 if( !SG_File_Exists(_File) )
825 {
826 return( false );
827 }
828
829 //-----------------------------------------------------
830 if( SG_File_Cmp_Extension(_File, "json") )
831 {
832 return( Load_JSON(_File) );
833 }
834
835 //-----------------------------------------------------
836 wxXmlDocument XML;
837
838 if( XML.Load(_File.c_str()) )
839 {
840 Load(XML.GetRoot());
841
842 return( true );
843 }
844
845 //-----------------------------------------------------
846 return( false );
847}
848
849//---------------------------------------------------------
851{
852 Destroy();
853
854 wxXmlDocument XML;
855
856 if( File.is_Reading() && XML.Load(*((wxInputStream *)File.Get_Stream())) )
857 {
858 Load(XML.GetRoot());
859
860 return( true );
861 }
862
863 return( false );
864}
865
866//---------------------------------------------------------
867bool CSG_MetaData::Load(wxXmlNode *pNode)
868{
869 m_Name = pNode->GetName ().wc_str();
870 m_Content = pNode->GetNodeContent().wc_str();
871
872 //-----------------------------------------------------
873 wxXmlAttribute *pProperty = pNode->GetAttributes();
874
875 while( pProperty )
876 {
877 Add_Property(&pProperty->GetName(), &pProperty->GetValue());
878
879 pProperty = pProperty->GetNext();
880 }
881
882 //-----------------------------------------------------
883 wxXmlNode *pChild = pNode->GetChildren();
884
885 while( pChild )
886 {
887 if( pChild->GetType() != wxXML_TEXT_NODE )
888 {
889 Add_Child()->Load(pChild);
890 }
891
892 pChild = pChild->GetNext();
893 }
894
895 return( true );
896}
897
898
900// //
902
903//---------------------------------------------------------
904bool CSG_MetaData::Save(const CSG_String &File, const SG_Char *Extension) const
905{
906 wxXmlDocument XML; wxXmlNode *pRoot = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, Get_Name().c_str());
907
908 XML.SetRoot(pRoot);
909
910 Save(pRoot);
911
912 if( XML.Save(SG_File_Make_Path("", File, Extension).c_str()) )
913 {
914 return( true );
915 }
916
917 return( false );
918}
919
920//---------------------------------------------------------
922{
923 wxXmlDocument XML; wxXmlNode *pRoot = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, Get_Name().c_str());
924
925 XML.SetRoot(pRoot);
926
927 Save(pRoot);
928
929 if( File.is_Writing() && XML.Save(*((wxOutputStream *)File.Get_Stream())) )
930 {
931 return( true );
932 }
933
934 return( false );
935}
936
937//---------------------------------------------------------
938bool CSG_MetaData::Save(wxXmlNode *pNode) const
939{
940 CSG_String Name(Get_Name()); if( Name.is_Empty() ) { Name = "NODE"; } else { Name.Replace(" ", "_"); } // name must not contain spaces!
941
942 pNode->SetName(Name.c_str());
943
944 pNode->SetContent(Get_Content().c_str());
945
946 if( Get_Content().Length() > 0 || (Get_Property_Count() == 0 && Get_Children_Count() == 0) )
947 {
948 wxXmlNode *pChild = new wxXmlNode(pNode, wxXML_TEXT_NODE, SG_T("TEXT"));
949
950 pChild->SetContent(Get_Content().c_str());
951 }
952
953 //-----------------------------------------------------
954 for(int i=0; i<Get_Property_Count(); i++)
955 {
956 pNode->AddAttribute(Get_Property_Name(i).c_str(), Get_Property(i));
957 }
958
959 //-----------------------------------------------------
960 for(int i=Get_Children_Count()-1; i>=0; i--)
961 {
962 Get_Child(i)->Save(new wxXmlNode(pNode, wxXML_ELEMENT_NODE, Get_Child(i)->Get_Name().c_str()));
963 }
964
965 return( true );
966}
967
968
970// //
972
973//---------------------------------------------------------
974bool CSG_MetaData::from_XML(const char *_XML, size_t Length, const char *Encoding)
975{
976 Destroy();
977
978 wxXmlDocument XML; wxMemoryInputStream Stream((const void *)_XML, Length);
979
980#if wxCHECK_VERSION(3, 3, 0)
981 if( XML.Load(Stream) )
982#else
983 if( XML.Load(Stream, Encoding) )
984#endif
985 {
986 Load(XML.GetRoot());
987
988 return( true );
989 }
990
991 return( false );
992}
993
994//---------------------------------------------------------
996{
997 Destroy();
998
999 wxXmlDocument XML; wxMemoryInputStream Stream((const void *)_XML.c_str(), sizeof(SG_Char) * _XML.Length());
1000
1001 if( XML.Load(Stream) )
1002 {
1003 Load(XML.GetRoot());
1004
1005 return( true );
1006 }
1007
1008 return( false );
1009}
1010
1011//---------------------------------------------------------
1013{
1014 wxXmlDocument XML; wxXmlNode *pRoot = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, Get_Name().c_str());
1015
1016 XML.SetRoot(pRoot);
1017
1018 Save(pRoot);
1019
1020 wxMemoryOutputStream Stream;
1021
1022 if( XML.Save(Stream) )
1023 {
1024 CSG_Array s(sizeof(char), Stream.GetSize());
1025
1026 Stream.CopyTo(s.Get_Array(), s.Get_Size());
1027
1028 _XML = (const char *)s.Get_Array();
1029
1030 return( true );
1031 }
1032
1033 return( false );
1034}
1035
1036
1038// //
1040
1041//---------------------------------------------------------
1042bool CSG_MetaData::Load_HTTP(const CSG_String &Server, const CSG_String &Path, const SG_Char *Username, const SG_Char *Password)
1043{
1044 Destroy();
1045
1046 //-----------------------------------------------------
1047 wxHTTP HTTP;
1048
1049 if( Username && *Username ) { HTTP.SetUser (Username); }
1050 if( Password && *Password ) { HTTP.SetPassword(Password); }
1051
1052 wxString s = Server.c_str();
1053
1054 if( s.Find("http://") == 0 )
1055 {
1056 s = s.Right(s.Length() - wxString("http://").Length());
1057 }
1058
1059 if( !HTTP.Connect(s) )
1060 {
1061 return( false );
1062 }
1063
1064 //-----------------------------------------------------
1065 s = Path.c_str();
1066
1067 if( s[0] != '/' )
1068 {
1069 s.Prepend("/");
1070 }
1071
1072 wxInputStream *pStream = HTTP.GetInputStream(s);
1073
1074 if( !pStream )
1075 {
1076 return( false );
1077 }
1078
1079 wxXmlDocument XML;
1080
1081 if( XML.Load(*pStream) )
1082 {
1083 Load(XML.GetRoot());
1084
1085 delete(pStream);
1086
1087 return( true );
1088 }
1089
1090 delete(pStream);
1091
1092 return( false );
1093}
1094
1095
1097// //
1099
1100//---------------------------------------------------------
1102{
1103 CSG_File Stream; CSG_String JSON;
1104
1105 if( Stream.Open(File, SG_FILE_R, false) && Stream.Read(JSON, (size_t)Stream.Length()) > 0 )
1106 {
1107 return( from_JSON(JSON) );
1108 }
1109
1110 return( false );
1111}
1112
1113//---------------------------------------------------------
1115{
1116 CSG_File Stream; CSG_String JSON;
1117
1118 if( Stream.Open(File, SG_FILE_W, false, SG_FILE_ENCODING_UTF8) && to_JSON(JSON) && Stream.Write(JSON) )
1119 {
1120 return( true );
1121 }
1122
1123 return( false );
1124}
1125
1126//---------------------------------------------------------
1128{
1129 Destroy();
1130 Create();
1131
1132 Set_Name("root");
1133
1134 picojson::value root;
1135 std::string err = picojson::parse(root, JSON.b_str());
1136
1137 if( !err.empty() )
1138 {
1139 return false;
1140 }
1141
1142 _JSON_To_MetaData(root, this);
1143
1144
1145 return( true );
1146}
1147
1148//---------------------------------------------------------
1150{
1151 if( Get_Parent() && !Get_Parent()->Cmp_Property("type", "array") )
1152 {
1153 JSON += "\"";
1154 JSON += _JSON_Escape_String(Get_Name());
1155 JSON += "\": ";
1156 }
1157
1158 if( Cmp_Property("type", "array") )
1159 {
1160 JSON += "[";
1161 for(int i=0; i<Get_Children_Count(); ++i)
1162 {
1163 if( i > 0 )
1164 {
1165 JSON += ",";
1166 }
1167 Get_Child(i)->to_JSON(JSON);
1168 }
1169 JSON += "]";
1170 }
1171 else if( Get_Children_Count() > 0 )
1172 {
1173 JSON += "{";
1174 for(int i=0; i<Get_Children_Count(); ++i)
1175 {
1176 if( i > 0 )
1177 {
1178 JSON += ",";
1179 }
1180 Get_Child(i)->to_JSON(JSON);
1181 }
1182 JSON += "}";
1183 }
1184 else
1185 {
1186 if( Cmp_Property("type","string") )
1187 {
1188 JSON += "\"";
1189 JSON += _JSON_Escape_String(Get_Content());
1190 JSON += "\"";
1191 }
1192 if( Cmp_Property("type","number") )
1193 {
1194 JSON += Get_Content();
1195 }
1196 if( Cmp_Property("type","boolean") )
1197 {
1198 JSON += Get_Content();
1199 }
1200 if( Cmp_Property("type","null") )
1201 {
1202 JSON += "null";
1203 }
1204 }
1205 return( true );
1206}
1207
1208//---------------------------------------------------------
1209void CSG_MetaData::_JSON_To_MetaData(const picojson::value &j, CSG_MetaData *pNode)
1210{
1211 if( j.is<picojson::object>() )
1212 {
1213 const picojson::object &obj = j.get<picojson::object>();
1214
1215 for( auto it = obj.begin(); it != obj.end(); ++it )
1216 {
1217 CSG_MetaData *pChild = pNode->Add_Child(it->first.c_str());
1218 _JSON_To_MetaData(it->second, pChild);
1219 }
1220 }
1221
1222 else if( j.is<picojson::array>() )
1223 {
1224 const picojson::array &arr = j.get<picojson::array>();
1225
1226 pNode->Add_Property("type", "array");
1227
1228 for( size_t i = 0; i < arr.size(); ++i )
1229 {
1230 CSG_MetaData *pItem = pNode->Add_Child("item");
1231 _JSON_To_MetaData(arr[i], pItem);
1232 }
1233 }
1234
1235 else
1236 {
1237 if( j.is<std::string>() )
1238 {
1239 pNode->Set_Content(j.get<std::string>().c_str());
1240 pNode->Add_Property("type", "string");
1241 }
1242 else if( j.is<double>() )
1243 {
1244 pNode->Set_Content(CSG_String::Format("%g", j.get<double>()));
1245 pNode->Add_Property("type", "number");
1246 }
1247
1248 else if( j.is<bool>() )
1249 {
1250 pNode->Set_Content(j.get<bool>() ? "true" : "false");
1251 pNode->Add_Property("type", "boolean");
1252 }
1253 else
1254 {
1255 pNode->Set_Content("null");
1256 pNode->Add_Property("type", "null");
1257 }
1258 }
1259}
1260
1261
1262//---------------------------------------------------------
1263CSG_String CSG_MetaData::_JSON_Escape_String(const CSG_String &s) const
1264{
1265 CSG_String out;
1266
1267 for(size_t i=0; i<s.Length(); ++i)
1268 {
1269 switch( s[i] )
1270 {
1271 case '\"': out += "\\\""; break;
1272 case '\\': out += "\\\\"; break;
1273 case '\b': out += "\\b" ; break;
1274 case '\f': out += "\\f" ; break;
1275 case '\n': out += "\\n" ; break;
1276 case '\r': out += "\\r" ; break;
1277 case '\t': out += "\\t" ; break;
1278 default: out += s[i] ; break;
1279 }
1280 }
1281 return( out );
1282}
1283
1284
1286// //
1287// //
1288// //
1290
1291//---------------------------------------------------------
1293{
1294 m_pHTTP = NULL;
1295}
1296
1298{
1299 return( Destroy() );
1300}
1301
1302//---------------------------------------------------------
1303CSG_HTTP::CSG_HTTP(const CSG_String &Server, const CSG_String &User, const CSG_String &Password)
1304{
1305 m_pHTTP = NULL;
1306
1307 Create(Server, User, Password);
1308}
1309
1310bool CSG_HTTP::Create(const CSG_String &Server, const CSG_String &User, const CSG_String &Password)
1311{
1312 Destroy();
1313
1314 m_pHTTP = new wxHTTP;
1315
1316 if( !User .is_Empty() ) { m_pHTTP->SetUser (User .wx_str()); }
1317 if( !Password.is_Empty() ) { m_pHTTP->SetPassword(Password.wx_str()); }
1318
1319 wxString Host= Server.wx_str(); unsigned short Port = 80;
1320
1321 #define SERVER_TRIM(s, p) { wxString sp(p); sp += "://"; if( s.Find(p) == 0 ) { s = s.Right(s.Length() - sp.Length()); } }
1322
1323 SERVER_TRIM(Host, "https");
1324 SERVER_TRIM(Host, "http" );
1325
1326 if( Host.Find(":") >= 0 )
1327 {
1328 long _Port;
1329
1330 if( Host.AfterLast(':').ToLong(&_Port) )
1331 {
1332 Port = (unsigned short)_Port;
1333 }
1334
1335 Host = Host.BeforeLast(':');
1336 }
1337
1338 if( !m_pHTTP->Connect(Host, Port) )
1339 {
1340 Destroy();
1341
1342 return( false );
1343 }
1344
1345 return( true );
1346}
1347
1348//---------------------------------------------------------
1350{
1351 Destroy();
1352}
1353
1355{
1356 if( m_pHTTP )
1357 {
1358 delete(m_pHTTP);
1359
1360 m_pHTTP = NULL;
1361 }
1362
1363 return( true );
1364}
1365
1366//---------------------------------------------------------
1367wxInputStream * CSG_HTTP::_Request(const CSG_String &Request)
1368{
1369 if( !is_Connected() )
1370 {
1371 wxString s(Request.c_str()); if( s[0] != '/' ) { s.Prepend("/"); }
1372
1373 wxInputStream *pStream = m_pHTTP->GetInputStream(s);
1374
1375 if( pStream )
1376 {
1377 if( pStream->CanRead() )
1378 {
1379 return( pStream );
1380 }
1381
1382 delete(pStream);
1383 }
1384 }
1385
1386 return( NULL );
1387}
1388
1389//---------------------------------------------------------
1391{
1392 wxInputStream *pStream = _Request(Request); if( !pStream ) { return( false ); }
1393
1394 wxXmlDocument XML;
1395
1396 if( !XML.Load(*pStream) )
1397 {
1398 delete(pStream);
1399
1400 return( false );
1401 }
1402
1403 Answer.Destroy(); Answer.Load(XML.GetRoot());
1404
1405 delete(pStream);
1406
1407 return( true );
1408}
1409
1410//---------------------------------------------------------
1412{
1413 wxInputStream *pStream = _Request(Request); if( !pStream ) { return( false ); }
1414
1415 Answer.Clear();
1416
1417 while( pStream->CanRead() )
1418 {
1419 char Byte; pStream->Read(&Byte, sizeof(Byte)); Answer += Byte;
1420 }
1421
1422 delete(pStream);
1423
1424 return( true );
1425}
1426
1427//---------------------------------------------------------
1429{
1430 wxInputStream *pStream = _Request(Request); if( !pStream ) { return( false ); }
1431
1432 Answer.Clear();
1433
1434 while( pStream->CanRead() )
1435 {
1436 char Byte; pStream->Read(&Byte, sizeof(Byte)); Answer += Byte;
1437 }
1438
1439 delete(pStream);
1440
1441 return( true );
1442}
1443
1444//---------------------------------------------------------
1446{
1447 wxInputStream *pStream = _Request(Request); if( !pStream ) { return( false ); }
1448
1449 wxFileOutputStream *pFile = new wxFileOutputStream(File);
1450
1451 if( !pFile )
1452 {
1453 delete(pStream);
1454
1455 return( false );
1456 }
1457
1458 pFile->Write(*pStream);
1459
1460 delete(pFile);
1461
1462 delete(pStream);
1463
1464 return( true );
1465}
1466
1467
1469// //
1471
1472//---------------------------------------------------------
1473#include <wx/protocol/ftp.h>
1474
1475//---------------------------------------------------------
1476bool SG_FTP_Download(const CSG_String &Target_Directory, const CSG_String &Source, const CSG_String &User, const CSG_String &Password, unsigned short Port, bool bBinary, bool bVerbose)
1477{
1478 CSG_String _Source(Source); _Source.Trim();
1479
1480 if( _Source.Find("ftp://") == 0 )
1481 {
1482 _Source = _Source.Right(_Source.Length() - CSG_String("ftp://").Length());
1483 }
1484
1485 CSG_String ftpHost = _Source.BeforeFirst('/');
1486 CSG_String ftpDir = _Source.AfterFirst ('/').BeforeLast('/'); // ftpDir.Prepend("/");
1487 CSG_String ftpFile = _Source.AfterLast ('/');
1488
1489 //-----------------------------------------------------
1490 wxFTP ftp;
1491
1492 if( !User .is_Empty() ) { ftp.SetUser (User .wx_str()); }
1493 if( !Password.is_Empty() ) { ftp.SetPassword(Password.wx_str()); }
1494
1495 if( !ftp.Connect(ftpHost.c_str(), Port) )
1496 {
1497 if( bVerbose )
1498 {
1499 SG_UI_Msg_Add_Error(_TL("Couldn't connect"));
1500 }
1501
1502 return( false );
1503 }
1504
1505 //-----------------------------------------------------
1506 if( !ftpDir.is_Empty() && !ftp.ChDir(ftpDir.c_str()) )
1507 {
1508 if( bVerbose )
1509 {
1510 SG_UI_Msg_Add_Error(CSG_String::Format("%s [%s]", _TL("Couldn't change to directory"), ftpDir.c_str()));
1511 }
1512
1513 return( false );
1514 }
1515
1516 if( ftp.GetFileSize(ftpFile.c_str()) == -1 )
1517 {
1518 if( bVerbose )
1519 {
1520 SG_UI_Msg_Add_Error(CSG_String::Format("%s [%s]", _TL("Couldn't get the file size"), ftpFile.c_str()));
1521 }
1522 }
1523
1524 //-----------------------------------------------------
1525 wxInputStream *pStream = ftp.GetInputStream(ftpFile.c_str());
1526
1527 if( !pStream )
1528 {
1529 if( bVerbose )
1530 {
1531 SG_UI_Msg_Add_Error(CSG_String::Format("%s [%s]", _TL("Couldn't get the file"), ftpFile.c_str()));
1532 }
1533
1534 return( false );
1535 }
1536
1537 //-----------------------------------------------------
1538 wxFileOutputStream *pFile = new wxFileOutputStream(SG_File_Make_Path(Target_Directory, ftpFile).c_str());
1539
1540 if( !pFile )
1541 {
1542 if( bVerbose )
1543 {
1544 SG_UI_Msg_Add_Error(CSG_String::Format("%s [%s]", _TL("Couldn't create target file"), SG_File_Make_Path(Target_Directory, ftpFile).c_str()));
1545 }
1546
1547 delete(pStream);
1548
1549 return( false );
1550 }
1551
1552 //-----------------------------------------------------
1553 pFile->Write(*pStream);
1554
1555 delete(pFile);
1556 delete(pStream);
1557
1558 return( true );
1559}
1560
1561
1563// //
1564// //
1565// //
1567
1568//---------------------------------------------------------
1570{
1571 Create();
1572}
1573
1575{
1576 Destroy();
1577
1578 return( true );
1579}
1580
1581//---------------------------------------------------------
1582CSG_WebRequest::CSG_WebRequest(const CSG_String &Server, const CSG_String &User, const CSG_String &Password)
1583{
1584 Create(Server, User, Password);
1585}
1586
1587bool CSG_WebRequest::Create(const CSG_String &Server, const CSG_String &User, const CSG_String &Password)
1588{
1589 Destroy();
1590
1591 m_Server = Server;
1592
1593 if( !User.is_Empty() || !Password.is_Empty() )
1594 {
1595 #if !wxCHECK_VERSION(3, 3, 0)
1596 m_User = User; m_Password = Password;
1597 #else
1598 wxURI URI; URI.SetUserAndPassword(User.wx_str(), Password.wx_str());
1599
1600 m_User = URI.GetUser().wx_str(); m_Password = URI.GetPassword().wx_str();
1601 #endif
1602 }
1603
1604 return( true );
1605}
1606
1607//---------------------------------------------------------
1609{
1610 m_Server.Clear(); m_User.Clear(), m_Password.Clear();
1611
1612 return( true );
1613}
1614
1615
1617// //
1619
1620//---------------------------------------------------------
1621#if !wxCHECK_VERSION(3, 3, 0)
1622bool CSG_WebRequest::Request( const CSG_String &Request, CSG_Bytes &Answer ) { return( false ); }
1623bool CSG_WebRequest::Request( const CSG_String &Request, CSG_MetaData &Answer ) { return( false ); }
1624bool CSG_WebRequest::Request( const CSG_String &Request, CSG_String &Answer ) { return( false ); }
1625bool CSG_WebRequest::Request( const CSG_String &Request, const SG_Char *File ) { return( false ); }
1626bool CSG_WebRequest::Request(const CSG_String &Server, const CSG_String &Request, CSG_Bytes &Answer ) { return( false ); }
1627bool CSG_WebRequest::Request(const CSG_String &Server, const CSG_String &Request, CSG_MetaData &Answer ) { return( false ); }
1628bool CSG_WebRequest::Request(const CSG_String &Server, const CSG_String &Request, CSG_String &Answer ) { return( false ); }
1629bool CSG_WebRequest::Request(const CSG_String &Server, const CSG_String &Request, const SG_Char *File ) { return( false ); }
1630bool CSG_WebRequest::Post ( const CSG_String &Request, const CSG_String &Body, CSG_Bytes &Answer, const CSG_String &ContentType) { return( false ); }
1631bool CSG_WebRequest::Post ( const CSG_String &Request, const CSG_String &Body, CSG_MetaData &Answer, const CSG_String &ContentType) { return( false ); }
1632bool CSG_WebRequest::Post ( const CSG_String &Request, const CSG_String &Body, CSG_String &Answer, const CSG_String &ContentType) { return( false ); }
1633bool CSG_WebRequest::Post ( const CSG_String &Request, const CSG_String &Body, const SG_Char *File, const CSG_String &ContentType) { return( false ); }
1634bool CSG_WebRequest::Post (const CSG_String &Server, const CSG_String &Request, const CSG_String &Body, CSG_Bytes &Answer, const CSG_String &ContentType) { return( false ); }
1635bool CSG_WebRequest::Post (const CSG_String &Server, const CSG_String &Request, const CSG_String &Body, CSG_MetaData &Answer, const CSG_String &ContentType) { return( false ); }
1636bool CSG_WebRequest::Post (const CSG_String &Server, const CSG_String &Request, const CSG_String &Body, CSG_String &Answer, const CSG_String &ContentType) { return( false ); }
1637bool CSG_WebRequest::Post (const CSG_String &Server, const CSG_String &Request, const CSG_String &Body, const SG_Char *File, const CSG_String &ContentType) { return( false ); }
1638#else
1639
1640
1641//---------------------------------------------------------
1642#include <wx/webrequest.h>
1643
1644//---------------------------------------------------------
1645static wxWebRequestSync _Create_Request(const CSG_String &Server, const CSG_String &User, const CSG_String &Password, const CSG_String &Request)
1646{
1647 if( !Server.is_Empty() && !Request.is_Empty() )
1648 {
1649 wxWebSessionSync::GetDefault().SetBaseURL(Server.wx_str());
1650 }
1651
1652 CSG_String URL = Request.is_Empty() ? Server : Request;
1653
1654 auto request = wxWebSessionSync::GetDefault().CreateRequest(URL.wx_str());
1655
1656 #if wxCHECK_VERSION(3, 3, 2)
1657 if( !User.is_Empty() || !Password.is_Empty() )
1658 {
1659 request.UseBasicAuth(wxWebCredentials(User.wx_str(), wxSecretValue(Password.wx_str())));
1660 }
1661 #endif
1662
1663 return( request );
1664}
1665
1666//---------------------------------------------------------
1667static bool _Response_to_Bytes(const wxWebResponse &Response, CSG_Bytes &Answer)
1668{
1669 wxInputStream *pStream = Response.GetStream();
1670
1671 if( pStream->IsOk() )
1672 {
1673 int Byte = pStream->GetC();
1674
1675 while( Byte != wxEOF )
1676 {
1677 Answer += (BYTE)Byte;
1678
1679 Byte = pStream->GetC();
1680 }
1681
1682 return( true );
1683 }
1684
1685 return( false );
1686}
1687
1688//---------------------------------------------------------
1689static bool _Response_to_MetaData(const wxWebResponse &Response, CSG_MetaData &Answer)
1690{
1691 wxString MimeType = Response.GetMimeType();
1692 CSG_String String = Response.AsString().wx_str();
1693
1694 if( MimeType.Contains("xml") )
1695 {
1696 wxXmlDocument XML;
1697
1698 if( XML.Load(*Response.GetStream()) )
1699 {
1700 Answer.Load(XML.GetRoot());
1701
1702 return( true );
1703 }
1704 }
1705 else if( MimeType.Contains("json") )
1706 {
1707 return( Answer.from_JSON(String) );
1708 }
1709
1710 return( false );
1711}
1712
1713//---------------------------------------------------------
1714static bool _Response_to_String(const wxWebResponse &Response, CSG_String &Answer)
1715{
1716 Answer = Response.AsString().wx_str();
1717
1718 return( true );
1719}
1720
1721//---------------------------------------------------------
1722static bool _Response_to_File(const wxWebResponse &Response, const SG_Char *File)
1723{
1724 wxFileOutputStream Output(File);
1725
1726 if( Output.IsOk() )
1727 {
1728 Output.Write(*Response.GetStream());
1729
1730 return( true );
1731 }
1732
1733 return( false );
1734}
1735
1736
1738// //
1740
1741//---------------------------------------------------------
1742bool CSG_WebRequest::Request(const CSG_String &Request, CSG_Bytes &Answer)
1743{
1744 auto request = _Create_Request(m_Server, m_User, m_Password, Request);
1745 if( !request.Execute() ) { return( false ); }
1746
1747 //-----------------------------------------------------
1748 return( _Response_to_Bytes(request.GetResponse(), Answer) );
1749}
1750
1751//---------------------------------------------------------
1752bool CSG_WebRequest::Request(const CSG_String &Request, CSG_MetaData &Answer)
1753{
1754 auto request = _Create_Request(m_Server, m_User, m_Password, Request);
1755 if( !request.Execute() ) { return( false ); }
1756
1757 //-----------------------------------------------------
1758 return( _Response_to_MetaData(request.GetResponse(), Answer) );
1759}
1760
1761//---------------------------------------------------------
1762bool CSG_WebRequest::Request(const CSG_String &Request, CSG_String &Answer)
1763{
1764 auto request = _Create_Request(m_Server, m_User, m_Password, Request);
1765 if( !request.Execute() ) { return( false ); }
1766
1767 //-----------------------------------------------------
1768 return( _Response_to_String(request.GetResponse(), Answer) );
1769}
1770
1771//---------------------------------------------------------
1772bool CSG_WebRequest::Request(const CSG_String &Request, const SG_Char *File)
1773{
1774 auto request = _Create_Request(m_Server, m_User, m_Password, Request);
1775 if( !request.Execute() ) { return( false ); }
1776
1777 //-----------------------------------------------------
1778 return( _Response_to_File(request.GetResponse(), File) );
1779}
1780
1781
1783// //
1785
1786//---------------------------------------------------------
1787bool CSG_WebRequest::Post(const CSG_String &Request, const CSG_String &Body, CSG_Bytes &Answer, const CSG_String &ContentType )
1788{
1789 auto request = _Create_Request(m_Server, m_User, m_Password, Request);
1790 request.SetMethod("POST");
1791 request.SetData(Body.wx_str(), ContentType.wx_str());
1792 if( !request.Execute() ) { return false; }
1793
1794 //-----------------------------------------------------
1795 return( _Response_to_Bytes(request.GetResponse(), Answer) );
1796}
1797
1798//---------------------------------------------------------
1799bool CSG_WebRequest::Post(const CSG_String &Request, const CSG_String &Body, CSG_MetaData &Answer, const CSG_String &ContentType )
1800{
1801 auto request = _Create_Request(m_Server, m_User, m_Password, Request);
1802 request.SetMethod("POST");
1803 request.SetData(Body.wx_str(), ContentType.wx_str());
1804 if( !request.Execute() ) { return false; }
1805
1806 //-----------------------------------------------------
1807 return( _Response_to_MetaData(request.GetResponse(), Answer) );
1808}
1809
1810//---------------------------------------------------------
1811bool CSG_WebRequest::Post(const CSG_String &Request, const CSG_String &Body, CSG_String &Answer, const CSG_String &ContentType )
1812{
1813 auto request = _Create_Request(m_Server, m_User, m_Password, Request);
1814 request.SetMethod("POST");
1815 request.SetData(Body.wx_str(), ContentType.wx_str());
1816 if( !request.Execute() ) { return false; }
1817
1818 //-----------------------------------------------------
1819 return( _Response_to_String(request.GetResponse(), Answer) );
1820}
1821
1822//---------------------------------------------------------
1823bool CSG_WebRequest::Post(const CSG_String &Request, const CSG_String &Body, const SG_Char *File, const CSG_String &ContentType )
1824{
1825 auto request = _Create_Request(m_Server, m_User, m_Password, Request);
1826 request.SetMethod("POST");
1827 request.SetData(Body.wx_str(), ContentType.wx_str());
1828 if( !request.Execute() ) { return false; }
1829
1830 //-----------------------------------------------------
1831 return( _Response_to_File(request.GetResponse(), File) );
1832}
1833
1834
1836// //
1838
1839//---------------------------------------------------------
1840bool CSG_WebRequest::Request(const CSG_String &Server, const CSG_String &Request, CSG_Bytes &Answer)
1841{
1842 auto request = _Create_Request(Server, "", "", Request);
1843 if( !request.Execute() ) { return( false ); }
1844
1845 //-----------------------------------------------------
1846 return( _Response_to_Bytes(request.GetResponse(), Answer) );
1847}
1848
1849//---------------------------------------------------------
1850bool CSG_WebRequest::Request(const CSG_String &Server, const CSG_String &Request, CSG_MetaData &Answer)
1851{
1852 auto request = _Create_Request(Server, "", "", Request);
1853 if( !request.Execute() ) { return( false ); }
1854
1855 //-----------------------------------------------------
1856 return( _Response_to_MetaData(request.GetResponse(), Answer) );
1857}
1858
1859//---------------------------------------------------------
1860bool CSG_WebRequest::Request(const CSG_String &Server, const CSG_String &Request, CSG_String &Answer)
1861{
1862 auto request = _Create_Request(Server, "", "", Request);
1863 if( !request.Execute() ) { return( false ); }
1864
1865 //-----------------------------------------------------
1866 return( _Response_to_String(request.GetResponse(), Answer) );
1867}
1868
1869
1870//---------------------------------------------------------
1871bool CSG_WebRequest::Request(const CSG_String &Server, const CSG_String &Request, const SG_Char *File)
1872{
1873 auto request = _Create_Request(Server, "", "", Request);
1874 if( !request.Execute() ) { return( false ); }
1875
1876 //-----------------------------------------------------
1877 return( _Response_to_File(request.GetResponse(), File) );
1878}
1879
1880
1882// //
1884
1885//---------------------------------------------------------
1886bool CSG_WebRequest::Post(const CSG_String &Server, const CSG_String &Request, const CSG_String &Body, CSG_Bytes &Answer, const CSG_String &ContentType)
1887{
1888 auto request = _Create_Request(Server, "", "", Request);
1889 request.SetMethod("POST");
1890 request.SetData(Body.wx_str(), ContentType.wx_str());
1891 if( !request.Execute() ) { return false; }
1892
1893 //-----------------------------------------------------
1894 return( _Response_to_Bytes(request.GetResponse(), Answer) );
1895}
1896
1897
1898//---------------------------------------------------------
1899bool CSG_WebRequest::Post(const CSG_String &Server, const CSG_String &Request, const CSG_String &Body, CSG_MetaData &Answer, const CSG_String &ContentType)
1900{
1901 auto request = _Create_Request(Server, "", "", Request);
1902 request.SetMethod("POST");
1903 request.SetData(Body.wx_str(), ContentType.wx_str());
1904 if( !request.Execute() ) { return false; }
1905
1906 //-----------------------------------------------------
1907 return( _Response_to_MetaData(request.GetResponse(), Answer) );
1908}
1909
1910//---------------------------------------------------------
1911bool CSG_WebRequest::Post(const CSG_String &Server, const CSG_String &Request, const CSG_String &Body, CSG_String &Answer, const CSG_String &ContentType)
1912{
1913 auto request = _Create_Request(Server, "", "", Request);
1914 request.SetMethod("POST");
1915 request.SetData(Body.wx_str(), ContentType.wx_str());
1916 if( !request.Execute() ) { return false; }
1917
1918 //-----------------------------------------------------
1919 return( _Response_to_String(request.GetResponse(), Answer) );
1920}
1921
1922//---------------------------------------------------------
1923bool CSG_WebRequest::Post(const CSG_String &Server, const CSG_String &Request, const CSG_String &Body, const SG_Char *File, const CSG_String &ContentType)
1924{
1925 auto request = _Create_Request(Server, "", "", Request);
1926 request.SetMethod("POST");
1927 request.SetData(Body.wx_str(), ContentType.wx_str());
1928 if( !request.Execute() ) { return false; }
1929
1930 //-----------------------------------------------------
1931 return( _Response_to_File(request.GetResponse(), File) );
1932}
1933
1934//---------------------------------------------------------
1935#endif // #if wxCHECK_VERSION(3, 3, 0)
1936
1937
1939// //
1940// //
1941// //
1943
1944//---------------------------------------------------------
void SG_UI_Msg_Add_Error(const char *Message)
SAGA_API_DLL_EXPORT bool SG_File_Exists(const CSG_String &FileName)
SAGA_API_DLL_EXPORT bool SG_File_Cmp_Extension(const CSG_String &File, const CSG_String &Extension)
signed long long sLong
Definition api_core.h:158
#define SG_T(s)
Definition api_core.h:537
SAGA_API_DLL_EXPORT CSG_String SG_File_Make_Path(const CSG_String &Directory, const CSG_String &Name)
@ SG_DATATYPE_String
Definition api_core.h:1055
#define SG_Char
Definition api_core.h:536
#define _TL(s)
Definition api_core.h:1618
@ SG_FILE_ENCODING_UTF8
Definition api_core.h:552
SAGA_API_DLL_EXPORT CSG_String SG_Get_String(double Value, int Precision=-99)
@ SG_FILE_W
Definition api_core.h:1161
@ SG_FILE_R
Definition api_core.h:1160
void * Get_Array(void) const
Definition api_core.h:336
sLong Get_Size(void) const
Definition api_core.h:327
bool Clear(void)
sLong Length(void) const
Definition api_file.cpp:230
virtual bool Open(const SG_Char *FileName, int Mode=SG_FILE_R, bool bBinary=true, int Encoding=SG_FILE_ENCODING_ANSI)
Definition api_file.cpp:113
size_t Write(void *Buffer, size_t Size, size_t Count=1) const
Definition api_file.cpp:382
size_t Read(void *Buffer, size_t Size, size_t Count=1) const
Definition api_file.cpp:338
class wxStreamBase * Get_Stream(void) const
Definition api_core.h:1190
bool is_Writing(void) const
Definition api_core.h:1197
bool is_Reading(void) const
Definition api_core.h:1196
virtual ~CSG_HTTP(void)
bool is_Connected(void) const
Definition metadata.h:254
bool Request(const CSG_String &Request, CSG_Bytes &Answer)
bool Destroy(void)
CSG_HTTP(void)
bool Create(void)
bool Mov_Child(int from_Index, int to_Index)
Definition metadata.cpp:281
void Fmt_Content(const char *Format,...)
Definition metadata.cpp:533
bool to_XML(CSG_String &XML) const
bool Cmp_Name(const CSG_String &String, bool bNoCase=true) const
Definition metadata.cpp:484
bool Save(const CSG_String &File, const SG_Char *Extension=NULL) const
Definition metadata.cpp:904
const CSG_String & Get_Name(void) const
Definition metadata.h:138
bool Create(void)
Definition metadata.cpp:73
bool Assign(const CSG_MetaData &MetaData, bool bAddChildren=true)
Definition metadata.cpp:780
bool Del_Children(int Depth=0, const SG_Char *Name=NULL)
Definition metadata.cpp:381
class CSG_Table Get_Table(int Flags=0) const
Definition metadata.cpp:756
int Get_Children_Count(void) const
Definition metadata.h:154
CSG_MetaData * Get_Child(int Index) const
Definition metadata.h:155
bool Load_JSON(const CSG_String &File)
bool Save_JSON(const CSG_String &File) const
bool Set_Property(const CSG_String &Name, const CSG_String &Value, bool bAddIfNotExists=true)
Definition metadata.cpp:638
bool Cmp_Content(const CSG_String &String, bool bNoCase=false) const
Definition metadata.cpp:571
CSG_MetaData * Get_Child_by_JSON_Pointer(const CSG_String &JSON_Pointer) const
Definition metadata.cpp:442
bool Cmp_Property(const CSG_String &Name, const CSG_String &String, bool bNoCase=false) const
Definition metadata.cpp:694
bool Add_Children(const CSG_MetaData &MetaData)
Definition metadata.cpp:359
CSG_MetaData * Get_Parent(void) const
Definition metadata.h:151
CSG_MetaData(void)
Definition metadata.cpp:68
const CSG_String & Get_Property_Name(int Index) const
Definition metadata.h:187
virtual ~CSG_MetaData(void)
Definition metadata.cpp:134
const CSG_String & Get_Content(void) const
Definition metadata.h:139
bool Load_HTTP(const CSG_String &Server, const CSG_String &Path, const SG_Char *Username=NULL, const SG_Char *Password=NULL)
const SG_Char * Get_Property(int Index) const
Definition metadata.h:188
bool Del_Child(int Index)
Definition metadata.cpp:321
void Set_Name(const CSG_String &Name)
Definition metadata.h:136
CSG_String asText(int Flags=0) const
Definition metadata.cpp:721
void Set_Content(const CSG_String &Content)
Definition metadata.h:146
bool from_JSON(const CSG_String &JSON)
CSG_MetaData * Add_Child(void)
Definition metadata.cpp:166
bool Load(const CSG_String &File, const SG_Char *Extension=NULL)
Definition metadata.cpp:809
void Destroy(void)
Definition metadata.cpp:140
bool Del_Property(const CSG_String &Name)
Definition metadata.cpp:611
bool from_XML(const char *XML, size_t Length, const char *Encoding="UTF-8")
Definition metadata.cpp:974
bool to_JSON(CSG_String &JSON) const
int Get_Property_Count(void) const
Definition metadata.h:186
CSG_MetaData * Ins_Child(int Position)
Definition metadata.cpp:207
bool Add_Property(const CSG_String &Name, const CSG_String &Value)
Definition metadata.cpp:582
size_t Length(void) const
CSG_String AfterFirst(char Character) const
bool asInt(int &Value) const
int CmpNoCase(const CSG_String &String) const
CSG_String AfterLast(char Character) const
int Cmp(const CSG_String &String) const
void Clear(void)
CSG_String BeforeFirst(char Character) const
size_t Replace(const CSG_String &Old, const CSG_String &New, bool bReplaceAll=true)
CSG_String BeforeLast(char Character) const
static CSG_String Format(const char *Format,...)
int Trim(bool fromRight=false)
int Find(char Character, bool fromEnd=false) const
bool asDouble(double &Value) const
CSG_String Right(size_t count) const
const SG_Char * c_str(void) const
bool is_Empty(void) const
const class wxString & wx_str(void) const
Definition api_core.h:595
CSG_String & Prepend(const CSG_String &String)
sLong asLongLong(void) const
Definition api_core.h:684
int Get_Count(void) const
Definition api_core.h:725
bool Set_Value(int Field, const CSG_String &Value)
virtual CSG_Table_Record * Add_Record(CSG_Table_Record *pCopy=NULL)
Definition table.cpp:823
virtual bool Add_Field(const CSG_String &Name, TSG_Data_Type Type, int Position=-1)
Definition table.cpp:479
bool Post(const CSG_String &Request, const CSG_String &Body, CSG_Bytes &Answer, const CSG_String &ContentType="application/json")
bool Destroy(void)
bool Create(void)
bool Request(const CSG_String &Request, CSG_Bytes &Answer)
bool SG_FTP_Download(const CSG_String &Target_Directory, const CSG_String &Source, const CSG_String &User, const CSG_String &Password, unsigned short Port, bool bBinary, bool bVerbose)
#define SERVER_TRIM(s, p)