Commit 1789227e authored by Sylvain Berfini's avatar Sylvain Berfini 🎩 Committed by Sandrine Avakian

Improved string manipulation, looks like it fixes the crash on Android

parent 1a496a31
......@@ -37,7 +37,8 @@
using namespace::std;
BELCARD_PUBLIC string belcard_fold(string input);
BELCARD_PUBLIC string belcard_unfold(string input);
BELCARD_PUBLIC string belcard_fold(const string &input);
BELCARD_PUBLIC string belcard_unfold(const string &input);
BELCARD_PUBLIC string belcard_read_file(const string &filename);
#endif
......@@ -117,22 +117,6 @@ shared_ptr<BelCardList> BelCardParser::parse(const string &input) {
}
shared_ptr<BelCardList> BelCardParser::parseFile(const string &filename) {
ifstream istr(filename.c_str(), ifstream::in | ifstream::binary);
if (!istr || !istr.is_open() || istr.fail()) {
bctbx_error("[belcard] Couldn't open file %s", filename.c_str());
return NULL;
}
string vcard;
istr.seekg(0, ios::end);
vcard.resize(istr.tellg());
istr.seekg(0, ios::beg);
istr.read(&vcard[0], vcard.size());
istr.close();
string vcards = belcard_unfold(vcard);
shared_ptr<BelCardGeneric> ret = _parse(vcards, "vcard-list");
shared_ptr<BelCardList> belCards = dynamic_pointer_cast<BelCardList>(ret);
return belCards;
string vcard = belcard_read_file(filename);
return parse(vcard);
}
......@@ -17,20 +17,26 @@
*/
#include "belcard/belcard_utils.hpp"
#include "bctoolbox/logging.h"
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace::std;
string belcard_fold(string input) {
string belcard_fold(const string &input) {
string output = input;
size_t crlf = 0;
size_t next_crlf = 0;
const char *endline = "\r\n";
while (next_crlf != string::npos) {
next_crlf = input.find(endline, crlf);
next_crlf = output.find(endline, crlf);
if (next_crlf != string::npos) {
if (next_crlf - crlf > 75) {
input.insert(crlf + 74, "\r\n ");
output.insert(crlf + 74, "\r\n ");
crlf += 76;
} else {
crlf = next_crlf + 2;
......@@ -38,27 +44,46 @@ string belcard_fold(string input) {
}
}
return input;
return output;
}
string belcard_unfold(string input) {
string belcard_unfold(const string &input) {
string output = input;
const char *endline = "\r\n";
size_t crlf = input.find(endline);
size_t crlf = output.find(endline);
if (crlf == string::npos) {
endline = "\n";
crlf = input.find(endline);
crlf = output.find(endline);
}
while (crlf != string::npos) {
if (isspace(input[crlf + strlen(endline)])) {
input.erase(crlf, strlen(endline) + 1);
if (isspace(output[crlf + strlen(endline)])) {
output.erase(crlf, strlen(endline) + 1);
} else {
crlf += strlen(endline);
}
crlf = input.find(endline, crlf);
crlf = output.find(endline, crlf);
}
return output;
}
string belcard_read_file(const string &filename) {
const char *fName = filename.c_str();
ifstream istr(fName, ifstream::in | ifstream::binary);
if (!istr || !istr.is_open() || istr.fail()) {
bctbx_error("[belcard] Couldn't open file %s", fName);
return NULL;
}
return input;
string vcard;
istr.seekg(0, ios::end);
vcard.resize(istr.tellg());
istr.seekg(0, ios::beg);
istr.read(&vcard[0], vcard.size());
istr.close();
return vcard;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment