Skip to content

Commit

Permalink
Merge pull request #3 from EmperorPenguin18/testing
Browse files Browse the repository at this point in the history
PR for 0.3.1
  • Loading branch information
EmperorPenguin18 authored May 27, 2022
2 parents 2154e78 + 4bd2a50 commit 155004e
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 215 deletions.
2 changes: 1 addition & 1 deletion PKGBUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Maintainer: Sebastien MacDougall-Landry

pkgname=libscry
pkgver=0.3
pkgver=0.3.1
pkgrel=1
pkgdesc='A Magic: The Gathering library'
url='https://github.com/EmperorPenguin18/libscry/'
Expand Down
3 changes: 3 additions & 0 deletions examples/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ int main(int argc, char **argv)
cout << card->dual_sided() << endl;
cout << card->json() << endl;
cout << card->loyalty() << endl;
cout << card->set() << endl;
cout << card->price() << endl;
cout << card->legality()[0] << endl;
cout << endl;

cout << "cards_named(\"vannifar\", &img_size)" << endl;
Expand Down
32 changes: 32 additions & 0 deletions src/card.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,35 @@ string Card::loyalty() {
if (!data.HasMember("loyalty")) return "";
return data["loyalty"].GetString();
}

string Card::set() {
return data["set_name"].GetString();
}

string Card::price() {
return data["prices"]["usd"].GetString();
}

vector<string> Card::legality() {
vector<string> output;
output.push_back(data["legalities"]["standard"].GetString());
output.push_back(data["legalities"]["future"].GetString());
output.push_back(data["legalities"]["historic"].GetString());
output.push_back(data["legalities"]["gladiator"].GetString());
output.push_back(data["legalities"]["pioneer"].GetString());
output.push_back(data["legalities"]["explorer"].GetString());
output.push_back(data["legalities"]["modern"].GetString());
output.push_back(data["legalities"]["legacy"].GetString());
output.push_back(data["legalities"]["pauper"].GetString());
output.push_back(data["legalities"]["vintage"].GetString());
output.push_back(data["legalities"]["penny"].GetString());
output.push_back(data["legalities"]["commander"].GetString());
output.push_back(data["legalities"]["brawl"].GetString());
output.push_back(data["legalities"]["historicbrawl"].GetString());
output.push_back(data["legalities"]["alchemy"].GetString());
output.push_back(data["legalities"]["paupercommander"].GetString());
output.push_back(data["legalities"]["duel"].GetString());
output.push_back(data["legalities"]["oldschool"].GetString());
output.push_back(data["legalities"]["premodern"].GetString());
return output;
}
7 changes: 7 additions & 0 deletions src/card.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#pragma once
#include <string>
#include <vector>
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
Expand Down Expand Up @@ -34,6 +35,12 @@ class Card {
virtual string json();
///Get the starting loyalty of the card
virtual string loyalty();
///Get the set name of the card
virtual string set();
///Get the USD non-foil price of the most recent printing of the card
virtual string price();
///Get the legalities (for most formats) of the card
virtual vector<string> legality();
private:
Document data;
};
38 changes: 12 additions & 26 deletions src/data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,9 @@ DataAccess::~DataAccess() {
}

void DataAccess::db_copy(int isSave) {
#ifdef DEBUG
cerr << "DB Copy:" << endl;
#endif
sqlite3 *pFile;
int rc = sqlite3_open(fname, &pFile);
if (rc == SQLITE_OK) {
/*size_t size = 0;
char* page_size = (isSave ? (char*)sql_read(db, "PRAGMA PAGE_SIZE;", &size) : (char*)sql_read(pFile, "PRAGMA PAGE_SIZE;", &size) );
#ifdef DEBUG
cerr << "Page size: " << (char*)page_size << endl;
#endif
char cmd[30] = "pragma page_size = ";
strcat(cmd, page_size);
strcat(cmd, ";");
if (isSave) sql_write(pFile, cmd, NULL, size);
else sql_write(db, cmd, NULL, size);*/
sqlite3 *pFrom = (isSave ? db : pFile);
sqlite3 *pTo = (isSave ? pFile : db);
sqlite3_backup *pBackup = sqlite3_backup_init(pTo, "main", pFrom, "main");
Expand All @@ -104,7 +91,7 @@ void DataAccess::db_copy(int isSave) {

byte* DataAccess::sql_read(sqlite3 *pDb, const char* cmd, size_t* size) {
#ifdef DEBUG
cerr << "Sql call: " << cmd << endl;
fprintf(stderr, "Sql call: %s\n", cmd);
#endif
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(pDb, cmd, -1, &stmt, NULL);
Expand All @@ -125,9 +112,10 @@ byte* DataAccess::sql_read(sqlite3 *pDb, const char* cmd, size_t* size) {
}
*size = sqlite3_column_bytes(stmt, 0);
#ifdef DEBUG
cerr << "Database returned (250 chars): ";
for (size_t i = 0; i < min((size_t)250, *size); i++) cerr << (char)rawoutput[i];
cerr << endl;
fprintf(stderr, "Database returned (250 chars): ");
if (*size < 250) for (size_t i = 0; i < *size; i++) fprintf(stderr, "%c", (char)rawoutput[i]);
else for (size_t i = 0; i < 250; i++) fprintf(stderr, "%c", (char)rawoutput[i]);
fprintf(stderr, "\n");
#endif
byte* output = (byte*)malloc(sizeof(byte)*(*size) + 1);
if (!output) {
Expand All @@ -142,7 +130,7 @@ byte* DataAccess::sql_read(sqlite3 *pDb, const char* cmd, size_t* size) {

void DataAccess::sql_write(sqlite3 *pDb, const char* cmd, byte* data, const size_t& size) {
#ifdef DEBUG
cerr << "Sql call: " << cmd << endl;
fprintf(stderr, "Sql call: %s\n", cmd);
#endif
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(pDb, cmd, -1, &stmt, NULL);
Expand All @@ -151,9 +139,10 @@ void DataAccess::sql_write(sqlite3 *pDb, const char* cmd, byte* data, const size
exit(1);
}
#ifdef DEBUG
cerr << "Data input (250 chars): ";
for (size_t i = 0; i < min((size_t)250, size); i++) cerr << (char)data[i];
cerr << endl;
fprintf(stderr, "Data input (250 chars): ");
if (size < 250) for (size_t i = 0; i < size; i++) fprintf(stderr, "%c", (char)data[i]);
else for (size_t i = 0; i < 250; i++) fprintf(stderr, "%c", (char)data[i]);
fprintf(stderr, "\n");
#endif
rc = sqlite3_bind_blob(stmt, 1, data, size, SQLITE_STATIC);
rc = sqlite3_step(stmt);
Expand All @@ -164,14 +153,14 @@ void DataAccess::sql_write(sqlite3 *pDb, const char* cmd, byte* data, const size
}
#ifdef DEBUG
if (rc == SQLITE_ROW)
cerr << "Write returned: " << (char*)sqlite3_column_blob(stmt, 0) << endl;
fprintf(stderr, "Write returned: %s\n", (char*)sqlite3_column_blob(stmt, 0));
#endif
sqlite3_finalize(stmt);
}

void DataAccess::sql_exec(sqlite3 *pDb, const char* cmd) {
#ifdef DEBUG
cerr << "Sql call: " << cmd << endl;
fprintf(stderr, "Sql call: %s\n", cmd);
#endif
int rc = sqlite3_exec(pDb, cmd, NULL, NULL, NULL);
if (rc != SQLITE_OK) {
Expand Down Expand Up @@ -221,9 +210,6 @@ int DataAccess::datecheck(const char* table, const char* search) {
}

void DataAccess::db_exec(const char* table) {
#ifdef DEBUG
cerr << "Creating table: " << table << endl;
#endif
string cmd = "CREATE TABLE IF NOT EXISTS " + string(table) + "(Key TEXT NOT NULL, Updated DATETIME NOT NULL, Value BLOB NOT NULL, PRIMARY KEY(Key));";
sql_exec(db, cmd.c_str());
}
Expand Down
5 changes: 0 additions & 5 deletions src/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
#include <dlfcn.h>
#include <sqlite3.h>

#ifdef DEBUG
#include <iostream>
#include <algorithm>
#endif

using namespace std;

///This class is used to access the database
Expand Down
31 changes: 11 additions & 20 deletions src/list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,11 @@
using namespace std;
using namespace rapidjson;

List::List(const char * rawjson) {
construct(rawjson);
}

List::List(vector<string> rawjsons) {
string str = "";
for (int i = 0; i < rawjsons.size(); i++) {
str += rawjsons[i] + '\n';
#ifdef DEBUG
cerr << "Last ten chars: " << rawjsons[i].substr(rawjsons[i].length()-10, 10) << endl;
cerr << "Size: " << rawjsons[i].size() << endl << "Capacity: " << rawjsons[i].capacity() << endl;
#endif
construct(rawjsons[i].c_str());
}
data.Parse(str.c_str());
}

void List::construct(const char * rawjson) {
void List::construct(char* rawjson) {
data.Parse(rawjson);
if (strcmp(data["object"].GetString(), "error") == 0) throw "Invalid List";
#ifdef DEBUG
if (data.IsObject()) cerr << "JSON is valid" << endl;
if (data.IsObject()) fprintf(stderr, "JSON is valid\n");
#endif
for (int i = 0; i < data["data"].Size(); i++) {
StringBuffer buffer;
Expand All @@ -45,12 +28,20 @@ void List::construct(const char * rawjson) {
smatch sm2; regex_search(url, sm2, q);
smatch sm3; regex_search(url, sm3, page);
#ifdef DEBUG
cerr << "Regex 1: " << sm1[0] << endl << "Regex 2: " << sm2[0] << endl << "Regex 3: " << sm3[0] << endl;
fprintf(stderr, "Regex 1: %s\nRegex 2: %s\nRegex 3: %s\n", sm1[0].str().c_str(), sm2[0].str().c_str(), sm3[0].str().c_str());
#endif
nextpage = string(sm1[0]) + string(sm2[0]) + string(sm3[0]).substr(0, 5);
} else nextpage = "";
}

List::List(vector<char*> rawjson) {
for (int i = 0; i < rawjson.size(); i++) construct(rawjson[i]);
}

List::List(char* rawjson) {
construct(rawjson);
}

List::~List() {
while (!content.empty()) {
delete content.back();
Expand Down
10 changes: 3 additions & 7 deletions src/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@
#include <rapidjson/stringbuffer.h>
#include "card.h"

#ifdef DEBUG
#include <iostream>
#endif

///This class is used to represent a list of cards (returned from a search for example)
class List {
public:
List(const char*);
List(vector<string>);
List(char*);
List(vector<char*>);
~List();

///Returns a vector with all the cards on this page of the list. For cards on all pages see allcards().
Expand All @@ -31,6 +27,6 @@ class List {
Document data;
vector<Card*> content;
string nextpage;
void construct(const char*);
virtual void construct(char*);
};

Loading

0 comments on commit 155004e

Please sign in to comment.