home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Languages / python / PyObjC-0.47-MIHS / pyobjc-0.47-src / Modules / OC_PythonBundle.m < prev    next >
Encoding:
Text File  |  1996-10-23  |  3.7 KB  |  157 lines

  1. /* Copyright (c) 1996 by Lele Gaifax.  All Rights Reserved
  2.  *
  3.  * This software may be used and distributed freely for any purpose
  4.  * provided that this notice is included unchanged on any and all
  5.  * copies. The author does not warrant or guarantee this software in
  6.  * any way.
  7.  *
  8.  * This file is part of the PyObjC package.
  9.  *
  10.  * $RCSfile: OC_PythonBundle.m,v $
  11.  * $Revision: 1.1.1.1 $
  12.  * $Date: 1996/10/23 17:19:52 $
  13.  *
  14.  * Created Wed Oct 16 17:39:54 1996.
  15.  */
  16.  
  17. #include <sys/param.h>
  18. #include "Python.h"
  19. #include "ObjC.h"
  20. #include "OC_PythonBundle.h"
  21. #include "ceval.h"
  22. #include "compile.h"
  23. #include "frameobject.h"
  24.  
  25. #define CLASS_VERSION 0
  26.  
  27. @implementation OC_PythonBundle
  28.  
  29. + (void) initialize
  30. {
  31.   if (self == [OC_PythonBundle class])
  32.     {
  33.       [OC_PythonBundle setVersion:CLASS_VERSION];
  34.     }
  35. }
  36.  
  37. + (OC_PYTHONBUNDLE_SUPER_CLASS *) mainBundle
  38. {
  39.   static OC_PYTHONBUNDLE_SUPER_CLASS *mainScriptBundle = nil;
  40.  
  41.   if (!mainScriptBundle)
  42.     {
  43.       PyFrameObject *frame = (PyFrameObject *) PyEval_GetFrame();
  44.       const char *mainscript;
  45.  
  46.       /* go back to the first frame */
  47.       while (frame->f_back)
  48.     frame = frame->f_back;
  49.       
  50.       mainscript = PyString_AsString (frame->f_code->co_filename);
  51.       if (strcmp (mainscript, "<stdin>"))
  52.     {
  53.       const char *lslash = strrchr (mainscript, '/');
  54.       
  55.       if (lslash)
  56.         {
  57.           unsigned int len = lslash - mainscript;
  58.           char pathbuf[len + 1];
  59.  
  60.           memcpy (pathbuf, mainscript, len);
  61.           pathbuf[len] = 0;
  62.  
  63.           mainScriptBundle = [OC_PYTHONBUNDLE_SUPER_CLASS alloc];
  64. #ifndef WITH_FOUNDATION
  65.           [mainScriptBundle initForDirectory:pathbuf];
  66. #else
  67.           [mainScriptBundle initWithPath:
  68.                   [NSString stringWithCString:pathbuf]];
  69. #endif
  70.         }
  71.     }
  72.  
  73.       if (!mainScriptBundle)
  74.     {
  75.       char cwdbuf[1024];
  76.       extern const char *getcwd (char *, unsigned int);
  77.       
  78.       if (getcwd (cwdbuf, sizeof (cwdbuf)))
  79.         {
  80.           mainScriptBundle = [OC_PYTHONBUNDLE_SUPER_CLASS alloc];
  81. #ifndef WITH_FOUNDATION
  82.           [mainScriptBundle initForDirectory:cwdbuf];
  83. #else
  84.           [mainScriptBundle initWithPath:
  85.                   [NSString stringWithCString:cwdbuf]];
  86. #endif
  87.         }
  88.     }
  89.     }
  90.  
  91.   return mainScriptBundle;
  92. }
  93.  
  94. @end /* OC_PythonBundle class implementation */
  95.  
  96. #ifndef WITH_FOUNDATION
  97.  
  98. @implementation NXBundle (OC_PythonBundle)
  99.  
  100. /* XXX I think this approach is safe, since these methods will almost
  101.    surely performed through the PyObjC module, and it will convert the
  102.    return value to a PyStringObject immediately. */
  103. static char pathbuf[MAXPATHLEN+1];
  104.  
  105. + (const char *) pathForResource:(const char *) name
  106.               ofType:(const char *) ext
  107.              inDirectory:(const char *) bundlePath
  108.              withVersion:(int) version
  109. {
  110.   if ([self getPath:pathbuf
  111.         forResource:name
  112.         ofType:ext
  113.         inDirectory:bundlePath
  114.         withVersion:version])
  115.     return pathbuf;
  116.   else
  117.     {
  118. #define ERRMSG "Resource `%s' of type `%s' is not available within `%s'"
  119.       char errbuf[sizeof ERRMSG + strlen (name) + strlen (ext) + strlen (bundlePath)];
  120.  
  121.       sprintf (errbuf, ERRMSG, name, ext, bundlePath);
  122.       PyErr_SetString (ObjC_Error, errbuf);
  123. #undef ERRMSG
  124.       return NULL;
  125.     }
  126. }
  127.   
  128. - (const char *) pathForResource:(const char *) name
  129.               ofType:(const char *) ext
  130. {
  131.   if ([self getPath:pathbuf
  132.         forResource:name
  133.         ofType:ext])
  134.     return pathbuf;
  135.   else
  136.     {
  137. #define ERRMSG "Resource `%s' of type `%s' is not available within `%s'"
  138.       const char *bundlePath = [self directory];
  139.       char errbuf[sizeof ERRMSG + strlen (name) + strlen (ext) + strlen (bundlePath)];
  140.  
  141.       sprintf (errbuf, ERRMSG, name, ext, bundlePath);
  142.       PyErr_SetString (ObjC_Error, errbuf);
  143. #undef ERRMSG
  144.       return NULL;
  145.     }
  146. }
  147.   
  148. #endif
  149.  
  150. @end
  151.  
  152. /*
  153. ** Local Variables:
  154. ** change-log-default-name:"../ChangeLog.PyObjC"
  155. ** End:
  156. */
  157.