Browse Source

Use heb12_config

master
Daniel 2 weeks ago
parent
commit
74d8d34154
5 changed files with 145 additions and 42 deletions
  1. +11
    -15
      Makefile
  2. +33
    -26
      app.c
  3. +1
    -1
      biblec
  4. +84
    -0
      config.c
  5. +16
    -0
      config.h

+ 11
- 15
Makefile View File

@@ -1,12 +1,9 @@
DIR ?= /home/daniel/Documents/cli
DEF_REF ?= Ps 1 1
DEF_BIB ?= web

CC := gcc
CFLAGS := -Wall -D DIR='"bibles/$(DEF_BIB).i"' -D DEF_REF='"$(DEF_REF)"'
DIR := /home/dan/.local/share/heb12/
CC := tcc
CFLAGS := -Wall

default: compile
test: compile demo
demo: compile test

msdos: msdosconfig compile
msdosconfig:
@@ -16,19 +13,18 @@ help:
@echo "For now, default settings are compiled with the program."
@echo "make Default, compile heb12cli"
@echo "make demo Runs John 3 16"
@echo "make bibles Install bibles"
@echo "make setup Install bibles"
@echo "make path Add heb12 to ~/.bashrc"

compile:
@$(CC) $(CFLAGS) biblec/biblec.c fbrp/fbrp.c app.c -o heb12
@$(CC) $(CFLAGS) biblec/biblec.c fbrp/fbrp.c config.c app.c -o heb12

demo:
@./heb12 -r "John 3 16"
test:
@./heb12 -l -r "John 3 16"

bibles:
@mkdir bibles
@wget http://api.heb12.com/translations/biblec/web.i -P bibles/
@wget http://api.heb12.com/translations/biblec/web.t -P bibles/
setup:
@wget http://api.heb12.com/translations/biblec/web.i -P $(DIR)
@wget http://api.heb12.com/translations/biblec/web.t -P $(DIR)

path:
echo "export PATH=\$PATH:$PWD >> ~/.bashrc"

+ 33
- 26
app.c View File

@@ -5,17 +5,13 @@

#include "fbrp/fbrp.h"
#include "biblec/main.h"
#include "config.h"

// Just in case it didn't go through
#ifndef DIR
#define DIR "bibles/web.i"
#define DEF_REF "John 3 16"
#endif
// Development constants
char *defaultIndex = "bibles/web.i";
char *defaultReference = "John 3 16";

char *defaultIndex = DIR;
char *defaultReference = DEF_REF;

struct Translation translation;
struct Biblec_translation translation;

// Simple function to print strings with text break
void printBreak(char *string, int limit) {
@@ -65,8 +61,8 @@ int printVerses(char *input, bool fancyPrint) {
verseEnd = ref.verse[r].r[1];
}

struct Reader reader;
int tryReader = reader_new(
struct Biblec_reader reader;
int tryReader = biblec_new(
&reader,
&translation,
ref.book,
@@ -80,8 +76,7 @@ int printVerses(char *input, bool fancyPrint) {
return -1;
}

while (1) {
if (reader_next(&reader)) {break;}
while (!biblec_next(&reader)) {
if (fancyPrint) {
putchar('\n');
printf("%d. ", verseStart + reader.linesRead - 1);
@@ -103,21 +98,33 @@ int printVerses(char *input, bool fancyPrint) {
}

int main(int argc, char *argv[]) {
char buf[256];
heb12_data_dir("web.i", sizeof(buf), buf);
defaultIndex = buf;
// Parse if the user wants a command line interface
if (argc != 1) {
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
if (argv[i][1] == 't') {
i++;
defaultIndex = argv[i];
} else if (argv[i][1] == 'r') {
i++;
defaultReference = argv[i];
}
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 't':
i++;
heb12_data_dir(argv[i], sizeof(buf), buf);
strcat(buf, ".i");
break;
case 'r':
i++;
defaultReference = argv[i];
break;
case 'l':
defaultIndex = "bibles/web.i";
break;
default:
printf("Invalid option %s\n", argv[i]);
return 0;
}
}

int tryFile = parseIndexFile(
int tryFile = biblec_parse(
&translation,
defaultIndex
);
@@ -130,9 +137,9 @@ int main(int argc, char *argv[]) {
return 0;
}

puts("@ Heb12Lite");
puts("@ Heb12 CLI");

int tryFile = parseIndexFile(
int tryFile = biblec_parse(
&translation,
defaultIndex
);


+ 1
- 1
biblec

@@ -1 +1 @@
Subproject commit 17d60735e1ff1906161d29af98615bd63aa3779d
Subproject commit 89a6a373018a0b37baa5910d562c4053bb5c3ef2

+ 84
- 0
config.c View File

@@ -0,0 +1,84 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __WIN32
#define SEPARATOR "\\"
#else
#define SEPARATOR "/"
#endif

// the subdirectory for configuration
#define NAME "heb12"

// TODO: consider a more generic find-and-replace scheme to clean up the code
// and merging the body of heb12_config_dir and heb12_data_dir, and making the
// directories more directly configurable with macros

// Return the configuration directory with an optional subdirectory or file (can
// accept NULL or "" to ignore)
char *heb12_config_dir(const char *subdir, size_t buf_len, char *buf)
{
#ifdef HEB12_CONFIG_DIR
if (HEB12_DATA_DIR[0] != '~') {
strncpy(buf, HEB12_DATA_DIR, buf_len);
} else {
strncpy(buf, getenv("HOME"), buf_len);
strncat(buf, HEB12_DATA_DIR, buf_len);
}
#else
#ifdef _WIN32
strncpy(buf, getenv("\%ProgramFiles\%"), buf_len);
strncat(buf, "\\", buf_len);
strncat(buf, NAME, buf_len);
#elif __unix__
strncpy(buf, getenv("HOME"), buf_len);
strncat(buf, "/.config/", buf_len);
strncat(buf, NAME, buf_len);
#else
#error "Unsupported OS"
#endif
#endif
if (subdir != NULL && strcmp(subdir, "") != 0) {
strncat(buf, SEPARATOR, buf_len);
strncat(buf, subdir, buf_len);
}
return buf;
}

// Return the data directory (for Bibles and similar) with an optional
// subdirectory or file (can accept NULL or "" to ignore)
char *heb12_data_dir(const char *subdir, size_t buf_len, char *buf)
{
#ifdef HEB12_DATA_DIR
if (HEB12_DATA_DIR[0] != '~') {
strncpy(buf, HEB12_DATA_DIR, buf_len);
} else {
strncpy(buf, getenv("HOME"), buf_len);
strncat(buf, HEB12_DATA_DIR, buf_len);
}
#else
#ifdef _WIN32
// should be C:\User\Name\AppData\Roaming
strncpy(buf, getenv("\%APPDATA\%"), buf_len);
strncat(buf, "\\", buf_len);
strncat(buf, NAME, buf_len);
#elif __unix__
char *dir;
if ((dir = getenv("XDG_DATA_HOME")) == NULL) {
strncpy(buf, getenv("HOME"), buf_len);
strncat(buf, "/.local/share/", buf_len);
strncat(buf, NAME, buf_len);
} else {
strncpy(buf, dir, buf_len);
};
#else
#error "Unsupported OS"
#endif
#endif
if (subdir != NULL && strcmp(subdir, "") != 0) {
strncat(buf, SEPARATOR, buf_len);
strncat(buf, subdir, buf_len);
}
return buf;
}

+ 16
- 0
config.h View File

@@ -0,0 +1,16 @@
#ifndef HEB12_CONFIG_H_
#define HEB12_CONFIG_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stddef.h>

char *heb12_config_dir(const char *, size_t, char *);
char *heb12_data_dir(const char *, size_t, char *);

#ifdef __cplusplus
}
#endif
#endif

Loading…
Cancel
Save