修复读取文件问题
This commit is contained in:
parent
3e1f116639
commit
4798b23d2f
@ -369,4 +369,28 @@ namespace a8
|
||||
return result;
|
||||
}
|
||||
|
||||
bool ReadStringFromFile(const std::string& filename, std::string& data)
|
||||
{
|
||||
FILE *fp = fopen(filename.c_str(), "rb");
|
||||
if (!fp) {
|
||||
return false;
|
||||
}
|
||||
fseek(fp, 0, SEEK_END);
|
||||
int fileSize = ftell(fp);
|
||||
if(fileSize){
|
||||
char *p = (char*)malloc(fileSize + 1);
|
||||
if(p){
|
||||
*(p + fileSize) = '\0';
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
fread(p, 1, fileSize, fp);
|
||||
if (fileSize > 0) {
|
||||
data.append(p, fileSize);
|
||||
}
|
||||
free(p);
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ namespace a8
|
||||
std::string HttpResponse(int code, const std::string& response);
|
||||
std::string JsonEscapeString(const std::string& str);
|
||||
std::string IntToFixedString(int val, int n);
|
||||
bool ReadStringFromFile(const std::string& filename, std::string& data);
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include <a8/a8.h>
|
||||
#include <a8/xobject.h>
|
||||
#include <a8/stringlist.h>
|
||||
|
||||
#include <json/reader.h>
|
||||
#include <tinyxml2.h>
|
||||
@ -293,36 +292,20 @@ namespace a8
|
||||
|
||||
bool XObject::ReadFromFile(const std::string& filename)
|
||||
{
|
||||
a8::StringList sl;
|
||||
sl.LoadFromFile(filename.c_str());
|
||||
return ReadFromJsonString(sl.Text());
|
||||
std::string data;
|
||||
if (!a8::ReadStringFromFile(filename, data)) {
|
||||
return false;
|
||||
}
|
||||
return ReadFromJsonString(data);
|
||||
}
|
||||
|
||||
bool XObject::ReadFromJsonFile(const std::string& filename)
|
||||
{
|
||||
std::string json_data;
|
||||
{
|
||||
FILE *fp = fopen(filename.c_str(), "rb");
|
||||
if (!fp) {
|
||||
return false;
|
||||
}
|
||||
fseek(fp, 0, SEEK_END);
|
||||
int fileSize = ftell(fp);
|
||||
if(fileSize){
|
||||
char *p = (char*)malloc(fileSize + 1);
|
||||
if(p){
|
||||
*(p + fileSize) = '\0';
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
fread(p, 1, fileSize, fp);
|
||||
if (fileSize > 0) {
|
||||
json_data.append(p, fileSize);
|
||||
}
|
||||
free(p);
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
std::string data;
|
||||
if (!a8::ReadStringFromFile(filename, data)) {
|
||||
return false;
|
||||
}
|
||||
return ReadFromJsonString(json_data);
|
||||
return ReadFromJsonString(data);
|
||||
}
|
||||
|
||||
bool XObject::ReadFromJsonString(const std::string& json_data)
|
||||
@ -338,9 +321,11 @@ namespace a8
|
||||
|
||||
bool XObject::ReadFromXmlFile(const std::string& filename)
|
||||
{
|
||||
a8::StringList sl;
|
||||
sl.LoadFromFile(filename.c_str());
|
||||
return ReadFromXmlString(sl.Text());
|
||||
std::string data;
|
||||
if (!a8::ReadStringFromFile(filename, data)) {
|
||||
return false;
|
||||
}
|
||||
return ReadFromXmlString(data);
|
||||
}
|
||||
|
||||
bool XObject::ReadFromXmlString(const std::string& xmldata)
|
||||
|
Loading…
x
Reference in New Issue
Block a user