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>
|
|
|
|
|
2016-03-21 11:02:00 +08:00
|
|
|
#include "platform/apple/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/CCDictionary.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "platform/CCSAXParser.h"
|
2013-02-01 11:20:46 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2016-04-14 11:59:25 +08:00
|
|
|
struct FileUtilsApple::IMPL {
|
|
|
|
IMPL(NSBundle* bundle):bundle_([NSBundle mainBundle]) {}
|
|
|
|
void setBundle(NSBundle* bundle) {
|
|
|
|
bundle_ = bundle;
|
|
|
|
}
|
|
|
|
NSBundle* getBundle() const {
|
|
|
|
return bundle_;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
NSBundle* bundle_;
|
|
|
|
};
|
|
|
|
|
2016-05-23 18:26:19 +08:00
|
|
|
static id convertCCValueToNSObject(const cocos2d::Value &value);
|
|
|
|
static cocos2d::Value convertNSObjectToCCValue(id object);
|
2013-02-03 10:56:59 +08:00
|
|
|
|
2016-05-23 18:26:19 +08:00
|
|
|
static void addNSObjectToCCMap(id nsKey, id nsValue, ValueMap& dict);
|
|
|
|
static void addCCValueToNSDictionary(const std::string& key, const Value& value, NSMutableDictionary *dict);
|
|
|
|
static void addNSObjectToCCVector(id item, ValueVector& array);
|
|
|
|
static void addCCValueToNSArray(const Value& value, NSMutableArray *array);
|
|
|
|
|
|
|
|
static id convertCCValueToNSObject(const cocos2d::Value &value)
|
|
|
|
{
|
|
|
|
switch (value.getType())
|
|
|
|
{
|
|
|
|
case Value::Type::NONE:
|
|
|
|
return [NSNull null];
|
|
|
|
|
|
|
|
case Value::Type::STRING:
|
|
|
|
return [NSString stringWithCString:value.asString().c_str() encoding:NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
case Value::Type::BYTE:
|
|
|
|
return [NSNumber numberWithInt:value.asByte()];
|
|
|
|
|
|
|
|
case Value::Type::INTEGER:
|
|
|
|
return [NSNumber numberWithInt:value.asInt()];
|
|
|
|
|
|
|
|
case Value::Type::UNSIGNED:
|
|
|
|
return [NSNumber numberWithUnsignedInt:value.asUnsignedInt()];
|
|
|
|
|
|
|
|
case Value::Type::FLOAT:
|
|
|
|
return [NSNumber numberWithFloat:value.asFloat()];
|
|
|
|
|
|
|
|
case Value::Type::DOUBLE:
|
|
|
|
return [NSNumber numberWithDouble:value.asDouble()];
|
|
|
|
|
|
|
|
case Value::Type::BOOLEAN:
|
|
|
|
return [NSNumber numberWithBool:value.asBool()];
|
|
|
|
|
|
|
|
case Value::Type::VECTOR: {
|
|
|
|
NSMutableArray *array = [NSMutableArray array];
|
|
|
|
const ValueVector &vector = value.asValueVector();
|
|
|
|
for (const auto &e : vector) {
|
|
|
|
addCCValueToNSArray(e, array);
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Value::Type::MAP: {
|
|
|
|
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
|
|
|
const ValueMap &map = value.asValueMap();
|
|
|
|
for (auto iter = map.begin(); iter != map.end(); ++iter) {
|
|
|
|
addCCValueToNSDictionary(iter->first, iter->second, dictionary);
|
|
|
|
}
|
|
|
|
return dictionary;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Value::Type::INT_KEY_MAP:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [NSNull null];
|
|
|
|
}
|
|
|
|
|
|
|
|
static cocos2d::Value convertNSObjectToCCValue(id item)
|
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]])
|
|
|
|
{
|
2016-05-23 18:26:19 +08:00
|
|
|
return Value([item UTF8String]);
|
2013-02-03 10:56:59 +08:00
|
|
|
}
|
2016-05-23 18:26:19 +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];
|
2016-05-23 18:26:19 +08:00
|
|
|
return Value(v);
|
2014-03-19 11:58:11 +08:00
|
|
|
}
|
|
|
|
else if(strcmp(numType, @encode(float)) == 0)
|
|
|
|
{
|
2016-05-23 18:26:19 +08:00
|
|
|
return Value([num floatValue]);
|
2014-03-19 11:58:11 +08:00
|
|
|
}
|
|
|
|
else if(strcmp(numType, @encode(double)) == 0)
|
|
|
|
{
|
2016-05-23 18:26:19 +08:00
|
|
|
return Value([num doubleValue]);
|
2014-03-19 11:58:11 +08:00
|
|
|
}
|
2016-05-23 18:26:19 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return Value([num intValue]);
|
2014-03-19 11:58:11 +08:00
|
|
|
}
|
2013-02-03 10:56:59 +08:00
|
|
|
}
|
2016-05-23 18:26:19 +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];
|
2016-05-23 18:26:19 +08:00
|
|
|
addNSObjectToCCMap(subKey, subValue, dict);
|
2013-02-03 10:56:59 +08:00
|
|
|
}
|
2016-05-23 18:26:19 +08:00
|
|
|
|
|
|
|
return Value(dict);
|
2013-02-03 10:56:59 +08:00
|
|
|
}
|
2016-05-23 18:26:19 +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)
|
|
|
|
{
|
2016-05-23 18:26:19 +08:00
|
|
|
addNSObjectToCCVector(subItem, subArray);
|
2013-02-03 10:56:59 +08:00
|
|
|
}
|
2016-05-23 18:26:19 +08:00
|
|
|
return Value(subArray);
|
2013-02-03 10:56:59 +08:00
|
|
|
}
|
2016-05-23 18:26:19 +08:00
|
|
|
|
|
|
|
return Value::Null;
|
2013-02-03 10:56:59 +08:00
|
|
|
}
|
|
|
|
|
2016-05-23 18:26:19 +08:00
|
|
|
static void addNSObjectToCCVector(id item, ValueVector& array)
|
2013-04-13 16:55:24 +08:00
|
|
|
{
|
2016-05-23 18:26:19 +08:00
|
|
|
array.push_back(convertNSObjectToCCValue(item));
|
|
|
|
}
|
2014-07-24 18:56:59 +08:00
|
|
|
|
2016-05-23 18:26:19 +08:00
|
|
|
static void addCCValueToNSArray(const Value& value, NSMutableArray *array)
|
|
|
|
{
|
|
|
|
[array addObject:convertCCValueToNSObject(value)];
|
2013-04-13 16:55:24 +08:00
|
|
|
}
|
|
|
|
|
2016-05-23 18:26:19 +08:00
|
|
|
static void addNSObjectToCCMap(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];
|
2016-05-23 18:26:19 +08:00
|
|
|
dict[key] = convertNSObjectToCCValue(nsValue);
|
2013-02-03 10:56:59 +08:00
|
|
|
}
|
|
|
|
|
2016-05-23 18:26:19 +08:00
|
|
|
static void addCCValueToNSDictionary(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];
|
2016-05-23 18:26:19 +08:00
|
|
|
[dict setObject:convertCCValueToNSObject(value) forKey:NSkey];
|
2013-04-13 16:55:24 +08:00
|
|
|
}
|
|
|
|
|
2016-05-23 18:26:19 +08:00
|
|
|
|
2016-04-14 11:59:25 +08:00
|
|
|
FileUtilsApple::FileUtilsApple() : pimpl_(new IMPL([NSBundle mainBundle])) {
|
2014-07-24 18:56:59 +08:00
|
|
|
}
|
|
|
|
|
2016-05-09 20:08:56 +08:00
|
|
|
FileUtilsApple::~FileUtilsApple() = default;
|
|
|
|
|
2016-04-14 11:59:25 +08:00
|
|
|
#if CC_FILEUTILS_APPLE_ENABLE_OBJC
|
2014-07-24 18:56:59 +08:00
|
|
|
void FileUtilsApple::setBundle(NSBundle* bundle) {
|
2016-04-14 11:59:25 +08:00
|
|
|
pimpl_->setBundle(bundle);
|
2014-07-24 18:56:59 +08:00
|
|
|
}
|
2016-04-14 11:59:25 +08:00
|
|
|
#endif
|
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
|
|
|
|
2016-04-14 11:59:25 +08:00
|
|
|
NSString* fullpath = [pimpl_->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
|
|
|
}
|
2016-03-29 21:39:16 +08:00
|
|
|
|
2015-07-13 17:06:01 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileUtilsApple::removeDirectory(const std::string& path)
|
|
|
|
{
|
2016-06-22 14:31:26 +08:00
|
|
|
if (path.empty())
|
2015-07-13 17:06:01 +08:00
|
|
|
{
|
2016-06-22 14:31:26 +08:00
|
|
|
CCLOGERROR("Fail to remove directory, path is empty!");
|
2015-07-13 17:06:01 +08:00
|
|
|
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
|
|
|
{
|
2016-04-14 11:59:25 +08:00
|
|
|
NSString* fullpath = [pimpl_->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
|
|
|
{
|
2016-03-29 21:39:16 +08:00
|
|
|
auto d(FileUtils::getInstance()->getDataFromFile(filename));
|
|
|
|
return getValueMapFromData(reinterpret_cast<char*>(d.getBytes()), static_cast<int>(d.getSize()));
|
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];
|
2016-05-23 18:26:19 +08:00
|
|
|
addNSObjectToCCMap(key, value, ret);
|
2014-07-29 17:06:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2016-06-10 13:52:35 +08:00
|
|
|
valueMapCompact(const_cast<ValueMap&>(dict));
|
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];
|
2016-03-29 21:39:16 +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
|
|
|
{
|
2016-05-23 18:26:19 +08:00
|
|
|
addCCValueToNSDictionary(iter->first, iter->second, nsDict);
|
2013-04-13 16:55:24 +08:00
|
|
|
}
|
2016-03-29 21:39:16 +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
|
2016-06-10 13:52:35 +08:00
|
|
|
return [nsDict writeToFile:file atomically:YES];
|
|
|
|
}
|
2016-03-29 21:39:16 +08:00
|
|
|
|
2016-06-10 13:52:35 +08:00
|
|
|
void FileUtilsApple::valueMapCompact(ValueMap& valueMap)
|
|
|
|
{
|
|
|
|
auto itr = valueMap.begin();
|
|
|
|
while(itr != valueMap.end()){
|
|
|
|
auto vtype = itr->second.getType();
|
|
|
|
switch(vtype){
|
|
|
|
case Value::Type::NONE:{
|
|
|
|
itr = valueMap.erase(itr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Value::Type::MAP:{
|
|
|
|
valueMapCompact(itr->second.asValueMap());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Value::Type::VECTOR:{
|
|
|
|
valueVectorCompact(itr->second.asValueVector());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
itr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileUtilsApple::valueVectorCompact(ValueVector& valueVector)
|
|
|
|
{
|
|
|
|
auto itr = valueVector.begin();
|
|
|
|
while(itr != valueVector.end()){
|
|
|
|
auto vtype = (*itr).getType();
|
|
|
|
switch(vtype){
|
|
|
|
case Value::Type::NONE:{
|
|
|
|
itr = valueVector.erase(itr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Value::Type::MAP:{
|
|
|
|
valueMapCompact((*itr).asValueMap());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Value::Type::VECTOR:{
|
|
|
|
valueVectorCompact((*itr).asValueVector());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
itr++;
|
|
|
|
}
|
2013-04-13 16:55:24 +08:00
|
|
|
}
|
|
|
|
|
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];
|
2016-03-29 21:39:16 +08:00
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
for (const auto &e : vecData)
|
|
|
|
{
|
2016-05-23 18:26:19 +08:00
|
|
|
addCCValueToNSArray(e, array);
|
2015-07-07 14:06:59 +08:00
|
|
|
}
|
2016-03-29 21:39:16 +08:00
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
[array writeToFile:path atomically:YES];
|
2016-03-29 21:39:16 +08:00
|
|
|
|
2015-07-07 14:06:59 +08:00
|
|
|
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)
|
|
|
|
{
|
2016-05-23 18:26:19 +08:00
|
|
|
addNSObjectToCCVector(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
|
|
|
}
|
|
|
|
|
2016-04-18 17:52:20 +08:00
|
|
|
bool FileUtilsApple::createDirectory(const std::string& path)
|
|
|
|
{
|
|
|
|
CCASSERT(!path.empty(), "Invalid path");
|
|
|
|
|
|
|
|
if (isDirectoryExist(path))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
NSError* error;
|
|
|
|
|
2016-04-19 15:17:31 +08:00
|
|
|
bool result = [s_fileManager createDirectoryAtPath:[NSString stringWithUTF8String:path.c_str()] withIntermediateDirectories:YES attributes:nil error:&error];
|
|
|
|
|
|
|
|
if(!result && error != nil)
|
|
|
|
{
|
|
|
|
CCLOGERROR("Fail to create directory \"%s\": %s", path.c_str(), [error.localizedDescription UTF8String]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2016-04-18 17:52:20 +08:00
|
|
|
}
|
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
NS_CC_END
|