home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 79 / maccd 79.iso / multimedial / GL Tron / Source / gltron / file.c < prev    next >
Encoding:
Text File  |  2001-05-19  |  1.7 KB  |  95 lines  |  [TEXT/CWIE]

  1. // #include "gltron.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #ifdef macintosh
  6. #    define SEPERATOR ':'
  7. #endif
  8.  
  9. #ifdef WIN32
  10. #    define SEPERATOR '\\'
  11. #endif
  12.  
  13. #ifndef SEPERATOR
  14. #    define SEPERATOR '/'
  15. #endif
  16.  
  17. static int n_dirs = 2;
  18.  
  19. #ifdef macintosh 
  20. static char *dirs[] = { ":Data", ":art" };
  21. #else
  22. static char *dirs[] = { "data", "art" };
  23. #endif
  24.  
  25. /* -dw- methods to tell if a file or folder exists */
  26. #ifdef macintosh
  27.  
  28. #include <Files.h>
  29.  
  30. extern OSStatus GetApplicationDirectory(short *vRefNum, long *dirID); /* in directory.c */
  31.  
  32. static int itemExists ( const char* path ) {
  33.     
  34.   OSStatus err;
  35.  
  36.   Str255    relPath;
  37.   StringPtr pstr = relPath; /* Str255 is an array, but I want pointer arithmetic */
  38.   
  39.   short vRefNum;
  40.   long  dirID;
  41.   FSSpec spec;
  42.   
  43.   int len = strlen(path) + 1;
  44.   
  45.   pstr[0] = len;
  46.   
  47.   if (*path != ':') { /* we only handle relative paths, so make it so */
  48.     pstr[0]++;
  49.     pstr[1]  = ':';
  50.     pstr++;
  51.   }
  52.  
  53.   memcpy (pstr + 1, path, len);
  54.   pstr [ len ] = ':';
  55.  
  56.   err = GetApplicationDirectory (&vRefNum, &dirID);
  57.   if (err != noErr) {
  58.     fprintf (stderr, "GetApplicationDirectory failed\n");
  59.     exit (-1);
  60.   }
  61.   
  62.   err = FSMakeFSSpec  (vRefNum, dirID, relPath, &spec);
  63.   
  64.   return (err == noErr);
  65. }
  66.  
  67. #else
  68.  
  69. #include <unistd.h>
  70.  
  71. static int itemExists ( const char* path) {
  72.  
  73.     return access (path, F_OK);
  74. }
  75.  
  76. #endif
  77.  
  78. char* getFullPath(char *filename) {
  79.   char *path;
  80.  
  81.   int i;
  82.   for(i = 0; i < n_dirs; i++) {
  83.     path = malloc(strlen(dirs[i]) + 1 + strlen(filename) + 1);
  84.     sprintf(path, "%s%c%s", dirs[i], SEPERATOR, filename);
  85.     printf("checking '%s'...", path);
  86.     if ( itemExists (path) ) {
  87.       printf("ok\n");
  88.       return path;
  89.     }
  90.     free(path);
  91.     printf("unsuccessful\n");
  92.   }
  93.   return NULL;
  94. }
  95.