1#include <base/logger.h>
2#include <base/system.h>
3#include <engine/storage.h>
4
5struct SListDirectoryContext
6{
7 const char *m_pPath;
8 IStorage *m_pStorage;
9};
10
11inline void ProcessItem(const char *pItemName, IStorage *pStorage)
12{
13 char aConfig[2048];
14
15 size_t Len = (size_t)str_length(str: pItemName) + 1; // including '\0'
16 if(Len > sizeof(aConfig))
17 {
18 dbg_msg(sys: "config_common", fmt: "can't process overlong filename '%s'", pItemName);
19 return;
20 }
21
22 if(!str_endswith(str: pItemName, suffix: ".map"))
23 {
24 dbg_msg(sys: "config_common", fmt: "can't process non-map file '%s'", pItemName);
25 return;
26 }
27
28 str_copy(dst: aConfig, src: pItemName, dst_size: sizeof(aConfig));
29 aConfig[Len - sizeof(".map")] = 0;
30 str_append(dst&: aConfig, src: ".cfg");
31 dbg_msg(sys: "config_common", fmt: "processing '%s'", pItemName);
32 Process(pStorage, pMapName: pItemName, pConfigName: aConfig);
33}
34
35static int ListdirCallback(const char *pItemName, int IsDir, int StorageType, void *pUser)
36{
37 if(!IsDir)
38 {
39 SListDirectoryContext Context = *((SListDirectoryContext *)pUser);
40 char aName[2048];
41 str_format(buffer: aName, buffer_size: sizeof(aName), format: "%s/%s", Context.m_pPath, pItemName);
42 ProcessItem(pItemName: aName, pStorage: Context.m_pStorage);
43 }
44
45 return 0;
46}
47
48int main(int argc, const char **argv) // NOLINT(misc-definitions-in-headers)
49{
50 CCmdlineFix CmdlineFix(&argc, &argv);
51 log_set_global_logger_default();
52
53 IStorage *pStorage = CreateLocalStorage();
54 if(!pStorage)
55 return -1;
56
57 if(argc == 1)
58 {
59 dbg_msg(sys: "usage", fmt: "%s FILE1 [ FILE2... ]", argv[0]);
60 dbg_msg(sys: "usage", fmt: "%s DIRECTORY", argv[0]);
61 return -1;
62 }
63 else if(argc == 2 && fs_is_dir(path: argv[1]))
64 {
65 SListDirectoryContext Context = {.m_pPath: argv[1], .m_pStorage: pStorage};
66 pStorage->ListDirectory(Type: IStorage::TYPE_ALL, pPath: argv[1], pfnCallback: ListdirCallback, pUser: &Context);
67 }
68
69 for(int i = 1; i < argc; i++)
70 {
71 ProcessItem(pItemName: argv[i], pStorage);
72 }
73 return 0;
74}
75