From 28a3140a762098495f4e46f360f0b251dca808d6 Mon Sep 17 00:00:00 2001 From: Ivan Mahonin Date: Jul 25 2020 06:12:59 +0000 Subject: scan directory without glib --- diff --git a/demo/src/SConstruct b/demo/src/SConstruct index 893fc46..269edf8 100644 --- a/demo/src/SConstruct +++ b/demo/src/SConstruct @@ -14,6 +14,7 @@ flags = ' -O0 -g -lm -Wall -fmessage-length=0 ' # files sources = [ + 'common.c', 'drawing.c', 'font.c', 'main.c', diff --git a/demo/src/common.c b/demo/src/common.c new file mode 100644 index 0000000..9da766a --- /dev/null +++ b/demo/src/common.c @@ -0,0 +1,72 @@ + +#include +#include + +#include + +#include "common.h" + + +static char buffer[4096] = {}; + + +static void scanDirectoryRecursive(const char *path, char **outTextBegin, char *outTextEnd, int level) { + Directory d = openDirectory(path); + if (d) { + int count = directoryGetCount(d); + + int lenPrefix = level*4 + 1; + + int lenPath = strlen(path) + 1; + int lenName = 0; + for(int i = 0; i < count; ++i) { + int ln = strlen(directoryGet(d, i)); + if (ln > lenName) lenName = ln; + } + int lenLine = lenPrefix + lenName + 1; + + char *subpath = calloc(1, lenPath + lenName + 1); + strcpy(subpath, path); + subpath[lenPath - 1] = '/'; + + for(int i = 0; i < count; ++i) { + if (*outTextBegin + lenLine > outTextEnd) break; + + const char *name = directoryGet(d, i); + + for(int i = 0; i < lenPrefix; ++i) *((*outTextBegin)++) = '-'; + char *typeSign = *outTextBegin - 1; + strcpy(*outTextBegin, name); + *outTextBegin += strlen(name); + *((*outTextBegin)++) = '\n'; + + strcpy(subpath + lenPath, name); + if (directoryExists(subpath)) { + *typeSign = '>'; + scanDirectoryRecursive(subpath, outTextBegin, outTextEnd, level+1); + } else + if (fileExists(subpath)) { + *typeSign = '*'; + } else { + *typeSign = '#'; + } + } + + free(subpath); + closeDirectory(d); + } +} + + +void commonInit() { + char *bufPtr = buffer; + scanDirectoryRecursive("data", &bufPtr, bufPtr + sizeof(buffer) - 1, 0); +} + +void commonDraw() { + textFontDefault(); + textSize(16); + text(buffer, 16, 16); +} + + diff --git a/demo/src/common.h b/demo/src/common.h new file mode 100644 index 0000000..4ccbfe3 --- /dev/null +++ b/demo/src/common.h @@ -0,0 +1,11 @@ +#ifndef COMMON_H +#define COMMON_H + + +void commonInit(); +void commonDraw(); + + +#endif + + diff --git a/demo/src/font.c b/demo/src/font.c index 8c9691c..0231bfe 100644 --- a/demo/src/font.c +++ b/demo/src/font.c @@ -7,7 +7,7 @@ -Font font; +static Font font; void fontInit() { diff --git a/demo/src/main.c b/demo/src/main.c index 9d4f080..0480bd8 100644 --- a/demo/src/main.c +++ b/demo/src/main.c @@ -1,6 +1,7 @@ #include +#include "common.h" #include "phisics.h" #include "sprites.h" #include "font.h" @@ -8,6 +9,7 @@ void init() { + commonInit(); phisicsInit(); spritesInit(); fontInit(); @@ -15,6 +17,7 @@ void init() { } void draw() { + commonDraw(); drawingDraw(); phisicsDraw(); spritesDraw(); diff --git a/demo/src/phisics.c b/demo/src/phisics.c index e34cd6d..fe7fa29 100644 --- a/demo/src/phisics.c +++ b/demo/src/phisics.c @@ -9,7 +9,7 @@ static Sprite ball, brick1, brick2, brick3; static Group edges; static Group movement; -Sound beep; +static Sound beep; diff --git a/demo/src/sprites.c b/demo/src/sprites.c index dab8bbd..cbac25e 100644 --- a/demo/src/sprites.c +++ b/demo/src/sprites.c @@ -6,7 +6,7 @@ #include "sprites.h" -Group pulse; +static Group pulse; void spritesInit() { diff --git a/src/common.c b/src/common.c index c16cbdf..bcd3200 100644 --- a/src/common.c +++ b/src/common.c @@ -1,4 +1,7 @@ +#include +#include + #include "private.h" @@ -39,24 +42,21 @@ double randomFloat() Directory openDirectory(const char *path) { if (!heliInitialized) return NULL; - - return NULL; - /* TODO:bw - GDir *dir = g_dir_open(path, 0, NULL); + + DIR *dir = opendir(path); if (!dir) return NULL; Directory d = calloc(1, sizeof(Directory)); - while(TRUE) { - const char* name = g_dir_read_name(dir); - if (!name) break; - if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) continue; - heliStringmapAdd(&d->files, name, NULL, NULL); + + struct dirent *ent; + while((ent = readdir(dir)) != NULL) { + if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue; + heliStringmapAdd(&d->files, ent->d_name, NULL, NULL); } - g_dir_close(dir); + closedir(dir); heliObjectRegister(d, (HeliFreeCallback)&closeDirectory); return d; - */ } void closeDirectory(Directory directory) { @@ -73,10 +73,15 @@ const char* directoryGet(Directory directory, int i) { return (const char*)heliArrayGetKey(&directory->files, i); } -int fileExists(const char *path) - { return FALSE; }//TODO:bw g_file_test(path, G_FILE_TEST_IS_REGULAR) ? TRUE : FALSE; } -int directoryExists(const char *path) - { return FALSE; }//TODO:bw g_file_test(path, G_FILE_TEST_IS_DIR) ? TRUE : FALSE; } +int fileExists(const char *path) { + struct stat s = {}; + return stat(path, &s) == 0 && !(s.st_mode & S_IFDIR); +} + +int directoryExists(const char *path) { + struct stat s = {}; + return stat(path, &s) == 0 && (s.st_mode & S_IFDIR); +} char* heliStringCopy(const char *x) {