CodingRules


Writing code with libFoundation

Preprocessor defines

There are some things to have to know when porting an OpenStep program to work with libFoundation. They do not change the general behavior of your program but they make your program work.

libFoundation defines some preprocessor values so you can check for them if you want to conditionally pass some code to the compiler. The following macros are defined in Foundation/NSObject.h:

#define LIB_FOUNDATION_LIBRARY 1 #define LIB_FOUNDATION_MAJOR_VERSION 0 #define LIB_FOUNDATION_MINOR_VERSION 7 #define LIB_FOUNDATION_SUBMINOR_VERSION 0

If you want to include a portion of code that is specific to libFoundation you should put it inside an #ifdef preprocessor command:

#ifdef LIB_FOUNDATION_LIBRARY ... #endif

This way you the code inside #ifdef is compiled only in the presence of libFoundation and will not affect other libraries.

Initializing your program

It is very important to properly initialize some libFoundation internal data structures when the program starts. These information are mainly related to the NSProcessInfo class. Because not on all platforms is possible to find out the arguments and the environment variables passed to the program, we have chosen to explicitly delegate this task to user. The first lines in a libFoundation program, before creating any other objects, should be the following:

void main (int argc, char** argv, char** env) { /* Declarations here */ ... #ifdef LIB_FOUNDATION_LIBRARY [NSProcessInfo initializeWithArguments:argv count:argc environment:env]; #endif /* Object initializations and other instructions from here */ ... }

Specifying the resources directory

It is very common to want to try the tests before you have installed the library in its default place. libFoundation however requires to have access to the resource files to function correctly. You can install only the resources in the installation directory, but there is more convenient way to let the library where are its resources. You can specify an environment variable that indicates where is the resource directory; the name of this variable is LIB_FOUNDATION_RESOURCES_PATH and it should indicate the Resources directory. In a sh-like shell for example, you can do:

LIB_FOUNDATION_RESOURCES_PATH=/home/ovidiu/libFoundation-0.7/libFoundation/Resources export LIB_FOUNDATION_RESOURCES_PATH

This environment variable is similar with the PATH variable: you can specify more directories by separating them using : character. Specifying resource directories this way takes precedence over the default installation directory. You can use this to change the resources if you want.