Bookmarks

This commit is contained in:
2023-11-26 09:39:34 +01:00
parent 34780db289
commit 226eb242e3
18 changed files with 441 additions and 200 deletions
-71
View File
@@ -1,71 +0,0 @@
#pragma once
/**
* The MIT License (MIT)
* Copyright (c) 2016 tomykaira
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <string>
namespace macaron {
class Base64 {
public:
static std::string Encode(const char* data, size_t length) {
static constexpr char sEncodingTable[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
};
size_t out_len = 4 * ((length + 2) / 3);
std::string ret(out_len, '\0');
size_t i;
char* p = const_cast<char*>(ret.c_str());
for (i = 0; i < length - 2; i += 3) {
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
*p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int)(data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | ((int)(data[i + 2] & 0xC0) >> 6)];
*p++ = sEncodingTable[data[i + 2] & 0x3F];
}
if (i < length) {
*p++ = sEncodingTable[(data[i] >> 2) & 0x3F];
if (i == (length - 1)) {
*p++ = sEncodingTable[((data[i] & 0x3) << 4)];
*p++ = '=';
}
else {
*p++ = sEncodingTable[((data[i] & 0x3) << 4) | ((int)(data[i + 1] & 0xF0) >> 4)];
*p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)];
}
*p++ = '=';
}
return ret;
}
};
}
+5 -1
View File
@@ -1,6 +1,10 @@
#pragma once
#define PACKAGE_DIR "/work/Files/"
#define WORK_DIR "/work/BookMarks/"
#define COMMANDS "KHOFMANN_PDF_READER_COMMANDS"
#define DATA "KHOFMANN_PDF_READER_DATA"
#define MAX_LIST 10
#define LIST "LIST"
#define LOAD "LOAD"
#define SAVE "SAVE"
#define MAX_LIST 10
@@ -1,28 +1,22 @@
#pragma once
#include <MSFS\Legacy\gauges.h>
#include <string>
#include <errno.h>
#include <dirent.h>
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "Base64.hpp"
#include "Utils.hpp"
#include "Utils.h"
#include "Constants.h"
#include "FileSystem.h"
namespace khofmann
{
/// <summary>
/// Read a file and return file contents
/// </summary>
/// <param name="path">Path of BASE64 encoded File</param>
/// <param name="alloc">Allocator</param>
/// <returns>File contents</returns>
static rapidjson::Value readFile(const char* path, rapidjson::Document::AllocatorType& alloc)
rapidjson::Value readFile(const char* path, rapidjson::Document::AllocatorType& alloc)
{
FILE* file = fopen(path, "r");
if (file) {
if (file)
{
fseek(file, 0, SEEK_END);
long fsize = ftell(file);
fseek(file, 0, SEEK_SET);
@@ -36,17 +30,30 @@ namespace khofmann
return retVal;
}
int err = errno;
log(stderr, "Error loading file %s: ERRNO %i\n", path, err);
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="files">Pointer to files value object</param>
void writeFile(const char* path, rapidjson::Value &contents)
{
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
contents.Accept(writer);
FILE* file = fopen(path, "w");
if (file)
{
fwrite(strbuf.GetString(), sizeof(char), strbuf.GetSize(), file);
fclose(file);
return;
}
int err = errno;
log(stderr, "Error saving file %s: ERRNO %i\n", path, err);
}
/// <param name="alloc">Allocator</param>
static void enumerateDir(const char* path, rapidjson::Value *files, rapidjson::Document::AllocatorType& alloc)
void enumerateDir(const char* path, rapidjson::Value& files, rapidjson::Document::AllocatorType& alloc)
{
DIR* d = opendir(path);
if (d)
@@ -84,7 +91,12 @@ namespace khofmann
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);
}
else
{
int err = errno;
log(stderr, "Could not open directory %s: ERRNO %i\n", dirPath.c_str(), err);
}
closedir(f);
}
@@ -96,10 +108,15 @@ namespace khofmann
entry.SetObject();
entry.AddMember("name", rapidjson::Value(dir->d_name, alloc).Move(), alloc);
entry.AddMember("type", "directory", alloc);
files->PushBack(entry.Move(), alloc);
files.PushBack(entry.Move(), alloc);
}
}
}
else
{
int err = errno;
log(stderr, "Could not open directory %s: ERRNO %i\n", path, err);
}
closedir(d);
}
}
+33
View File
@@ -0,0 +1,33 @@
#pragma once
#include <MSFS/Legacy/gauges.h>
#include "rapidjson/document.h"
namespace khofmann
{
/// <summary>
/// Read a file and return file contents
/// </summary>
/// <param name="path">Path of BASE64 encoded File</param>
/// <param name="alloc">Allocator</param>
/// <returns>File contents</returns>
rapidjson::Value readFile(const char* path, rapidjson::Document::AllocatorType& alloc);
/// <summary>
/// Write a file to disk
/// </summary>
/// <param name="path">Path to file</param>
/// <param name="contents">Contents of file</param>
/// <param name="length">Length of contents</param>
void writeFile(const char* path, rapidjson::Value& contents);
/// <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="files">Pointer to files value object</param>
/// <param name="alloc">Allocator</param>
void enumerateDir(const char* path, rapidjson::Value& files, rapidjson::Document::AllocatorType& alloc);
}
+79 -47
View File
@@ -1,55 +1,57 @@
#include <MSFS\MSFS.h>
#include <MSFS\Legacy\gauges.h>
#include <MSFS\MSFS_CommBus.h>
#include <MSFS/MSFS.h>
#include <MSFS/MSFS_CommBus.h>
#include <MSFS/Legacy/gauges.h>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <iterator>
#include <string.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
#include "FileSystem.hpp"
#include "Utils.hpp"
#include "FileSystem.h"
#include "Utils.h"
#include "Constants.h"
static void CommandWasmCallback(const char* args, unsigned int size, void* ctx);
static FILE* logFile;
extern "C"
{
MSFS_CALLBACK void module_init(void)
{
khofmann::log(stdout, "[PDF-Reader Module] Starting init\n");
khofmann::log(stdout, "Starting init\n");
khofmann::logFile = fopen("/work/log.txt", "w");
if (khofmann::logFile == NULL)
{
khofmann::log(stderr, "[PDF-Reader Module] Error creating logfile\n");
khofmann::log(stderr, "Error creating logfile\n");
}
if (!fsCommBusRegister(COMMANDS, CommandWasmCallback))
{
khofmann::log(stderr, "[PDF-Reader Module] Error registering command CommBus\n");
khofmann::log(stderr, "Error registering command CommBus\n");
}
khofmann::log(stdout, "[PDF-Reader Module] Inited\n");
khofmann::log(stdout, "Inited\n");
}
extern "C" MSFS_CALLBACK void module_deinit(void)
{
khofmann::log(stdout, "[PDF-Reader Module] Starting deinit\n");
khofmann::log(stdout, "Starting deinit\n");
int c = fsCommBusUnregister(COMMANDS, CommandWasmCallback);
khofmann::log(stdout, "%i unregistered", &c);
khofmann::log(stdout, "[PDF-Reader Module] Deinited\n");
khofmann::log(stdout, "Deinited\n");
fclose(khofmann::logFile);
}
@@ -61,57 +63,87 @@ extern "C"
const char* cmd = inDoc["cmd"].GetString();
if (strcmp(cmd, "LIST") == 0)
rapidjson::Document outDoc;
rapidjson::Document::AllocatorType& allocator = outDoc.GetAllocator();
outDoc.SetObject();
outDoc.AddMember("id", rapidjson::Value(cmd, allocator).Move(), allocator);
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
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", "LIST", allocator);
rapidjson::Value files;
files.SetArray();
khofmann::enumerateDir(path.c_str(), &files, allocator);
khofmann::enumerateDir(path.c_str(), files, allocator);
outDoc.AddMember("data", files.Move(), allocator);
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
outDoc.Accept(writer);
fsCommBusCall(DATA, strbuf.GetString(), strbuf.GetSize(), FsCommBusBroadcast_JS);
}
else
else if (strcmp(cmd, LOAD) == 0)
{
rapidjson::Document inDoc;
inDoc.Parse(args);
std::string filePath;
filePath += PACKAGE_DIR;
filePath += inDoc["file"].GetString();
const char* cmd = inDoc["cmd"].GetString();
if (strcmp(cmd, "LOAD") == 0)
std::string marksPath;
marksPath += inDoc["file"].GetString();
size_t lastSlash = marksPath.find_last_of("/");
if (lastSlash != std::string::npos)
{
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);
marksPath.erase(lastSlash);
}
marksPath.erase(std::remove(marksPath.begin(), marksPath.end(), '/'), marksPath.end());
marksPath = WORK_DIR + marksPath + "_bookmarks.json";
rapidjson::Value data;
data.SetObject();
khofmann::log(stdout, "Loading file %s\n", filePath.c_str());
data.AddMember("file", khofmann::readFile(filePath.c_str(), allocator).Move(), allocator);
if (filePath.find("/1.bjpg") != std::string::npos)
{
khofmann::log(stdout, "Loading book marks %s\n", marksPath.c_str());
rapidjson::Document marks;
marks.Parse(khofmann::readFile(marksPath.c_str(), allocator).GetString());
if (marks.IsArray())
{
data.AddMember("bookMarks", marks.Move(), allocator);
}
else
{
rapidjson::Value t;
t.SetArray();
data.AddMember("bookMarks", t.Move(), allocator);
}
}
outDoc.AddMember("data", data.Move(), allocator);
outDoc.Accept(writer);
fsCommBusCall(DATA, strbuf.GetString(), strbuf.GetSize(), FsCommBusBroadcast_JS);
}
else if (strcmp(cmd, SAVE) == 0)
{
std::string marksPath;
marksPath += inDoc["path"].GetString();
marksPath.erase(std::remove(marksPath.begin(), marksPath.end(), '/'), marksPath.end());
marksPath = WORK_DIR + marksPath + "_bookmarks.json";
khofmann::log(stdout, "Saving book marks %s\n", marksPath.c_str());
khofmann::writeFile(marksPath.c_str(), inDoc["bookMarks"]);
outDoc.Accept(writer);
fsCommBusCall(DATA, strbuf.GetString(), strbuf.GetSize(), FsCommBusBroadcast_JS);
}
}
}
+4 -3
View File
@@ -116,16 +116,17 @@
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="FileSystem.hpp" />
<ClCompile Include="FileSystem.cpp" />
<ClCompile Include="Module.cpp" />
<ClCompile Include="Utils.hpp">
<ClCompile Include="Utils.cpp">
<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="Constants.h" />
<ClInclude Include="FileSystem.h" />
<ClInclude Include="Utils.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
@@ -2,11 +2,23 @@
<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" />
<ClCompile Include="FileSystem.cpp" />
<ClCompile Include="Utils.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Base64.hpp" />
<ClInclude Include="Constants.h" />
<ClInclude Include="FileSystem.h">
<Filter>Headers</Filter>
</ClInclude>
<ClInclude Include="Utils.h">
<Filter>Headers</Filter>
</ClInclude>
<ClInclude Include="Constants.h">
<Filter>Headers</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="Headers">
<UniqueIdentifier>{cc9ebc6c-fa76-4cce-aad2-8c91df6e65c1}</UniqueIdentifier>
</Filter>
</ItemGroup>
</Project>
+41
View File
@@ -0,0 +1,41 @@
#include <cstdarg>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "Utils.h"
namespace khofmann
{
void log(FILE* stream, const char* format, ...)
{
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = gmtime(&rawtime);
char fmt[256];
char time[256];
strcpy(time, asctime(timeinfo));
time[strlen(time) - 1] = 0;
sprintf(fmt, "[PDF - Reader Module] %s - %s", time, format);
va_list args;
va_start(args, format);
if (logFile != NULL)
{
vfprintf(logFile, fmt, args);
fflush(logFile);
}
vfprintf(stream, fmt, args);
va_end(args);
//free(fmt);
}
}
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include <MSFS/Legacy/gauges.h>
namespace khofmann
{
extern FILE* logFile;
/// <summary>
/// Write to logfile and console
/// </summary>
/// <param name="stream">Console stream</param>
/// <param name="format">Format string</param>
void log(FILE* stream, const char* format, ...);
}
-26
View File
@@ -1,26 +0,0 @@
#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);
}
}