2013-02-01 11:20:46 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
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>
|
|
|
|
|
2014-09-10 07:50:02 +08:00
|
|
|
#include "CCFileUtils-apple.h"
|
2014-09-10 03:18:31 +08:00
|
|
|
|
2015-07-10 14:37:42 +08:00
|
|
|
#include <ftw.h>
|
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
#include <string>
|
|
|
|
#include <stack>
|
2014-09-10 03:18:31 +08:00
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2014-09-10 03:18:31 +08:00
|
|
|
#include "deprecated/CCString.h"
|
|
|
|
#include "deprecated/CCDictionary.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "platform/CCSAXParser.h"
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
static void addValueToDict(id nsKey, id nsValue, ValueMap& dict);
|
2013-12-20 09:54:50 +08:00
|
|
|
static void addObjectToNSDict(const std::string& key, const Value& value, NSMutableDictionary *dict);
|
2013-02-03 10:56:59 +08:00
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
static void addItemToArray(id item, ValueVector& array)
|
2013-02-03 10:56:59 +08:00
|
|
|
{
|
|
|
|
// add string value into array
|
2013-12-03 14:47:35 +08:00
|
|
|
if ([item isKindOfClass:[NSString class]])
|
|
|
|
{
|
|
|
|
array.push_back(Value([item UTF8String]));
|
2013-02-03 10:56:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-02-03 10:56:59 +08:00
|
|
|
// add number value into array(such as int, float, bool and so on)
|
2014-03-19 11:58:11 +08:00
|
|
|
// the value is a number
|
2013-12-03 14:47:35 +08:00
|
|
|
if ([item isKindOfClass:[NSNumber class]])
|
|
|
|
{
|
2014-03-19 11:58:11 +08:00
|
|
|
NSNumber* num = item;
|
|
|
|
const char* numType = [num objCType];
|
|
|
|
if(num == (void*)kCFBooleanFalse || num == (void*)kCFBooleanTrue)
|
|
|
|
{
|
2016-02-16 17:01:54 +08:00
|
|
|
bool v = [num boolValue];
|
|
|
|
array.push_back(Value(v));
|
2014-03-19 11:58:11 +08:00
|
|
|
}
|
|
|
|
else if(strcmp(numType, @encode(float)) == 0)
|
|
|
|
{
|
|
|
|
array.push_back(Value([num floatValue]));
|
|
|
|
}
|
|
|
|
else if(strcmp(numType, @encode(double)) == 0)
|
|
|
|
{
|
|
|
|
array.push_back(Value([num doubleValue]));
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
array.push_back(Value([num intValue]));
|
|
|
|
}
|
2013-02-03 10:56:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-03-19 11:58:11 +08:00
|
|
|
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-02-03 10:56:59 +08:00
|
|
|
// add dictionary value into array
|
2013-12-03 14:47:35 +08:00
|
|
|
if ([item isKindOfClass:[NSDictionary class]])
|
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap dict;
|
2013-12-03 14:47:35 +08:00
|
|
|
for (id subKey in [item allKeys])
|
|
|
|
{
|
2013-02-03 10:56:59 +08:00
|
|
|
id subValue = [item objectForKey:subKey];
|
2013-12-03 14:47:35 +08:00
|
|
|
addValueToDict(subKey, subValue, dict);
|
2013-02-03 10:56:59 +08:00
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
array.push_back(Value(dict));
|
2013-02-03 10:56:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-02-03 10:56:59 +08:00
|
|
|
// add array value into array
|
2013-12-03 14:47:35 +08:00
|
|
|
if ([item isKindOfClass:[NSArray class]])
|
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueVector subArray;
|
2013-12-03 14:47:35 +08:00
|
|
|
for (id subItem in item)
|
|
|
|
{
|
|
|
|
addItemToArray(subItem, subArray);
|
2013-02-03 10:56:59 +08:00
|
|
|
}
|
2013-12-03 14:47:35 +08:00
|
|
|
array.push_back(Value(subArray));
|
2013-02-03 10:56:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 09:54:50 +08:00
|
|
|
static void addObjectToNSArray(const Value& value, NSMutableArray *array)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
|
|
|
// add string into array
|
2013-12-03 14:47:35 +08:00
|
|
|
if (value.getType() == Value::Type::STRING)
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
NSString *element = [NSString stringWithCString:value.asString().c_str() encoding:NSUTF8StringEncoding];
|
|
|
|
[array addObject:element];
|
2013-04-13 16:55:24 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2014-03-18 14:59:59 +08:00
|
|
|
//add float into array
|
|
|
|
if (value.getType() == Value::Type::FLOAT) {
|
|
|
|
NSNumber *number = [NSNumber numberWithFloat:value.asFloat()];
|
|
|
|
[array addObject:number];
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2014-03-18 14:59:59 +08:00
|
|
|
//add double into array
|
|
|
|
if (value.getType() == Value::Type::DOUBLE) {
|
|
|
|
NSNumber *number = [NSNumber numberWithDouble:value.asDouble()];
|
|
|
|
[array addObject:number];
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2014-03-18 14:59:59 +08:00
|
|
|
//add boolean into array
|
|
|
|
if (value.getType() == Value::Type::BOOLEAN) {
|
|
|
|
NSNumber *element = [NSNumber numberWithBool:value.asBool()];
|
|
|
|
[array addObject:element];
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2014-03-18 14:59:59 +08:00
|
|
|
if (value.getType() == Value::Type::INTEGER) {
|
|
|
|
NSNumber *element = [NSNumber numberWithInt:value.asInt()];
|
|
|
|
[array addObject:element];
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2014-03-18 14:59:59 +08:00
|
|
|
//todo: add date and data support
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-04-13 16:55:24 +08:00
|
|
|
// add array into array
|
2013-12-04 17:46:57 +08:00
|
|
|
if (value.getType() == Value::Type::VECTOR)
|
2013-12-03 14:47:35 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
NSMutableArray *element = [NSMutableArray array];
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueVector valueArray = value.asValueVector();
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-12-20 09:54:50 +08:00
|
|
|
for (const auto &e : valueArray)
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
addObjectToNSArray(e, element);
|
2013-12-20 09:54:50 +08:00
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
[array addObject:element];
|
2013-04-13 16:55:24 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-04-13 16:55:24 +08:00
|
|
|
// add dictionary value into array
|
2013-12-04 17:46:57 +08:00
|
|
|
if (value.getType() == Value::Type::MAP)
|
2013-12-03 14:47:35 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
NSMutableDictionary *element = [NSMutableDictionary dictionary];
|
2013-12-03 14:47:35 +08:00
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
auto valueDict = value.asValueMap();
|
2013-12-03 14:47:35 +08:00
|
|
|
for (auto iter = valueDict.begin(); iter != valueDict.end(); ++iter)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
addObjectToNSDict(iter->first, iter->second, element);
|
2013-04-13 16:55:24 +08:00
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
[array addObject:element];
|
2013-04-13 16:55:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
static void addValueToDict(id nsKey, id nsValue, ValueMap& dict)
|
2013-02-03 10:56:59 +08:00
|
|
|
{
|
|
|
|
// the key must be a string
|
2013-12-03 14:47:35 +08:00
|
|
|
CCASSERT([nsKey isKindOfClass:[NSString class]], "The key should be a string!");
|
|
|
|
std::string key = [nsKey UTF8String];
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
// the value is a string
|
|
|
|
if ([nsValue isKindOfClass:[NSString class]])
|
|
|
|
{
|
|
|
|
dict[key] = Value([nsValue UTF8String]);
|
2013-02-03 10:56:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
// the value is a number
|
|
|
|
if ([nsValue isKindOfClass:[NSNumber class]])
|
|
|
|
{
|
2014-03-19 11:58:11 +08:00
|
|
|
NSNumber* num = nsValue;
|
|
|
|
const char* numType = [num objCType];
|
|
|
|
if(num == (void*)kCFBooleanFalse || num == (void*)kCFBooleanTrue)
|
|
|
|
{
|
2016-02-16 16:50:54 +08:00
|
|
|
bool v = [num boolValue];
|
|
|
|
dict[key] = Value(v);
|
2014-03-19 11:58:11 +08:00
|
|
|
}
|
|
|
|
else if(strcmp(numType, @encode(float)) == 0)
|
|
|
|
{
|
|
|
|
dict[key] = Value([num floatValue]);
|
|
|
|
}
|
|
|
|
else if(strcmp(numType, @encode(double)) == 0)
|
|
|
|
{
|
|
|
|
dict[key] = Value([num doubleValue]);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
dict[key] = Value([num intValue]);
|
|
|
|
}
|
2013-02-03 10:56:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-03-18 14:59:59 +08:00
|
|
|
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
// the value is a new dictionary
|
|
|
|
if ([nsValue isKindOfClass:[NSDictionary class]])
|
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap subDict;
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
for (id subKey in [nsValue allKeys])
|
|
|
|
{
|
|
|
|
id subValue = [nsValue objectForKey:subKey];
|
|
|
|
addValueToDict(subKey, subValue, subDict);
|
|
|
|
}
|
|
|
|
dict[key] = Value(subDict);
|
2013-02-03 10:56:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-02-03 10:56:59 +08:00
|
|
|
// the value is a array
|
2013-12-03 14:47:35 +08:00
|
|
|
if ([nsValue isKindOfClass:[NSArray class]])
|
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueVector valueArray;
|
2013-12-03 14:47:35 +08:00
|
|
|
|
|
|
|
for (id item in nsValue)
|
|
|
|
{
|
|
|
|
addItemToArray(item, valueArray);
|
2013-02-03 10:56:59 +08:00
|
|
|
}
|
2013-12-03 14:47:35 +08:00
|
|
|
dict[key] = Value(valueArray);
|
2013-02-03 10:56:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-02-03 10:56:59 +08:00
|
|
|
}
|
|
|
|
|
2013-12-20 09:54:50 +08:00
|
|
|
static void addObjectToNSDict(const std::string& key, const Value& value, NSMutableDictionary *dict)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
NSString *NSkey = [NSString stringWithCString:key.c_str() encoding:NSUTF8StringEncoding];
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// the object is a Dictionary
|
2013-12-04 17:46:57 +08:00
|
|
|
if (value.getType() == Value::Type::MAP)
|
2013-12-03 14:47:35 +08:00
|
|
|
{
|
2013-04-13 16:55:24 +08:00
|
|
|
NSMutableDictionary *dictElement = [NSMutableDictionary dictionary];
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap subDict = value.asValueMap();
|
2013-12-03 14:47:35 +08:00
|
|
|
for (auto iter = subDict.begin(); iter != subDict.end(); ++iter)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
addObjectToNSDict(iter->first, iter->second, dictElement);
|
2013-04-13 16:55:24 +08:00
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-04-13 16:55:24 +08:00
|
|
|
[dict setObject:dictElement forKey:NSkey];
|
|
|
|
return;
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2014-03-18 14:59:59 +08:00
|
|
|
//add float into dict
|
|
|
|
if (value.getType() == Value::Type::FLOAT) {
|
|
|
|
NSNumber *number = [NSNumber numberWithFloat:value.asFloat()];
|
|
|
|
[dict setObject:number forKey:NSkey];
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2014-03-18 14:59:59 +08:00
|
|
|
//add double into dict
|
|
|
|
if (value.getType() == Value::Type::DOUBLE) {
|
|
|
|
NSNumber *number = [NSNumber numberWithDouble:value.asDouble()];
|
|
|
|
[dict setObject:number forKey:NSkey];
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2014-03-18 14:59:59 +08:00
|
|
|
//add boolean into dict
|
|
|
|
if (value.getType() == Value::Type::BOOLEAN) {
|
|
|
|
NSNumber *element = [NSNumber numberWithBool:value.asBool()];
|
|
|
|
[dict setObject:element forKey:NSkey];
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2014-03-18 14:59:59 +08:00
|
|
|
//add integer into dict
|
|
|
|
if (value.getType() == Value::Type::INTEGER) {
|
|
|
|
NSNumber *element = [NSNumber numberWithInt:value.asInt()];
|
|
|
|
[dict setObject:element forKey:NSkey];
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// the object is a String
|
2013-12-03 14:47:35 +08:00
|
|
|
if (value.getType() == Value::Type::STRING)
|
|
|
|
{
|
|
|
|
NSString *strElement = [NSString stringWithCString:value.asString().c_str() encoding:NSUTF8StringEncoding];
|
2013-04-13 16:55:24 +08:00
|
|
|
[dict setObject:strElement forKey:NSkey];
|
|
|
|
return;
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// the object is a Array
|
2013-12-04 17:46:57 +08:00
|
|
|
if (value.getType() == Value::Type::VECTOR)
|
2013-12-03 14:47:35 +08:00
|
|
|
{
|
2013-04-13 16:55:24 +08:00
|
|
|
NSMutableArray *arrElement = [NSMutableArray array];
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueVector array = value.asValueVector();
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-12-20 09:54:50 +08:00
|
|
|
for(const auto& v : array)
|
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
addObjectToNSArray(v, arrElement);
|
2013-12-20 09:54:50 +08:00
|
|
|
}
|
2013-12-03 14:47:35 +08:00
|
|
|
|
2013-04-13 16:55:24 +08:00
|
|
|
[dict setObject:arrElement forKey:NSkey];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-24 18:56:59 +08:00
|
|
|
FileUtilsApple::FileUtilsApple() {
|
|
|
|
_bundle = [NSBundle mainBundle];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileUtilsApple::setBundle(NSBundle* bundle) {
|
|
|
|
_bundle = bundle;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSBundle* FileUtilsApple::getBundle() const {
|
|
|
|
return _bundle;
|
|
|
|
}
|
|
|
|
|
2013-09-07 06:33:28 +08:00
|
|
|
|
|
|
|
#pragma mark - FileUtils
|
|
|
|
|
|
|
|
static NSFileManager* s_fileManager = [NSFileManager defaultManager];
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
FileUtils* FileUtils::getInstance()
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
if (s_sharedFileUtils == nullptr)
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
s_sharedFileUtils = new (std::nothrow) FileUtilsApple();
|
2013-07-23 20:44:08 +08:00
|
|
|
if(!s_sharedFileUtils->init())
|
|
|
|
{
|
|
|
|
delete s_sharedFileUtils;
|
2013-12-18 17:47:20 +08:00
|
|
|
s_sharedFileUtils = nullptr;
|
2013-09-07 06:33:28 +08:00
|
|
|
CCLOG("ERROR: Could not init CCFileUtilsApple");
|
2013-07-23 20:44:08 +08:00
|
|
|
}
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
return s_sharedFileUtils;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-07 13:52:23 +08:00
|
|
|
std::string FileUtilsApple::getWritablePath() const
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2015-02-28 18:21:57 +08:00
|
|
|
if (_writablePath.length())
|
|
|
|
{
|
|
|
|
return _writablePath;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-02 15:35:09 +08:00
|
|
|
bool FileUtilsApple::isFileExistInternal(const std::string& filePath) const
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2014-04-02 15:35:09 +08:00
|
|
|
if (filePath.empty())
|
2013-04-25 21:51:13 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
bool ret = false;
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
if (filePath[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-12-18 17:47:20 +08:00
|
|
|
size_t pos = filePath.find_last_of("/");
|
2013-02-01 11:20:46 +08:00
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
file = filePath.substr(pos+1);
|
|
|
|
path = filePath.substr(0, pos+1);
|
2013-04-25 22:02:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
file = filePath;
|
2013-04-25 22:02:20 +08:00
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
|
|
|
NSString* fullpath = [getBundle() pathForResource:[NSString stringWithUTF8String:file.c_str()]
|
2013-04-25 22:02:20 +08:00
|
|
|
ofType:nil
|
|
|
|
inDirectory:[NSString stringWithUTF8String:path.c_str()]];
|
|
|
|
if (fullpath != nil) {
|
2013-12-18 17:47:20 +08:00
|
|
|
ret = true;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Search path is an absolute path.
|
2013-12-18 17:47:20 +08:00
|
|
|
if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:filePath.c_str()]]) {
|
|
|
|
ret = true;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
static int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
|
2015-07-10 14:37:42 +08:00
|
|
|
{
|
2015-07-13 17:06:01 +08:00
|
|
|
auto ret = remove(fpath);
|
|
|
|
if (ret)
|
2015-07-10 14:37:42 +08:00
|
|
|
{
|
2015-07-13 17:06:01 +08:00
|
|
|
log("Fail to remove: %s ",fpath);
|
2015-07-10 14:37:42 +08:00
|
|
|
}
|
2015-07-13 17:06:01 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileUtilsApple::removeDirectory(const std::string& path)
|
|
|
|
{
|
|
|
|
if (path.size() > 0 && path[path.size() - 1] != '/')
|
|
|
|
{
|
|
|
|
CCLOGERROR("Fail to remove directory, path must termniate with '/': %s", path.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nftw(path.c_str(),unlink_cb, 64, FTW_DEPTH | FTW_PHYS))
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
2015-07-10 14:37:42 +08:00
|
|
|
}
|
|
|
|
|
2015-04-07 22:15:15 +08:00
|
|
|
std::string FileUtilsApple::getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename) const
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
if (directory[0] != '/')
|
2013-02-01 11:20:46 +08:00
|
|
|
{
|
2014-07-24 18:56:59 +08:00
|
|
|
NSString* fullpath = [getBundle() pathForResource:[NSString stringWithUTF8String:filename.c_str()]
|
2013-02-01 11:20:46 +08:00
|
|
|
ofType:nil
|
2013-12-18 17:47:20 +08:00
|
|
|
inDirectory:[NSString stringWithUTF8String:directory.c_str()]];
|
2013-02-01 11:20:46 +08:00
|
|
|
if (fullpath != nil) {
|
|
|
|
return [fullpath UTF8String];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
std::string fullPath = directory+filename;
|
2013-02-01 11:20:46 +08:00
|
|
|
// Search path is an absolute path.
|
|
|
|
if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:fullPath.c_str()]]) {
|
|
|
|
return fullPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2013-12-04 17:50:57 +08:00
|
|
|
ValueMap FileUtilsApple::getValueMapFromFile(const std::string& filename)
|
2013-02-01 18:48:44 +08:00
|
|
|
{
|
2013-09-07 13:52:23 +08:00
|
|
|
std::string fullPath = fullPathForFilename(filename);
|
2013-09-18 16:06:39 +08:00
|
|
|
NSString* path = [NSString stringWithUTF8String:fullPath.c_str()];
|
|
|
|
NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:path];
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap ret;
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-09-18 16:06:39 +08:00
|
|
|
if (dict != nil)
|
2013-06-06 12:50:42 +08:00
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
for (id key in [dict allKeys])
|
|
|
|
{
|
2013-09-18 16:06:39 +08:00
|
|
|
id value = [dict objectForKey:key];
|
|
|
|
addValueToDict(key, value, ret);
|
2013-06-06 12:50:42 +08:00
|
|
|
}
|
2013-02-01 18:48:44 +08:00
|
|
|
}
|
2013-12-03 14:47:35 +08:00
|
|
|
return ret;
|
2013-02-01 18:48:44 +08:00
|
|
|
}
|
|
|
|
|
2014-07-29 17:06:43 +08:00
|
|
|
ValueMap FileUtilsApple::getValueMapFromData(const char* filedata, int filesize)
|
|
|
|
{
|
|
|
|
NSData* file = [NSData dataWithBytes:filedata length:filesize];
|
|
|
|
NSPropertyListFormat format;
|
|
|
|
NSError* error;
|
|
|
|
NSDictionary* dict = [NSPropertyListSerialization propertyListWithData:file options:NSPropertyListImmutable format:&format error:&error];
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-07-29 17:06:43 +08:00
|
|
|
ValueMap ret;
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2014-07-29 17:06:43 +08:00
|
|
|
if (dict != nil)
|
|
|
|
{
|
|
|
|
for (id key in [dict allKeys])
|
|
|
|
{
|
|
|
|
id value = [dict objectForKey:key];
|
|
|
|
addValueToDict(key, value, ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-01-27 11:22:46 +08:00
|
|
|
bool FileUtilsApple::writeToFile(const ValueMap& dict, const std::string &fullPath)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
2015-07-07 14:06:59 +08:00
|
|
|
return writeValueMapToFile(dict, fullPath);
|
|
|
|
}
|
|
|
|
|
2016-01-27 11:22:46 +08:00
|
|
|
bool FileUtils::writeValueMapToFile(const ValueMap& dict, const std::string& fullPath)
|
2015-07-07 14:06:59 +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];
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
for (auto iter = dict.begin(); iter != dict.end(); ++iter)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
addObjectToNSDict(iter->first, iter->second, nsDict);
|
2013-04-13 16:55:24 +08:00
|
|
|
}
|
2015-07-07 14:06:59 +08:00
|
|
|
|
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];
|
2015-07-07 14:06:59 +08:00
|
|
|
|
2013-04-13 16:55:24 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-27 11:22:46 +08:00
|
|
|
bool FileUtils::writeValueVectorToFile(const ValueVector& vecData, const std::string& fullPath)
|
2015-07-07 14:06:59 +08:00
|
|
|
{
|
|
|
|
NSString* path = [NSString stringWithUTF8String:fullPath.c_str()];
|
|
|
|
NSMutableArray* array = [NSMutableArray array];
|
|
|
|
|
|
|
|
for (const auto &e : vecData)
|
|
|
|
{
|
|
|
|
addObjectToNSArray(e, array);
|
|
|
|
}
|
|
|
|
|
|
|
|
[array writeToFile:path atomically:YES];
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-12-04 17:50:57 +08:00
|
|
|
ValueVector FileUtilsApple::getValueVectorFromFile(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-09-07 13:52:23 +08:00
|
|
|
std::string fullPath = fullPathForFilename(filename);
|
2013-08-22 10:15:47 +08:00
|
|
|
NSString* path = [NSString stringWithUTF8String:fullPath.c_str()];
|
|
|
|
NSArray* array = [NSArray arrayWithContentsOfFile:path];
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueVector ret;
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-08-22 10:15:47 +08:00
|
|
|
for (id value in array)
|
|
|
|
{
|
|
|
|
addItemToArray(value, ret);
|
2013-02-01 18:48:44 +08:00
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2013-08-22 10:15:47 +08:00
|
|
|
return ret;
|
2013-02-01 18:48:44 +08:00
|
|
|
}
|
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
NS_CC_END
|