1
1
#include < boost/filesystem.hpp>
2
+ #include < boost/lexical_cast.hpp>
2
3
#include < sstream>
3
4
#include " meta.h"
4
5
#include " ../log/log.h"
@@ -373,10 +374,27 @@ std::string MetaBase<Mutex> :: serialize(MetaFormat fmt, unsigned flags) const
373
374
else
374
375
return root.toStyledString ();
375
376
}
376
- // else if (fmt == MetaFormat::BSON)
377
- // {
378
-
379
- // }
377
+ else if (fmt == MetaFormat::INI)
378
+ {
379
+ for (auto && e: m_Elements)
380
+ {
381
+ std::shared_ptr<Meta> m;
382
+ try {
383
+ m = boost::any_cast<std::shared_ptr<Meta>>(e.value );
384
+ }catch (boost::bad_any_cast&){}
385
+ if (m)
386
+ {
387
+ data += " [" +e.key +" ]\n " ;
388
+ for (auto && j: *m)
389
+ data += j.key + " =" + kit::any_to_string (j.value ) + " \n " ;
390
+ data += " \n " ;
391
+ }
392
+ else
393
+ {
394
+ data += e.key + " =" + kit::any_to_string (e.value ) + " \n " ;
395
+ }
396
+ }
397
+ }
380
398
// else if (fmt == MetaFormat::HUMAN)
381
399
// assert(false);
382
400
else
@@ -414,7 +432,7 @@ void MetaBase<Mutex> :: deserialize(
414
432
415
433
template <class Mutex >
416
434
void MetaBase<Mutex> :: deserialize(MetaFormat fmt, const std::string& data, const std::string& fn, const std::string& pth, unsigned flags)
417
- {
435
+ {
418
436
std::istringstream iss (data);
419
437
deserialize (fmt, iss, fn, pth, flags);
420
438
}
@@ -493,10 +511,51 @@ void MetaBase<Mutex> :: deserialize(MetaFormat fmt, std::istream& data, const st
493
511
// human deserialization unsupported
494
512
assert (false );
495
513
}
496
- // else if (fmt == MetaFormat::BSON)
497
- // {
498
-
499
- // }
514
+ else if (fmt == MetaFormat::INI)
515
+ {
516
+ try {
517
+ MetaBase<Mutex>* m = this ;
518
+ std::string line;
519
+ while (std::getline (data, line))
520
+ {
521
+ if (boost::starts_with (line, " [" ))
522
+ {
523
+ auto m2 = std::make_shared<MetaBase<Mutex>>();
524
+ set (line.substr (1 , line.length ()-2 ), m2);
525
+ m = m2.get ();
526
+ }
527
+ else if (not boost::starts_with (line," ;" ) && line.length () > 1 )
528
+ {
529
+ std::vector<std::string> toks;
530
+ boost::split (toks, line, boost::is_any_of (" =" ));
531
+ auto k = toks.at (0 );
532
+ auto v = toks.at (1 );
533
+ if (v.find (" ." ) != std::string::npos){
534
+ try {
535
+ auto r = boost::lexical_cast<double >(v);
536
+ m->set <double >(k,r);
537
+ continue ;
538
+ }catch (...){}
539
+ }
540
+ try {
541
+ auto r = boost::lexical_cast<int >(v);
542
+ m->set <int >(k,r);
543
+ continue ;
544
+ }catch (...){ }
545
+ if (v==" false" || v==" true" )
546
+ m->set <bool >(k,v==" true" );
547
+ else
548
+ m->set <std::string>(k,v);
549
+ }
550
+ }
551
+ }catch (...){
552
+ if (fn.empty ()) {
553
+ K_ERROR (PARSE, " MetaBase input stream data" )
554
+ } else {
555
+ K_ERROR (PARSE, fn);
556
+ }
557
+ }
558
+ }
500
559
else
501
560
{
502
561
WARNING (" Unknown serialization format" );
@@ -521,7 +580,8 @@ void MetaBase<Mutex> :: deserialize(const std::string& fn, unsigned flags)
521
580
std::fstream file (fn, std::fstream::out); // create
522
581
if (not file.good ())
523
582
K_ERROR (READ, fn);
524
- file << " {}\n " ;
583
+ if (boost::ends_with (boost::to_lower_copy (fn), " .json" ))
584
+ file << " {}\n " ;
525
585
file.flush ();
526
586
}
527
587
}
@@ -829,8 +889,10 @@ template<class Mutex>
829
889
/* static*/ MetaFormat MetaBase<Mutex> :: filename_to_format(const std::string& fn)
830
890
{
831
891
boost::filesystem::path p (fn);
832
- return p.extension ().string () == " .json" ?
833
- MetaFormat::JSON:
834
- MetaFormat::UNKNOWN;
892
+ if (p.extension ().string () == " .json" )
893
+ return MetaFormat::JSON;
894
+ else if (p.extension ().string () == " .ini" )
895
+ return MetaFormat::INI;
896
+ return MetaFormat::UNKNOWN;
835
897
}
836
898
0 commit comments