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

polserver / polserver / 21541532363

31 Jan 2026 08:14AM UTC coverage: 60.532% (+0.03%) from 60.507%
21541532363

push

github

web-flow
Tidy modernize for loops (#862)

* trigger loop convert

* Automated clang-tidy change: modernize-loop-convert

* fixed refactor

* Automated clang-tidy change: modernize-loop-convert

* compile

* first look through

* fixes and start to use a few ranges

* revert autogenerated file

* compilation fix

* second pass

* renamed loop variable

---------

Co-authored-by: Clang Tidy <clang-tidy@users.noreply.github.com>

164 of 447 new or added lines in 61 files covered. (36.69%)

6 existing lines in 5 files now uncovered.

44377 of 73312 relevant lines covered (60.53%)

499857.83 hits per line

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

64.71
/pol-core/pol/cmdlevel.cpp
1
/** @file
2
 *
3
 * @par History
4
 * - 2005/07/26 Shinigami: you can use "commands/cmdlevel" to hold textcmds in packages too
5
 */
6

7

8
#include "cmdlevel.h"
9

10
#include <filesystem>
11
#include <memory>
12
#include <stddef.h>
13
#include <string>
14
#include <system_error>
15

16
#include "../bscript/bstruct.h"
17
#include "../bscript/impstr.h"
18
#include "../clib/cfgelem.h"
19
#include "../clib/cfgfile.h"
20
#include "../clib/clib.h"
21
#include "../clib/fileutil.h"
22
#include "../clib/stlutil.h"
23
#include "../clib/strutil.h"
24
#include "../plib/pkg.h"
25
#include "../plib/systemstate.h"
26
#include "bscript/dict.h"
27
#include "globals/uvars.h"
28

29
namespace Pol::Core
30
{
31
namespace fs = std::filesystem;
32
CmdLevel::CmdLevel( Clib::ConfigElem& elem, int cmdlevelnum )
6✔
33
    : name( elem.rest() ), cmdlevel( static_cast<unsigned char>( cmdlevelnum ) )
6✔
34
{
35
  Clib::mklowerASCII( name );
6✔
36
  std::string tmp;
6✔
37
  while ( elem.remove_prop( "DIR", &tmp ) )
6✔
38
  {
39
    Clib::mklowerASCII( tmp );
×
40
    add_searchdir( nullptr, Clib::normalized_dir_form( tmp ) );
×
41
  }
42
  while ( elem.remove_prop( "ALIAS", &tmp ) )
6✔
43
  {
44
    Clib::mklowerASCII( tmp );
×
45
    aliases.push_back( tmp );
×
46
  }
47
}
6✔
48

49
bool CmdLevel::matches( const std::string& i_name ) const
×
50
{
51
  if ( Clib::stringicmp( i_name, name ) == 0 )
×
52
    return true;
×
NEW
53
  for ( const auto& alias : aliases )
×
54
  {
NEW
55
    if ( Clib::stringicmp( i_name, alias ) == 0 )
×
56
      return true;
×
57
  }
58
  return false;
×
59
}
60
void CmdLevel::add_searchdir( Plib::Package* pkg, const std::string& dir )
×
61
{
62
  searchlist.emplace_back( SearchDir{ pkg, dir } );
×
63
}
×
64
void CmdLevel::add_searchdir_front( Plib::Package* pkg, const std::string& dir )
3✔
65
{
66
  searchlist.insert( searchlist.begin(), SearchDir{ pkg, dir } );
3✔
67
}
3✔
68

69
size_t CmdLevel::estimateSize() const
2✔
70
{
71
  size_t size = name.capacity() + sizeof( unsigned char ); /*cmdlevel*/
2✔
72

73
  size += Clib::memsize( searchlist );
2✔
74
  for ( const auto& ele : searchlist )
3✔
75
    size += ele.dir.capacity();
1✔
76
  size += Clib::memsize( aliases );
2✔
77
  return size;
2✔
78
}
79

80

81
CmdLevel* find_cmdlevel( const char* name )
106✔
82
{
83
  for ( auto& cmdlvl : gamestate.cmdlevels )
108✔
84
  {
85
    if ( stricmp( name, cmdlvl.name.c_str() ) == 0 )
108✔
86
      return &cmdlvl;
106✔
87
  }
88
  return nullptr;
×
89
}
90

91
CmdLevel* FindCmdLevelByAlias( const std::string& str )
×
92
{
93
  for ( auto& cmdlvl : gamestate.cmdlevels )
×
94
  {
95
    if ( cmdlvl.matches( str ) )
×
96
      return &cmdlvl;
×
97
  }
98
  return nullptr;
×
99
}
100

101
std::unique_ptr<Bscript::BDictionary> ListAllCommandsInPackage( Plib::Package* m_pkg,
20✔
102
                                                                int max_cmdlevel /*= -1*/ )
103
{
104
  auto cmd_lvl_list = std::make_unique<Bscript::BDictionary>();
20✔
105

106
  if ( max_cmdlevel < 0 )
20✔
107
    max_cmdlevel = static_cast<int>( Core::gamestate.cmdlevels.size() - 1 );
20✔
108

109
  for ( int num = 0; num <= max_cmdlevel; ++num )
60✔
110
  {
111
    auto script_list = Core::ListCommandsInPackageAtCmdlevel( m_pkg, num );
40✔
112
    if ( script_list->ref_arr.empty() )
40✔
113
      continue;
39✔
114
    cmd_lvl_list->addMember( new Bscript::BLong( num ), script_list.release() );
1✔
115
  }
40✔
116
  return cmd_lvl_list;
20✔
117
}
×
118

119

120
std::unique_ptr<Bscript::ObjArray> ListCommandsInPackageAtCmdlevel( Plib::Package* m_pkg,
40✔
121
                                                                    int cmdlvl_num )
122
{
123
  auto script_names = std::make_unique<Bscript::ObjArray>();
40✔
124

125
  if ( cmdlvl_num >= static_cast<int>( gamestate.cmdlevels.size() ) )
40✔
126
    cmdlvl_num = static_cast<int>( gamestate.cmdlevels.size() - 1 );
×
127

128
  CmdLevel& cmdlevel = gamestate.cmdlevels[cmdlvl_num];
40✔
129

130
  for ( const auto& search_dir : cmdlevel.searchlist )
60✔
131
  {
132
    Plib::Package* pkg = search_dir.pkg;
20✔
133
    std::string dir_name = search_dir.dir;
20✔
134
    if ( ( !pkg && m_pkg ) || ( pkg && !m_pkg ) )
20✔
135
      continue;
1✔
136
    if ( pkg && m_pkg )
19✔
137
    {
138
      if ( pkg != m_pkg )
19✔
139
        continue;
18✔
140
      dir_name = pkg->dir() + dir_name;
1✔
141
    }
142
    std::error_code ec;
1✔
143
    for ( const auto& dir_entry : fs::directory_iterator( dir_name, ec ) )
7✔
144
    {
145
      if ( !dir_entry.is_regular_file() )
3✔
146
        continue;
×
147
      if ( auto fn = dir_entry.path().filename().string(); !fn.empty() && *fn.begin() == '.' )
3✔
148
        continue;
3✔
149

150
      const auto ext = dir_entry.path().extension();
3✔
151
      if ( !ext.compare( ".ecl" ) )
3✔
152
      {
153
        std::unique_ptr<Bscript::BStruct> cmdinfo( new Bscript::BStruct );
1✔
154
        cmdinfo->addMember( "dir", new Bscript::String( search_dir.dir ) );
1✔
155
        cmdinfo->addMember( "script", new Bscript::String( dir_entry.path().filename().string() ) );
1✔
156
        script_names->addElement( cmdinfo.release() );
1✔
157
      }
1✔
158
    }
4✔
159
  }
20✔
160

161
  return script_names;
40✔
162
}
×
163

164
void load_cmdlevels()
3✔
165
{
166
  Clib::ConfigFile cf( "config/cmds.cfg", "CmdLevel" );
3✔
167
  Clib::ConfigElem elem;
3✔
168

169
  while ( cf.read( elem ) )
9✔
170
  {
171
    CmdLevel cmdlevel( elem, static_cast<int>( gamestate.cmdlevels.size() ) );
6✔
172
    gamestate.cmdlevels.push_back( cmdlevel );
6✔
173
  }
6✔
174
}
3✔
175

176
void process_package_cmds_cfg( Plib::Package* pkg )
×
177
{
178
  // ConfigFile cf( (pkg->dir() + "cmds.cfg").c_str(), "Commands" );
179
  Clib::ConfigFile cf( GetPackageCfgPath( pkg, "cmds.cfg" ).c_str(), "Commands" );
×
180
  Clib::ConfigElem elem;
×
181
  while ( cf.read( elem ) )
×
182
  {
183
    CmdLevel* cmdlevel = find_cmdlevel( elem.rest() );
×
184
    if ( !cmdlevel )
×
185
    {
186
      elem.throw_error( std::string( "Command Level " ) + elem.rest() + " not found." );
×
187
    }
188

189
    std::string tmp;
×
190
    while ( elem.remove_prop( "DIR", &tmp ) )
×
191
    {
192
      Clib::mklowerASCII( tmp );
×
193
      cmdlevel->add_searchdir_front( pkg, Clib::normalized_dir_form( pkg->dir() + tmp ) );
×
194
    }
195
  }
×
196
}
×
197

198
// look for a "textcmd/cmdlevel" or "commands/cmdlevel" directory for each name and alias
199
void implicit_package_cmds_cfg( Plib::Package* pkg )
57✔
200
{
201
  for ( auto& cmdlevel : gamestate.cmdlevels )
171✔
202
  {
203
    std::string dir, part;
114✔
204

205
    // first check for the package name
206
    part = "textcmd/" + cmdlevel.name + "/";
114✔
207
    dir = pkg->dir() + part;
114✔
208
    if ( Clib::FileExists( dir.c_str() ) )
114✔
209
      cmdlevel.add_searchdir_front( pkg, part );
3✔
210

211
    part = "commands/" + cmdlevel.name + "/";
114✔
212
    dir = pkg->dir() + part;
114✔
213
    if ( Clib::FileExists( dir.c_str() ) )
114✔
214
      cmdlevel.add_searchdir_front( pkg, part );
×
215

216
    // then each alias
217
    for ( CmdLevel::Aliases::iterator itr = cmdlevel.aliases.begin(); itr != cmdlevel.aliases.end();
114✔
218
          ++itr )
×
219
    {
220
      part = "textcmd/" + *itr + "/";
×
221
      dir = pkg->dir() + part;
×
222
      if ( Clib::FileExists( dir.c_str() ) )
×
223
        cmdlevel.add_searchdir_front( pkg, part );
×
224

225
      part = "commands/" + *itr + "/";
×
226
      dir = pkg->dir() + part;
×
227
      if ( Clib::FileExists( dir.c_str() ) )
×
228
        cmdlevel.add_searchdir_front( pkg, part );
×
229
    }
230
  }
114✔
231
}
57✔
232

233
void load_package_cmdlevels()
3✔
234
{
235
  for ( auto pkg : Plib::systemstate.packages )
60✔
236
  {
237
    std::string filename = Plib::GetPackageCfgPath( pkg, "cmds.cfg" );
57✔
238
    if ( Clib::FileExists( filename.c_str() ) )
57✔
239
    {
240
      process_package_cmds_cfg( pkg );
×
241
    }
242
    else
243
    {
244
      implicit_package_cmds_cfg( pkg );
57✔
245
    }
246
  }
57✔
247
}
3✔
248
}  // 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