131 lines
3.5 KiB
C++
131 lines
3.5 KiB
C++
// Copyright 2021, Paul Mosier
|
|
//
|
|
// This file is part of Scriptura, a ncurses-based Bible study
|
|
// software for the libsword backend.
|
|
//
|
|
// Scriptura is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, under version 2 of the License.
|
|
//
|
|
// Scriptura is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Scriptura. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
// free.cpp - free functions / globals
|
|
|
|
#include <ncurses.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#include <wchar.h>
|
|
|
|
#include <swbuf.h>
|
|
|
|
#include "free.h"
|
|
|
|
char CLITEXT[] =
|
|
"Scriptura - a ncurses-based Bible study program.\n"
|
|
" -h / --help - this help message\n"
|
|
" -c <file> - alternate config file to load\n"
|
|
"\0";
|
|
|
|
char HELPTEXT[] =
|
|
"Scriptura\n\n"
|
|
"Main key bindings:\n"
|
|
" ? this help message\n"
|
|
" q quit\n\n"
|
|
" ESC close floating window\n"
|
|
" TAB change active window\n"
|
|
" g go to a specific passage\n"
|
|
" o open a new module in the active window\n"
|
|
" s search\n"
|
|
" arrow up/dn scroll up/down one line\n"
|
|
" page up/dn scroll up/down one page\n"
|
|
"\n"
|
|
"Pages:\n"
|
|
" 0-4 go to the numbered page\n"
|
|
" p create new page\n"
|
|
" d delete page\n"
|
|
"Formatting changes:\n"
|
|
" (takes effect at next search/go to, and depends on loaded module)\n"
|
|
" f toggle footnotes\n"
|
|
" n toggle non-textual words\n"
|
|
" r toggle red letters\n"
|
|
" t toggle Strong's numbers\n"
|
|
" w toggle raw text (straight from the module)\n"
|
|
"\n(Press any key to return to the main screen)\n"
|
|
"\0";
|
|
|
|
int DEFSEARCH = 1;
|
|
int DEFKEY = 0;
|
|
|
|
void wrapup(int exitval, const char* str) {
|
|
endwin();
|
|
if (str != NULL) perror(str);
|
|
exit(exitval);
|
|
}
|
|
|
|
void trim(char* str) {
|
|
size_t len;
|
|
len = strlen(str);
|
|
while (len-- && isspace(str[len])) str[len] = 0;
|
|
}
|
|
|
|
//int strmatch(wchar_t text[], const wchar_t* key, int matchnow) {
|
|
int strmatch(char text[], const char* key, int matchnow) {
|
|
int retval = -1;
|
|
//int length = wcslen(key);
|
|
int length = strlen(key);
|
|
//wchar_t* substr = (wchar_t*) malloc((length + 1) * sizeof(wchar_t));
|
|
char* substr = (char*) malloc((length + 1) * sizeof(char*));
|
|
//wmemset(substr, L'\0', length + 1);
|
|
memset(substr, '\0', length + 1);
|
|
if (! substr) wrapup(1, "Error allocating memory in strmatch.\n");
|
|
|
|
int i = 0;
|
|
//int textlen = wcslen(text);
|
|
int textlen = strlen(text);
|
|
while ((text[i] != '\0') && (i < (textlen - length))) {
|
|
//wmemcpy(substr, &text[i], length);
|
|
memcpy(substr, &text[i], length);
|
|
//if (! wcscmp(substr, key)) {
|
|
if (! strcmp(substr, key)) {
|
|
retval = i;
|
|
break;
|
|
}
|
|
if (matchnow) break;
|
|
i++;
|
|
}
|
|
|
|
free(substr);
|
|
return retval;
|
|
}
|
|
|
|
int parseConf(sword::SWBuf buf) {
|
|
return (buf.length() == 1 ? (int) buf.c_str()[0] - 48 : 0);
|
|
}
|
|
|
|
starray stappend(starray arr, const char* newstr) {
|
|
arr.length++;
|
|
if (arr.length > 1) {
|
|
arr.strings = (const char**)
|
|
realloc(arr.strings, arr.length * sizeof(char*));
|
|
}
|
|
|
|
if (! arr.strings) wrapup(1, "Error resizing memory in stappend.\n");
|
|
|
|
(arr.strings)[arr.length - 1] = newstr;
|
|
return arr;
|
|
}
|
|
|
|
starray stinit(starray arr) {
|
|
arr.length = 0;
|
|
arr.strings = (const char**) malloc(sizeof(char*));
|
|
return arr;
|
|
}
|
|
|