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

polserver / polserver / 21100551564

17 Jan 2026 08:40PM UTC coverage: 60.504% (+0.01%) from 60.492%
21100551564

Pull #857

github

turleypol
fixed scope
Pull Request #857: ClangTidy readability-else-after-return

837 of 1874 new or added lines in 151 files covered. (44.66%)

48 existing lines in 26 files now uncovered.

44445 of 73458 relevant lines covered (60.5%)

515341.61 hits per line

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

20.07
/pol-core/pol/binaryfilescrobj.cpp
1
/** @file
2
 *
3
 * @par History
4
 * - 2009/12/21 Turley:    ._method() call fix
5
 */
6

7

8
#include "binaryfilescrobj.h"
9

10
#include <fstream>
11
#include <stddef.h>
12

13
#include "../bscript/berror.h"
14
#include "../bscript/bobject.h"
15
#include "../bscript/executor.h"
16
#include "../bscript/impstr.h"
17
#include "../bscript/objmembers.h"
18
#include "../bscript/objmethods.h"
19
#include "../clib/clib_endian.h"
20
#include "../clib/rawtypes.h"
21

22

23
namespace Pol::Core
24
{
25
BBinaryfile::BBinaryfile()
×
26
    : Bscript::BObjectImp( OTBinaryFile ),
27
      _filename( "" ),
×
28
      openmode( std::ios::in ),
×
29
      bigendian( true )
×
30
{
31
}
×
32

33
BBinaryfile::BBinaryfile( std::string filename, unsigned short mode, bool _bigendian )
8✔
34
    : Bscript::BObjectImp( OTBinaryFile ),
35
      _filename( std::move( filename ) ),
8✔
36
      bigendian( _bigendian )
8✔
37
{
38
  using std::ios;
39

40
  // FIXME: ms::stl has different flag values then stlport :(
41
  openmode = static_cast<ios::openmode>( 0x0 );
8✔
42
  if ( mode & 0x01 )
8✔
43
    openmode |= ios::in;
1✔
44
  if ( mode & 0x02 )
8✔
45
    openmode |= ios::out;
7✔
46
  if ( mode & 0x04 )
8✔
47
    openmode |= ios::ate;
×
48
  if ( mode & 0x08 )
8✔
49
    openmode |= ios::app;
×
50
  if ( mode & 0x10 )
8✔
51
    openmode |= ios::trunc;
7✔
52

53
  if ( !file.Open( _filename, openmode ) )
8✔
54
    return;
×
55
}
×
56

57
BBinaryfile::~BBinaryfile()
16✔
58
{
59
  file.Close();
8✔
60
}
16✔
61

62
size_t BBinaryfile::sizeEstimate() const
×
63
{
64
  return sizeof( *this ) + _filename.capacity();
×
65
}
66

67
Bscript::BObjectRef BBinaryfile::get_member_id( const int /*id*/ )  // id test
×
68
{
69
  return Bscript::BObjectRef( new Bscript::BLong( 0 ) );
×
70
}
71
Bscript::BObjectRef BBinaryfile::get_member( const char* membername )
×
72
{
73
  Bscript::ObjMember* objmember = Bscript::getKnownObjMember( membername );
×
74
  if ( objmember != nullptr )
×
75
    return this->get_member_id( objmember->id );
×
NEW
76
  return Bscript::BObjectRef( Bscript::UninitObject::create() );
×
77
}
78

79
Bscript::BObjectImp* BBinaryfile::call_method( const char* methodname, Bscript::Executor& ex )
×
80
{
81
  Bscript::ObjMethod* objmethod = Bscript::getKnownObjMethod( methodname );
×
82
  if ( objmethod != nullptr )
×
83
    return this->call_method_id( objmethod->id, ex );
×
NEW
84
  return nullptr;
×
85
}
86

87
Bscript::BObjectImp* BBinaryfile::call_method_id( const int id, Bscript::Executor& ex,
7,939✔
88
                                                  bool /*forcebuiltin*/ )
89
{
90
  using namespace Bscript;
91
  switch ( id )
7,939✔
92
  {
93
  case MTH_CLOSE:
2✔
94
    file.Close();
2✔
95
    return new BLong( 1 );
2✔
96
  case MTH_SIZE:
×
97
    return new BLong( static_cast<int>( file.FileSize( ex ) ) );
×
98
  case MTH_SEEK:
×
99
  {
100
    using std::ios;
101

102
    if ( ex.numParams() != 2 )
×
103
      return new BError( "Seek requires 2 parameter." );
×
104
    int value, type;
105
    if ( ( !ex.getParam( 0, value ) ) || ( !ex.getParam( 1, type ) ) )
×
106
      return new BError( "Invalid parameter" );
×
107
    // FIXME: ms::stl has different flag values then stlport :(
108
    ios::seekdir seekdir;
109
    if ( type & 0x01 )
×
110
      seekdir = ios::cur;
×
111
    else if ( type & 0x02 )
×
112
      seekdir = ios::end;
×
113
    else
114
      seekdir = ios::beg;
×
115
    if ( !file.Seek( value, seekdir ) )
×
116
      return new BLong( 0 );
×
117
    return new BLong( 1 );
×
118
  }
119
  case MTH_TELL:
×
120
    return new BLong( static_cast<int>( file.Tell() ) );
×
121
  case MTH_PEEK:
×
122
    return new BLong( file.Peek() );
×
123
  case MTH_FLUSH:
×
124
    file.Flush();
×
125
    return new BLong( 1 );
×
126

127
  case MTH_GETINT32:
×
128
  {
129
    u32 _u32;
130
    if ( !file.Read( _u32 ) )
×
131
      return new BError( "Failed to read" );
×
132
    if ( bigendian )
×
133
      _u32 = cfBEu32( _u32 );
×
134
    return new BLong( _u32 );
×
135
  }
136
  case MTH_GETSINT32:
×
137
  {
138
    s32 _s32;
139
    if ( !file.Read( _s32 ) )
×
140
      return new BError( "Failed to read" );
×
141
    if ( bigendian )
×
142
      _s32 = cfBEu32( _s32 );
×
143
    return new BLong( _s32 );
×
144
  }
145
  case MTH_GETINT16:
×
146
  {
147
    u16 _u16;
148
    if ( !file.Read( _u16 ) )
×
149
      return new BError( "Failed to read" );
×
150
    if ( bigendian )
×
151
      _u16 = cfBEu16( _u16 );
×
152
    return new BLong( _u16 );
×
153
  }
154
  case MTH_GETSINT16:
×
155
  {
156
    s16 _s16;
157
    if ( !file.Read( _s16 ) )
×
158
      return new BError( "Failed to read" );
×
159
    if ( bigendian )
×
160
      _s16 = cfBEu16( _s16 );
×
161
    return new BLong( _s16 );
×
162
  }
163
  case MTH_GETINT8:
3,969✔
164
  {
165
    u8 _u8;
166
    if ( !file.Read( _u8 ) )
3,969✔
167
      return new BError( "Failed to read" );
1✔
168
    return new BLong( _u8 );
3,968✔
169
  }
170
  case MTH_GETSINT8:
×
171
  {
172
    s8 _s8;
173
    if ( !file.Read( _s8 ) )
×
174
      return new BError( "Failed to read" );
×
175
    return new BLong( _s8 );
×
176
  }
177
  case MTH_GETSTRING:
×
178
  {
179
    if ( ex.numParams() != 1 )
×
180
      return new BError( "GetString requires 1 parameter." );
×
181
    int value;
182
    if ( !ex.getParam( 0, value ) )
×
183
      return new BError( "Invalid parameter" );
×
184
    if ( value < 1 )
×
185
      return new BError( "Len is negative or 0." );
×
186
    std::vector<unsigned char> _char;
×
187
    _char.resize( value );
×
188
    if ( !file.Read( &_char[0], value ) )
×
189
      return new BError( "Failed to read" );
×
190
    size_t len = 0;
×
191
    const char* _str = reinterpret_cast<const char*>( &_char[0] );
×
192
    // Returns maximum of len characters or up to the first null-byte
193
    while ( len < static_cast<size_t>( value ) && *( _str + len ) )
×
194
      ++len;
×
195
    return new String( _str, len, String::Tainted::YES );
×
196
  }
×
197

198
  case MTH_SETINT32:
×
199
  {
200
    if ( ex.numParams() != 1 )
×
201
      return new BError( "SetInt32 requires 1 parameter." );
×
202
    int value;
203
    if ( !ex.getParam( 0, value ) )
×
204
      return new BError( "Invalid parameter" );
×
205
    u32 _u32 = static_cast<u32>( value );
×
206
    if ( bigendian )
×
207
      _u32 = cfBEu32( _u32 );
×
208
    if ( !file.Write( _u32 ) )
×
209
      return new BError( "Failed to write" );
×
210
    return new BLong( 1 );
×
211
  }
212
  case MTH_SETSINT32:
×
213
  {
214
    if ( ex.numParams() != 1 )
×
215
      return new BError( "SetSInt32 requires 1 parameter." );
×
216
    int value;
217
    if ( !ex.getParam( 0, value ) )
×
218
      return new BError( "Invalid parameter" );
×
219
    s32 _s32 = static_cast<s32>( value );
×
220
    if ( bigendian )
×
221
      _s32 = cfBEu32( _s32 );
×
222
    if ( !file.Write( _s32 ) )
×
223
      return new BError( "Failed to write" );
×
224
    return new BLong( 1 );
×
225
  }
226
  case MTH_SETINT16:
×
227
  {
228
    if ( ex.numParams() != 1 )
×
229
      return new BError( "SetInt16 requires 1 parameter." );
×
230
    int value;
231
    if ( !ex.getParam( 0, value ) )
×
232
      return new BError( "Invalid parameter" );
×
233
    u16 _u16 = static_cast<u16>( value );
×
234
    if ( bigendian )
×
235
      _u16 = cfBEu16( _u16 );
×
236
    if ( !file.Write( _u16 ) )
×
237
      return new BError( "Failed to write" );
×
238
    return new BLong( 1 );
×
239
  }
240
  case MTH_SETSINT16:
×
241
  {
242
    if ( ex.numParams() != 1 )
×
243
      return new BError( "SetSInt16 requires 1 parameter." );
×
244
    int value;
245
    if ( !ex.getParam( 0, value ) )
×
246
      return new BError( "Invalid parameter" );
×
247
    s16 _s16 = static_cast<s16>( value );
×
248
    if ( bigendian )
×
249
      _s16 = cfBEu16( _s16 );
×
250
    if ( !file.Write( _s16 ) )
×
251
      return new BError( "Failed to write" );
×
252
    return new BLong( 1 );
×
253
  }
254
  case MTH_SETINT8:
3,968✔
255
  {
256
    if ( ex.numParams() != 1 )
3,968✔
257
      return new BError( "SetInt8 requires 1 parameter." );
×
258
    int value;
259
    if ( !ex.getParam( 0, value ) )
3,968✔
260
      return new BError( "Invalid parameter" );
×
261
    u8 _u8 = static_cast<u8>( value );
3,968✔
262
    if ( !file.Write( _u8 ) )
3,968✔
263
      return new BError( "Failed to write" );
×
264
    return new BLong( 1 );
3,968✔
265
  }
266
  case MTH_SETSINT8:
×
267
  {
268
    if ( ex.numParams() != 1 )
×
269
      return new BError( "SetSInt8 requires 1 parameter." );
×
270
    int value;
271
    if ( !ex.getParam( 0, value ) )
×
272
      return new BError( "Invalid parameter" );
×
273
    s8 _s8 = static_cast<s8>( value );
×
274
    if ( !file.Write( _s8 ) )
×
275
      return new BError( "Failed to write" );
×
276
    return new BLong( 1 );
×
277
  }
278
  case MTH_SETSTRING:
×
279
  {
280
    if ( ex.numParams() != 2 )
×
281
      return new BError( "SetString requires 2 parameters." );
×
282
    int value;
283
    const String* text;
284
    if ( ( !ex.getStringParam( 0, text ) ) || ( !ex.getParam( 1, value ) ) )
×
285
      return new BError( "Invalid parameter" );
×
286
    u32 len = static_cast<u32>( text->value().length() );
×
287
    if ( value == 1 )
×
288
      len++;
×
289
    if ( !file.WriteString( text->value().c_str(), len ) )
×
290
      return new BError( "Failed to write" );
×
291
    return new BLong( 1 );
×
292
  }
293

294
  default:
×
295
    return nullptr;
×
296
  }
297
}
298

299
Bscript::BObjectImp* BBinaryfile::copy() const
×
300
{
301
  return new BBinaryfile( _filename, static_cast<unsigned short>( openmode ), bigendian );
×
302
}
303

304
std::string BBinaryfile::getStringRep() const
×
305
{
306
  return "BinaryFile";
×
307
}
308

309
bool BBinaryfile::isTrue() const
2✔
310
{
311
  if ( _filename.empty() )
2✔
312
    return false;
×
313
  return file.IsOpen();
2✔
314
}
315

316
bool BBinaryfile::operator==( const Bscript::BObjectImp& objimp ) const
×
317
{
318
  if ( objimp.isa( Bscript::BObjectImp::OTBinaryFile ) )
×
319
  {
320
    if ( ( (BBinaryfile&)objimp )._filename == _filename )
×
321
      return true;
×
322
  }
323
  else if ( objimp.isa( Bscript::BObjectImp::OTBoolean ) )
×
324
    return isTrue() == static_cast<const Bscript::BBoolean&>( objimp ).isTrue();
×
325
  return false;
×
326
}
327

328
BinFile::BinFile( const std::string& filename, std::ios::openmode mode )
×
329
{
330
  Open( filename, mode );
×
331
}
×
332

333
BinFile::~BinFile()
8✔
334
{
335
  Close();
8✔
336
}
8✔
337

338
bool BinFile::Open( const std::string& filename, std::ios::openmode mode )
8✔
339
{
340
  if ( _file.is_open() )
8✔
341
    return true;
×
342

343
  _file.open( filename.c_str(), mode | std::ios::binary );
8✔
344
  if ( !_file.is_open() )
8✔
345
    return false;
×
346
  return true;
8✔
347
}
348

349
bool BinFile::IsOpen()
2✔
350
{
351
  return _file.is_open();
2✔
352
}
353

354
void BinFile::Close()
18✔
355
{
356
  if ( !_file.is_open() )
18✔
357
    return;
10✔
358

359
  _file.close();
8✔
360
}
361

362
bool BinFile::Seek( std::fstream::pos_type abs_offset, std::ios::seekdir origin )
×
363
{
364
  if ( !_file.is_open() )
×
365
    return false;
×
366

367
  if ( !_file.seekg( abs_offset, origin ) )
×
368
    return false;
×
369
  return true;
×
370
}
371

372
bool BinFile::ReadBuffer( void* buffer, std::streamsize length )
3,969✔
373
{
374
  if ( !_file.read( reinterpret_cast<char*>( buffer ), length ) )
3,969✔
375
    return false;
1✔
376
  return true;
3,968✔
377
}
378

379
bool BinFile::WriteBuffer( void* buffer, std::streamsize length )
3,968✔
380
{
381
  if ( !_file.write( reinterpret_cast<char*>( buffer ), length ) )
3,968✔
382
    return false;
×
383
  return true;
3,968✔
384
}
385

386
bool BinFile::WriteString( const char* chr, unsigned len )
×
387
{
388
  if ( !_file.write( chr, len ) )
×
389
    return false;
×
390
  return true;
×
391
}
392

393
std::fstream::pos_type BinFile::FileSize( Bscript::Executor& exec )
×
394
{
395
  if ( !_file.is_open() )
×
396
    return std::fstream::pos_type( 0 );
×
397

398
  std::fstream::pos_type save_pos = _file.tellg();
×
399
  if ( save_pos == std::fstream::pos_type( -1 ) )
×
400
  {
401
    exec.setFunctionResult(
×
402
        new Bscript::BError( "FileSize failed to determine current position." ) );
×
403
    return std::fstream::pos_type( 0 );
×
404
  }
405
  if ( !_file.seekg( 0, std::ios::end ) )
×
406
  {
407
    exec.setFunctionResult( new Bscript::BError( "FileSize failed to seek to end of file." ) );
×
408
    return std::fstream::pos_type( 0 );
×
409
  }
410
  std::fstream::pos_type size = _file.tellg();
×
411
  if ( size == std::fstream::pos_type( -1 ) )
×
412
  {
413
    exec.setFunctionResult( new Bscript::BError( "FileSize could not determine file size." ) );
×
414
    return std::fstream::pos_type( 0 );
×
415
  }
416
  if ( !_file.seekg( save_pos ) )
×
417
  {
418
    exec.setFunctionResult(
×
419
        new Bscript::BError( "FileSize failed to seek to original position." ) );
×
420
    return std::fstream::pos_type( 0 );
×
421
  }
422
  return size;
×
423
}
424

425
std::fstream::pos_type BinFile::Tell()
×
426
{
427
  if ( !_file.is_open() )
×
428
    return -1;
×
429
  return _file.tellg();
×
430
}
431

432
int BinFile::Peek()
×
433
{
434
  if ( !_file.is_open() )
×
435
    return -1;
×
436
  return _file.peek();
×
437
}
438

439
void BinFile::Flush()
×
440
{
441
  if ( _file.is_open() )
×
442
    _file.flush();
×
443
}
×
444
}  // namespace Pol::Core
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