2013-02-01 11:20:46 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import <UIKit/UIDevice.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <stack>
|
|
|
|
#include "cocoa/CCString.h"
|
|
|
|
#include "CCFileUtils.h"
|
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "CCSAXParser.h"
|
|
|
|
#include "CCDictionary.h"
|
|
|
|
#include "support/zip_support/unzip.h"
|
|
|
|
|
|
|
|
#include "CCFileUtilsIOS.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
static void addValueToDict(id key, id value, Dictionary* pDict);
|
|
|
|
static void addObjectToNSDict(const char*key, Object* object, NSMutableDictionary *dict);
|
2013-02-03 10:56:59 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
static void addItemToArray(id item, Array *pArray)
|
2013-02-03 10:56:59 +08:00
|
|
|
{
|
|
|
|
// add string value into array
|
|
|
|
if ([item isKindOfClass:[NSString class]]) {
|
2013-06-20 14:13:12 +08:00
|
|
|
String* pValue = new String([item UTF8String]);
|
2013-02-03 10:56:59 +08:00
|
|
|
|
|
|
|
pArray->addObject(pValue);
|
|
|
|
pValue->release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add number value into array(such as int, float, bool and so on)
|
|
|
|
if ([item isKindOfClass:[NSNumber class]]) {
|
|
|
|
NSString* pStr = [item stringValue];
|
2013-06-20 14:13:12 +08:00
|
|
|
String* pValue = new String([pStr UTF8String]);
|
2013-02-03 10:56:59 +08:00
|
|
|
|
|
|
|
pArray->addObject(pValue);
|
|
|
|
pValue->release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add dictionary value into array
|
|
|
|
if ([item isKindOfClass:[NSDictionary class]]) {
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary* pDictItem = new Dictionary();
|
2013-02-03 10:56:59 +08:00
|
|
|
for (id subKey in [item allKeys]) {
|
|
|
|
id subValue = [item objectForKey:subKey];
|
2013-06-20 14:13:12 +08:00
|
|
|
addValueToDict(subKey, subValue, pDictItem);
|
2013-02-03 10:56:59 +08:00
|
|
|
}
|
|
|
|
pArray->addObject(pDictItem);
|
|
|
|
pDictItem->release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add array value into array
|
|
|
|
if ([item isKindOfClass:[NSArray class]]) {
|
2013-06-20 14:13:12 +08:00
|
|
|
Array *pArrayItem = new Array();
|
2013-02-03 10:56:59 +08:00
|
|
|
pArrayItem->init();
|
|
|
|
for (id subItem in item) {
|
2013-06-20 14:13:12 +08:00
|
|
|
addItemToArray(subItem, pArrayItem);
|
2013-02-03 10:56:59 +08:00
|
|
|
}
|
|
|
|
pArray->addObject(pArrayItem);
|
|
|
|
pArrayItem->release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
static void addObjectToNSArray(Object *object, NSMutableArray *array)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
|
|
|
// add string into array
|
2013-06-20 14:13:12 +08:00
|
|
|
if (String *ccString = dynamic_cast<String *>(object)) {
|
2013-04-13 16:55:24 +08:00
|
|
|
NSString *strElement = [NSString stringWithCString:ccString->getCString() encoding:NSUTF8StringEncoding];
|
|
|
|
[array addObject:strElement];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add array into array
|
2013-06-20 14:13:12 +08:00
|
|
|
if (Array *ccArray = dynamic_cast<Array *>(object)) {
|
2013-04-13 16:55:24 +08:00
|
|
|
NSMutableArray *arrElement = [NSMutableArray array];
|
2013-06-20 14:13:12 +08:00
|
|
|
Object *element = NULL;
|
2013-04-13 16:55:24 +08:00
|
|
|
CCARRAY_FOREACH(ccArray, element)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
addObjectToNSArray(element, arrElement);
|
2013-04-13 16:55:24 +08:00
|
|
|
}
|
|
|
|
[array addObject:arrElement];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add dictionary value into array
|
2013-06-20 14:13:12 +08:00
|
|
|
if (Dictionary *ccDict = dynamic_cast<Dictionary *>(object)) {
|
2013-04-13 16:55:24 +08:00
|
|
|
NSMutableDictionary *dictElement = [NSMutableDictionary dictionary];
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement *element = NULL;
|
2013-04-13 16:55:24 +08:00
|
|
|
CCDICT_FOREACH(ccDict, element)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
addObjectToNSDict(element->getStrKey(), element->getObject(), dictElement);
|
2013-04-13 16:55:24 +08:00
|
|
|
}
|
|
|
|
[array addObject:dictElement];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
static void addValueToDict(id key, id value, Dictionary* pDict)
|
2013-02-03 10:56:59 +08:00
|
|
|
{
|
|
|
|
// the key must be a string
|
|
|
|
CCAssert([key isKindOfClass:[NSString class]], "The key should be a string!");
|
|
|
|
std::string pKey = [key UTF8String];
|
|
|
|
|
|
|
|
// the value is a new dictionary
|
|
|
|
if ([value isKindOfClass:[NSDictionary class]]) {
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary* pSubDict = new Dictionary();
|
2013-02-03 10:56:59 +08:00
|
|
|
for (id subKey in [value allKeys]) {
|
|
|
|
id subValue = [value objectForKey:subKey];
|
2013-06-20 14:13:12 +08:00
|
|
|
addValueToDict(subKey, subValue, pSubDict);
|
2013-02-03 10:56:59 +08:00
|
|
|
}
|
|
|
|
pDict->setObject(pSubDict, pKey.c_str());
|
|
|
|
pSubDict->release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the value is a string
|
|
|
|
if ([value isKindOfClass:[NSString class]]) {
|
2013-06-20 14:13:12 +08:00
|
|
|
String* pValue = new String([value UTF8String]);
|
2013-02-03 10:56:59 +08:00
|
|
|
|
|
|
|
pDict->setObject(pValue, pKey.c_str());
|
|
|
|
pValue->release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the value is a number
|
|
|
|
if ([value isKindOfClass:[NSNumber class]]) {
|
|
|
|
NSString* pStr = [value stringValue];
|
2013-06-20 14:13:12 +08:00
|
|
|
String* pValue = new String([pStr UTF8String]);
|
2013-02-03 10:56:59 +08:00
|
|
|
|
|
|
|
pDict->setObject(pValue, pKey.c_str());
|
|
|
|
pValue->release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the value is a array
|
|
|
|
if ([value isKindOfClass:[NSArray class]]) {
|
2013-06-20 14:13:12 +08:00
|
|
|
Array *pArray = new Array();
|
2013-02-03 10:56:59 +08:00
|
|
|
pArray->init();
|
|
|
|
for (id item in value) {
|
2013-06-20 14:13:12 +08:00
|
|
|
addItemToArray(item, pArray);
|
2013-02-03 10:56:59 +08:00
|
|
|
}
|
|
|
|
pDict->setObject(pArray, pKey.c_str());
|
|
|
|
pArray->release();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
static void addObjectToNSDict(const char * key, Object* object, NSMutableDictionary *dict)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
|
|
|
NSString *NSkey = [NSString stringWithCString:key encoding:NSUTF8StringEncoding];
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// the object is a Dictionary
|
|
|
|
if (Dictionary *ccDict = dynamic_cast<Dictionary *>(object)) {
|
2013-04-13 16:55:24 +08:00
|
|
|
NSMutableDictionary *dictElement = [NSMutableDictionary dictionary];
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement *element = NULL;
|
2013-04-13 16:55:24 +08:00
|
|
|
CCDICT_FOREACH(ccDict, element)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
addObjectToNSDict(element->getStrKey(), element->getObject(), dictElement);
|
2013-04-13 16:55:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[dict setObject:dictElement forKey:NSkey];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// the object is a String
|
|
|
|
if (String *element = dynamic_cast<String *>(object)) {
|
2013-04-13 16:55:24 +08:00
|
|
|
NSString *strElement = [NSString stringWithCString:element->getCString() encoding:NSUTF8StringEncoding];
|
|
|
|
[dict setObject:strElement forKey:NSkey];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// the object is a Array
|
|
|
|
if (Array *ccArray = dynamic_cast<Array *>(object)) {
|
2013-04-13 16:55:24 +08:00
|
|
|
NSMutableArray *arrElement = [NSMutableArray array];
|
2013-06-20 14:13:12 +08:00
|
|
|
Object *element = NULL;
|
2013-04-13 16:55:24 +08:00
|
|
|
CCARRAY_FOREACH(ccArray, element)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
addObjectToNSArray(element, arrElement);
|
2013-04-13 16:55:24 +08:00
|
|
|
}
|
|
|
|
[dict setObject:arrElement forKey:NSkey];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
FileUtils* FileUtils::getInstance()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
if (s_sharedFileUtils == NULL)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
s_sharedFileUtils = new FileUtilsIOS();
|
2013-02-01 11:20:46 +08:00
|
|
|
s_sharedFileUtils->init();
|
|
|
|
}
|
|
|
|
return s_sharedFileUtils;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static NSFileManager* s_fileManager = [NSFileManager defaultManager];
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
std::string FileUtilsIOS::getWritablePath()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
// save to document folder
|
2013-02-06 18:11:27 +08:00
|
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
2013-02-01 11:20:46 +08:00
|
|
|
NSString *documentsDirectory = [paths objectAtIndex:0];
|
|
|
|
std::string strRet = [documentsDirectory UTF8String];
|
|
|
|
strRet.append("/");
|
|
|
|
return strRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool FileUtilsIOS::isFileExist(const std::string& strFilePath)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-04-25 21:51:13 +08:00
|
|
|
if (0 == strFilePath.length())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
bool bRet = false;
|
|
|
|
|
2013-02-01 22:19:58 +08:00
|
|
|
if (strFilePath[0] != '/')
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-04-25 22:02:20 +08:00
|
|
|
std::string path;
|
2013-02-01 11:20:46 +08:00
|
|
|
std::string file;
|
2013-04-25 22:02:20 +08:00
|
|
|
size_t pos = strFilePath.find_last_of("/");
|
2013-02-01 11:20:46 +08:00
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
2013-04-25 22:02:20 +08:00
|
|
|
file = strFilePath.substr(pos+1);
|
|
|
|
path = strFilePath.substr(0, pos+1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file = strFilePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString* fullpath = [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:file.c_str()]
|
|
|
|
ofType:nil
|
|
|
|
inDirectory:[NSString stringWithUTF8String:path.c_str()]];
|
|
|
|
if (fullpath != nil) {
|
|
|
|
bRet = true;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Search path is an absolute path.
|
2013-02-01 22:19:58 +08:00
|
|
|
if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:strFilePath.c_str()]]) {
|
2013-02-01 11:20:46 +08:00
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
std::string FileUtilsIOS::getFullPathForDirectoryAndFilename(const std::string& strDirectory, const std::string& strFilename)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
if (strDirectory[0] != '/')
|
|
|
|
{
|
|
|
|
NSString* fullpath = [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:strFilename.c_str()]
|
|
|
|
ofType:nil
|
|
|
|
inDirectory:[NSString stringWithUTF8String:strDirectory.c_str()]];
|
|
|
|
if (fullpath != nil) {
|
|
|
|
return [fullpath UTF8String];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string fullPath = strDirectory+strFilename;
|
|
|
|
// Search path is an absolute path.
|
|
|
|
if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:fullPath.c_str()]]) {
|
|
|
|
return fullPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool FileUtilsIOS::isAbsolutePath(const std::string& strPath)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
|
|
|
NSString* path = [NSString stringWithUTF8String:strPath.c_str()];
|
|
|
|
return [path isAbsolutePath] ? true : false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary* FileUtilsIOS::createDictionaryWithContentsOfFile(const std::string& filename)
|
2013-02-01 18:48:44 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename.c_str());
|
2013-02-01 18:48:44 +08:00
|
|
|
NSString* pPath = [NSString stringWithUTF8String:fullPath.c_str()];
|
|
|
|
NSDictionary* pDict = [NSDictionary dictionaryWithContentsOfFile:pPath];
|
|
|
|
|
2013-06-06 12:50:42 +08:00
|
|
|
if (pDict != nil)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Dictionary* pRet = new Dictionary();
|
2013-06-06 12:50:42 +08:00
|
|
|
for (id key in [pDict allKeys]) {
|
|
|
|
id value = [pDict objectForKey:key];
|
2013-06-20 14:13:12 +08:00
|
|
|
addValueToDict(key, value, pRet);
|
2013-06-06 12:50:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
2013-02-01 18:48:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool FileUtilsIOS::writeToFile(Dictionary *dict, const std::string &fullPath)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
//CCLOG("iOS||Mac Dictionary %d write to file %s", dict->_ID, fullPath.c_str());
|
2013-04-13 16:55:24 +08:00
|
|
|
NSMutableDictionary *nsDict = [NSMutableDictionary dictionary];
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement *element = NULL;
|
2013-04-13 16:55:24 +08:00
|
|
|
CCDICT_FOREACH(dict, element)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
addObjectToNSDict(element->getStrKey(), element->getObject(), nsDict);
|
2013-04-13 16:55:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NSString *file = [NSString stringWithUTF8String:fullPath.c_str()];
|
2013-04-16 12:38:53 +08:00
|
|
|
// do it atomically
|
|
|
|
[nsDict writeToFile:file atomically:YES];
|
2013-04-13 16:55:24 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* FileUtilsIOS::createArrayWithContentsOfFile(const std::string& filename)
|
2013-02-01 18:48:44 +08:00
|
|
|
{
|
|
|
|
// NSString* pPath = [NSString stringWithUTF8String:pFileName];
|
|
|
|
// NSString* pathExtension= [pPath pathExtension];
|
|
|
|
// pPath = [pPath stringByDeletingPathExtension];
|
|
|
|
// pPath = [[NSBundle mainBundle] pathForResource:pPath ofType:pathExtension];
|
2013-06-20 14:13:12 +08:00
|
|
|
// fixing cannot read data using Array::createWithContentsOfFile
|
2013-07-12 06:24:23 +08:00
|
|
|
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename.c_str());
|
2013-02-01 18:48:44 +08:00
|
|
|
NSString* pPath = [NSString stringWithUTF8String:fullPath.c_str()];
|
|
|
|
NSArray* pArray = [NSArray arrayWithContentsOfFile:pPath];
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* pRet = new Array();
|
2013-02-01 18:48:44 +08:00
|
|
|
for (id value in pArray) {
|
2013-06-20 14:13:12 +08:00
|
|
|
addItemToArray(value, pRet);
|
2013-02-01 18:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
NS_CC_END
|
|
|
|
|