Pagination

This commit is contained in:
2023-11-22 04:54:02 +01:00
parent c693c96100
commit f52ca435eb
10 changed files with 168 additions and 41 deletions
+2 -1
View File
@@ -2,4 +2,5 @@
#define PACKAGE_DIR "/work/Files/"
#define COMMANDS "KHOFMANN_PDF_READER_COMMANDS"
#define DATA "KHOFMANN_PDF_READER_DATA"
#define DATA "KHOFMANN_PDF_READER_DATA"
#define MAX_LIST 10
+35 -18
View File
@@ -3,6 +3,7 @@
#include <MSFS\Legacy\gauges.h>
#include <dirent.h>
#include <errno.h>
#include "rapidjson/document.h"
@@ -13,6 +14,16 @@
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,26 +53,30 @@ namespace khofmann
/// Return tumbnail as BASE64 and page count if present.
/// </summary>
/// <param name="path">Path of directory</param>
/// <param name="files">Pointer to files value object</param>
/// <param name="alloc">Allocator</param>
/// <returns>List of Files</returns>
static rapidjson::Value enumerateDir(const char* path, rapidjson::Document::AllocatorType& alloc)
/// <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)
{
rapidjson::Value files;
files.SetArray();
int count = 0;
int numDirs = 0;
DIR* d;
struct dirent* dir;
d = opendir(path);
DIR* d = opendir(path);
if (d)
{
while ((dir = readdir(d)) != NULL)
struct dirent **dir;
numDirs = scandir(path, &dir, excludeDotDirs, alphasort);
if (numDirs == -1)
{
if (strcmp(dir->d_name, ".") && strcmp(dir->d_name, ".."))
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->d_name;
dirPath += dir[i]->d_name;
std::string thumb(dirPath);
thumb += "/thumb.bjpg";
FILE* check = fopen(thumb.c_str(), "r");
@@ -73,7 +88,7 @@ namespace khofmann
f = opendir(dirPath.c_str());
if (f)
{
log(stdout, "Found file %s\n", dir->d_name);
log(stdout, "Found file %s\n", dir[i]->d_name);
while ((file = readdir(f)) != NULL)
{
if (file->d_type == DT_REG)
@@ -83,29 +98,31 @@ namespace khofmann
}
rapidjson::Value entry;
entry.SetObject();
entry.AddMember("name", rapidjson::Value(dir->d_name, alloc).Move(), alloc);
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);
files->PushBack(entry.Move(), alloc);
}
closedir(f);
}
else
{
log(stdout, "Found directory %s\n", dir->d_name);
log(stdout, "Found directory %s\n", dir[i]->d_name);
rapidjson::Value entry;
entry.SetObject();
entry.AddMember("name", rapidjson::Value(dir->d_name, alloc).Move(), alloc);
entry.AddMember("name", rapidjson::Value(dir[i]->d_name, alloc).Move(), alloc);
entry.AddMember("type", "directory", alloc);
files.PushBack(entry.Move(), alloc);
files->PushBack(entry.Move(), alloc);
}
}
free(dir[i]->d_name);
}
free(dir);
}
closedir(d);
return files;
return numDirs;
}
}
+7 -1
View File
@@ -63,6 +63,8 @@ extern "C"
if (strcmp(cmd, "LIST") == 0)
{
int offset = inDoc["offset"].GetInt();
std::string path;
path += PACKAGE_DIR;
path += inDoc["path"].GetString();
@@ -73,7 +75,11 @@ extern "C"
outDoc.SetObject();
outDoc.AddMember("id", "LIST", allocator);
outDoc.AddMember("data", khofmann::enumerateDir(path.c_str(), allocator).Move(), allocator);
rapidjson::Value files;
files.SetArray();
int numDirs = khofmann::enumerateDir(path.c_str(), &files, allocator, offset);
outDoc.AddMember("count", numDirs, allocator);
outDoc.AddMember("data", files.Move(), allocator);
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);