Added some error handling so that bad files don't crash the app, and/or horizonOS
This commit is contained in:
parent
c66f8fe2f2
commit
3bce64af9f
|
@ -47,9 +47,9 @@ static void save_last_page(const char *book_name, int current_page) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BookReader::BookReader(const char *path) {
|
BookReader::BookReader(const char *path, int* result) {
|
||||||
if (ctx == NULL) {
|
if (ctx == NULL) {
|
||||||
ctx = fz_new_context(NULL, NULL, 128 << 10);
|
ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
|
||||||
fz_register_document_handlers(ctx);
|
fz_register_document_handlers(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,15 +62,34 @@ BookReader::BookReader(const char *path) {
|
||||||
book_name.erase(std::remove(book_name.begin(), book_name.end(), c), book_name.end());
|
book_name.erase(std::remove(book_name.begin(), book_name.end(), c), book_name.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "fz_open_document" << std::endl;
|
fz_try(ctx) {
|
||||||
doc = fz_open_document(ctx, path);
|
std::cout << "fz_open_document" << std::endl;
|
||||||
|
doc = fz_open_document(ctx, path);
|
||||||
|
|
||||||
int current_page = load_last_page(book_name.c_str());
|
if (!doc)
|
||||||
//int current_page = 0;
|
{
|
||||||
switch_current_page_layout(_currentPageLayout, current_page);
|
std::cout << "Error opening file!" << std::endl;
|
||||||
|
*result = -1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (current_page > 0) {
|
std::cout << "doc opened ok ?!" << std::endl;
|
||||||
show_status_bar();
|
|
||||||
|
int current_page = load_last_page(book_name.c_str());
|
||||||
|
//int current_page = 0;
|
||||||
|
|
||||||
|
std::cout << "current_page = " << current_page << std::endl;
|
||||||
|
|
||||||
|
switch_current_page_layout(_currentPageLayout, current_page);
|
||||||
|
|
||||||
|
if (current_page > 0) {
|
||||||
|
show_status_bar();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fz_catch(ctx){
|
||||||
|
std::cout << "fz_catch reached, closing gracefully" << std::endl;
|
||||||
|
*result = -2;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,6 +154,7 @@ void BookReader::switch_page_layout() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void BookReader::draw(bool drawHelp) {
|
void BookReader::draw(bool drawHelp) {
|
||||||
|
//std::cout << "BookReader::draw" << std::endl;
|
||||||
if (configDarkMode == true) {
|
if (configDarkMode == true) {
|
||||||
SDL_ClearScreen(RENDERER, BLACK);
|
SDL_ClearScreen(RENDERER, BLACK);
|
||||||
} else {
|
} else {
|
||||||
|
@ -203,6 +223,8 @@ void BookReader::show_status_bar() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void BookReader::switch_current_page_layout(BookPageLayout bookPageLayout, int current_page) {
|
void BookReader::switch_current_page_layout(BookPageLayout bookPageLayout, int current_page) {
|
||||||
|
std::cout << "enter switch_current_page_layout " << std::endl;
|
||||||
|
|
||||||
if (layout) {
|
if (layout) {
|
||||||
current_page = layout->current_page();
|
current_page = layout->current_page();
|
||||||
delete layout;
|
delete layout;
|
||||||
|
@ -219,4 +241,6 @@ void BookReader::switch_current_page_layout(BookPageLayout bookPageLayout, int c
|
||||||
layout = new LandscapePageLayout(doc, current_page);
|
layout = new LandscapePageLayout(doc, current_page);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::cout << "exit switch_current_page_layout " << std::endl;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ typedef enum {
|
||||||
|
|
||||||
class BookReader {
|
class BookReader {
|
||||||
public:
|
public:
|
||||||
BookReader(const char *path);
|
BookReader(const char *path, int *result);
|
||||||
~BookReader();
|
~BookReader();
|
||||||
|
|
||||||
bool permStatusBar = false;
|
bool permStatusBar = false;
|
||||||
|
|
|
@ -10,14 +10,21 @@ extern "C" {
|
||||||
#include "BookReader.hpp"
|
#include "BookReader.hpp"
|
||||||
|
|
||||||
void Menu_OpenBook(char *path) {
|
void Menu_OpenBook(char *path) {
|
||||||
BookReader *reader = new BookReader(path);
|
BookReader *reader = NULL;
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
reader = new BookReader(path, &result);
|
||||||
|
|
||||||
|
if(result < 0){
|
||||||
|
std::cout << "Menu_OpenBook: document not loaded" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
/*TouchInfo touchInfo;
|
/*TouchInfo touchInfo;
|
||||||
Touch_Init(&touchInfo);*/
|
Touch_Init(&touchInfo);*/
|
||||||
|
|
||||||
bool helpMenu = false;
|
bool helpMenu = false;
|
||||||
|
|
||||||
while(appletMainLoop()) {
|
while(result >= 0 && appletMainLoop()) {
|
||||||
reader->draw(helpMenu);
|
reader->draw(helpMenu);
|
||||||
|
|
||||||
hidScanInput();
|
hidScanInput();
|
||||||
|
|
Loading…
Reference in New Issue