• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

polserver / polserver / 20692831969

04 Jan 2026 12:22PM UTC coverage: 60.404% (+0.2%) from 60.252%
20692831969

push

github

web-flow
Integer overflow when using GetConfigInt (#844)

* impptr_if now accepts also const BObjectImp*

* cfg::GetConfigInt returns error if the value would produce an overflow
code cleanup of cfgmod
added tests

* increase test coverage

* added comment why the exception should be catched without reaction
fixed member_id test

* added corechanges
its 2026
removed else

42 of 75 new or added lines in 2 files covered. (56.0%)

1 existing line in 1 file now uncovered.

44356 of 73432 relevant lines covered (60.4%)

498208.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.01
/pol-core/pol/module/cfgmod.cpp
1
/** @file
2
 *
3
 * @par History
4
 * - 2005/07/04 Shinigami: mf_AppendConfigFileElem will reload file
5
 * - 2006/09/26 Shinigami: GCC 3.4.x fix - added "template<>" to TmplExecutorModule
6
 */
7

8

9
#include "cfgmod.h"
10
#include <ctype.h>
11
#include <fstream>
12
#include <string>
13
#include <utility>
14

15
#include "../../bscript/berror.h"
16
#include "../../bscript/bobject.h"
17
#include "../../bscript/bstruct.h"
18
#include "../../bscript/contiter.h"
19
#include "../../bscript/dict.h"
20
#include "../../bscript/execmodl.h"
21
#include "../../bscript/executor.h"
22
#include "../../bscript/impstr.h"
23
#include "../../bscript/objmembers.h"
24
#include "../../clib/rawtypes.h"
25
#include "../../clib/refptr.h"
26
#include "../../clib/strutil.h"
27
#include "../../plib/pkg.h"
28
#include "../cfgrepos.h"
29

30
#include <module_defs/cfgfile.h>
31

32
namespace Pol
33
{
34
namespace Module
35
{
36
}
37
namespace Module
38
{
39
Bscript::BApplicObjType cfgfile_type;
40
Bscript::BApplicObjType cfgelem_type;
41

42

43
EConfigFileRefObjImp::EConfigFileRefObjImp( ref_ptr<Core::StoredConfigFile> rcfile )
36✔
44
    : EConfigFileRefObjImpBase( &cfgfile_type, rcfile )
36✔
45
{
46
}
36✔
47

48
class ConfigFileIterator final : public Bscript::ContIterator
49
{
50
public:
51
  ConfigFileIterator( EConfigFileRefObjImp* node, Bscript::BObject* pIter );
52
  Bscript::BObject* step() override;
53

54
private:
55
  Bscript::BObject m_ConfigObj;
56
  EConfigFileRefObjImp* node;
57
  Bscript::BObjectRef m_IterVal;
58
  Core::StoredConfigFile::ElementsByName::const_iterator name_itr;
59
};
60

61
ConfigFileIterator::ConfigFileIterator( EConfigFileRefObjImp* node, Bscript::BObject* pIter )
6✔
62
    : ContIterator(),
63
      m_ConfigObj( node ),
6✔
64
      node( node ),
6✔
65
      m_IterVal( pIter ),
6✔
66
      name_itr( node->obj_->elements_byname_.begin() )
12✔
67
{
68
}
6✔
69

70
Bscript::BObject* ConfigFileIterator::step()
36✔
71
{
72
  if ( name_itr == node->obj_->byname_end() )
36✔
73
    return nullptr;
6✔
74

75
  m_IterVal->setimp( new Bscript::String( name_itr->first ) );
30✔
76
  auto res = std::make_unique<Bscript::BObject>( new EConfigElemRefObjImp( name_itr->second ) );
30✔
77
  ++name_itr;
30✔
78
  return res.release();
30✔
79
}
30✔
80

81
Bscript::ContIterator* EConfigFileRefObjImp::createIterator( Bscript::BObject* pIterVal )
6✔
82
{
83
  return new ConfigFileIterator( this, pIterVal );
6✔
84
}
85

86
Bscript::BObjectRef EConfigFileRefObjImp::OperSubscript( const Bscript::BObject& obj )
63✔
87
{
88
  ref_ptr<Core::StoredConfigElem> celem;
63✔
89
  if ( const auto* str = obj.impptr_if<const Bscript::String>() )
63✔
90
  {
91
    celem = obj_->findelem( str->value() );
33✔
92
  }
93
  else if ( const auto* l = obj.impptr_if<const Bscript::BLong>() )
30✔
94
  {
95
    celem = obj_->findelem( l->value() );
27✔
96
  }
97

98
  if ( celem.get() != nullptr )
63✔
99
  {
100
    return Bscript::BObjectRef( new EConfigElemRefObjImp( celem ) );
120✔
101
  }
102
  return Bscript::BObjectRef( new Bscript::BError( "Element not found" ) );
3✔
103
}
63✔
104

105
const char* EConfigFileRefObjImp::typeOf() const
12✔
106
{
107
  return "ConfigFileRef";
12✔
108
}
109
u8 EConfigFileRefObjImp::typeOfInt() const
3✔
110
{
111
  return OTConfigFileRef;
3✔
112
}
113
Bscript::BObjectImp* EConfigFileRefObjImp::copy() const
9✔
114
{
115
  return new EConfigFileRefObjImp( obj_ );
9✔
116
}
117

118
EConfigElemRefObjImp::EConfigElemRefObjImp( ref_ptr<Core::StoredConfigElem> rcelem )
108✔
119
    : EConfigElemRefObjImpBase( &cfgelem_type, rcelem )
108✔
120
{
121
}
108✔
122

123
const char* EConfigElemRefObjImp::typeOf() const
21✔
124
{
125
  return "ConfigElemRef";
21✔
126
}
127
u8 EConfigElemRefObjImp::typeOfInt() const
3✔
128
{
129
  return OTConfigElemRef;
3✔
130
}
131
Bscript::BObjectImp* EConfigElemRefObjImp::copy() const
×
132
{
133
  return new EConfigElemRefObjImp( obj_ );
×
134
}
135

136
Bscript::BObjectRef EConfigElemRefObjImp::get_member_id( const int id )  // id test
3✔
137
{
138
  Bscript::ObjMember* memb = Bscript::getObjMember( id );
3✔
139

140
  return get_member( memb->code );
3✔
141
}
142
Bscript::BObjectRef EConfigElemRefObjImp::get_member( const char* membername )
51✔
143
{
144
  Bscript::BObjectImp* imp = obj_->getimp( membername );
51✔
145
  if ( imp != nullptr )
51✔
146
    return Bscript::BObjectRef( imp );
45✔
147

148
  return Bscript::BObjectRef( new Bscript::UninitObject );
6✔
149
}
150

151
Bscript::BObjectRef EConfigElemRefObjImp::OperSubscript( const Bscript::BObject& obj )
33✔
152
{
153
  if ( const auto* str = obj.impptr_if<const Bscript::String>() )
33✔
154
  {
155
    return get_member( str->data() );
33✔
156
  }
NEW
157
  if ( const auto* l = obj.impptr_if<const Bscript::BLong>() )
×
158
  {
NEW
159
    return get_member( std::to_string( l->value() ).c_str() );
×
160
  }
161
  return Bscript::BObjectRef( new Bscript::BError( "Element not found" ) );
×
162
}
163

164
ConfigFileExecutorModule::ConfigFileExecutorModule( Bscript::Executor& exec )
2,354✔
165
    : TmplExecutorModule<ConfigFileExecutorModule, Bscript::ExecutorModule>( exec )
2,354✔
166
{
167
}
2,354✔
168

169
bool ConfigFileExecutorModule::get_cfgfilename( const std::string& cfgdesc, std::string* cfgfile,
45✔
170
                                                std::string* errmsg, std::string* allpkgbase )
171
{
172
  if ( allpkgbase )
45✔
173
    *allpkgbase = "";
39✔
174
  const Plib::Package* pkg = exec.prog()->pkg;
45✔
175

176
  if ( cfgdesc[0] == ':' )
45✔
177
  {
178
    if ( cfgdesc[1] == ':' )
45✔
179
    {
180
      // Austin 2005-11-15
181
      // This is a bit of a kludge to make /regions/ config files readable.
182
      // Will go away when the paths here work like they do for file.em.
183
      // Looks for ::regions/filename
184
      if ( cfgdesc.substr( 2, 8 ) == "regions/" )
×
185
      {
186
        *cfgfile = cfgdesc.substr( 2, std::string::npos ) + ".cfg";
×
187
        return true;
×
188
      }
189
      // "::cfgfile" - core config file
NEW
190
      *cfgfile = "config/" + cfgdesc.substr( 2, std::string::npos ) + ".cfg";
×
NEW
191
      return true;
×
192
    }
193
    else  // ":pkgname:configfile" - config file in some package
194
    {
195
      std::string::size_type second_colon = cfgdesc.find( ':', 2 );
45✔
196
      if ( second_colon != std::string::npos )
45✔
197
      {
198
        std::string pkgname = cfgdesc.substr( 1, second_colon - 1 );
45✔
199
        std::string cfgbase = cfgdesc.substr( second_colon + 1, std::string::npos );
45✔
200

201
        if ( pkgname == "*" )
45✔
202
        {
203
          if ( allpkgbase )
×
204
          {
205
            *cfgfile = cfgdesc;
×
206
            *allpkgbase = cfgbase;
×
207
            return true;
×
208
          }
NEW
209
          return false;
×
210
        }
211

212
        Plib::Package* dstpkg = Plib::find_package( pkgname );
45✔
213
        if ( dstpkg != nullptr )
45✔
214
        {
215
          *cfgfile = GetPackageCfgPath( dstpkg, cfgbase + ".cfg" );
45✔
216
          return true;
45✔
217
        }
NEW
218
        *errmsg = "Unable to find package " + pkgname;
×
UNCOV
219
        return false;
×
220
      }
45✔
NEW
221
      *errmsg = "Poorly formed config file descriptor: " + cfgdesc;
×
NEW
222
      return false;
×
223
    }
224
  }
NEW
225
  if ( pkg != nullptr )
×
226
  {
NEW
227
    *cfgfile = GetPackageCfgPath( const_cast<Plib::Package*>( pkg ), cfgdesc + ".cfg" );
×
NEW
228
    return true;
×
229
  }
NEW
230
  *cfgfile = "config/" + cfgdesc + ".cfg";
×
NEW
231
  return true;
×
232
}
233

234

235
Bscript::BObjectImp* ConfigFileExecutorModule::mf_ReadConfigFile()
42✔
236
{
237
  const Bscript::String* cfgdesc_str;
238
  if ( exec.getStringParam( 0, cfgdesc_str ) )
42✔
239
  {
240
    const std::string& cfgdesc = cfgdesc_str->value();
39✔
241
    std::string cfgfile;
39✔
242
    std::string errmsg;
39✔
243
    std::string allpkgbase;
39✔
244
    if ( !get_cfgfilename( cfgdesc, &cfgfile, &errmsg, &allpkgbase ) )
39✔
245
    {
246
      return new Bscript::BError( errmsg );
×
247
    }
248

249
    ref_ptr<Core::StoredConfigFile> cfile = Core::FindConfigFile( cfgfile, allpkgbase );
39✔
250

251
    if ( cfile.get() != nullptr )
39✔
252
    {
253
      return new EConfigFileRefObjImp( cfile );
27✔
254
    }
255
    return new Bscript::BError( "Config file not found" );
12✔
256
  }
39✔
257
  return new Bscript::BError( "Invalid parameter type" );
3✔
258
}
259

260
bool legal_scp_filename( const char* filename )
×
261
{
262
  while ( filename && *filename )
×
263
  {
264
    if ( !isalnum( *filename ) )
×
265
      return false;
×
266
    ++filename;
×
267
  }
268
  return true;
×
269
}
270

271
Bscript::BObjectImp* ConfigFileExecutorModule::mf_LoadTusScpFile()
×
272
{
273
  const Bscript::String* filename_str;
274
  if ( !exec.getStringParam( 0, filename_str ) )
×
275
  {
276
    return new Bscript::BError( "Invalid parameter type" );
×
277
  }
278

279
  if ( !legal_scp_filename( filename_str->data() ) )
×
280
  {
281
    return new Bscript::BError( "Filename cannot include path information or special characters" );
×
282
  }
283

284
  ref_ptr<Core::StoredConfigFile> cfile =
285
      Core::LoadTusScpFile( "import/tus/" + filename_str->value() + ".scp" );
×
286

287
  if ( cfile.get() == nullptr )
×
288
  {
289
    return new Bscript::BError( "File not found" );
×
290
  }
291

292
  return new EConfigFileRefObjImp( cfile );
×
293
}
×
294

295
Bscript::BObjectImp* ConfigFileExecutorModule::mf_GetConfigMaxIntKey()
3✔
296
{
297
  Core::StoredConfigFile* cfile;
298

299
  if ( getStoredConfigFileParam( *this, 0, cfile ) )
3✔
300
  {
301
    return new Bscript::BLong( cfile->maxintkey() );
3✔
302
  }
NEW
303
  return new Bscript::BError( "Parameter 0 must be a Config File" );
×
304
}
305

306
Bscript::BObjectImp* ConfigFileExecutorModule::mf_GetConfigStringKeys()
9✔
307
{
308
  Core::StoredConfigFile* cfile;
309
  if ( getStoredConfigFileParam( *this, 0, cfile ) )
9✔
310
  {
311
    std::unique_ptr<Bscript::ObjArray> arr( new Bscript::ObjArray );
9✔
312
    Core::StoredConfigFile::ElementsByName::const_iterator itr = cfile->byname_begin(),
9✔
313
                                                           end = cfile->byname_end();
9✔
314
    for ( ; itr != end; ++itr )
24✔
315
    {
316
      arr->addElement( new Bscript::String( itr->first ) );
15✔
317
    }
318
    return arr.release();
9✔
319
  }
9✔
NEW
320
  return new Bscript::BError( "GetConfigStringKeys param 0 must be a Config File" );
×
321
}
322

323
Bscript::BObjectImp* ConfigFileExecutorModule::mf_GetConfigIntKeys()
3✔
324
{
325
  Core::StoredConfigFile* cfile;
326
  if ( getStoredConfigFileParam( *this, 0, cfile ) )
3✔
327
  {
328
    std::unique_ptr<Bscript::ObjArray> arr( new Bscript::ObjArray );
3✔
329
    Core::StoredConfigFile::ElementsByNum::const_iterator itr = cfile->bynum_begin(),
3✔
330
                                                          end = cfile->bynum_end();
3✔
331
    for ( ; itr != end; ++itr )
9✔
332
    {
333
      arr->addElement( new Bscript::BLong( itr->first ) );
6✔
334
    }
335
    return arr.release();
3✔
336
  }
3✔
NEW
337
  return new Bscript::BError( "GetConfigIntKeys param 0 must be a Config File" );
×
338
}
339

340
Bscript::BObjectImp* ConfigFileExecutorModule::mf_FindConfigElem()
18✔
341
{
342
  Core::StoredConfigFile* cfile;
343

344
  if ( getStoredConfigFileParam( *this, 0, cfile ) )
18✔
345
  {
346
    Bscript::BObjectImp* keyimp = exec.getParamImp( 1 );
18✔
347

348
    ref_ptr<Core::StoredConfigElem> celem;
18✔
349

350
    if ( auto* l = impptrIf<Bscript::BLong>( keyimp ) )
18✔
351
    {
352
      celem = cfile->findelem( l->value() );
3✔
353
    }
354
    else if ( auto* str = impptrIf<Bscript::String>( keyimp ) )
15✔
355
    {
356
      celem = cfile->findelem( str->value() );
15✔
357
    }
358
    else
359
    {
360
      return new Bscript::BError( "Param 1 must be an Integer or a String" );
×
361
    }
362

363
    if ( celem.get() != nullptr )
18✔
364
    {
365
      return new EConfigElemRefObjImp( celem );
18✔
366
    }
NEW
367
    return new Bscript::BError( "Element not found" );
×
368
  }
18✔
NEW
369
  return new Bscript::BError( "Parameter 0 must be a Config File" );
×
370
}
371

372
Bscript::BObjectImp* ConfigFileExecutorModule::mf_GetElemProperty()
×
373
{
374
  return mf_GetConfigString();
×
375
}
376

377
Bscript::BObjectImp* ConfigFileExecutorModule::mf_GetConfigString()
93✔
378
{
379
  Core::StoredConfigElem* celem;
380
  const Bscript::String* propname_str;
381

382
  if ( getStoredConfigElemParam( *this, 0, celem ) && getStringParam( 1, propname_str ) )
93✔
383
  {
384
    Bscript::BObjectImp* imp = celem->getimp( propname_str->value() );
93✔
385
    if ( imp != nullptr )
93✔
386
    {
387
      return new Bscript::String( imp->getStringRep() );
93✔
388
    }
NEW
389
    return new Bscript::BError( "Property not found" );
×
390
  }
NEW
391
  return new Bscript::BError( "Invalid parameter type" );
×
392
}
393

394
Bscript::BObjectImp* ConfigFileExecutorModule::mf_GetConfigStringArray()
3✔
395
{
396
  Core::StoredConfigElem* celem;
397
  const Bscript::String* propname_str;
398

399
  if ( getStoredConfigElemParam( *this, 0, celem ) && getStringParam( 1, propname_str ) )
3✔
400
  {
401
    std::pair<Core::StoredConfigElem::const_iterator, Core::StoredConfigElem::const_iterator> pr =
402
        celem->equal_range( propname_str->data() );
3✔
403
    Core::StoredConfigElem::const_iterator itr = pr.first;
3✔
404
    Core::StoredConfigElem::const_iterator end = pr.second;
3✔
405

406
    std::unique_ptr<Bscript::ObjArray> ar( new Bscript::ObjArray );
3✔
407
    for ( ; itr != end; ++itr )
12✔
408
    {
409
      Bscript::BObjectImp* imp = ( *itr ).second.get();
9✔
410
      // Added 9-03-2005  Austin
411
      // Will no longer place the string right into the array.
412
      // Instead a check is done to make sure something is there.
413
      if ( imp->getStringRep().length() >= 1 )
9✔
414
        ar->addElement( new Bscript::String( imp->getStringRep() ) );
6✔
415
    }
416
    return ar.release();
3✔
417
  }
3✔
NEW
418
  return new Bscript::BError( "Invalid parameter type" );
×
419
}
420

421
Bscript::BObjectImp* ConfigFileExecutorModule::mf_GetConfigStringDictionary()
3✔
422
{
423
  Core::StoredConfigElem* celem;
424
  const Bscript::String* propname_str;
425

426
  if ( getStoredConfigElemParam( *this, 0, celem ) && getStringParam( 1, propname_str ) )
3✔
427
  {
428
    std::pair<Core::StoredConfigElem::const_iterator, Core::StoredConfigElem::const_iterator> pr =
429
        celem->equal_range( propname_str->data() );
3✔
430
    Core::StoredConfigElem::const_iterator itr = pr.first;
3✔
431
    Core::StoredConfigElem::const_iterator end = pr.second;
3✔
432

433
    std::unique_ptr<Bscript::BDictionary> dict( new Bscript::BDictionary );
3✔
434
    for ( ; itr != end; ++itr )
9✔
435
    {
436
      Bscript::BObjectImp* line = ( *itr ).second.get();
6✔
437

438
      std::string line_str = line->getStringRep();
6✔
439
      if ( line_str.length() < 1 )
6✔
440
        continue;
×
441

442
      /* Example:
443
       * Elem
444
       * {
445
       *     PropName data moredata more data
446
       *     PropName stuff morestuff stuffity stuff
447
       * }
448
       * dict "data"->"moredata more data", "stuff"->"morestuff stuffity stuff!"
449
       */
450
      std::string prop_name, prop_value;
6✔
451
      Clib::splitnamevalue( line_str, prop_name, prop_value );
6✔
452

453
      dict->addMember( new Bscript::String( prop_name ), new Bscript::String( prop_value ) );
6✔
454
    }
6✔
455

456
    return dict.release();
3✔
457
  }
3✔
NEW
458
  return new Bscript::BError( "Invalid parameter type" );
×
459
}
460

461

462
Bscript::BObjectImp* ConfigFileExecutorModule::mf_GetConfigInt()
9✔
463
{
464
  Core::StoredConfigElem* celem;
465
  const Bscript::String* propname_str;
466

467
  if ( getStoredConfigElemParam( *this, 0, celem ) && getStringParam( 1, propname_str ) )
9✔
468
  {
469
    Bscript::BObjectImp* imp = celem->getimp( propname_str->value() );
9✔
470
    if ( imp != nullptr )
9✔
471
    {
472
      if ( imp->isa( Bscript::BObjectImp::OTLong ) )
9✔
473
      {
474
        return imp;
3✔
475
      }
476
      if ( auto* dbl = impptrIf<Bscript::Double>( imp ) )
6✔
477
      {
478
        return new Bscript::BLong( static_cast<int>( dbl->value() ) );
3✔
479
      }
480
      if ( auto* str = impptrIf<Bscript::String>( imp ) )
3✔
481
      {
482
        try
483
        {
484
          return new Bscript::BLong( std::stoi( str->value(), nullptr, 0 ) );
3✔
485
        }
486
        catch ( const std::logic_error& )
3✔
487
        {
488
          return new Bscript::BError( "Integer overflow error" );
3✔
489
        }
3✔
490
      }
NEW
491
      return new Bscript::BError( "Invalid type in config file! (internal error)" );
×
492
    }
NEW
493
    return new Bscript::BError( "Property not found" );
×
494
  }
NEW
495
  return new Bscript::BError( "Invalid parameter type" );
×
496
}
497

498
Bscript::BObjectImp* ConfigFileExecutorModule::mf_GetConfigIntArray()
9✔
499
{
500
  Core::StoredConfigElem* celem;
501
  const Bscript::String* propname_str;
502

503
  if ( getStoredConfigElemParam( *this, 0, celem ) && getStringParam( 1, propname_str ) )
9✔
504
  {
505
    auto pr = celem->equal_range( propname_str->data() );
9✔
506
    Core::StoredConfigElem::const_iterator itr = pr.first;
9✔
507
    Core::StoredConfigElem::const_iterator end = pr.second;
9✔
508

509
    std::unique_ptr<Bscript::ObjArray> ar( new Bscript::ObjArray );
9✔
510
    for ( ; itr != end; ++itr )
21✔
511
    {
512
      Bscript::BObjectImp* imp = itr->second.get();
12✔
513
      // Will no longer place the string right into the array.
514
      // Instead a check is done to make sure something is there.
515

516
      if ( auto* l = impptrIf<Bscript::BLong>( imp ) )
12✔
517
      {
518
        ar->addElement( l );
6✔
519
      }
520
      else if ( auto* d = impptrIf<Bscript::Double>( imp ) )
6✔
521
      {
522
        ar->addElement( new Bscript::BLong( static_cast<int>( d->value() ) ) );
3✔
523
      }
524
      else if ( auto* str = impptrIf<Bscript::String>( imp ) )
3✔
525
      {
526
        if ( str->length() >= 1 )
3✔
527
        {
528
          try
529
          {
530
            ar->addElement( new Bscript::BLong( std::stoi( str->value(), nullptr, 0 ) ) );
3✔
531
          }
532
          catch ( const std::logic_error& )
3✔
533
          {
534
            // overflow error: skip element
535
          }
3✔
536
        }
537
      }
538
    }
539
    return ar.release();
9✔
540
  }
9✔
NEW
541
  return new Bscript::BError( "Invalid parameter type" );
×
542
}
543

544
Bscript::BObjectImp* ConfigFileExecutorModule::mf_GetConfigReal()
9✔
545
{
546
  Core::StoredConfigElem* celem;
547
  const Bscript::String* propname_str;
548

549
  if ( getStoredConfigElemParam( *this, 0, celem ) && getStringParam( 1, propname_str ) )
9✔
550
  {
551
    Bscript::BObjectImp* imp = celem->getimp( propname_str->value() );
9✔
552
    if ( imp != nullptr )
9✔
553
    {
554
      if ( imp->isa( Bscript::BObjectImp::OTDouble ) )
9✔
555
      {
556
        return imp;
3✔
557
      }
558
      if ( auto* l = impptrIf<Bscript::BLong>( imp ) )
6✔
559
      {
560
        return new Bscript::Double( l->value() );
3✔
561
      }
562
      if ( auto* str = impptrIf<Bscript::String>( imp ) )
3✔
563
      {
564
        return new Bscript::Double( strtod( str->data(), nullptr ) );
3✔
565
      }
NEW
566
      return new Bscript::BError( "Invalid type in config file! (internal error)" );
×
567
    }
NEW
568
    return new Bscript::BError( "Property not found" );
×
569
  }
NEW
570
  return new Bscript::BError( "Invalid parameter type" );
×
571
}
572

573
Bscript::BObjectImp* ConfigFileExecutorModule::mf_ListConfigElemProps()
45✔
574
{
575
  Core::StoredConfigElem* celem;
576
  if ( getStoredConfigElemParam( *this, 0, celem ) )
45✔
577
  {
578
    // should return an array or prop-names
579
    return celem->listprops();
45✔
580
  }
NEW
581
  return new Bscript::BError( "Invalid parameter type" );
×
582
}
583

584
/* The elements in the array passed should each be a structure (name, value) */
585
Bscript::BObjectImp* ConfigFileExecutorModule::mf_AppendConfigFileElem()
6✔
586
{
587
  const Bscript::String* filename;
588
  const Bscript::String* elemtype;
589
  Bscript::ObjArray* objarr;
590
  if ( !exec.getStringParam( 0, filename ) || !exec.getStringParam( 1, elemtype ) ||
12✔
591
       !exec.getObjArrayParam( 3, objarr ) )
6✔
592
  {
593
    return new Bscript::BError( "Invalid parameter type" );
×
594
  }
595
  std::string elemkey = getParamImp( 2 )->getStringRep();
6✔
596

597
  std::string pathname, errmsg;
6✔
598
  const std::string& cfgdesc = filename->value();
6✔
599
  if ( !get_cfgfilename( cfgdesc, &pathname, &errmsg ) )
6✔
600
  {
601
    return new Bscript::BError( errmsg );
×
602
  }
603

604
  std::ofstream ofs( pathname.c_str(), std::ios::app );
6✔
605
  if ( !ofs.is_open() )
6✔
606
    return new Bscript::BError( "Failed to open file" );
3✔
607
  ofs << std::endl << elemtype->value() << " " << elemkey << std::endl << "{" << std::endl;
3✔
608
  for ( Bscript::ObjArray::const_iterator itr = objarr->ref_arr.begin(),
3✔
609
                                          end = objarr->ref_arr.end();
3✔
610
        itr != end; ++itr )
9✔
611
  {
612
    Bscript::BObject* bo = itr->get();
6✔
613
    if ( bo != nullptr )
6✔
614
    {
615
      if ( auto* inarr = bo->impptr_if<Bscript::ObjArray>() )
6✔
616
      {
617
        if ( inarr->ref_arr.size() == 2 )
3✔
618
        {
619
          Bscript::BObject* nobj = inarr->ref_arr[0].get();
3✔
620
          Bscript::BObject* vobj = inarr->ref_arr[1].get();
3✔
621
          if ( nobj != nullptr && nobj->isa( Bscript::BObjectImp::OTString ) && vobj != nullptr )
3✔
622
          {
623
            Bscript::String* namestr = nobj->impptr<Bscript::String>();
3✔
624
            std::string value = vobj->impptr()->getStringRep();
3✔
625

626
            ofs << "\t" << namestr->value() << "\t" << value << std::endl;
3✔
627
          }
3✔
628
        }
629
      }
630
      else if ( auto* instruct = bo->impptr_if<Bscript::BStruct>() )
3✔
631
      {
632
        const Bscript::BObjectImp* name_imp = instruct->FindMember( "name" );
3✔
633
        const Bscript::BObjectImp* value_imp = instruct->FindMember( "value" );
3✔
634
        if ( const auto* namestr = impptrIf<const Bscript::String>( name_imp );
3✔
635
             namestr && value_imp )
3✔
636
        {
637
          std::string value = value_imp->getStringRep();
3✔
638

639
          ofs << "\t" << namestr->value() << "\t" << value << std::endl;
3✔
640
        }
3✔
641
      }
642
    }
643
  }
644
  ofs << "}" << std::endl;
3✔
645

646
  Core::UnloadConfigFile( pathname );
3✔
647

648
  return new Bscript::BLong( 1 );
3✔
649
}
6✔
650

651
Bscript::BObjectImp* ConfigFileExecutorModule::mf_UnloadConfigFile()
×
652
{
653
  const Bscript::String* filename;
654
  if ( getStringParam( 0, filename ) )
×
655
  {
656
    const std::string& cfgdesc = filename->value();
×
657
    std::string cfgfile;
×
658
    std::string errmsg;
×
659
    std::string allpkgbase;
×
660
    if ( !get_cfgfilename( cfgdesc, &cfgfile, &errmsg, &allpkgbase ) )
×
661
    {
662
      return new Bscript::BError( errmsg );
×
663
    }
664

665
    return new Bscript::BLong( Core::UnloadConfigFile( cfgfile ) );
×
666
  }
×
NEW
667
  return new Bscript::BError( "Invalid parameter" );
×
668
}
669

670
bool getStoredConfigFileParam( Bscript::ExecutorModule& exmod, unsigned param,
33✔
671
                               Core::StoredConfigFile*& cfile )
672
{
673
  Bscript::BApplicObjBase* ao_cfile_base = exmod.exec.getApplicObjParam( param, &cfgfile_type );
33✔
674
  if ( ao_cfile_base != nullptr )
33✔
675
  {
676
    EConfigFileRefObjImp* ao_cfile = static_cast<EConfigFileRefObjImp*>( ao_cfile_base );
33✔
677

678
    cfile = ao_cfile->value().get();
33✔
679

680
    return true;
33✔
681
  }
NEW
682
  return false;
×
683
}
684

685
bool getStoredConfigElemParam( Bscript::ExecutorModule& exmod, unsigned param,
171✔
686
                               Core::StoredConfigElem*& celem )
687
{
688
  Bscript::BApplicObjBase* ao_celem_base = exmod.exec.getApplicObjParam( param, &cfgelem_type );
171✔
689
  if ( ao_celem_base != nullptr )
171✔
690
  {
691
    EConfigElemRefObjImp* ao_celem = static_cast<EConfigElemRefObjImp*>( ao_celem_base );
171✔
692

693
    celem = ao_celem->value().get();
171✔
694

695
    return true;
171✔
696
  }
NEW
697
  return false;
×
698
}
699
}  // namespace Module
700
}  // namespace Pol
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc