FileUtils::Status FileUtils::getContents(const std::string& filename, ResizableBuffer* buffer) { if (filename.empty()) return Status::NotExists; auto fs = FileUtils::getInstance(); std::string fullPath = fs->fullPathForFilename(filename); if (fullPath.empty()) return Status::NotExists; FILE *fp = fopen(fs->getSuitableFOpen(fullPath).c_str(), "rb"); if (!fp) return Status::OpenFailed; fseek(fp, 0, SEEK_END); size_t size = ftell(fp); fseek(fp, 0, SEEK_SET); buffer->resize(size); size_t readsize = fread(buffer->buffer(), 1, size, fp); fclose(fp); if (readsize < size) { buffer->resize(readsize); return Status::ReadFailed; } return Status::OK; }
unsigned char* FileUtils::getFileData(const std::string& filename, const char* mode, ssize_t *size) { unsigned char * buffer = nullptr; CCASSERT(!filename.empty() && size != nullptr && mode != nullptr, "Invalid parameters."); if (filename.empty() || mode == nullptr || size == nullptr) { return nullptr; } *size = 0; do { // read the file from hardware const std::string fullPath = fullPathForFilename(filename); FILE *fp = fopen(getSuitableFOpen(fullPath).c_str(), mode); CC_BREAK_IF(!fp); fseek(fp,0,SEEK_END); *size = ftell(fp); fseek(fp,0,SEEK_SET); buffer = (unsigned char*)malloc(*size); *size = fread(buffer,sizeof(unsigned char), *size,fp); fclose(fp); } while (0); if (!buffer) { std::string msg = "Get data from file("; msg.append(filename).append(") failed!"); CCLOG("%s", msg.c_str()); } return buffer; }
static Data getData(const std::string& filename, bool forString) { if (filename.empty()) { return Data::Null; } Data ret; unsigned char* buffer = nullptr; size_t size = 0; size_t readsize; const char* mode = nullptr; if (forString) mode = "rt"; else mode = "rb"; auto fileutils = FileUtils::getInstance(); do { // Read the file from hardware std::string fullPath = fileutils->fullPathForFilename(filename); FILE *fp = fopen(fileutils->getSuitableFOpen(fullPath).c_str(), mode); CC_BREAK_IF(!fp); fseek(fp,0,SEEK_END); size = ftell(fp); fseek(fp,0,SEEK_SET); if (forString) { buffer = (unsigned char*)malloc(sizeof(unsigned char) * (size + 1)); buffer[size] = '\0'; } else { buffer = (unsigned char*)malloc(sizeof(unsigned char) * size); } readsize = fread(buffer, sizeof(unsigned char), size, fp); fclose(fp); if (forString && readsize < size) { buffer[readsize] = '\0'; } } while (0); if (nullptr == buffer || 0 == readsize) { CCLOG("Get data from file %s failed", filename.c_str()); } else { ret.fastSet(buffer, readsize); } return ret; }
void GHttpTask::openFile() { if(!this->file) { const char* mode = "wb"; auto fileutils = FileUtils::getInstance(); do { // Read the file from hardware this->file = fopen(fileutils->getSuitableFOpen(this->path).c_str(), mode); CC_BREAK_IF(!this->file); } while (0); } }
/* * Use tinyxml2 to write plist files */ bool FileUtils::writeToFile(ValueMap& dict, const std::string &fullPath) { //CCLOG("tinyxml2 Dictionary %d writeToFile %s", dict->_ID, fullPath.c_str()); tinyxml2::XMLDocument *doc = new tinyxml2::XMLDocument(); if (nullptr == doc) return false; tinyxml2::XMLDeclaration *declaration = doc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\""); if (nullptr == declaration) { delete doc; return false; } doc->LinkEndChild(declaration); tinyxml2::XMLElement *docType = doc->NewElement("!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\""); doc->LinkEndChild(docType); tinyxml2::XMLElement *rootEle = doc->NewElement("plist"); rootEle->SetAttribute("version", "1.0"); if (nullptr == rootEle) { delete doc; return false; } doc->LinkEndChild(rootEle); tinyxml2::XMLElement *innerDict = generateElementForDict(dict, doc); if (nullptr == innerDict ) { delete doc; return false; } rootEle->LinkEndChild(innerDict); bool ret = tinyxml2::XML_SUCCESS == doc->SaveFile(getSuitableFOpen(fullPath).c_str()); delete doc; return ret; }
bool FileUtils::writeDataToFile(const Data& data, const std::string& fullPath) { size_t size = 0; const char* mode = "wb"; CCASSERT(!fullPath.empty() && data.getSize() != 0, "Invalid parameters."); auto fileutils = FileUtils::getInstance(); do { // Read the file from hardware FILE *fp = fopen(fileutils->getSuitableFOpen(fullPath).c_str(), mode); CC_BREAK_IF(!fp); size = data.getSize(); fwrite(data.getBytes(), size, 1, fp); fclose(fp); return true; } while (0); return false; }