Directory Support
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#define PACKAGE_DIR "/work/Files/"
|
||||
#define COMMANDS "KHOFMANN_PDF_READER_COMMANDS"
|
||||
#define DATA "KHOFMANN_PDF_READER_DATA"
|
||||
@@ -0,0 +1,111 @@
|
||||
#pragma once
|
||||
|
||||
#include <MSFS\Legacy\gauges.h>
|
||||
|
||||
#include <dirent.h>
|
||||
|
||||
#include "rapidjson/document.h"
|
||||
|
||||
#include "Base64.hpp"
|
||||
#include "Utils.hpp"
|
||||
|
||||
#include "Constants.h"
|
||||
|
||||
namespace khofmann
|
||||
{
|
||||
/// <summary>
|
||||
/// Read a file and return BASE64 encoded file contents
|
||||
/// </summary>
|
||||
/// <param name="path">Path of File</param>
|
||||
/// <param name="alloc">Allocator</param>
|
||||
/// <returns>BASE64 encoded file contents</returns>
|
||||
static rapidjson::Value readFile(const char* path, rapidjson::Document::AllocatorType& alloc)
|
||||
{
|
||||
FILE* file = fopen(path, "r");
|
||||
if (file) {
|
||||
fseek(file, 0, SEEK_END);
|
||||
long fsize = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
|
||||
char* string = (char*)calloc(sizeof(char), fsize + 1);
|
||||
fread(string, fsize, 1, file);
|
||||
fclose(file);
|
||||
|
||||
return rapidjson::Value(macaron::Base64::Encode(string, fsize).c_str(), alloc);
|
||||
}
|
||||
|
||||
return rapidjson::Value("", alloc);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enumerate a directory and return list of directories.
|
||||
/// Return tumbnail as BASE64 and page count if present.
|
||||
/// </summary>
|
||||
/// <param name="path">Path of directory</param>
|
||||
/// <param name="alloc">Allocator</param>
|
||||
/// <returns>List of Files</returns>
|
||||
static rapidjson::Value enumerateDir(const char* path, rapidjson::Document::AllocatorType& alloc)
|
||||
{
|
||||
rapidjson::Value files;
|
||||
files.SetArray();
|
||||
|
||||
int count = 0;
|
||||
|
||||
DIR* d;
|
||||
struct dirent* dir;
|
||||
d = opendir(path);
|
||||
if (d)
|
||||
{
|
||||
while ((dir = readdir(d)) != NULL)
|
||||
{
|
||||
if (strcmp(dir->d_name, ".") && strcmp(dir->d_name, ".."))
|
||||
{
|
||||
std::string dirPath(path);
|
||||
dirPath += dir->d_name;
|
||||
std::string thumb(dirPath);
|
||||
thumb += "/thumb.png";
|
||||
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->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->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->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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
|
||||
return files;
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,6 @@
|
||||
#include <MSFS\Legacy\gauges.h>
|
||||
#include <MSFS\MSFS_CommBus.h>
|
||||
|
||||
#include "rapidjson/document.h"
|
||||
#include "rapidjson/stringbuffer.h"
|
||||
#include "rapidjson/writer.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iterator>
|
||||
@@ -15,136 +11,69 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "Base64.hpp"
|
||||
#include "Module.h"
|
||||
#include "rapidjson/document.h"
|
||||
#include "rapidjson/stringbuffer.h"
|
||||
#include "rapidjson/writer.h"
|
||||
|
||||
#define COMMANDS "KHOFMANN_PDF_READER_COMMANDS"
|
||||
#define DATA "KHOFMANN_PDF_READER_DATA"
|
||||
#include "FileSystem.hpp"
|
||||
#include "Utils.hpp"
|
||||
|
||||
FILE* logFile;
|
||||
#include "Constants.h"
|
||||
|
||||
static void CommandWasmCallback(const char* args, unsigned int size, void* ctx);
|
||||
void log(FILE *file, const char *format, void* optionalElement = NULL);
|
||||
std::string readFile(const char* path);
|
||||
|
||||
extern "C" MSFS_CALLBACK void module_init(void)
|
||||
extern "C"
|
||||
{
|
||||
log(stdout, "[PDF-Reader Module] Starting init\n");
|
||||
|
||||
logFile = fopen("\\work\\log.txt", "w");
|
||||
if (logFile == NULL)
|
||||
MSFS_CALLBACK void module_init(void)
|
||||
{
|
||||
log(stderr, "[PDF-Reader Module] Error creating logfile\n");
|
||||
}
|
||||
khofmann::log(stdout, "[PDF-Reader Module] Starting init\n");
|
||||
|
||||
if (!fsCommBusRegister(COMMANDS, CommandWasmCallback))
|
||||
{
|
||||
log(stderr, "[PDF-Reader Module] Error registering command CommBus\n");
|
||||
}
|
||||
|
||||
log(stdout, "[PDF-Reader Module] Inited\n");
|
||||
}
|
||||
|
||||
extern "C" MSFS_CALLBACK void module_deinit(void)
|
||||
{
|
||||
log(stdout, "[PDF-Reader Module] Starting deinit\n");
|
||||
|
||||
int c = fsCommBusUnregister(COMMANDS, CommandWasmCallback);
|
||||
log(stdout, "%i unregistered", &c);
|
||||
|
||||
log(stdout, "[PDF-Reader Module] Deinited\n");
|
||||
}
|
||||
|
||||
static void CommandWasmCallback(const char* args, unsigned int size, void* ctx)
|
||||
{
|
||||
if (strcmp(args, "LIST") == 0)
|
||||
{
|
||||
rapidjson::Document jsonDoc;
|
||||
rapidjson::Document::AllocatorType& allocator = jsonDoc.GetAllocator();
|
||||
|
||||
jsonDoc.SetObject();
|
||||
jsonDoc.AddMember("id", "LIST", allocator);
|
||||
|
||||
rapidjson::Value files;
|
||||
files.SetArray();
|
||||
|
||||
DIR* d;
|
||||
struct dirent* dir;
|
||||
d = opendir("\\work\\Files");
|
||||
if (d)
|
||||
khofmann::logFile = fopen("/work/log.txt", "w");
|
||||
if (khofmann::logFile == NULL)
|
||||
{
|
||||
log(stdout, "Found files:\n");
|
||||
while ((dir = readdir(d)) != NULL)
|
||||
{
|
||||
if (dir->d_type == DT_DIR && strcmp(dir->d_name, ".") && strcmp(dir->d_name, ".."))
|
||||
{
|
||||
log(stdout, "%s\n", dir->d_name);
|
||||
|
||||
std::string path("\\work\\Files\\");
|
||||
path += dir->d_name;
|
||||
|
||||
int count = 0;
|
||||
DIR* s;
|
||||
struct dirent* dir_s;
|
||||
s = opendir(path.c_str());
|
||||
if (s)
|
||||
{
|
||||
while ((dir_s = readdir(s)) != NULL)
|
||||
{
|
||||
if (dir_s->d_type == DT_REG)
|
||||
{
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
closedir(s);
|
||||
|
||||
std::string thumb(dir->d_name);
|
||||
thumb += "\\thumb.png";
|
||||
|
||||
rapidjson::Value name(dir->d_name, allocator);
|
||||
rapidjson::Value _thumb(readFile(thumb.c_str()).c_str(), allocator);
|
||||
|
||||
rapidjson::Value entry;
|
||||
entry.SetObject();
|
||||
entry.AddMember("name", name.Move(), allocator);
|
||||
entry.AddMember("thumb", _thumb.Move(), allocator);
|
||||
entry.AddMember("pages", count - 1, allocator);
|
||||
|
||||
files.PushBack(entry.Move(), allocator);
|
||||
}
|
||||
}
|
||||
|
||||
jsonDoc.AddMember("data", files, allocator);
|
||||
|
||||
rapidjson::StringBuffer strbuf;
|
||||
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
|
||||
jsonDoc.Accept(writer);
|
||||
|
||||
fsCommBusCall(DATA, strbuf.GetString(), strbuf.GetSize(), FsCommBusBroadcast_JS);
|
||||
|
||||
closedir(d);
|
||||
khofmann::log(stderr, "[PDF-Reader Module] Error creating logfile\n");
|
||||
}
|
||||
|
||||
if (!fsCommBusRegister(COMMANDS, CommandWasmCallback))
|
||||
{
|
||||
khofmann::log(stderr, "[PDF-Reader Module] Error registering command CommBus\n");
|
||||
}
|
||||
|
||||
khofmann::log(stdout, "[PDF-Reader Module] Inited\n");
|
||||
}
|
||||
else
|
||||
|
||||
extern "C" MSFS_CALLBACK void module_deinit(void)
|
||||
{
|
||||
khofmann::log(stdout, "[PDF-Reader Module] Starting deinit\n");
|
||||
|
||||
int c = fsCommBusUnregister(COMMANDS, CommandWasmCallback);
|
||||
khofmann::log(stdout, "%i unregistered", &c);
|
||||
|
||||
khofmann::log(stdout, "[PDF-Reader Module] Deinited\n");
|
||||
|
||||
fclose(khofmann::logFile);
|
||||
}
|
||||
|
||||
static void CommandWasmCallback(const char* args, unsigned int size, void* ctx)
|
||||
{
|
||||
rapidjson::Document inDoc;
|
||||
inDoc.Parse(args);
|
||||
|
||||
const char* cmd = inDoc["cmd"].GetString();
|
||||
|
||||
if (strcmp(cmd, "LOAD") == 0) {
|
||||
const char* data = inDoc["file"].GetString();
|
||||
|
||||
log(stdout, "Loading file %s\n", (void*)data);
|
||||
if (strcmp(cmd, "LIST") == 0)
|
||||
{
|
||||
std::string path;
|
||||
path += PACKAGE_DIR;
|
||||
path += inDoc["path"].GetString();
|
||||
|
||||
rapidjson::Document outDoc;
|
||||
rapidjson::Document::AllocatorType& allocator = outDoc.GetAllocator();
|
||||
|
||||
outDoc.SetObject();
|
||||
outDoc.AddMember("id", "LOAD", allocator);
|
||||
outDoc.AddMember("data", rapidjson::Value(readFile(data).c_str(), allocator).Move(), allocator);
|
||||
outDoc.AddMember("id", "LIST", allocator);
|
||||
|
||||
outDoc.AddMember("data", khofmann::enumerateDir(path.c_str(), allocator).Move(), allocator);
|
||||
|
||||
rapidjson::StringBuffer strbuf;
|
||||
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
|
||||
@@ -152,37 +81,34 @@ static void CommandWasmCallback(const char* args, unsigned int size, void* ctx)
|
||||
|
||||
fsCommBusCall(DATA, strbuf.GetString(), strbuf.GetSize(), FsCommBusBroadcast_JS);
|
||||
}
|
||||
else
|
||||
{
|
||||
rapidjson::Document inDoc;
|
||||
inDoc.Parse(args);
|
||||
|
||||
const char* cmd = inDoc["cmd"].GetString();
|
||||
|
||||
if (strcmp(cmd, "LOAD") == 0)
|
||||
{
|
||||
std::string path;
|
||||
path += PACKAGE_DIR;
|
||||
path += inDoc["file"].GetString();
|
||||
|
||||
khofmann::log(stdout, "Loading file %s\n", (void*)path.c_str());
|
||||
|
||||
rapidjson::Document outDoc;
|
||||
rapidjson::Document::AllocatorType& allocator = outDoc.GetAllocator();
|
||||
|
||||
outDoc.SetObject();
|
||||
outDoc.AddMember("id", "LOAD", allocator);
|
||||
outDoc.AddMember("data", khofmann::readFile(path.c_str(), allocator).Move(), allocator);
|
||||
|
||||
rapidjson::StringBuffer strbuf;
|
||||
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
|
||||
outDoc.Accept(writer);
|
||||
|
||||
fsCommBusCall(DATA, strbuf.GetString(), strbuf.GetSize(), FsCommBusBroadcast_JS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string readFile(const char* path)
|
||||
{
|
||||
std::string _path;
|
||||
_path += "\\work\\Files\\";
|
||||
_path += path;
|
||||
|
||||
FILE* file = fopen(_path.c_str(), "r");
|
||||
if (file) {
|
||||
fseek(file, 0, SEEK_END);
|
||||
long fsize = ftell(file);
|
||||
fseek(file, 0, SEEK_SET); /* same as rewind(f); */
|
||||
|
||||
char* string = (char*)calloc(sizeof(char), fsize + 1);
|
||||
fread(string, fsize, 1, file);
|
||||
fclose(file);
|
||||
|
||||
return macaron::Base64::Encode(string, fsize);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
void log(FILE* file, const char* format, void* optionalElement)
|
||||
{
|
||||
if (logFile != NULL)
|
||||
{
|
||||
fprintf(logFile, format, optionalElement);
|
||||
fflush(logFile);
|
||||
}
|
||||
fprintf(file, format, optionalElement);
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
#pragma once
|
||||
@@ -45,7 +45,7 @@
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|MSFS'">
|
||||
<TargetName>$(ProjectName)</TargetName>
|
||||
<TargetName>$(ProjectName)-$(Configuration)</TargetName>
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
<TargetExt>.wasm</TargetExt>
|
||||
<IncludePath>$(MSFS_IncludePath)</IncludePath>
|
||||
@@ -55,7 +55,8 @@
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
<TargetExt>.wasm</TargetExt>
|
||||
<IncludePath>$(MSFS_IncludePath)</IncludePath>
|
||||
<OutDir>$(SolutionDir)$(Platform)\</OutDir>
|
||||
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
|
||||
<TargetName>$(ProjectName)</TargetName>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|MSFS'">
|
||||
<ClCompile>
|
||||
@@ -78,7 +79,7 @@
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<NoEntryPoint>true</NoEntryPoint>
|
||||
<OutputFile>$(OutDir)$(TargetName)-$(Configuration)$(TargetExt)</OutputFile>
|
||||
<OutputFile>$(OutDir)$(TargetName)$(TargetExt)</OutputFile>
|
||||
</Link>
|
||||
<PostBuildEvent />
|
||||
</ItemDefinitionGroup>
|
||||
@@ -106,7 +107,7 @@
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>false</GenerateDebugInformation>
|
||||
<NoEntryPoint>true</NoEntryPoint>
|
||||
<OutputFile>$(OutDir)$(TargetName)-$(Configuration)$(TargetExt)</OutputFile>
|
||||
<OutputFile>$(OutDir)$(TargetName)$(TargetExt)</OutputFile>
|
||||
</Link>
|
||||
<PostBuildEvent />
|
||||
<PostBuildEvent>
|
||||
@@ -115,11 +116,16 @@
|
||||
</PostBuildEvent>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="FileSystem.hpp" />
|
||||
<ClCompile Include="Module.cpp" />
|
||||
<ClCompile Include="Utils.hpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|MSFS'">false</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|MSFS'">false</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Base64.hpp" />
|
||||
<ClInclude Include="Module.h" />
|
||||
<ClInclude Include="Constants.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Module.cpp" />
|
||||
<ClCompile Include="FileSystem.hpp" />
|
||||
<ClCompile Include="Utils.hpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Module.h" />
|
||||
<ClInclude Include="Base64.h" />
|
||||
<ClInclude Include="Base64.hpp" />
|
||||
<ClInclude Include="Constants.h" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <MSFS\Legacy\gauges.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
namespace khofmann
|
||||
{
|
||||
static FILE* logFile;
|
||||
|
||||
/// <summary>
|
||||
/// Write to logfile and console
|
||||
/// </summary>
|
||||
/// <param name="stream">Console stream</param>
|
||||
/// <param name="format">Format string wiht up to one specifier</param>
|
||||
/// <param name="optionalElement">Optional element specified by specifier</param>
|
||||
static void log(FILE* stream, const char* format, void* optionalElement = NULL)
|
||||
{
|
||||
if (logFile != NULL)
|
||||
{
|
||||
fprintf(logFile, format, optionalElement);
|
||||
fflush(logFile);
|
||||
}
|
||||
fprintf(stream, format, optionalElement);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user