Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ alpaca.key
*.vim
unit-tests/out-png-test.png
unit-tests/hello/
.vscode
.vscode
.vs/
30 changes: 30 additions & 0 deletions Backend/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Backend/Core/CMakeLists.txt

cmake_minimum_required(VERSION 3.5.1)

add_library(GCore
../Networking/GMutex.cpp
../Networking/GCondition.cpp
../Networking/GThread.cpp
)

# Public include dirs must be split: build vs install
target_include_directories(GCore
PUBLIC
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/Backend>
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/Backend/Networking>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/Backend>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/Backend/Networking>
)

find_package(Threads REQUIRED)
target_link_libraries(GCore PUBLIC ${CMAKE_THREAD_LIBS_INIT})

install(TARGETS GCore
EXPORT shmeaTargets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
87 changes: 40 additions & 47 deletions Backend/Database/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,57 +1,50 @@
#Threading lib
# Database lib: DB

if(EMSCRIPTEN)
message(STATUS "Emscripten detected, enabling Freetype port.")
add_compile_definitions(USE_FREETYPE_PORT) # Optional, for your app logic
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_FREETYPE=1")
message(STATUS "Emscripten detected, enabling Freetype port for DB.")
add_compile_definitions(USE_FREETYPE_PORT)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_FREETYPE=1")
else()
include(FindPkgConfig)
find_package(Freetype REQUIRED)
include_directories(${FREETYPE_INCLUDE_DIRS})
find_package(Freetype REQUIRED)
endif()

# Set C++03 standard flag
set(CMAKE_CXX_STANDARD 98)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(DB_src_files
lodepng.cpp
png-helper.cpp
image.cpp
GVector.h
GPointer.h
GType.cpp
GType_helpers.cpp
GType_operators.cpp
GString.cpp
GString_helpers.cpp
GList.cpp
GLogger.cpp
GTable.cpp
GObject.cpp
maxid.cpp
SaveTable.cpp
SaveFolder.cpp
Serializable.cpp
ServiceData.cpp
standardizable.cpp
PNGPlotter.cpp
add_library(DB
lodepng.cpp
png-helper.cpp
image.cpp
GVector.h
GPointer.h
GType.cpp
GType_helpers.cpp
GType_operators.cpp
GString.cpp
GString_helpers.cpp
GList.cpp
GLogger.cpp
GTable.cpp
GObject.cpp
maxid.cpp
SaveTable.cpp
SaveFolder.cpp
Serializable.cpp
ServiceData.cpp
standardizable.cpp
PNGPlotter.cpp
)
add_library(DB ${DB_src_files})

#Link libraries
if(EMSCRIPTEN)
else()
target_link_libraries(DB ${FREETYPE_LIBRARIES})
# Freetype stays PRIVATE
if(NOT EMSCRIPTEN)
target_include_directories(DB PRIVATE ${FREETYPE_INCLUDE_DIRS})
target_link_libraries(DB PRIVATE ${FREETYPE_LIBRARIES})
endif()

install(TARGETS DB EXPORT shmeaConfig
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
# NEW: DB needs the core threading symbols (GMutex etc.)
target_link_libraries(DB PRIVATE GCore)

install(EXPORT shmeaConfig DESTINATION share/shmea/cmake)

export(TARGETS DB FILE shmeaConfig.cmake)
install(TARGETS DB
EXPORT shmeaTargets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
34 changes: 13 additions & 21 deletions Backend/Database/GList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,29 @@ void GList::loadWords(const GString& fname)
if (fname.length() == 0)
return;

FILE* fd = fopen(fname.c_str(), "ro");
FILE* fd = fopen(fname.c_str(), "r");
printf("[WORDS] %c%s\n", (fd != NULL) ? '+' : '-', fname.c_str());

if (!fd)
return;

// Allocate a buffer
int MAX_LINE_SIZE = 1024;
const int MAX_LINE_SIZE = 1024;
char buffer[MAX_LINE_SIZE];
char *ptr = NULL;

shmea::GList newRow;
bzero(buffer, MAX_LINE_SIZE);
while( !feof( fd ) )
while( fgets(buffer, MAX_LINE_SIZE, fd) )
{
fgets(&buffer[0], MAX_LINE_SIZE, fd);
GString delim_char(' '); //delimiter

if (!feof(fd))
{
buffer[strlen(buffer)-1] = '\0';
ptr = strtok(buffer, (const char*)delim_char.c_str());
while (ptr)
{
GString word(ptr);
newRow.addString(word.makeAlphaNum().toLower());

// Get the next token
ptr = strtok( NULL, (const char *)delim_char.c_str() );
}
}
// Remove trailing newline safely
buffer[strcspn(buffer, "\r\n")] = 0;

char* ptr = strtok(buffer, " ");
while (ptr)
{
GString word(ptr);
newRow.addString(word.makeAlphaNum().toLower());
ptr = strtok(NULL, " ");
}
}

printf( "[CSV] %d cells of data\n", newRow.size());
Expand Down
11 changes: 9 additions & 2 deletions Backend/Database/GLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "GLogger.h"
#include "GType.h"
#include <sys/time.h>
#ifdef _WIN32
#include <direct.h>
#define mkdir(dir, mode) _mkdir(dir)
#include <sys/stat.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#include <time.h>
#include <sys/stat.h>

using namespace shmea;

Expand Down
22 changes: 16 additions & 6 deletions Backend/Database/GPointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef _GPOINTER
#define _GPOINTER

#include "../Networking/GMutex.h"
#include "GDeleter.h"
#include <ctime>
#include <stdio.h>
Expand All @@ -33,7 +34,7 @@ class GPointer

T* data;
unsigned int* refCount;
pthread_mutex_t* refMutex;
GMutex* refMutex;

public:

Expand All @@ -44,7 +45,7 @@ class GPointer
explicit GPointer(T* newData = NULL) :
data(newData),
refCount(newData ? new unsigned int(1) : NULL),
refMutex(NULL) {}
refMutex(newData ? new GMutex() : NULL) {}

GPointer(const GPointer<T, Deleter>& g2) :
data(NULL),
Expand Down Expand Up @@ -87,6 +88,7 @@ class GPointer
// Store local copies before nulling members
T* dataToDelete = data;
unsigned int* countToDelete = refCount;
GMutex* mutextoDelete = refMutex;

// Null members first
data = NULL;
Expand All @@ -99,6 +101,7 @@ class GPointer
Deleter(dataToDelete);
}
delete countToDelete;
delete mutextoDelete;
} else {
// Just null our references
data = NULL;
Expand All @@ -114,20 +117,27 @@ class GPointer

unsigned int increment()
{
if (refCount)
if (refCount && refMutex)
{
refMutex->lock();
++(*refCount);
refMutex->unlock();
return *refCount;
}
return refCount ? *refCount : 0;
return 0;
}

unsigned int decrement()
{
if (refCount)
if (refCount && refMutex)
{
refMutex->lock();
--(*refCount);
unsigned int count = *refCount;
refMutex->unlock();
return count;
}
return refCount ? *refCount : 0;
return 0;
}

T& operator*()
Expand Down
3 changes: 3 additions & 0 deletions Backend/Database/GString.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#ifndef _GSTRING
#define _GSTRING

#ifdef _WIN32
#include <stdint.h>
#endif
#include <ctime>
#include <iomanip>
#include <sstream>
Expand Down
2 changes: 1 addition & 1 deletion Backend/Database/GTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void GTable::importFromFile(const GString& fname)
do
{
shmea::GList newRow;
bzero(buffer, MAX_LINE_SIZE);
memset(buffer, 0, MAX_LINE_SIZE);

// get the current line
char readBuffer[MAX_LINE_SIZE];
Expand Down
5 changes: 5 additions & 0 deletions Backend/Database/GType.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
#ifndef _GTYPES
#define _GTYPES

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdint.h>
#endif
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
Expand Down
5 changes: 5 additions & 0 deletions Backend/Database/PNGPlotter.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#ifndef PNGPLOTTER_H
#define PNGPLOTTER_H

#ifdef _WIN32
#include <winsock2.h>
#include <stdint.h>
#define M_PI 3.14159265358979323846
#endif
#include "image.h"
#include <ft2build.h>
#include FT_FREETYPE_H
Expand Down
6 changes: 5 additions & 1 deletion Backend/Database/SaveFolder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ SaveTable* SaveFolder::newItem(const GString& siName, const GTable& newTable)
if (stat(dirname.c_str(), &info) != 0)
{
// make the directory
int status = mkdir(dirname.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
#ifdef _WIN32
int status = mkdir(dirname.c_str());
#else
int status = mkdir(dirname.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
#endif
if (status < 0)
{
printf("[DB] %s mkdir failed\n", dirname.c_str());
Expand Down
13 changes: 10 additions & 3 deletions Backend/Database/Serializable.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
#ifndef _GSERIALIZABLE
#define _GSERIALIZABLE

#ifdef _WIN32
#include <winsock2.h>
#include <winsock2.h>
#include <windows.h>
#else
#include <sys/signal.h>
#include <sys/stat.h>
#include <pthread.h>
#endif

#include "GList.h"
#include "GTable.h"
#include "GObject.h"
Expand All @@ -25,13 +35,10 @@
#include <algorithm>
#include <dirent.h>
#include <map>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/signal.h>
#include <sys/stat.h>
#include <vector>

namespace shmea {
Expand Down
4 changes: 4 additions & 0 deletions Backend/Database/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#ifndef IMAGE_H
#define IMAGE_H

#ifdef _WIN32
#include <winsock2.h>
#endif

#include <stdio.h>
#include <string.h>
#include <string>
Expand Down
4 changes: 2 additions & 2 deletions Backend/Database/maxid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ void MaxID::parseMaxIDFile(const GString& name, char* fileContents, int64_t fSiz
{
// get the line contents
char* line = (char*)malloc(sizeof(char) * (len + 1)); //+1 because of indexing
bzero(line, len + 1); //+1 because of indexing
memset(line, 0, len + 1); //+1 because of indexing
memcpy(line, fileContents, len);

int nl = (len == fSize) ? 0 : 1; // no nl if its the last line
fSize -= len + nl;
memcpy(fileContents, &fileContents[len + nl], fSize);
bzero(&fileContents[fSize], len);
memset(&fileContents[fSize], 0, len);

// set the new max id
newMaxID = atoll(line);
Expand Down
Loading