Browse Source

better error checking and variables names in user_data save/load

miguelfreitas
Miguel Freitas 11 years ago
parent
commit
9d1b5915f9
  1. 2
      src/twister.cpp
  2. 95
      src/twister_utils.cpp

2
src/twister.cpp

@ -163,7 +163,7 @@ void ThreadWaitExtIP()
boost::filesystem::path userDataPath = GetDataDir() / "user_data"; boost::filesystem::path userDataPath = GetDataDir() / "user_data";
loadUserData(userDataPath.string(), m_users); loadUserData(userDataPath.string(), m_users);
printf("loaded user_data for %d users\n", m_users.size()); printf("loaded user_data for %z users\n", m_users.size());
// now restart the user torrents (all m_following) // now restart the user torrents (all m_following)
std::map<std::string,UserData>::const_iterator i; std::map<std::string,UserData>::const_iterator i;

95
src/twister_utils.cpp

@ -131,37 +131,43 @@ entry jsonToEntry(const Value &v)
int saveUserData(std::string const& filename, std::map<std::string,UserData> const &users) int saveUserData(std::string const& filename, std::map<std::string,UserData> const &users)
{ {
entry userEntry; entry userDict;
std::map<std::string,UserData>::const_iterator i; std::map<std::string,UserData>::const_iterator i;
for (i = users.begin(); i != users.end(); ++i) { for (i = users.begin(); i != users.end(); ++i) {
UserData const &data = i->second; UserData const &udata = i->second;
entry &dataEntry = userEntry[i->first]; entry &userData = userDict[i->first];
entry &followingEntry = dataEntry["following"]; if( udata.m_following.size() ) {
BOOST_FOREACH( std::string const &n, data.m_following) { entry &followingList = userData["following"];
followingEntry.list().push_back(n); BOOST_FOREACH( std::string const &n, udata.m_following) {
followingList.list().push_back(n);
}
} }
entry &dmEntry = dataEntry["dm"]; if( udata.m_directmsg.size() ) {
entry &dmDict = userData["dm"];
std::map<std::string, std::list<StoredDirectMsg> >::const_iterator j; std::map<std::string, std::list<StoredDirectMsg> >::const_iterator j;
for (j = data.m_directmsg.begin(); j != data.m_directmsg.end(); ++j) { for (j = udata.m_directmsg.begin(); j != udata.m_directmsg.end(); ++j) {
std::list<StoredDirectMsg> const &stoDmList = j->second; std::list<StoredDirectMsg> const &dmsToFromUser = j->second;
entry &stoDmLstEntry = dmEntry[j->first]; entry &dmList = dmDict[j->first];
BOOST_FOREACH( StoredDirectMsg const &dm, stoDmList) { BOOST_FOREACH( StoredDirectMsg const &stoDm, dmsToFromUser) {
entry stoDmEntry; entry dmElem;
stoDmEntry["time"] = dm.m_utcTime; dmElem["time"] = stoDm.m_utcTime;
stoDmEntry["text"] = dm.m_text; dmElem["text"] = stoDm.m_text;
stoDmEntry["fromMe"] = dm.m_fromMe; dmElem["fromMe"] = stoDm.m_fromMe;
stoDmLstEntry.list().push_back(stoDmEntry); dmList.list().push_back(dmElem);
}
} }
} }
} }
std::vector<char> buf; std::vector<char> buf;
bencode(std::back_inserter(buf), userEntry); if( users.size() ) {
bencode(std::back_inserter(buf), userDict);
}
return save_file(filename, buf); return save_file(filename, buf);
} }
@ -171,40 +177,47 @@ int loadUserData(std::string const& filename, std::map<std::string,UserData> &us
{ {
std::vector<char> in; std::vector<char> in;
if (load_file(filename, in) == 0) { if (load_file(filename, in) == 0) {
lazy_entry userEntry; lazy_entry userDict;
error_code ec; error_code ec;
if (lazy_bdecode(&in[0], &in[0] + in.size(), userEntry, ec) == 0) { if (lazy_bdecode(&in[0], &in[0] + in.size(), userDict, ec) == 0) {
if( userEntry.type() != lazy_entry::dict_t ) goto data_error; if( userDict.type() != lazy_entry::dict_t ) goto data_error;
for( int i = 0; i < userEntry.dict_size(); i++) {
UserData data; for( int i = 0; i < userDict.dict_size(); i++) {
UserData udata;
const lazy_entry *dataEntry = userEntry.dict_at(i).second; const lazy_entry *userData = userDict.dict_at(i).second;
const lazy_entry *followingEntry = dataEntry->dict_find("following"); if( userData->type() != lazy_entry::dict_t ) goto data_error;
if( followingEntry->type() != lazy_entry::list_t ) goto data_error;
for( int j = 0; j < followingEntry->list_size(); j++ ) { const lazy_entry *followingList = userData->dict_find("following");
data.m_following.insert( followingEntry->list_string_value_at(j) ); if( followingList ) {
if( followingList->type() != lazy_entry::list_t ) goto data_error;
for( int j = 0; j < followingList->list_size(); j++ ) {
udata.m_following.insert( followingList->list_string_value_at(j) );
}
} }
const lazy_entry *dmEntry = dataEntry->dict_find("dm"); const lazy_entry *dmDict = userData->dict_find("dm");
if( dmEntry->type() != lazy_entry::dict_t ) goto data_error; if( dmDict ) {
if( dmDict->type() != lazy_entry::dict_t ) goto data_error;
for( int j = 0; j < dmEntry->dict_size(); j++ ) { for( int j = 0; j < dmDict->dict_size(); j++ ) {
const lazy_entry *stoDmLstEntry = dmEntry->dict_at(j).second; const lazy_entry *dmList = dmDict->dict_at(j).second;
if( stoDmLstEntry->type() != lazy_entry::list_t ) goto data_error; if( !dmList || dmList->type() != lazy_entry::list_t ) goto data_error;
for( int k = 0; k < stoDmLstEntry->list_size(); k++ ) { for( int k = 0; k < dmList->list_size(); k++ ) {
const lazy_entry *stoDmEntry = stoDmLstEntry->list_at(k); const lazy_entry *dmElem = dmList->list_at(k);
if( stoDmEntry->type() != lazy_entry::dict_t ) goto data_error; if( dmElem->type() != lazy_entry::dict_t ) goto data_error;
StoredDirectMsg dm; StoredDirectMsg stoDm;
dm.m_text = stoDmEntry->dict_find_string_value("text"); stoDm.m_text = dmElem->dict_find_string_value("text");
dm.m_utcTime = stoDmEntry->dict_find_int_value("time"); stoDm.m_utcTime = dmElem->dict_find_int_value("time");
dm.m_fromMe = stoDmEntry->dict_find_int_value("fromMe"); stoDm.m_fromMe = dmElem->dict_find_int_value("fromMe");
data.m_directmsg[dmEntry->dict_at(j).first].push_back(dm); udata.m_directmsg[dmDict->dict_at(j).first].push_back(stoDm);
}
} }
} }
users[userEntry.dict_at(i).first] = data; users[userDict.dict_at(i).first] = udata;
} }
return 0; return 0;
} }

Loading…
Cancel
Save