feat: adds SvelteKit + Tauri

This commit is contained in:
2024-01-22 14:01:43 -05:00
parent 2599319222
commit f28fd699cf
43 changed files with 6036 additions and 11 deletions

39
api/csv.cpp Normal file
View File

@ -0,0 +1,39 @@
#include "csv.h"
#include <iostream>
std::string strip_quotes(std::string input)
{
if (input[0] != '"')
return input;
return input.substr(1, input.size() - 2);
}
int get_next_comma(std::string input)
{
int i;
bool in_string = false;
for (i = 0; i < input.size(); i++) {
if (input[i] == '"')
in_string = !in_string;
if (input[i] == ',' && !in_string)
return i;
}
return -1;
}
std::vector<std::string> get_csv_as_vector(std::string input)
{
std::vector<std::string> output;
int next_comma;
std::string current_string;
while ((next_comma = get_next_comma(input)) != -1) {
output.push_back(strip_quotes(input.substr(0, next_comma)));
input = input.substr(next_comma + 1, input.size());
}
output.push_back(input);
return output;
}

15
api/csv.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef CSV_H
#define CSV_H
#include <string>
#include <vector>
std::string strip_quotes(std::string input);
int get_next_comma(std::string input);
std::vector<std::string> get_csv_as_vector(std::string input);
#endif

71
api/main.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <crow.h>
#include <crow/app.h>
#include <fstream>
#include "csv.h"
#define BOOK_FILENAME "books.csv"
std::string book_vec_to_json(std::vector<std::string> headers, std::vector<std::string> book)
{
int i;
std::string out = "{";
for (i = 0; i < book.size(); i++) {
out += "\"" + headers[i] + "\":";
out += "\"" + book[i] + "\",";
}
return out + "}";
}
std::string get_all_books()
{
std::ifstream file;
int file_exists;
std::string line, total_lines;
std::vector<std::string> book_vec;
std::vector<std::string> header_vec;
file.open(BOOK_FILENAME);
if (!file.good()) {
fprintf(stderr, "Theres no books available!\n");
return "No books saved!";
}
std::getline(file, line);
// this contains the headers so that we can fill the json file
header_vec = get_csv_as_vector(line);
total_lines = "{";
while (std::getline(file, line)) {
book_vec = get_csv_as_vector(line);
total_lines += book_vec_to_json(header_vec, book_vec) + ",";
}
return total_lines + "}";
}
std::string print_hello()
{
return "Hello, world!";
}
int main()
{
crow::SimpleApp app;
CROW_ROUTE(app, "/")([](){
return crow::response(print_hello());
});
CROW_ROUTE(app, "/books")([](){
return crow::response(get_all_books());
});
app.port(18080).run();
}