SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
modules/utils/source/Utils_iOS.mm
Go to the documentation of this file.
1 /**
2  * \file Utils_iOS.mm
3  * \date September 2011 (HS11)
4  * \authors Marcus Hudritsch
5  * \copyright http://opensource.org/licenses/GPL-3.0
6 */
7 
8 #include "Utils_iOS.h"
9 #include <Utils.h>
10 #include <sys/stat.h> //dirent
11 
12 #import <Foundation/Foundation.h>
13 //-----------------------------------------------------------------------------
14 /*! Utils_iOS::fileExists returns true if the file exists. This code works
15 only Apple OSX and iOS. If no file matches, it checks all files of the same
16 directory and compares them case insensitive. If now one file matches the
17 passed filename is converted to the existing casesensitive filename.
18 Because I was not able to do this task in C++, I have to do this with a
19 C++/ObjectiveC mix.
20 */
21 bool Utils_iOS::fileExists(string& pathfilename)
22 {
23  // This stat compare is done casesensitive only on ARM hardware
24  struct stat stFileInfo;
25  if (stat(pathfilename.c_str(), &stFileInfo) == 0)
26  return true;
27 
28  // Get path and file name seperately and as NSString
29  std::string path = Utils::getPath(pathfilename);
30  std::string file = Utils::getFileName(pathfilename);
31  NSString* nsPath = [NSString stringWithCString:path.c_str()
32  encoding:[NSString defaultCStringEncoding]];
33  NSString* nsFile = [NSString stringWithCString:file.c_str()
34  encoding:[NSString defaultCStringEncoding]];
35 
36  NSFileManager* fileManager = [NSFileManager defaultManager];
37  if ([fileManager fileExistsAtPath:nsPath])
38  {
39  BOOL isDir = NO;
40  [fileManager fileExistsAtPath:nsPath isDirectory:(&isDir)];
41  if (isDir == YES)
42  {
43  NSArray* contents;
44  contents = [fileManager contentsOfDirectoryAtPath:nsPath error:nil];
45 
46  // Loop over all files of directory and compare caseinsensitive
47  for (NSString* entity in contents)
48  {
49  // NSLog(@"filesystemname = %@, searchname = %@", entity, nsFile);
50 
51  if ([entity length] == [nsFile length])
52  {
53  if ([entity caseInsensitiveCompare:nsFile] == NSOrderedSame)
54  {
55  // update the pathfilename with the real filename
56  pathfilename = path + [entity UTF8String];
57  return true;
58  }
59  }
60  }
61  }
62  }
63  return false;
64 }
65 //-----------------------------------------------------------------------------
66 vector<string> Utils_iOS::getAllNamesInDir(const string& dirName, bool fullPath)
67 {
68  vector<string> folderContent;
69 
70  // Get path and file name seperately and as NSString
71  std::string path = Utils::getPath(dirName);
72  std::string folder = Utils::getFileName(dirName);
73 
74  NSString* nsPath = [NSString stringWithCString:path.c_str()
75  encoding:[NSString defaultCStringEncoding]];
76  [NSString stringWithCString:folder.c_str() encoding:[NSString defaultCStringEncoding]];
77 
78  NSFileManager* fileManager = [NSFileManager defaultManager];
79 
80  if ([fileManager fileExistsAtPath:nsPath])
81  {
82  BOOL isDir = NO;
83  [fileManager fileExistsAtPath:nsPath isDirectory:(&isDir)];
84 
85  if (isDir == YES)
86  {
87  NSArray* contents;
88  contents = [fileManager contentsOfDirectoryAtPath:nsPath error:nil];
89 
90  for (NSString* entity in contents)
91  {
92  if (fullPath)
93  folderContent.emplace_back(path + [entity UTF8String]);
94  else
95  folderContent.emplace_back([entity UTF8String]);
96  }
97  }
98  }
99  return folderContent;
100 }
101 //-----------------------------------------------------------------------------
103 {
104  // Get library directory for config file
105  NSArray* paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
106  NSUserDomainMask,
107  YES);
108  NSString* libraryDirectory = [paths objectAtIndex:0];
109  string configDir = [libraryDirectory UTF8String];
110  configDir += "/SLProject";
111  NSString* configPath = [NSString stringWithUTF8String:configDir.c_str()];
112 
113  // Create if it does not exist
114  NSError* error;
115  if (![[NSFileManager defaultManager] fileExistsAtPath:configPath])
116  [[NSFileManager defaultManager] createDirectoryAtPath:configPath
117  withIntermediateDirectories:NO
118  attributes:nil
119  error:&error];
120 
121  return configDir + "/";
122 }
123 //-----------------------------------------------------------------------------
125 {
126  // Get the main bundle path and pass it the SLTexture and SLShaderProg
127  // This will be the default storage location for textures and shaders
128  NSString* bundlePath = [[NSBundle mainBundle] resourcePath];
129  string cwd = [bundlePath UTF8String];
130  return cwd + "/";
131 }
132 //-----------------------------------------------------------------------------
133 bool Utils_iOS::deleteFile(std::string& pathfilename)
134 {
135  if (Utils_iOS::fileExists(pathfilename))
136  return remove(pathfilename.c_str()) != 0;
137  return false;
138 }
139 //-----------------------------------------------------------------------------
static bool deleteFile(std::string &pathfilename)
Deletes a file on the filesystem.
static std::vector< std::string > getAllNamesInDir(const std::string &dirName, bool fullPath=true)
Returns all files and folders in a directory as a vector.
static std::string getCurrentWorkingDir()
Returns the working directory.
static bool fileExists(std::string &pathfilename)
Returns true if a file exists.
static std::string getAppsWritableDir()
Returns the writable configuration directory.
string getFileName(const string &pathFilename)
Returns the filename of path-filename string.
Definition: Utils.cpp:580
string getPath(const string &pathFilename)
Returns the path w. '\' of path-filename string.
Definition: Utils.cpp:392