Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
26 changes: 26 additions & 0 deletions lib/Debug/debug_print.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "debug_print.h"

#include <stdarg.h>
#include <stdio.h>
#include <string.h>


#if defined(_WIN32) || defined(_WIN64)
#include <io.h>
#define write _write
#else
#include <unistd.h>
#endif

void debug_log(const char *format, ...) {
char buf[256];
va_list ap;
va_start(ap, format);
int n = vsnprintf(buf, sizeof(buf), format, ap);
va_end(ap);
if (n > 0) {
if (n > (int)sizeof(buf)) n = sizeof(buf);
/* Use low-level write to avoid stdio buffering/locks that can block under some debuggers/targets */
(void)write(2, buf, n);
}
}
8 changes: 8 additions & 0 deletions lib/Debug/debug_print.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef DEBUG_PRINT_H_
#define DEBUG_PRINT_H_

#include <stdarg.h>

void debug_log(const char *format, ...);

#endif // DEBUG_PRINT_H_
64 changes: 63 additions & 1 deletion lib/Desktop-File-Interface/desktopFileInterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ typedef struct {
FILE *file;
} FILE_INFO;

void *setupFile(char *filename) {
void *setupFile(const char *filename) {
FILE_INFO *fileInfo = malloc(sizeof(FILE_INFO));
int nameLen = strlen(filename);
fileInfo->filename = calloc(1, nameLen + 1);
Expand All @@ -22,6 +22,27 @@ void tearDownFile(void *file) {
free(file);
}

int8_t FILE_REMOVE(void *file) {
if (file == NULL) return 1;
FILE_INFO *fileInfo = (FILE_INFO *)file;

if (fileInfo->file != NULL) {
fclose(fileInfo->file);
fileInfo->file = NULL;
}

int8_t result = 1;
if (fileInfo->filename != NULL) {
if (remove(fileInfo->filename) != 0) {
result = 0;
#ifdef PRINT_ERRORS
perror("ERROR: Failed to remove temp file");
#endif
}
return result;
}
}

int8_t FILE_READ(void *buffer, uint32_t pageNum, uint32_t pageSize, void *file) {
FILE_INFO *fileInfo = (FILE_INFO *)file;
fseek(fileInfo->file, pageSize * pageNum, SEEK_SET);
Expand Down Expand Up @@ -121,6 +142,39 @@ int32_t FILE_TELL(void *file) {
return ftell(fileInfo->file);
}

char *tempFilePath(void) {
static char tempPathBuffer[256];

#if defined(_WIN32) || defined(_WIN64)
char *path = _tempnam(NULL, "embeddb_");
if (path != NULL) {
strncpy(tempPathBuffer, path, sizeof(tempPathBuffer) - 1);
tempPathBuffer[sizeof(tempPathBuffer) - 1] = '\0';
free(path);
} else {
/* Fallback */
snprintf(tempPathBuffer, sizeof(tempPathBuffer),
"embeddb_%lu.tmp", (unsigned long)rand());
}

#else
/* POSIX systems */
snprintf(tempPathBuffer, sizeof(tempPathBuffer),
"/tmp/embeddb_%luXXXXXX", (unsigned long)rand());

int fd = mkstemp(tempPathBuffer);
if (fd >= 0) {
close(fd);
}
#endif

char *out = malloc(strlen(tempPathBuffer) + 1);
if (out) {
strcpy(out, tempPathBuffer);
}
return out;
}

embedDBFileInterface *getFileInterface() {
embedDBFileInterface *fileInterface = malloc(sizeof(embedDBFileInterface));
fileInterface->close = FILE_CLOSE;
Expand All @@ -135,6 +189,10 @@ embedDBFileInterface *getFileInterface() {
fileInterface->writeRel = FILE_WRITE_REL;
fileInterface->seek = FILE_SEEK;
fileInterface->tell = FILE_TELL;
fileInterface->setup = setupFile;
fileInterface->teardown = tearDownFile;
fileInterface->removeFile = FILE_REMOVE;
fileInterface->tempFilePath = tempFilePath;
return fileInterface;
}

Expand All @@ -152,5 +210,9 @@ embedDBFileInterface *getMockEraseFileInterface() {
fileInterface->writeRel = FILE_WRITE_REL;
fileInterface->seek = FILE_SEEK;
fileInterface->tell = FILE_TELL;
fileInterface->setup = setupFile;
fileInterface->teardown = tearDownFile;
fileInterface->removeFile = FILE_REMOVE;
fileInterface->tempFilePath = tempFilePath;
return fileInterface;
}
2 changes: 1 addition & 1 deletion lib/Desktop-File-Interface/desktopFileInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extern "C" {
/* File functions */
embedDBFileInterface *getFileInterface();
embedDBFileInterface *getMockEraseFileInterface();
void *setupFile(char *filename);
void *setupFile(const char *filename);
void tearDownFile(void *file);

#ifdef __cplusplus
Expand Down
107 changes: 85 additions & 22 deletions lib/SD-File-Interface/SDFileInterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ typedef struct {
SD_FILE *sdFile;
} SD_FILE_INFO;

void *setupSDFile(char *filename) {
void *setupSDFile(const char *filename) {
SD_FILE_INFO *fileInfo = malloc(sizeof(SD_FILE_INFO));
int nameLen = strlen(filename);
fileInfo->filename = calloc(1, nameLen + 1);
Expand All @@ -57,6 +57,22 @@ void tearDownSDFile(void *file) {
free(file);
}

int8_t FILE_REMOVE(void *file) {
if (file == NULL) return 1;
SD_FILE_INFO *fileInfo = (SD_FILE_INFO *)file;

if (fileInfo->sdFile != NULL) {
sd_fclose(fileInfo->sdFile);
fileInfo->sdFile = NULL;
}

if (fileInfo->filename != NULL) {
int result = sd_remove(fileInfo->filename);
return (result == 0);
}
return 1;
}

int8_t FILE_READ(void *buffer, uint32_t pageNum, uint32_t pageSize, void *file) {
SD_FILE_INFO *fileInfo = (SD_FILE_INFO *)file;
sd_fseek(fileInfo->sdFile, pageSize * pageNum, SEEK_SET);
Expand All @@ -65,31 +81,25 @@ int8_t FILE_READ(void *buffer, uint32_t pageNum, uint32_t pageSize, void *file)

int8_t FILE_WRITE(void *buffer, uint32_t pageNum, uint32_t pageSize, void *file) {
SD_FILE_INFO *fileInfo = (SD_FILE_INFO *)file;
if (fileInfo->sdFile == NULL) return 0;

size_t fileSize = sd_length(fileInfo->sdFile);
size_t requiredSize = pageNum * pageSize;
if (fileSize < pageNum * pageSize) {
int8_t seekSuccess = sd_fseek(fileInfo->sdFile, fileSize, SEEK_SET);
if (seekSuccess == -1) {
return -1;
}
size_t currentSize = fileSize;
uint32_t max = UINT32_MAX;
uint32_t writeSuccess = 0;
while (currentSize < requiredSize) {
writeSuccess = sd_fwrite(&max, sizeof(uint32_t), 1, fileInfo->sdFile);
if (writeSuccess == 0)
return -1;
currentSize += 4;
size_t requiredSize = (size_t)pageNum * pageSize;

if (fileSize < requiredSize) {
sd_fseek(fileInfo->sdFile, 0, SEEK_END);
uint8_t zero = 0;
while (sd_length(fileInfo->sdFile) < requiredSize) {
if (sd_fwrite(&zero, 1, 1, fileInfo->sdFile) != 1) return 0;
}
}
int8_t seekSuccess = sd_fseek(fileInfo->sdFile, pageNum * pageSize, SEEK_SET);
if (seekSuccess == -1) {
return -1;

if (sd_fseek(fileInfo->sdFile, requiredSize, SEEK_SET) != 0) return 0;

if (sd_fwrite(buffer, pageSize, 1, fileInfo->sdFile) == 1) {
return 1;
}
int8_t writeSuccess = sd_fwrite(buffer, pageSize, 1, fileInfo->sdFile) == pageSize;
if (seekSuccess == -1)
return 0;
return 1;
return 0;
}

int8_t FILE_ERASE(uint32_t startPage, uint32_t endPage, uint32_t pageSize, void *file) {
Expand All @@ -103,6 +113,16 @@ int8_t FILE_CLOSE(void *file) {
return 1;
}

int8_t FILE_READ_REL(void *buffer, uint32_t size, uint32_t n, void *file) {
SD_FILE_INFO *fileInfo = (SD_FILE_INFO *)file;
return sd_fread(buffer, size, n, fileInfo->sdFile);
}

int8_t FILE_WRITE_REL(void *buffer, uint32_t size, uint32_t n, void *file) {
SD_FILE_INFO *fileInfo = (SD_FILE_INFO *)file;
return sd_fwrite(buffer, size, n, fileInfo->sdFile);
}

int8_t FILE_FLUSH(void *file) {
SD_FILE_INFO *fileInfo = (SD_FILE_INFO *)file;
return sd_fflush(fileInfo->sdFile) == 0;
Expand All @@ -126,13 +146,56 @@ int8_t FILE_OPEN(void *file, uint8_t mode) {
}
}

int32_t FILE_TELL(void *file) {
SD_FILE_INFO *fileInfo = (SD_FILE_INFO *)file;
if (fileInfo == NULL || fileInfo->sdFile == NULL) {
return -1;
}
return (int32_t)sd_ftell(fileInfo->sdFile);
}

char *sdFat_tempFilePath(void) {
char tempPathBuffer[32];
snprintf(tempPathBuffer, sizeof(tempPathBuffer), "TMP%lu.DAT", random());

char *out = malloc(strlen(tempPathBuffer) + 1);
if (out) {
strcpy(out, tempPathBuffer);
}
return out;
}

int8_t FILE_SEEK(uint32_t n, void *file) {
SD_FILE_INFO *fileInfo = (SD_FILE_INFO *)file;
return sd_fseek(fileInfo->sdFile, n, SEEK_SET);
}

int8_t FILE_ERROR(void *file) {
if (file == NULL) return 1;
SD_FILE_INFO *fileInfo = (SD_FILE_INFO *)file;

if (sd_ferror(fileInfo->sdFile)) {
return 1;
}
return 0;
}

embedDBFileInterface *getSDInterface() {
embedDBFileInterface *fileInterface = malloc(sizeof(embedDBFileInterface));
fileInterface->close = FILE_CLOSE;
fileInterface->read = FILE_READ;
fileInterface->write = FILE_WRITE;
fileInterface->erase = FILE_ERASE;
fileInterface->open = FILE_OPEN;
fileInterface->seek = FILE_SEEK;
fileInterface->flush = FILE_FLUSH;
fileInterface->error = FILE_ERROR;
fileInterface->readRel = FILE_READ_REL;
fileInterface->writeRel = FILE_WRITE_REL;
fileInterface->tell = FILE_TELL;
fileInterface->setup = setupSDFile;
fileInterface->teardown = tearDownSDFile;
fileInterface->removeFile = FILE_REMOVE;
fileInterface->tempFilePath = sdFat_tempFilePath;
return fileInterface;
}
2 changes: 1 addition & 1 deletion lib/SD-File-Interface/SDFileInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ extern "C" {
#include "sdcard_c_iface.h"

embedDBFileInterface *getSDInterface();
void *setupSDFile(char *filename);
void *setupSDFile(const char *filename);
void tearDownSDFile(void *file);

#ifdef __cplusplus
Expand Down
40 changes: 30 additions & 10 deletions lib/SD-Wrapper/sdcard_c_iface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,32 +104,52 @@ SD_FILE *sd_fopen(const char *filename, const char *mode) {

size_t sd_fread(void *ptr, size_t size, size_t nmemb, SD_FILE *stream) {
/* read is the size of bytes * num of size-bytes */
int16_t num_bytes = stream->f.read((char *)ptr, size * nmemb);
int32_t num_bytes = stream->f.read((char *)ptr, size * nmemb);

if (num_bytes < 0)
return 0;
return num_bytes / size;
}

int sd_fseek(SD_FILE *stream, unsigned long int offset, int whence) {
if (NULL == stream)
return -1;
if (NULL == stream) return -1;

bool result = stream->f.seek(offset);
if (!result)
return -1;
return 0;
unsigned long absolute_pos = offset;
if (whence == SEEK_CUR) {
absolute_pos = stream->f.position() + offset;
} else if (whence == SEEK_END) {
absolute_pos = stream->f.size() - offset;
}
return stream->f.seek(absolute_pos) ? 0 : -1;
}

size_t sd_fwrite(void *ptr, size_t size, size_t nmemb, SD_FILE *stream) {
size_t total_count = size * nmemb;
size_t bytes_written = stream->f.write(ptr, total_count);

if (total_count != bytes_written)
return 0;
return total_count;
if (bytes_written == 0) return 0;
return bytes_written / size;
}

size_t sd_length(SD_FILE *stream) {
return stream->f.size();
}

int sd_remove(const char *filename) {
if (sdcard->remove(filename)) {
return 0;
}
return -1;
}

long sd_ftell(SD_FILE *stream) {
if (stream == NULL) {
return -1;
}
return (long)stream->f.position();
}

int sd_ferror(SD_FILE *stream) {
if (stream == NULL) return 1;
return stream->f.getWriteError() ? 1 : 0;
}
16 changes: 16 additions & 0 deletions lib/SD-Wrapper/sdcard_c_iface.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,22 @@ sd_fwrite(
*/
size_t sd_length(SD_FILE *stream);

/**
@brief Remove (delete) a file from the SD card.
@param filename The name of the file to delete.
@returns 0 on success, -1 on failure.
*/
int sd_remove(const char *filename);

/**
@brief Find location of current file position.
@param stream A pointer to a C file struct type associated with an SD file object.
@returns 0 on success, -1 on failure.
*/
long sd_ftell(SD_FILE *stream);

int sd_ferror(SD_FILE *stream);

void init_sdcard(void *sd);

#if defined(__cplusplus)
Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ DEV_TEST_OBJECTS = $(EMBEDDB_OBJECTS) $(QUERY_OBJECTS) $(EMBEDDB_FILE_INTERFACE)


TEST_FLAGS = -I. -I$(PATHU) -I $(PATHS) -I$(PATH_UTILITY) -I$(PATH_FILE_INTERFACE) -D TEST
EXAMPLE_FLAGS = -I. -I$(PATHS) -I$(PATH_UTILITY) -I$(PATH_FILE_INTERFACE) -I$(PATH_DISTRIBUTION) -DPRINT_ERRORS
EXAMPLE_FLAGS = -I. -I$(PATHS) -I$(PATH_UTILITY) -I$(PATH_FILE_INTERFACE) -I$(PATH_DISTRIBUTION)
TEST_DIST_FLAGS = -I. -I$(PATHS) -I$(PATHU) -I$(PATH_FILE_INTERFACE) -I$(PATH_DISTRIBUTION) -I$(PATH_UTILITY) -DDIST -D TEST

override CFLAGS += $(if $(filter test-dist,$(MAKECMDGOALS)), $(TEST_DIST_FLAGS), $(if $(filter test,$(MAKECMDGOALS)),$(TEST_FLAGS),$(EXAMPLE_FLAGS)) )
Expand Down
Loading
Loading