diff --git a/.gitignore b/.gitignore index d0a76711..aa05b2aa 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,5 @@ ocarina.bin .sconsign.dblite bin/ -lib/ PKGBUILD !PKGBUILD.tmpl diff --git a/config b/config index 01bc45f3..af83e173 100644 --- a/config +++ b/config @@ -25,13 +25,19 @@ class Config: self.DEBUG = CONFIG_DEBUG self.ENV = CONFIG_ENV self.FILE = False + self.TEST = False + + def package(self, name): + env.ParseConfig("pkg-config --cflags --libs %s" % name) def reconfigure(self): env.Replace( CCFLAGS = self.ENV ) if self.FILE: env.Append( CCFLAGS = "-DCONFIG_FILE" ) + if self.TEST: env.Append( CCFLAGS = "-DCONFIG_TEST" ) def reset(self): self.FILE = False + self.TEST = False self.reconfigure() CONFIG = Config() diff --git a/include/file.h b/include/file.h new file mode 100644 index 00000000..0e5d3760 --- /dev/null +++ b/include/file.h @@ -0,0 +1,25 @@ +/* + * Copyright 2013 (c) Bryan Schumaker. + */ +#ifndef OCARINA_FILE_H +#define OCARINA_FILE_H + +#include + +enum FileLocHint { + FILE_TYPE_CONFIG, + FILE_TYPE_DATA, + FILE_TYPE_LEGACY, +}; + +class File { +private: + FileLocHint hint; + std::string filepath; + +public: + File(std::string, FileLocHint); + const char *get_filepath(); +}; + +#endif /* OCARINA_FILE_H */ diff --git a/lib/Sconscript b/lib/Sconscript new file mode 100644 index 00000000..73c00c68 --- /dev/null +++ b/lib/Sconscript @@ -0,0 +1,10 @@ +#!/usr/bin/python +Import("env", "CONFIG") + +build = [] + +if CONFIG.FILE: + CONFIG.package("glib-2.0") + build += [ env.Object("file.cpp") ] + +Return("build") diff --git a/lib/file.cpp b/lib/file.cpp new file mode 100644 index 00000000..478602c5 --- /dev/null +++ b/lib/file.cpp @@ -0,0 +1,49 @@ +/* + * Copyright 2013 (c) Bryan Schumaker. + */ +#include + +#include + +#ifdef CONFIG_TEST +#define OCARINA_DIR "ocarina-test" +#elif CONFIG_DEBUG +#define OCARINA_DIR "ocarina-debug" +#else +#define OCARINA_DIR "ocarina" +#endif + +File :: File(std::string path, FileLocHint file_hint) + : hint(file_hint) +{ + std::string dir; + + switch (file_hint) { + case FILE_TYPE_CONFIG: + dir = g_get_user_config_dir(); + break; + case FILE_TYPE_DATA: + dir = g_get_user_data_dir(); + break; + case FILE_TYPE_LEGACY: + dir = g_get_home_dir(); + } + + dir += "/"; + switch (file_hint) { + case FILE_TYPE_CONFIG: + case FILE_TYPE_DATA: + dir += OCARINA_DIR; + break; + case FILE_TYPE_LEGACY: + dir += "."; + dir += OCARINA_DIR; + } + + filepath = dir + "/" + path; +} + +const char *File :: get_filepath() +{ + return filepath.c_str(); +} diff --git a/tests/Sconscript b/tests/Sconscript index 403cc1df..78aa4fc7 100644 --- a/tests/Sconscript +++ b/tests/Sconscript @@ -49,6 +49,7 @@ Export("Test") scripts = [ "basic", "file" ] for s in scripts: CONFIG.reset() + CONFIG.TEST = True SConscript("%s/Sconscript" % s) Default("tests") diff --git a/tests/file/Sconscript b/tests/file/Sconscript new file mode 100644 index 00000000..a48edec3 --- /dev/null +++ b/tests/file/Sconscript @@ -0,0 +1,6 @@ +#!/usr/bin/python +Import("Test", "CONFIG") + +CONFIG.FILE = True + +Test("file", "compile.cpp") diff --git a/tests/file/compile-debug.good b/tests/file/compile-debug.good new file mode 100644 index 00000000..c8893370 --- /dev/null +++ b/tests/file/compile-debug.good @@ -0,0 +1,3 @@ +File path is: /home/anna/.config/ocarina-test/test.file +File path is: /home/anna/.local/share/ocarina-test/test.file +File path is: /home/anna/.ocarina-test/test.file diff --git a/tests/file/compile.cpp b/tests/file/compile.cpp new file mode 100644 index 00000000..68cedbbb --- /dev/null +++ b/tests/file/compile.cpp @@ -0,0 +1,19 @@ +/* + * Test that a file object can be created. + */ +#include +#include + +void test_filepath(FileLocHint hint) +{ + File file("test.file", hint); + print("File path is: %s\n", file.get_filepath()); +} + +int main(int argc, char **argv) +{ + test_filepath(FILE_TYPE_CONFIG); + test_filepath(FILE_TYPE_DATA); + test_filepath(FILE_TYPE_LEGACY); + return 0; +} diff --git a/tests/file/compile.good b/tests/file/compile.good new file mode 100644 index 00000000..c8893370 --- /dev/null +++ b/tests/file/compile.good @@ -0,0 +1,3 @@ +File path is: /home/anna/.config/ocarina-test/test.file +File path is: /home/anna/.local/share/ocarina-test/test.file +File path is: /home/anna/.ocarina-test/test.file