// @ Project : hello eagl ++ // @ File Name : IO.mm // @ Date : 2008-4-8 // @ Author : kongfu.yang // @ Company : http://www.play3d.net // @ Copyright : 2008-4, Example source license. // By keep this header comment, // you may use this source file in your project for non-commercial or commercial purpose. #import "IO.h" #import "Foundation/Foundation.h" #ifdef _cplusplus extern "C" { #endif // cpp static inline unsigned int getCString(NSString * str, char * cstr, unsigned int bufSize) { const char * utf8 = [str UTF8String]; unsigned int size = [str length]; if ( size > bufSize ) size = bufSize-1; memcpy(cstr, utf8, size); return size; } int getCurrentDir(char * path, unsigned int maxLen) { NSString * cur = [[NSFileManager defaultManager] currentDirectoryPath]; return getCString(cur, path, maxLen); } int getUserHome(char * path, unsigned int maxLen) { NSString * home = NSHomeDirectory(); return getCString(home, path, maxLen); } int getAppHome(char * path, unsigned int maxLen) { NSBundle * mb = [NSBundle mainBundle]; NSString * mbp = [mb bundlePath]; return getCString(mbp, path, maxLen); } inline unsigned short pathSeparator(void) { return '/'; } void appendPath(char * path, char * component) { unsigned int len = strlen(path); if ( path[len] != pathSeparator() ) { path[len] = (char) pathSeparator(); path[len+1] = 0; } char * p = component; if ( *p == pathSeparator() ) p++; strcat(path, p); } char * concatePath(char * path1, char * path2) { size_t len1 = strlen(path1); size_t len2 = strlen(path2); char * resultPath = (char * ) malloc( len1 + len2 + 1 ); strcpy(resultPath, path1); appendPath(resultPath, path2); return resultPath; } #ifdef _cplusplus } #endif // cpp