lib: Add basic file class

The class resolves the path to the ocarina directory based on the file
hint provided.  NOTE: compile-debug.good and compile.good resolve paths
to my home directory.  Other users will need to change the values for
the test to pass.

Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
Bryan Schumaker 2013-07-27 11:40:16 -04:00 committed by Anna Schumaker
parent 307bc88cb6
commit e0a32c10ac
10 changed files with 122 additions and 1 deletions

1
.gitignore vendored
View File

@ -9,6 +9,5 @@
ocarina.bin
.sconsign.dblite
bin/
lib/
PKGBUILD
!PKGBUILD.tmpl

6
config
View File

@ -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()

25
include/file.h Normal file
View File

@ -0,0 +1,25 @@
/*
* Copyright 2013 (c) Bryan Schumaker.
*/
#ifndef OCARINA_FILE_H
#define OCARINA_FILE_H
#include <string>
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 */

10
lib/Sconscript Normal file
View File

@ -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")

49
lib/file.cpp Normal file
View File

@ -0,0 +1,49 @@
/*
* Copyright 2013 (c) Bryan Schumaker.
*/
#include <file.h>
#include <glib.h>
#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();
}

View File

@ -49,6 +49,7 @@ Export("Test")
scripts = [ "basic", "file" ]
for s in scripts:
CONFIG.reset()
CONFIG.TEST = True
SConscript("%s/Sconscript" % s)
Default("tests")

6
tests/file/Sconscript Normal file
View File

@ -0,0 +1,6 @@
#!/usr/bin/python
Import("Test", "CONFIG")
CONFIG.FILE = True
Test("file", "compile.cpp")

View File

@ -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

19
tests/file/compile.cpp Normal file
View File

@ -0,0 +1,19 @@
/*
* Test that a file object can be created.
*/
#include <file.h>
#include <print.h>
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;
}

3
tests/file/compile.good Normal file
View File

@ -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