Compare commits
No commits in common. "81eced26360ee4a928eb2aa75c9fb3b942176694" and "ca544e74fa351ff5dc688932a87a2ec2255c4f7b" have entirely different histories.
81eced2636
...
ca544e74fa
12
Makefile
12
Makefile
@ -18,9 +18,9 @@ DEBUG=0
|
|||||||
INCFLAGS=
|
INCFLAGS=
|
||||||
LDFLAGS=-lsqlite3
|
LDFLAGS=-lsqlite3
|
||||||
DEFS=
|
DEFS=
|
||||||
CFLAGS=$(INCFLAGS) -std=c++20 -Wall -Wextra -Wfatal-errors -Werror
|
CFLAGS=$(INCFLAGS) -std=gnu99 -Wall -Wextra -Wfatal-errors -Werror
|
||||||
HDRS=src/util.hpp src/arg_parse.hpp src/db.hpp src/cmd.hpp
|
HDRS=src/arg_parse.h src/util.h src/db.h src/cmd.h
|
||||||
OBJS=src/main.o src/util.o src/arg_parse.o src/db.o src/cmd.o
|
OBJS=src/main.o src/arg_parse.o src/db.o src/cmd.o
|
||||||
VERSION=1.0
|
VERSION=1.0
|
||||||
|
|
||||||
ifeq ($(PREFIX),)
|
ifeq ($(PREFIX),)
|
||||||
@ -33,11 +33,11 @@ else
|
|||||||
CFLAGS+=-O2 -DNDEBUG
|
CFLAGS+=-O2 -DNDEBUG
|
||||||
endif
|
endif
|
||||||
|
|
||||||
%.o:%.cpp $(HDRS)
|
%.o:%.c $(HDRS)
|
||||||
$(CXX) -c -o $@ $< $(CFLAGS) -DVERSION=\"$(VERSION)\"
|
$(CC) -c -o $@ $< $(CFLAGS) -DVERSION=\"$(VERSION)\"
|
||||||
|
|
||||||
menu-helper: $(OBJS)
|
menu-helper: $(OBJS)
|
||||||
$(CXX) -o $@ $^ $(LDFLAGS)
|
$(CC) -o $@ $^ $(LDFLAGS)
|
||||||
|
|
||||||
.PHONY: clean distclean install
|
.PHONY: clean distclean install
|
||||||
|
|
||||||
|
@ -11,8 +11,8 @@ Ensure the `XDG_DATA_HOME` variable is set (e.g. to `$HOME/.local/share`).
|
|||||||
|
|
||||||
To build the program you will require the following dependencies:
|
To build the program you will require the following dependencies:
|
||||||
|
|
||||||
- A C++ compiler compatible with GNU99 C (preferably GCC).
|
- A C compiler compatible with GNU99 C (preferably GCC).
|
||||||
- SQLite3 C/C++ library
|
- SQLite3 C library
|
||||||
- Make
|
- Make
|
||||||
|
|
||||||
Once installed, compile the project with the `make` command.
|
Once installed, compile the project with the `make` command.
|
||||||
|
@ -15,13 +15,16 @@
|
|||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
#include "arg_parse.hpp"
|
#include "arg_parse.h"
|
||||||
|
|
||||||
enum cmd_id parse_args(const std::string &cmd) {
|
#include "util.h"
|
||||||
for(const auto &command : commands) {
|
#include <string.h>
|
||||||
for(const auto &alias : command.second) {
|
|
||||||
if(cmd == alias)
|
enum cmd_id parse_args(const char *cmd) {
|
||||||
return command.first;
|
for(int i = 0; i < (int)ARRAY_LEN(commands); ++i) {
|
||||||
|
for(int j = 0; j < (int)ARRAY_LEN(commands[i].str); ++j) {
|
||||||
|
if(commands[i].str[j] && strcmp(commands[i].str[j], cmd) == 0)
|
||||||
|
return commands[i].id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -17,10 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <iostream>
|
#include <stdio.h>
|
||||||
#include <string>
|
|
||||||
#include <map>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
enum cmd_id {
|
enum cmd_id {
|
||||||
CMD_UNKNOWN = 0,
|
CMD_UNKNOWN = 0,
|
||||||
@ -29,29 +26,35 @@ enum cmd_id {
|
|||||||
CMD_VERSION,
|
CMD_VERSION,
|
||||||
};
|
};
|
||||||
|
|
||||||
static const std::map<enum cmd_id, std::vector<std::string>> commands = {
|
struct cmd {
|
||||||
|
enum cmd_id id;
|
||||||
|
const char *str[3];
|
||||||
|
};
|
||||||
|
|
||||||
|
static const struct cmd commands[] = {
|
||||||
{ CMD_ADD, {"add", "new"} },
|
{ CMD_ADD, {"add", "new"} },
|
||||||
{ CMD_HELP, {"help", "-h", "--help"} },
|
{ CMD_HELP, {"help", "-h", "--help"} },
|
||||||
{ CMD_VERSION, {"version", "-v", "--version"} },
|
{ CMD_VERSION, {"version", "-v", "--version"} },
|
||||||
};
|
};
|
||||||
|
|
||||||
static inline void print_version(void) {
|
static inline void print_version(void) {
|
||||||
std::cout << "menu-helper v" << VERSION << "\n" << std::endl;
|
printf("menu-helper v%s\n\n", VERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void print_usage(void) {
|
static inline void print_usage(void) {
|
||||||
std::cout << "USAGE: menu-helper <cmd> [options]\n" << std::endl;
|
printf("USAGE: menu-helper <cmd> [options]\n\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void print_help(void) {
|
static inline void print_help(void) {
|
||||||
print_version();
|
print_version();
|
||||||
print_usage();
|
print_usage();
|
||||||
|
|
||||||
std::cout << "COMMANDS:\n"
|
printf("COMMANDS:\n"
|
||||||
"\tadd, new Add a new recipe to the database\n"
|
"\tadd, new Add a new recipe to the database\n"
|
||||||
"\thelp, -h, --help Show this help information.\n"
|
"\thelp, -h, --help Show this help information.\n"
|
||||||
"\tversion, -v, --version Show version information.\n"
|
"\tversion, -v, --version Show version information.\n"
|
||||||
<< std::endl;
|
"\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
enum cmd_id parse_args(const std::string &cmd);
|
|
||||||
|
enum cmd_id parse_args(const char *cmd);
|
100
src/cmd.c
Normal file
100
src/cmd.c
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2024 Nicolás Ortega Froysa <nicolas@ortegas.org>
|
||||||
|
* Nicolás Ortega Froysa <nicolas@ortegas.org>
|
||||||
|
*
|
||||||
|
* This program 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, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#include "cmd.h"
|
||||||
|
#include "db.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
int command_add(void) {
|
||||||
|
char *name = NULL, *description = NULL, *ingredients = NULL, *tags = NULL;
|
||||||
|
size_t name_len, description_len, ingredients_len, tags_len;
|
||||||
|
int recipe_id, ingredient_id, tag_id;
|
||||||
|
|
||||||
|
if(!db_open()) {
|
||||||
|
fprintf(stderr, "Failed to open database. Cannot add new entry.\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Name: ");
|
||||||
|
getline(&name, &name_len, stdin);
|
||||||
|
// eliminate trailing newline
|
||||||
|
name[strlen(name) - 1] = '\0';
|
||||||
|
|
||||||
|
printf("Description: ");
|
||||||
|
getline(&description, &description_len, stdin);
|
||||||
|
// eliminate trailing newline
|
||||||
|
description[strlen(description) - 1] = '\0';
|
||||||
|
|
||||||
|
printf("Ingredients (comma separated): ");
|
||||||
|
getline(&ingredients, &ingredients_len, stdin);
|
||||||
|
// eliminate trailing newline
|
||||||
|
ingredients[strlen(ingredients) - 1] = '\0';
|
||||||
|
|
||||||
|
printf("Tags (comma separated): ");
|
||||||
|
getline(&tags, &tags_len, stdin);
|
||||||
|
// eliminate trailing newline
|
||||||
|
tags[strlen(tags) - 1] = '\0';
|
||||||
|
|
||||||
|
if((recipe_id = db_get_recipe_id(name)) <= 0)
|
||||||
|
recipe_id = db_add_recipe(name, description);
|
||||||
|
free(name);
|
||||||
|
free(description);
|
||||||
|
|
||||||
|
for(char *i = strtok(ingredients, ","); i; i = strtok(NULL,",")) {
|
||||||
|
// remove leading blank spaces
|
||||||
|
while(isblank(i[0]))
|
||||||
|
i += sizeof(char);
|
||||||
|
|
||||||
|
// remove trailing blank spaces
|
||||||
|
size_t i_len = strlen(i);
|
||||||
|
while(isblank(i[i_len - 1])) {
|
||||||
|
i[i_len - 1] = '\0';
|
||||||
|
--i_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
if((ingredient_id = db_get_ingredient_id(i)) <= 0)
|
||||||
|
ingredient_id = db_add_ingredient(i);
|
||||||
|
db_conn_recipe_ingredient(recipe_id, ingredient_id);
|
||||||
|
}
|
||||||
|
free(ingredients);
|
||||||
|
|
||||||
|
for(char *i = strtok(tags, ","); i; i = strtok(NULL, ",")) {
|
||||||
|
// remove leading blank spaces
|
||||||
|
while(isblank(i[0]))
|
||||||
|
i += sizeof(char);
|
||||||
|
|
||||||
|
// remove trailing blank spaces
|
||||||
|
size_t i_len = strlen(i);
|
||||||
|
while(isblank(i[i_len - 1])) {
|
||||||
|
i[i_len - 1] = '\0';
|
||||||
|
--i_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
if((tag_id = db_get_tag_id(i)) <= 0)
|
||||||
|
tag_id = db_add_tag(i);
|
||||||
|
db_conn_recipe_tag(recipe_id, tag_id);
|
||||||
|
}
|
||||||
|
free(tags);
|
||||||
|
|
||||||
|
db_close();
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
69
src/cmd.cpp
69
src/cmd.cpp
@ -1,69 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024 Nicolás Ortega Froysa <nicolas@ortegas.org>
|
|
||||||
* Nicolás Ortega Froysa <nicolas@ortegas.org>
|
|
||||||
*
|
|
||||||
* This program 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, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
#include "cmd.hpp"
|
|
||||||
#include "db.hpp"
|
|
||||||
#include "util.hpp"
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
int command_add(void) {
|
|
||||||
std::string name, description, ingredients, tags;
|
|
||||||
int recipe_id, ingredient_id, tag_id;
|
|
||||||
|
|
||||||
if(not db_open()) {
|
|
||||||
std::cerr << "Failed to open database. Cannot add new entry." << std::endl;
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << "Name: ";
|
|
||||||
getline(std::cin, name);
|
|
||||||
|
|
||||||
std::cout << "Description: ";
|
|
||||||
getline(std::cin, description);
|
|
||||||
|
|
||||||
std::cout << "Ingredients (comma separated): ";
|
|
||||||
getline(std::cin, ingredients);
|
|
||||||
|
|
||||||
std::cout << "Tags (comma separated): ";
|
|
||||||
getline(std::cin, tags);
|
|
||||||
|
|
||||||
if((recipe_id = db_get_recipe_id(name)) <= 0)
|
|
||||||
recipe_id = db_add_recipe(name, description);
|
|
||||||
|
|
||||||
for(auto &ingredient : split(ingredients, ",")) {
|
|
||||||
trim(ingredient);
|
|
||||||
|
|
||||||
if((ingredient_id = db_get_ingredient_id(ingredient)) <= 0)
|
|
||||||
ingredient_id = db_add_ingredient(ingredient);
|
|
||||||
db_conn_recipe_ingredient(recipe_id, ingredient_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
for(auto &tag : split(tags, ",")) {
|
|
||||||
trim(tag);
|
|
||||||
|
|
||||||
if((tag_id = db_get_tag_id(tag)) <= 0)
|
|
||||||
tag_id = db_add_tag(tag);
|
|
||||||
db_conn_recipe_tag(recipe_id, tag_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
db_close();
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
|
||||||
}
|
|
221
src/db.c
Normal file
221
src/db.c
Normal file
@ -0,0 +1,221 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2024 Nicolás Ortega Froysa <nicolas@ortegas.org>
|
||||||
|
* Nicolás Ortega Froysa <nicolas@ortegas.org>
|
||||||
|
*
|
||||||
|
* This program 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, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
#include "db.h"
|
||||||
|
|
||||||
|
#include <sqlite3.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
static sqlite3 *db = NULL;
|
||||||
|
static const int db_version = 1;
|
||||||
|
|
||||||
|
int db_open(void) {
|
||||||
|
const char *xdg_data_home;
|
||||||
|
char *db_path;
|
||||||
|
int new_db = 0;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
if(!(xdg_data_home = getenv("XDG_DATA_HOME"))) {
|
||||||
|
printf("Cannot find environment variable XDG_DATA_HOME. Please define it before continuing. E.g.:\nexport XDG_DATA_HOME=\"$HOME/.local/share\"\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
db_path = malloc(strlen(xdg_data_home) + strlen("/menu-helper") + strlen("/recipes.db") + 1);
|
||||||
|
strcpy(db_path, xdg_data_home);
|
||||||
|
strcat(db_path, "/menu-helper");
|
||||||
|
|
||||||
|
if(access(db_path, F_OK) != 0)
|
||||||
|
mkdir(db_path, 0700);
|
||||||
|
|
||||||
|
strcat(db_path, "/recipes.db");
|
||||||
|
|
||||||
|
if(access(db_path, F_OK) != 0) {
|
||||||
|
printf("Creating database: %s\n", db_path);
|
||||||
|
new_db = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
rc = sqlite3_open(db_path, &db);
|
||||||
|
free(db_path);
|
||||||
|
|
||||||
|
if(rc == SQLITE_OK && new_db) {
|
||||||
|
char insert_version_stmt[64];
|
||||||
|
sqlite3_exec(db, "CREATE TABLE db_version(version INTEGER UNIQUE NOT NULL);", NULL, NULL, NULL);
|
||||||
|
snprintf(insert_version_stmt, 64, "INSERT INTO db_version VALUES(%d);", db_version);
|
||||||
|
sqlite3_exec(db, insert_version_stmt, NULL, NULL, NULL);
|
||||||
|
sqlite3_exec(db, "CREATE TABLE tags(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE);", NULL, NULL, NULL);
|
||||||
|
sqlite3_exec(db, "CREATE TABLE ingredients(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE);", NULL, NULL, NULL);
|
||||||
|
sqlite3_exec(db, "CREATE TABLE recipes(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE, description STRING);", NULL, NULL, NULL);
|
||||||
|
sqlite3_exec(db, "CREATE TABLE recipe_tag(recipe_id INTEGER REFERENCES recipes(id) ON DELETE CASCADE, tag_id INTEGER REFERENCES tags(id) ON DELETE CASCADE);", NULL, NULL, NULL);
|
||||||
|
sqlite3_exec(db, "CREATE TABLE recipe_ingredient(recipe_id INTEGER REFERENCES recipes(id) ON DELETE CASCADE, ingredient_id INTEGER REFERENCES ingredients(id) ON DELETE CASCADE);", NULL, NULL, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rc == SQLITE_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
void db_close(void) {
|
||||||
|
if(!db)
|
||||||
|
return;
|
||||||
|
|
||||||
|
sqlite3_close(db);
|
||||||
|
}
|
||||||
|
|
||||||
|
int query_id_cb(void *recipe_id_var, int col_num, char **col_data, char **col_name) {
|
||||||
|
int *recipe_id_ptr = (int*)recipe_id_var;
|
||||||
|
int ret = 1;
|
||||||
|
|
||||||
|
for(int i = 0; i < col_num; ++i) {
|
||||||
|
if(strcmp(col_name[i], "id") == 0) {
|
||||||
|
*recipe_id_ptr = atoi(col_data[i]);
|
||||||
|
ret = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int table_get_id_by_name(const char *table, const char *name) {
|
||||||
|
const char *sel_query_fmt = "SELECT id FROM %s WHERE lower(name)=lower('%s');";
|
||||||
|
char *sel_query;
|
||||||
|
int id = 0;
|
||||||
|
|
||||||
|
sel_query = malloc(strlen(table) + strlen(name) + strlen(sel_query_fmt) + 1);
|
||||||
|
sprintf(sel_query, sel_query_fmt, table, name);
|
||||||
|
if(sqlite3_exec(db, sel_query, query_id_cb, &id, NULL) != SQLITE_OK) {
|
||||||
|
free(sel_query);
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(sel_query);
|
||||||
|
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
int db_add_recipe(const char *name, const char *description) {
|
||||||
|
const char *add_query_fmt = "INSERT INTO recipes(name,description) VALUES('%s','%s');";
|
||||||
|
char *add_query;
|
||||||
|
|
||||||
|
if(!db)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
add_query = malloc(strlen(name) + strlen(description) + strlen(add_query_fmt) + 1);
|
||||||
|
sprintf(add_query, add_query_fmt, name, description);
|
||||||
|
if(sqlite3_exec(db, add_query, NULL, NULL, NULL) != SQLITE_OK) {
|
||||||
|
free(add_query);
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
free(add_query);
|
||||||
|
|
||||||
|
return db_get_recipe_id(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
int db_get_recipe_id(const char *name) {
|
||||||
|
if(!db)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return table_get_id_by_name("recipes", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
int db_add_ingredient(const char *name) {
|
||||||
|
const char *add_query_fmt = "INSERT INTO ingredients(name) VALUES(lower('%s'));";
|
||||||
|
char *add_query;
|
||||||
|
|
||||||
|
if(!db)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
add_query = malloc(strlen(name) + strlen(add_query_fmt) + 1);
|
||||||
|
sprintf(add_query, add_query_fmt, name);
|
||||||
|
if(sqlite3_exec(db, add_query, NULL, NULL, NULL) != SQLITE_OK) {
|
||||||
|
free(add_query);
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
free(add_query);
|
||||||
|
|
||||||
|
return db_get_ingredient_id(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
int db_get_ingredient_id(const char *name) {
|
||||||
|
if(!db)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return table_get_id_by_name("ingredients", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
int db_add_tag(const char *name) {
|
||||||
|
const char *add_query_fmt = "INSERT INTO tags(name) VALUES('%s');";
|
||||||
|
char *add_query;
|
||||||
|
|
||||||
|
if(!db)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
add_query = malloc(strlen(name) + strlen(add_query_fmt) + 1);
|
||||||
|
sprintf(add_query, add_query_fmt, name);
|
||||||
|
if(sqlite3_exec(db, add_query, NULL, NULL, NULL) != SQLITE_OK) {
|
||||||
|
free(add_query);
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
free(add_query);
|
||||||
|
|
||||||
|
return db_get_tag_id(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
int db_get_tag_id(const char *name) {
|
||||||
|
if(!db)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return table_get_id_by_name("tags", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
int db_conn_recipe_ingredient(int recipe_id, int ingredient_id) {
|
||||||
|
const char *add_conn_fmt = "INSERT INTO recipe_ingredient(recipe_id, ingredient_id) VALUES(%d,%d);";
|
||||||
|
char *add_conn_query;
|
||||||
|
|
||||||
|
if(!db)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
add_conn_query = malloc(strlen(add_conn_fmt) + (recipe_id % 10) + (ingredient_id % 10));
|
||||||
|
sprintf(add_conn_query, add_conn_fmt, recipe_id, ingredient_id);
|
||||||
|
if(sqlite3_exec(db, add_conn_query, NULL, NULL, NULL) != SQLITE_OK) {
|
||||||
|
free(add_conn_query);
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
free(add_conn_query);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int db_conn_recipe_tag(int recipe_id, int tag_id) {
|
||||||
|
const char *add_conn_fmt = "INSERT INTO recipe_tag(recipe_id, tag_id) VALUES(%d,%d);";
|
||||||
|
char *add_conn_query;
|
||||||
|
|
||||||
|
if(!db)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
add_conn_query = malloc(strlen(add_conn_fmt) + (recipe_id % 10) + (tag_id % 10));
|
||||||
|
sprintf(add_conn_query, add_conn_fmt, recipe_id, tag_id);
|
||||||
|
if(sqlite3_exec(db, add_conn_query, NULL, NULL, NULL) != SQLITE_OK) {
|
||||||
|
free(add_conn_query);
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
free(add_conn_query);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
177
src/db.cpp
177
src/db.cpp
@ -1,177 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024 Nicolás Ortega Froysa <nicolas@ortegas.org>
|
|
||||||
* Nicolás Ortega Froysa <nicolas@ortegas.org>
|
|
||||||
*
|
|
||||||
* This program 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, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
#include "db.hpp"
|
|
||||||
|
|
||||||
#include <sqlite3.h>
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <iostream>
|
|
||||||
#include <filesystem>
|
|
||||||
#include <format>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#define DB_VERSION 1
|
|
||||||
static sqlite3 *db = NULL;
|
|
||||||
|
|
||||||
int db_open(void) {
|
|
||||||
std::string xdg_data_home;
|
|
||||||
std::string db_path;
|
|
||||||
bool new_db = false;
|
|
||||||
int rc;
|
|
||||||
|
|
||||||
if((xdg_data_home = std::getenv("XDG_DATA_HOME")).empty()) {
|
|
||||||
std::cerr << "Cannot find environment variable XDG_DATA_HOME. Please define it before continuing. E.g.:\n"
|
|
||||||
"export XDG_DATA_HOME=\"$HOME/.local/share\"" << std::endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
db_path = xdg_data_home + "/menu-helper";
|
|
||||||
|
|
||||||
if(not std::filesystem::exists(db_path))
|
|
||||||
std::filesystem::create_directories(db_path);
|
|
||||||
|
|
||||||
db_path += "/recipes.db";
|
|
||||||
|
|
||||||
//if(access(db_path, F_OK) != 0) {
|
|
||||||
if(not std::filesystem::exists(db_path)) {
|
|
||||||
std::cout << "Creating database: " << db_path << std::endl;
|
|
||||||
new_db = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = sqlite3_open(db_path.c_str(), &db);
|
|
||||||
|
|
||||||
if(rc == SQLITE_OK && new_db) {
|
|
||||||
sqlite3_exec(db, "CREATE TABLE db_version(version INTEGER UNIQUE NOT NULL);", NULL, NULL, NULL);
|
|
||||||
//snprintf(insert_version_stmt, 64, "INSERT INTO db_version VALUES(%d);", DB_VERSION);
|
|
||||||
sqlite3_exec(db, std::format("INSERT INTO db_version VALUES({});", DB_VERSION).c_str(), NULL, NULL, NULL);
|
|
||||||
sqlite3_exec(db, "CREATE TABLE tags(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE);", NULL, NULL, NULL);
|
|
||||||
sqlite3_exec(db, "CREATE TABLE ingredients(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE);", NULL, NULL, NULL);
|
|
||||||
sqlite3_exec(db, "CREATE TABLE recipes(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE, description STRING);", NULL, NULL, NULL);
|
|
||||||
sqlite3_exec(db, "CREATE TABLE recipe_tag(recipe_id INTEGER REFERENCES recipes(id) ON DELETE CASCADE, tag_id INTEGER REFERENCES tags(id) ON DELETE CASCADE);", NULL, NULL, NULL);
|
|
||||||
sqlite3_exec(db, "CREATE TABLE recipe_ingredient(recipe_id INTEGER REFERENCES recipes(id) ON DELETE CASCADE, ingredient_id INTEGER REFERENCES ingredients(id) ON DELETE CASCADE);", NULL, NULL, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
return rc == SQLITE_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
void db_close(void) {
|
|
||||||
if(not db)
|
|
||||||
return;
|
|
||||||
|
|
||||||
sqlite3_close(db);
|
|
||||||
}
|
|
||||||
|
|
||||||
int query_id_cb(void *recipe_id_var, int col_num, char **col_data, char **col_name) {
|
|
||||||
int *recipe_id_ptr = (int*)recipe_id_var;
|
|
||||||
int ret = 1;
|
|
||||||
|
|
||||||
for(int i = 0; i < col_num; ++i) {
|
|
||||||
if(std::string(col_name[i]) == "id") {
|
|
||||||
*recipe_id_ptr = std::atoi(col_data[i]);
|
|
||||||
ret = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int table_get_id_by_name(const std::string &table, const std::string &name) {
|
|
||||||
int id = 0;
|
|
||||||
|
|
||||||
if(sqlite3_exec(db, std::format("SELECT id FROM {} WHERE lower(name)=lower('{}');", table, name).c_str(),
|
|
||||||
query_id_cb, &id, NULL) != SQLITE_OK)
|
|
||||||
return -2;
|
|
||||||
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
int db_add_recipe(const std::string &name, const std::string &description) {
|
|
||||||
if(not db)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if(sqlite3_exec(db, std::format("INSERT INTO recipes(name,description) VALUES('{}','{}');", name, description).c_str(),
|
|
||||||
NULL, NULL, NULL) != SQLITE_OK)
|
|
||||||
return -2;
|
|
||||||
|
|
||||||
return db_get_recipe_id(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
int db_get_recipe_id(const std::string &name) {
|
|
||||||
if(not db)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return table_get_id_by_name("recipes", name);
|
|
||||||
}
|
|
||||||
|
|
||||||
int db_add_ingredient(const std::string &name) {
|
|
||||||
if(not db)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if(sqlite3_exec(db, std::format("INSERT INTO ingredients(name) VALUES(lower('{}'));", name).c_str(),
|
|
||||||
NULL, NULL, NULL) != SQLITE_OK)
|
|
||||||
return -2;
|
|
||||||
|
|
||||||
return db_get_ingredient_id(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
int db_get_ingredient_id(const std::string &name) {
|
|
||||||
if(not db)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return table_get_id_by_name("ingredients", name);
|
|
||||||
}
|
|
||||||
|
|
||||||
int db_add_tag(const std::string &name) {
|
|
||||||
if(not db)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if(sqlite3_exec(db, std::format("INSERT INTO tags(name) VALUES('{}');", name).c_str(),
|
|
||||||
NULL, NULL, NULL) != SQLITE_OK)
|
|
||||||
return -2;
|
|
||||||
|
|
||||||
return db_get_tag_id(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
int db_get_tag_id(const std::string &name) {
|
|
||||||
if(not db)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
return table_get_id_by_name("tags", name);
|
|
||||||
}
|
|
||||||
|
|
||||||
int db_conn_recipe_ingredient(int recipe_id, int ingredient_id) {
|
|
||||||
if(not db)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if(sqlite3_exec(db, std::format("INSERT INTO recipe_ingredient(recipe_id, ingredient_id) VALUES({},{});", recipe_id, ingredient_id).c_str(),
|
|
||||||
NULL, NULL, NULL) != SQLITE_OK)
|
|
||||||
return -2;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int db_conn_recipe_tag(int recipe_id, int tag_id) {
|
|
||||||
if(not db)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if(sqlite3_exec(db, std::format("INSERT INTO recipe_tag(recipe_id, tag_id) VALUES({},{});", recipe_id, tag_id).c_str(),
|
|
||||||
NULL, NULL, NULL) != SQLITE_OK)
|
|
||||||
return -2;
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
@ -17,8 +17,6 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
int db_open(void);
|
int db_open(void);
|
||||||
void db_close(void);
|
void db_close(void);
|
||||||
|
|
||||||
@ -30,9 +28,9 @@ void db_close(void);
|
|||||||
*
|
*
|
||||||
* @return ID of newly created recipe, -1 if DB isn't open, -2 on other failure.
|
* @return ID of newly created recipe, -1 if DB isn't open, -2 on other failure.
|
||||||
*/
|
*/
|
||||||
int db_add_recipe(const std::string &name, const std::string &description);
|
int db_add_recipe(const char *name, const char *description);
|
||||||
int db_get_recipe_id(const std::string &name);
|
int db_get_recipe_id(const char *name);
|
||||||
static inline int db_recipe_exists(const std::string &name) {
|
static inline int db_recipe_exists(const char *name) {
|
||||||
return (db_get_recipe_id(name) > 0);
|
return (db_get_recipe_id(name) > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,9 +41,9 @@ static inline int db_recipe_exists(const std::string &name) {
|
|||||||
*
|
*
|
||||||
* @return ID of newly created ingredient, -1 if DB isn't open, -2 on other failure.
|
* @return ID of newly created ingredient, -1 if DB isn't open, -2 on other failure.
|
||||||
*/
|
*/
|
||||||
int db_add_ingredient(const std::string &name);
|
int db_add_ingredient(const char *name);
|
||||||
int db_get_ingredient_id(const std::string &name);
|
int db_get_ingredient_id(const char *name);
|
||||||
static inline int db_ingredient_exists(const std::string &name) {
|
static inline int db_ingredient_exists(const char *name) {
|
||||||
return (db_get_ingredient_id(name) > 0);
|
return (db_get_ingredient_id(name) > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,9 +54,9 @@ static inline int db_ingredient_exists(const std::string &name) {
|
|||||||
*
|
*
|
||||||
* @return ID of newly created tag, -1 if DB isn't open, -2 on other failure.
|
* @return ID of newly created tag, -1 if DB isn't open, -2 on other failure.
|
||||||
*/
|
*/
|
||||||
int db_add_tag(const std::string &name);
|
int db_add_tag(const char *name);
|
||||||
int db_get_tag_id(const std::string &name);
|
int db_get_tag_id(const char *name);
|
||||||
static inline int db_tag_exists(const std::string &name) {
|
static inline int db_tag_exists(const char *name) {
|
||||||
return (db_get_tag_id(name) > 0);
|
return (db_get_tag_id(name) > 0);
|
||||||
}
|
}
|
||||||
|
|
@ -15,18 +15,17 @@
|
|||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include "arg_parse.h"
|
||||||
#include <cstdlib>
|
#include "cmd.h"
|
||||||
|
|
||||||
#include "arg_parse.hpp"
|
|
||||||
#include "cmd.hpp"
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
enum cmd_id id;
|
enum cmd_id id;
|
||||||
|
|
||||||
if(argc < 2) {
|
if(argc < 2) {
|
||||||
std::cerr << "Invalid number of arguments. Use 'help' sub-command." << std::endl;
|
fprintf(stderr, "Invalid number of arguments. Use 'help' sub-command.\n");
|
||||||
print_usage();
|
print_usage();
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
@ -44,7 +43,7 @@ int main(int argc, char *argv[]) {
|
|||||||
print_version();
|
print_version();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
std::cerr << "No such command '" << argv[1] << "'. Use 'help' sub-command." << std::endl;
|
fprintf(stderr, "No such command '%s'. Use 'help' sub-command.\n", argv[1]);
|
||||||
print_usage();
|
print_usage();
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
45
src/util.cpp
45
src/util.cpp
@ -1,45 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024 Nicolás Ortega Froysa <nicolas@ortegas.org>
|
|
||||||
* Nicolás Ortega Froysa <nicolas@ortegas.org>
|
|
||||||
*
|
|
||||||
* This program 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, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
#include "util.hpp"
|
|
||||||
#include <algorithm>
|
|
||||||
#include <cctype>
|
|
||||||
|
|
||||||
std::vector<std::string> split(std::string str, const std::string &delim) {
|
|
||||||
std::vector<std::string> result;
|
|
||||||
std::string substr;
|
|
||||||
size_t pos = 0;
|
|
||||||
|
|
||||||
while((pos = str.find(delim)) not_eq std::string::npos) {
|
|
||||||
substr = str.substr(0, pos);
|
|
||||||
result.push_back(substr);
|
|
||||||
str.erase(0, pos + delim.size());
|
|
||||||
}
|
|
||||||
result.push_back(str);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void trim(std::string &str) {
|
|
||||||
str.erase(str.begin(),
|
|
||||||
std::find_if(str.begin(), str.end(), [](char c) {
|
|
||||||
return not std::isspace(c);
|
|
||||||
}));
|
|
||||||
str.erase(std::find_if(str.rbegin(), str.rend(), [](char c) {
|
|
||||||
return not std::isspace(c);
|
|
||||||
}).base(), str.end());
|
|
||||||
}
|
|
@ -17,8 +17,4 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <vector>
|
#define ARRAY_LEN(arr) (sizeof(arr) / sizeof(arr[0]))
|
||||||
#include <string>
|
|
||||||
|
|
||||||
std::vector<std::string> split(std::string str, const std::string &delim);
|
|
||||||
void trim(std::string &str);
|
|
Loading…
Reference in New Issue
Block a user