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