From 4798b23d2f771cf014b245f2f508506dbc68c64f Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 1 Jun 2019 19:13:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AF=BB=E5=8F=96=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- a8/strutils.cc | 24 ++++++++++++++++++++++++ a8/strutils.h | 1 + a8/xobject.cc | 43 ++++++++++++++----------------------------- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/a8/strutils.cc b/a8/strutils.cc index ffe3f86..0255863 100644 --- a/a8/strutils.cc +++ b/a8/strutils.cc @@ -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; + } + } diff --git a/a8/strutils.h b/a8/strutils.h index d65b6b3..5ecb86d 100644 --- a/a8/strutils.h +++ b/a8/strutils.h @@ -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); } diff --git a/a8/xobject.cc b/a8/xobject.cc index 1341313..671a83c 100644 --- a/a8/xobject.cc +++ b/a8/xobject.cc @@ -1,6 +1,5 @@ #include #include -#include #include #include @@ -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)