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