2806: Allow to use config_retrieve on whole directory r=heinrich5991 a=BannZay

Work on topmost directory only

Co-authored-by: Andrii <bannzay3@gmail.com>
This commit is contained in:
bors[bot] 2020-09-12 21:51:48 +00:00 committed by GitHub
commit 7f5da16168
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,49 @@
#include <base/system.h>
#include <engine/storage.h>
struct ListDirectoryContext
{
const char *pPath;
IStorage *pStorage;
};
void ProcessItem(const char *pItemName, IStorage *pStorage)
{
char aConfig[2048];
size_t Len = (size_t)str_length(pItemName) + 1; // including '\0'
if(Len > sizeof(aConfig))
{
dbg_msg("config_common", "can't process overlong filename '%s'", pItemName);
return;
}
if(!str_endswith(pItemName, ".map"))
{
dbg_msg("config_common", "can't process non-map file '%s'", pItemName);
return;
}
str_copy(aConfig, pItemName, sizeof(aConfig));
aConfig[Len - sizeof(".map")] = 0;
str_append(aConfig, ".cfg", sizeof(aConfig));
dbg_msg("config_common", "processing '%s'", pItemName);
Process(pStorage, pItemName, aConfig);
}
static int ListdirCallback(const char *pItemName, int IsDir, int StorageType, void *pUser)
{
if(!IsDir)
{
ListDirectoryContext Context = *((ListDirectoryContext *)pUser);
char aName[2048];
str_format(aName, sizeof(aName), "%s/%s", Context.pPath, pItemName);
ProcessItem(aName, Context.pStorage);
}
return 0;
}
int main(int argc, const char **argv)
{
dbg_logger_stdout();
@ -8,30 +51,18 @@ int main(int argc, const char **argv)
if(argc == 1)
{
dbg_msg("usage", "%s FILE1 [ FILE2... ]", argv[0]);
dbg_msg("usage", "%s DIRECTORY", argv[0]);
return -1;
}
else if(argc == 2 && fs_is_dir(argv[1]))
{
ListDirectoryContext Context = {argv[1], pStorage};
pStorage->ListDirectory(IStorage::TYPE_ALL, argv[1], ListdirCallback, &Context);
}
for(int i = 1; i < argc; i++)
{
char aConfig[2048];
size_t Len = str_length(argv[i]) + 1; // including '\0'
if(Len > sizeof(aConfig))
{
dbg_msg("config_common", "can't process overlong filename '%s'", argv[i]);
continue;
}
if(!str_endswith(argv[i], ".map"))
{
dbg_msg("config_common", "can't process non-map file '%s'", argv[i]);
continue;
}
str_copy(aConfig, argv[i], sizeof(aConfig));
aConfig[Len - sizeof(".map")] = 0;
str_append(aConfig, ".cfg", sizeof(aConfig));
dbg_msg("config_common", "processing '%s'", argv[i]);
Process(pStorage, argv[i], aConfig);
ProcessItem(argv[i], pStorage);
}
return 0;
}