More pagination

converter settings json
This commit is contained in:
2023-11-22 07:51:16 +01:00
parent f52ca435eb
commit 515cbd8a20
9 changed files with 232 additions and 153 deletions
+44 -67
View File
@@ -3,7 +3,6 @@
#include <MSFS\Legacy\gauges.h>
#include <dirent.h>
#include <errno.h>
#include "rapidjson/document.h"
@@ -14,16 +13,6 @@
namespace khofmann
{
/// <summary>
/// Exclude . and .. directory
/// </summary>
/// <param name="entry">Entry to check</param>
/// <returns>TRUE if not . or .., FALSE otherwise</returns>
static int excludeDotDirs(const struct dirent* entry)
{
return strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0;
}
/// <summary>
/// Read a file and return file contents
/// </summary>
@@ -42,7 +31,9 @@ namespace khofmann
fread(string, fsize, 1, file);
fclose(file);
return rapidjson::Value(string, alloc);
rapidjson::Value retVal(string, alloc);
free(string);
return retVal;
}
return rapidjson::Value("", alloc);
@@ -55,74 +46,60 @@ namespace khofmann
/// <param name="path">Path of directory</param>
/// <param name="files">Pointer to files value object</param>
/// <param name="alloc">Allocator</param>
/// <param name="offset">Offset into file list</param>
/// <returns>Total numbers of directories</returns>
static int enumerateDir(const char* path, rapidjson::Value *files, rapidjson::Document::AllocatorType& alloc, int offset)
static void enumerateDir(const char* path, rapidjson::Value *files, rapidjson::Document::AllocatorType& alloc)
{
int count = 0;
int numDirs = 0;
DIR* d = opendir(path);
if (d)
{
struct dirent **dir;
numDirs = scandir(path, &dir, excludeDotDirs, alphasort);
if (numDirs == -1)
struct dirent* dir;
while ((dir = readdir(d)) != NULL)
{
log(stdout, "Error: %i", (void*)errno);
}
for (int i = 0; i < numDirs; i++)
{
if (i >= offset && i < offset + MAX_LIST)
{
std::string dirPath(path);
dirPath += dir[i]->d_name;
std::string thumb(dirPath);
thumb += "/thumb.bjpg";
FILE* check = fopen(thumb.c_str(), "r");
if (check)
{
fclose(check);
DIR* f;
struct dirent* file;
f = opendir(dirPath.c_str());
if (f)
{
log(stdout, "Found file %s\n", dir[i]->d_name);
while ((file = readdir(f)) != NULL)
{
if (file->d_type == DT_REG)
{
count++;
}
}
rapidjson::Value entry;
entry.SetObject();
entry.AddMember("name", rapidjson::Value(dir[i]->d_name, alloc).Move(), alloc);
entry.AddMember("pages", count - 1, alloc);
entry.AddMember("thumb", readFile(thumb.c_str(), alloc).Move(), alloc);
entry.AddMember("type", "file", alloc);
files->PushBack(entry.Move(), alloc);
}
closedir(f);
}
else
{
log(stdout, "Found directory %s\n", dir[i]->d_name);
if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) continue;
std::string dirPath(path);
dirPath += dir->d_name;
std::string thumb(dirPath);
thumb += "/thumb.bjpg";
FILE* check = fopen(thumb.c_str(), "r");
if (check)
{
fclose(check);
DIR* f;
struct dirent* file;
f = opendir(dirPath.c_str());
if (f)
{
int count = 0;
log(stdout, "Found file %s\n", dir->d_name);
while ((file = readdir(f)) != NULL)
{
if (file->d_type == DT_REG && strstr(file->d_name, ".bjpg") != NULL)
{
count++;
}
}
rapidjson::Value entry;
entry.SetObject();
entry.AddMember("name", rapidjson::Value(dir[i]->d_name, alloc).Move(), alloc);
entry.AddMember("type", "directory", alloc);
entry.AddMember("name", rapidjson::Value(dir->d_name, alloc).Move(), alloc);
entry.AddMember("pages", count - 1, alloc);
entry.AddMember("thumb", readFile(thumb.c_str(), alloc).Move(), alloc);
entry.AddMember("type", "file", alloc);
files->PushBack(entry.Move(), alloc);
}
closedir(f);
}
else if (dir->d_type == DT_DIR)
{
log(stdout, "Found directory %s\n", dir->d_name);
rapidjson::Value entry;
entry.SetObject();
entry.AddMember("name", rapidjson::Value(dir->d_name, alloc).Move(), alloc);
entry.AddMember("type", "directory", alloc);
files->PushBack(entry.Move(), alloc);
}
free(dir[i]->d_name);
}
free(dir);
}
closedir(d);
return numDirs;
}
}
+1 -4
View File
@@ -63,8 +63,6 @@ extern "C"
if (strcmp(cmd, "LIST") == 0)
{
int offset = inDoc["offset"].GetInt();
std::string path;
path += PACKAGE_DIR;
path += inDoc["path"].GetString();
@@ -77,8 +75,7 @@ extern "C"
rapidjson::Value files;
files.SetArray();
int numDirs = khofmann::enumerateDir(path.c_str(), &files, allocator, offset);
outDoc.AddMember("count", numDirs, allocator);
khofmann::enumerateDir(path.c_str(), &files, allocator);
outDoc.AddMember("data", files.Move(), allocator);
rapidjson::StringBuffer strbuf;