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