Finish porting to C++.

This commit is contained in:
Nicolás A. Ortega Froysa 2024-10-09 21:12:16 +02:00
parent b5094d68e9
commit 81eced2636
6 changed files with 161 additions and 165 deletions

View File

@ -18,9 +18,9 @@ DEBUG=0
INCFLAGS= INCFLAGS=
LDFLAGS=-lsqlite3 LDFLAGS=-lsqlite3
DEFS= DEFS=
CFLAGS=$(INCFLAGS) -std=c++11 -Wall -Wextra -Wfatal-errors -Werror CFLAGS=$(INCFLAGS) -std=c++20 -Wall -Wextra -Wfatal-errors -Werror
HDRS=src/arg_parse.hpp src/db.hpp src/cmd.hpp HDRS=src/util.hpp src/arg_parse.hpp src/db.hpp src/cmd.hpp
OBJS=src/main.o src/arg_parse.o src/db.o src/cmd.o OBJS=src/main.o src/util.o src/arg_parse.o src/db.o src/cmd.o
VERSION=1.0 VERSION=1.0
ifeq ($(PREFIX),) ifeq ($(PREFIX),)

View File

@ -17,84 +17,53 @@
*/ */
#include "cmd.hpp" #include "cmd.hpp"
#include "db.hpp" #include "db.hpp"
#include "util.hpp"
#include <stdio.h> #include <iostream>
#include <stdlib.h> #include <cstdlib>
#include <string.h> #include <string>
#include <ctype.h>
int command_add(void) { int command_add(void) {
char *name = NULL, *description = NULL, *ingredients = NULL, *tags = NULL; std::string name, description, ingredients, tags;
size_t name_len, description_len, ingredients_len, tags_len;
int recipe_id, ingredient_id, tag_id; int recipe_id, ingredient_id, tag_id;
if(!db_open()) { if(not db_open()) {
fprintf(stderr, "Failed to open database. Cannot add new entry.\n"); std::cerr << "Failed to open database. Cannot add new entry." << std::endl;
return 0; return EXIT_FAILURE;
} }
printf("Name: "); std::cout << "Name: ";
getline(&name, &name_len, stdin); getline(std::cin, name);
// eliminate trailing newline
name[strlen(name) - 1] = '\0';
printf("Description: "); std::cout << "Description: ";
getline(&description, &description_len, stdin); getline(std::cin, description);
// eliminate trailing newline
description[strlen(description) - 1] = '\0';
printf("Ingredients (comma separated): "); std::cout << "Ingredients (comma separated): ";
getline(&ingredients, &ingredients_len, stdin); getline(std::cin, ingredients);
// eliminate trailing newline
ingredients[strlen(ingredients) - 1] = '\0';
printf("Tags (comma separated): "); std::cout << "Tags (comma separated): ";
getline(&tags, &tags_len, stdin); getline(std::cin, tags);
// eliminate trailing newline
tags[strlen(tags) - 1] = '\0';
if((recipe_id = db_get_recipe_id(name)) <= 0) if((recipe_id = db_get_recipe_id(name)) <= 0)
recipe_id = db_add_recipe(name, description); recipe_id = db_add_recipe(name, description);
free(name);
free(description);
for(char *i = strtok(ingredients, ","); i; i = strtok(NULL,",")) { for(auto &ingredient : split(ingredients, ",")) {
// remove leading blank spaces trim(ingredient);
while(isblank(i[0]))
i += sizeof(char);
// remove trailing blank spaces if((ingredient_id = db_get_ingredient_id(ingredient)) <= 0)
size_t i_len = strlen(i); ingredient_id = db_add_ingredient(ingredient);
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); db_conn_recipe_ingredient(recipe_id, ingredient_id);
} }
free(ingredients);
for(char *i = strtok(tags, ","); i; i = strtok(NULL, ",")) { for(auto &tag : split(tags, ",")) {
// remove leading blank spaces trim(tag);
while(isblank(i[0]))
i += sizeof(char);
// remove trailing blank spaces if((tag_id = db_get_tag_id(tag)) <= 0)
size_t i_len = strlen(i); tag_id = db_add_tag(tag);
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); db_conn_recipe_tag(recipe_id, tag_id);
} }
free(tags);
db_close(); db_close();
return 1; return EXIT_SUCCESS;
} }

View File

@ -18,48 +18,46 @@
#include "db.hpp" #include "db.hpp"
#include <sqlite3.h> #include <sqlite3.h>
#include <unistd.h> #include <cstdlib>
#include <sys/stat.h> #include <iostream>
#include <string.h> #include <filesystem>
#include <stdlib.h> #include <format>
#include <stdio.h> #include <string>
#define DB_VERSION 1
static sqlite3 *db = NULL; static sqlite3 *db = NULL;
static const int db_version = 1;
int db_open(void) { int db_open(void) {
const char *xdg_data_home; std::string xdg_data_home;
char *db_path; std::string db_path;
int new_db = 0; bool new_db = false;
int rc; int rc;
if(!(xdg_data_home = getenv("XDG_DATA_HOME"))) { if((xdg_data_home = std::getenv("XDG_DATA_HOME")).empty()) {
printf("Cannot find environment variable XDG_DATA_HOME. Please define it before continuing. E.g.:\nexport XDG_DATA_HOME=\"$HOME/.local/share\"\n"); 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; return 0;
} }
db_path = malloc(strlen(xdg_data_home) + strlen("/menu-helper") + strlen("/recipes.db") + 1); db_path = xdg_data_home + "/menu-helper";
strcpy(db_path, xdg_data_home);
strcat(db_path, "/menu-helper");
if(access(db_path, F_OK) != 0) if(not std::filesystem::exists(db_path))
mkdir(db_path, 0700); std::filesystem::create_directories(db_path);
strcat(db_path, "/recipes.db"); db_path += "/recipes.db";
if(access(db_path, F_OK) != 0) { //if(access(db_path, F_OK) != 0) {
printf("Creating database: %s\n", db_path); if(not std::filesystem::exists(db_path)) {
new_db = 1; std::cout << "Creating database: " << db_path << std::endl;
new_db = true;
} }
rc = sqlite3_open(db_path, &db); rc = sqlite3_open(db_path.c_str(), &db);
free(db_path);
if(rc == SQLITE_OK && new_db) { 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); 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); //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, 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 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 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 recipes(id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING UNIQUE, description STRING);", NULL, NULL, NULL);
@ -71,7 +69,7 @@ int db_open(void) {
} }
void db_close(void) { void db_close(void) {
if(!db) if(not db)
return; return;
sqlite3_close(db); sqlite3_close(db);
@ -82,8 +80,8 @@ int query_id_cb(void *recipe_id_var, int col_num, char **col_data, char **col_na
int ret = 1; int ret = 1;
for(int i = 0; i < col_num; ++i) { for(int i = 0; i < col_num; ++i) {
if(strcmp(col_name[i], "id") == 0) { if(std::string(col_name[i]) == "id") {
*recipe_id_ptr = atoi(col_data[i]); *recipe_id_ptr = std::atoi(col_data[i]);
ret = 0; ret = 0;
break; break;
} }
@ -92,130 +90,88 @@ int query_id_cb(void *recipe_id_var, int col_num, char **col_data, char **col_na
return ret; return ret;
} }
int table_get_id_by_name(const char *table, const char *name) { int table_get_id_by_name(const std::string &table, const std::string &name) {
const char *sel_query_fmt = "SELECT id FROM %s WHERE lower(name)=lower('%s');";
char *sel_query;
int id = 0; int id = 0;
sel_query = malloc(strlen(table) + strlen(name) + strlen(sel_query_fmt) + 1); if(sqlite3_exec(db, std::format("SELECT id FROM {} WHERE lower(name)=lower('{}');", table, name).c_str(),
sprintf(sel_query, sel_query_fmt, table, name); query_id_cb, &id, NULL) != SQLITE_OK)
if(sqlite3_exec(db, sel_query, query_id_cb, &id, NULL) != SQLITE_OK) {
free(sel_query);
return -2; return -2;
}
free(sel_query);
return id; return id;
} }
int db_add_recipe(const char *name, const char *description) { int db_add_recipe(const std::string &name, const std::string &description) {
const char *add_query_fmt = "INSERT INTO recipes(name,description) VALUES('%s','%s');"; if(not db)
char *add_query;
if(!db)
return -1; return -1;
add_query = malloc(strlen(name) + strlen(description) + strlen(add_query_fmt) + 1); if(sqlite3_exec(db, std::format("INSERT INTO recipes(name,description) VALUES('{}','{}');", name, description).c_str(),
sprintf(add_query, add_query_fmt, name, description); NULL, NULL, NULL) != SQLITE_OK)
if(sqlite3_exec(db, add_query, NULL, NULL, NULL) != SQLITE_OK) {
free(add_query);
return -2; return -2;
}
free(add_query);
return db_get_recipe_id(name); return db_get_recipe_id(name);
} }
int db_get_recipe_id(const char *name) { int db_get_recipe_id(const std::string &name) {
if(!db) if(not db)
return -1; return -1;
return table_get_id_by_name("recipes", name); return table_get_id_by_name("recipes", name);
} }
int db_add_ingredient(const char *name) { int db_add_ingredient(const std::string &name) {
const char *add_query_fmt = "INSERT INTO ingredients(name) VALUES(lower('%s'));"; if(not db)
char *add_query;
if(!db)
return -1; return -1;
add_query = malloc(strlen(name) + strlen(add_query_fmt) + 1); if(sqlite3_exec(db, std::format("INSERT INTO ingredients(name) VALUES(lower('{}'));", name).c_str(),
sprintf(add_query, add_query_fmt, name); NULL, NULL, NULL) != SQLITE_OK)
if(sqlite3_exec(db, add_query, NULL, NULL, NULL) != SQLITE_OK) {
free(add_query);
return -2; return -2;
}
free(add_query);
return db_get_ingredient_id(name); return db_get_ingredient_id(name);
} }
int db_get_ingredient_id(const char *name) { int db_get_ingredient_id(const std::string &name) {
if(!db) if(not db)
return -1; return -1;
return table_get_id_by_name("ingredients", name); return table_get_id_by_name("ingredients", name);
} }
int db_add_tag(const char *name) { int db_add_tag(const std::string &name) {
const char *add_query_fmt = "INSERT INTO tags(name) VALUES('%s');"; if(not db)
char *add_query;
if(!db)
return -1; return -1;
add_query = malloc(strlen(name) + strlen(add_query_fmt) + 1); if(sqlite3_exec(db, std::format("INSERT INTO tags(name) VALUES('{}');", name).c_str(),
sprintf(add_query, add_query_fmt, name); NULL, NULL, NULL) != SQLITE_OK)
if(sqlite3_exec(db, add_query, NULL, NULL, NULL) != SQLITE_OK) {
free(add_query);
return -2; return -2;
}
free(add_query);
return db_get_tag_id(name); return db_get_tag_id(name);
} }
int db_get_tag_id(const char *name) { int db_get_tag_id(const std::string &name) {
if(!db) if(not db)
return -1; return -1;
return table_get_id_by_name("tags", name); return table_get_id_by_name("tags", name);
} }
int db_conn_recipe_ingredient(int recipe_id, int ingredient_id) { 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);"; if(not db)
char *add_conn_query;
if(!db)
return -1; return -1;
add_conn_query = malloc(strlen(add_conn_fmt) + (recipe_id % 10) + (ingredient_id % 10)); if(sqlite3_exec(db, std::format("INSERT INTO recipe_ingredient(recipe_id, ingredient_id) VALUES({},{});", recipe_id, ingredient_id).c_str(),
sprintf(add_conn_query, add_conn_fmt, recipe_id, ingredient_id); NULL, NULL, NULL) != SQLITE_OK)
if(sqlite3_exec(db, add_conn_query, NULL, NULL, NULL) != SQLITE_OK) {
free(add_conn_query);
return -2; return -2;
}
free(add_conn_query);
return 1; return 1;
} }
int db_conn_recipe_tag(int recipe_id, int tag_id) { 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);"; if(not db)
char *add_conn_query;
if(!db)
return -1; return -1;
add_conn_query = malloc(strlen(add_conn_fmt) + (recipe_id % 10) + (tag_id % 10)); if(sqlite3_exec(db, std::format("INSERT INTO recipe_tag(recipe_id, tag_id) VALUES({},{});", recipe_id, tag_id).c_str(),
sprintf(add_conn_query, add_conn_fmt, recipe_id, tag_id); NULL, NULL, NULL) != SQLITE_OK)
if(sqlite3_exec(db, add_conn_query, NULL, NULL, NULL) != SQLITE_OK) {
free(add_conn_query);
return -2; return -2;
}
free(add_conn_query);
return 1; return 1;
} }

View File

@ -17,6 +17,8 @@
*/ */
#pragma once #pragma once
#include <string>
int db_open(void); int db_open(void);
void db_close(void); void db_close(void);
@ -28,9 +30,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 char *name, const char *description); int db_add_recipe(const std::string &name, const std::string &description);
int db_get_recipe_id(const char *name); int db_get_recipe_id(const std::string &name);
static inline int db_recipe_exists(const char *name) { static inline int db_recipe_exists(const std::string &name) {
return (db_get_recipe_id(name) > 0); return (db_get_recipe_id(name) > 0);
} }
@ -41,9 +43,9 @@ static inline int db_recipe_exists(const char *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 char *name); int db_add_ingredient(const std::string &name);
int db_get_ingredient_id(const char *name); int db_get_ingredient_id(const std::string &name);
static inline int db_ingredient_exists(const char *name) { static inline int db_ingredient_exists(const std::string &name) {
return (db_get_ingredient_id(name) > 0); return (db_get_ingredient_id(name) > 0);
} }
@ -54,9 +56,9 @@ static inline int db_ingredient_exists(const char *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 char *name); int db_add_tag(const std::string &name);
int db_get_tag_id(const char *name); int db_get_tag_id(const std::string &name);
static inline int db_tag_exists(const char *name) { static inline int db_tag_exists(const std::string &name) {
return (db_get_tag_id(name) > 0); return (db_get_tag_id(name) > 0);
} }

45
src/util.cpp Normal file
View File

@ -0,0 +1,45 @@
/*
* 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());
}

24
src/util.hpp Normal file
View File

@ -0,0 +1,24 @@
/*
* 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/>.
*/
#pragma once
#include <vector>
#include <string>
std::vector<std::string> split(std::string str, const std::string &delim);
void trim(std::string &str);