Skip to content

Commit

Permalink
store addresses in csv format
Browse files Browse the repository at this point in the history
  • Loading branch information
orignal committed Nov 27, 2014
1 parent b9806ac commit 092b445
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
83 changes: 81 additions & 2 deletions AddressBook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,21 @@ namespace client

AddressBookFilesystemStorage ();
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 (std::string& name, const i2p::data::IdentHash& ident);
void RemoveAddress (const i2p::data::IdentHash& ident);

int Load ();
int Save ();

private:

boost::filesystem::path GetPath () const { return i2p::util::filesystem::GetDefaultDataDir() / "addressbook"; };

private:

std::map<std::string, i2p::data::IdentHash> m_Addresses;
};


Expand Down Expand Up @@ -81,13 +90,79 @@ namespace client
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)
{
auto filename = GetPath () / (ident.ToBase32() + ".b32");
if (boost::filesystem::exists (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)
{
Expand Down Expand Up @@ -204,10 +279,11 @@ namespace client
}
return;
}
int numAddresses = 0;

if (!m_Storage)
m_Storage = CreateStorage ();
int numAddresses = 0;
std::string s;

while (!f.eof ())
{
getline(f, s);
Expand All @@ -225,10 +301,13 @@ namespace client
i2p::data::IdentityEx ident;
ident.FromBase64(addr);
m_Addresses[name] = ident.GetIdentHash ();
m_Storage->AddAddress (ident);
m_Storage->AddAddress (name, ident.GetIdentHash ());
numAddresses++;
}
}
LogPrint (numAddresses, " addresses loaded");
m_Storage->Save ();
m_IsLoaded = true;
}

Expand Down
5 changes: 5 additions & 0 deletions AddressBook.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ namespace client

virtual ~AddressBookStorage () {};
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 RemoveAddress (const i2p::data::IdentHash& ident) = 0;

virtual int Load () = 0;
virtual int Save () = 0;
};

class AddressBook
Expand Down
5 changes: 5 additions & 0 deletions Identity.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ namespace data
return std::string (str);
}

void FromBase32 (const std::string& s)
{
i2p::data::Base32ToByteStream (s.c_str (), s.length (), m_Buf, sz);
}

private:

union // 8 bytes alignment
Expand Down

0 comments on commit 092b445

Please sign in to comment.