File: file.c

package info (click to toggle)
gltron 0.61-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,948 kB
  • ctags: 1,175
  • sloc: ansic: 7,580; perl: 168; makefile: 149; sh: 15
file content (87 lines) | stat: -rw-r--r-- 1,749 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// #include "gltron.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#ifdef macintosh
#    define SEPERATOR ':'
#endif

#ifdef WIN32
#    define SEPERATOR '\\'
#endif

#ifndef SEPERATOR
#    define SEPERATOR '/'
#endif

static int n_dirs = 3;

#ifdef macintosh 
static char *dirs[] = { ":Data", ":Art", ":Sounds" };
#else
static char *dirs[] = { "data", "art", "sounds" };
#endif

/* fopen() in MacOS on a directory returns NULL, here is a better solution */
/* that tells us if a file OR directory exists */
#ifdef macintosh

#include <Files.h>

extern OSStatus GetApplicationDirectory(short *vRefNum, long *dirID); /* in directory.c */

static int itemExists ( const char* path ) {
    
  OSStatus  err;
  Str255    relPath; 
  short     vRefNum;
  long      dirID;
  FSSpec    spec;

  if (*path != ':')
   sprintf (relPath, "%c:%s", (strlen(path) + 1), path);
  else 
   sprintf (relPath, "%c%s", (strlen(path)) , path);

  err = GetApplicationDirectory (&vRefNum, &dirID);
  if (err != noErr) {
    fprintf (stderr, "GetApplicationDirectory failed\n");
    exit (-1);
  }
  
  err = FSMakeFSSpec  (vRefNum, dirID, relPath, &spec);
  
  return (err == noErr);
}

#else /* UNIX, Windoze, MacOS X */

#include <unistd.h>

static int itemExists (const char* path) {

    return (access (path, F_OK) == 0);
}

#endif


char* getFullPath(char *filename) {
  char *path;

  int i;
  for(i = 0; i < n_dirs; i++) {
    path = malloc(strlen(dirs[i]) + 1 + strlen(filename) + 1);
    sprintf(path, "%s%c%s", dirs[i], SEPERATOR, filename);
    printf("checking '%s'...", path);
    if (itemExists (path) ) {
      printf("ok\n");
      return path;
    }
    free(path);
    printf("unsuccessful\n");
  }
  return NULL;
}