SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
apps/source/platforms/ios/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 //-----------------------------------------------------------------------------
13 /*! Utils_iOS::fileExists returns true if the file exists. This code works
14 only Apple OSX and iOS. If no file matches, it checks all files of the same
15 directory and compares them case insensitive. If now one file matches the
16 passed filename is converted to the existing casesensitive filename.
17 Because I was not able to do this task in C++, I have to do this with a
18 C++/ObjectiveC mix.
19 */
20 bool Utils_iOS::fileExists(string& pathfilename)
21 {
22  // This stat compare is done casesensitive only on ARM hardware
23  struct stat stFileInfo;
24  if (stat(pathfilename.c_str(), &stFileInfo) == 0)
25  return true;
26 
27  // Get path and file name seperately and as NSString
28  std::string path = Utils::getPath(pathfilename);
29  std::string file = Utils::getFileName(pathfilename);
30  NSString* nsPath = [NSString stringWithCString:path.c_str()
31  encoding:[NSString defaultCStringEncoding]];
32  NSString* nsFile = [NSString stringWithCString:file.c_str()
33  encoding:[NSString defaultCStringEncoding]];
34 
35  NSFileManager* fileManager = [NSFileManager defaultManager];
36  if ([fileManager fileExistsAtPath:nsPath])
37  {
38  BOOL isDir = NO;
39  [fileManager fileExistsAtPath:nsPath isDirectory:(&isDir)];
40  if (isDir == YES)
41  {
42  NSArray* contents;
43  contents = [fileManager contentsOfDirectoryAtPath:nsPath error:nil];
44 
45  // Loop over all files of directory and compare caseinsensitive
46  for (NSString* entity in contents)
47  {
48  //NSLog(@"filesystemname = %@, searchname = %@", entity, nsFile);
49 
50  if ([entity length] == [nsFile length])
51  {
52  if ([entity caseInsensitiveCompare:nsFile] == NSOrderedSame)
53  {
54  // update the pathfilename with the real filename
55  pathfilename = path + [entity UTF8String];
56  return true;
57  }
58  }
59  }
60  }
61  }
62  return false;
63 }
64 //-----------------------------------------------------------------------------
65 vector<string> Utils_iOS::getAllNamesInDir(const string& dirName)
66 {
67  vector<string> folderContent;
68 
69  // Get path and file name seperately and as NSString
70  std::string path = Utils::getPath(dirName);
71  std::string folder = Utils::getFileName(dirName);
72 
73  NSString* nsPath = [NSString stringWithCString:path.c_str()
74  encoding:[NSString defaultCStringEncoding]];
75  NSString* nsFolder = [NSString stringWithCString:folder.c_str()
76  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  folderContent.emplace_back(path + [entity UTF8String]);
93  }
94  }
95  }
96  return folderContent;
97 }
98 //-----------------------------------------------------------------------------
100 {
101  // Get library directory for config file
102  NSArray* paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
103  NSUserDomainMask,
104  YES);
105  NSString* libraryDirectory = [paths objectAtIndex:0];
106  string configDir = [libraryDirectory UTF8String];
107  configDir += "/SLProject";
108  NSString* configPath = [NSString stringWithUTF8String:configDir.c_str()];
109 
110  // Create if it does not exist
111  NSError* error;
112  if (![[NSFileManager defaultManager] fileExistsAtPath:configPath])
113  [[NSFileManager defaultManager] createDirectoryAtPath:configPath
114  withIntermediateDirectories:NO
115  attributes:nil
116  error:&error];
117 
118  return configDir + "/";
119 }
120 //-----------------------------------------------------------------------------
122 {
123  // Get the main bundle path and pass it the SLTexture and SLShaderProg
124  // This will be the default storage location for textures and shaders
125  NSString* bundlePath = [[NSBundle mainBundle] resourcePath];
126  string cwd = [bundlePath UTF8String];
127  return cwd + "/";
128 }
129 //-----------------------------------------------------------------------------
130 bool Utils_iOS::deleteFile(std::string& pathfilename)
131 {
132  if (Utils_iOS::fileExists(pathfilename))
133  return remove(pathfilename.c_str()) != 0;
134  return false;
135 }
136 //-----------------------------------------------------------------------------
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