Skip to content

Commit

Permalink
Fixed some things in 'potion_find_file' and added some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
orangea committed Dec 31, 2010
1 parent a3aabec commit 1948649
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
30 changes: 20 additions & 10 deletions core/load.c
Expand Up @@ -80,11 +80,11 @@ void potion_load_dylib(Potion *P, const char *filename) {
}

static PN pn_loader_path;
static char *pn_loader_extensions[] = {
static const char *pn_loader_extensions[] = {
".pn", ".so", ".dylib"
};

static char *find_extension(char *str) {
static const char *find_extension(char *str) {
int i;
PN_SIZE str_len = strlen(str);
struct stat st;
Expand All @@ -110,21 +110,31 @@ char *potion_find_file(char *str, PN_SIZE str_len) {
char dirname[prefix_len + 1 + str_len + 1];
char *str_pos = dirname + prefix_len + 1;
char *dot;
const char *ext;
memcpy(str_pos, str, str_len);
for (dot = str_pos; dot < str_pos + str_len; dot++)
if (*dot == '.') { *dot = '\0'; break; }
if (dot == str_pos + str_len)
dirname[prefix_len + 1 + str_len] = '\0';
dot = memchr(str, '.', str_len);
if (dot == NULL)
dirname[sizeof(dirname) - 1] = '\0';
else
*dot = '\0';
memcpy(dirname, PN_STR_PTR(prefix), prefix_len);
dirname[prefix_len] = '/';
if (stat(dirname, &st) == 0 && S_ISREG(st.st_mode)) {
if (asprintf(&r, "%s", str) == -1) potion_allocation_error();
if (asprintf(&r, "%s", dirname) == -1) potion_allocation_error();
break;
} else if ((ext = find_extension(dirname)) != NULL) {
if (asprintf(&r, "%s%s", dirname, ext) == -1) potion_allocation_error();
break;
} else {
if (asprintf(&r, "%s/%s", dirname, str) == -1) potion_allocation_error();
char *file;
if ((file = strrchr(str, '/')) == NULL)
file = str;
else
file++;
if (asprintf(&r, "%s/%s", dirname, file) == -1) potion_allocation_error();
if (stat(r, &st) != 0 || !S_ISREG(st.st_mode)) {
char *ext = find_extension(r);
int r_len = prefix_len + 1 + str_len * 2 + 1;
if (ext == NULL) { free(r); r = NULL; continue; }
if ((ext = find_extension(r)) == NULL) { free(r); r = NULL; continue; }
r = realloc(r, r_len + strlen(ext));
if (r == NULL) potion_allocation_error();
strcpy(r + r_len, ext);
Expand Down
3 changes: 3 additions & 0 deletions test/classes/meta.pn
@@ -0,0 +1,3 @@
Q = class
Q meta m = : 0x068d.
Q m # 1677
1 change: 1 addition & 0 deletions test/misc/global.pn
@@ -0,0 +1 @@
X = 'X', (X, X kind) # (X, String)
2 changes: 2 additions & 0 deletions test/misc/load.pn
@@ -0,0 +1,2 @@
load 'test/misc/global'
X # X

0 comments on commit 1948649

Please sign in to comment.