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