Browse Source

store addresses in csv format

pull/113/head
orignal 10 years ago
parent
commit
092b445e36
  1. 83
      AddressBook.cpp
  2. 5
      AddressBook.h
  3. 5
      Identity.h

83
AddressBook.cpp

@ -21,12 +21,21 @@ namespace client
AddressBookFilesystemStorage (); AddressBookFilesystemStorage ();
bool GetAddress (const i2p::data::IdentHash& ident, i2p::data::IdentityEx& address) const; bool GetAddress (const i2p::data::IdentHash& ident, i2p::data::IdentityEx& address) const;
const i2p::data::IdentHash * FindAddress (const std::string& name) const;
void AddAddress (const i2p::data::IdentityEx& address); void AddAddress (const i2p::data::IdentityEx& address);
void AddAddress (std::string& name, const i2p::data::IdentHash& ident);
void RemoveAddress (const i2p::data::IdentHash& ident); void RemoveAddress (const i2p::data::IdentHash& ident);
int Load ();
int Save ();
private: private:
boost::filesystem::path GetPath () const { return i2p::util::filesystem::GetDefaultDataDir() / "addressbook"; }; boost::filesystem::path GetPath () const { return i2p::util::filesystem::GetDefaultDataDir() / "addressbook"; };
private:
std::map<std::string, i2p::data::IdentHash> m_Addresses;
}; };
@ -81,6 +90,11 @@ namespace client
LogPrint (eLogError, "Can't open file ", filename); LogPrint (eLogError, "Can't open file ", filename);
} }
void AddressBookFilesystemStorage::AddAddress (std::string& name, const i2p::data::IdentHash& ident)
{
m_Addresses[name] = ident;
}
void AddressBookFilesystemStorage::RemoveAddress (const i2p::data::IdentHash& ident) void AddressBookFilesystemStorage::RemoveAddress (const i2p::data::IdentHash& ident)
{ {
auto filename = GetPath () / (ident.ToBase32() + ".b32"); auto filename = GetPath () / (ident.ToBase32() + ".b32");
@ -88,6 +102,67 @@ namespace client
boost::filesystem::remove (filename); boost::filesystem::remove (filename);
} }
int AddressBookFilesystemStorage::Load ()
{
int num = 0;
auto filename = GetPath () / "addresses.csv";
std::ifstream f (filename.c_str (), std::ofstream::in); // in text mode
if (f.is_open ())
{
m_Addresses.clear ();
while (!f.eof ())
{
std::string s;
getline(f, s);
if (!s.length())
continue; // skip empty line
size_t pos = s.find(',');
if (pos != std::string::npos)
{
std::string name = s.substr(0, pos++);
std::string addr = s.substr(pos);
i2p::data::IdentHash ident;
ident.FromBase32 (addr);
m_Addresses[name] = ident;
num++;
}
}
LogPrint (eLogInfo, num, " addresses loaded");
}
else
LogPrint (eLogWarning, filename, " not found");
return num;
}
int AddressBookFilesystemStorage::Save ()
{
int num = 0;
auto filename = GetPath () / "addresses.csv";
std::ofstream f (filename.c_str (), std::ofstream::out); // in text mode
if (f.is_open ())
{
for (auto it: m_Addresses)
{
f << it.first << "," << it.second.ToBase32 () << std::endl;
num++;
}
LogPrint (eLogInfo, num, " addresses save");
}
else
LogPrint (eLogError, "Can't open file ", filename);
return num;
}
const i2p::data::IdentHash * AddressBookFilesystemStorage::FindAddress (const std::string& name) const
{
auto it = m_Addresses.find (name);
if (it != m_Addresses.end ())
return &it->second;
return nullptr;
}
//--------------------------------------------------------------------- //---------------------------------------------------------------------
AddressBook::AddressBook (): m_IsLoaded (false), m_IsDowloading (false) AddressBook::AddressBook (): m_IsLoaded (false), m_IsDowloading (false)
{ {
@ -204,10 +279,11 @@ namespace client
} }
return; return;
} }
int numAddresses = 0;
if (!m_Storage)
m_Storage = CreateStorage ();
int numAddresses = 0;
std::string s; std::string s;
while (!f.eof ()) while (!f.eof ())
{ {
getline(f, s); getline(f, s);
@ -225,10 +301,13 @@ namespace client
i2p::data::IdentityEx ident; i2p::data::IdentityEx ident;
ident.FromBase64(addr); ident.FromBase64(addr);
m_Addresses[name] = ident.GetIdentHash (); m_Addresses[name] = ident.GetIdentHash ();
m_Storage->AddAddress (ident);
m_Storage->AddAddress (name, ident.GetIdentHash ());
numAddresses++; numAddresses++;
} }
} }
LogPrint (numAddresses, " addresses loaded"); LogPrint (numAddresses, " addresses loaded");
m_Storage->Save ();
m_IsLoaded = true; m_IsLoaded = true;
} }

5
AddressBook.h

@ -19,8 +19,13 @@ namespace client
virtual ~AddressBookStorage () {}; virtual ~AddressBookStorage () {};
virtual bool GetAddress (const i2p::data::IdentHash& ident, i2p::data::IdentityEx& address) const = 0; virtual bool GetAddress (const i2p::data::IdentHash& ident, i2p::data::IdentityEx& address) const = 0;
virtual const i2p::data::IdentHash * FindAddress (const std::string& name) const = 0;
virtual void AddAddress (std::string& name, const i2p::data::IdentHash& ident) = 0;
virtual void AddAddress (const i2p::data::IdentityEx& address) = 0; virtual void AddAddress (const i2p::data::IdentityEx& address) = 0;
virtual void RemoveAddress (const i2p::data::IdentHash& ident) = 0; virtual void RemoveAddress (const i2p::data::IdentHash& ident) = 0;
virtual int Load () = 0;
virtual int Save () = 0;
}; };
class AddressBook class AddressBook

5
Identity.h

@ -56,6 +56,11 @@ namespace data
return std::string (str); return std::string (str);
} }
void FromBase32 (const std::string& s)
{
i2p::data::Base32ToByteStream (s.c_str (), s.length (), m_Buf, sz);
}
private: private:
union // 8 bytes alignment union // 8 bytes alignment

Loading…
Cancel
Save