Permalink
Browse files

Updated such that messages are heap-allocated.

  NOTE: This makes a big memory leak each time the subset of
 messages changes.

  This is a transition as I rework the code as per issue #3.
  • Loading branch information...
1 parent ddbb63b commit e6ad44bb971dfcb2458617ebbc89f934498bec05 @skx skx committed May 13, 2013
Showing with 94 additions and 93 deletions.
  1. +22 −22 bindings.cc
  2. +28 −28 global.cc
  3. +4 −4 global.h
  4. +35 −34 maildir.cc
  5. +1 −1 maildir.h
  6. +4 −4 screen.cc
View
@@ -398,37 +398,37 @@ int current_maildir(lua_State * L)
*/
int scroll_index_to(lua_State * L)
{
- const char *str = lua_tostring(L, -1);
+ const char *str = lua_tostring(L, -1);
- if (str == NULL)
- return luaL_error(L, "Missing argument to scroll_index_to(..)");
+ if (str == NULL)
+ return luaL_error(L, "Missing argument to scroll_index_to(..)");
/**
* get the current messages
*/
- CGlobal *global = CGlobal::Instance();
- std::vector < CMessage > messages = global->get_messages();
- int max = messages.size();
- int selected = global->get_selected_message();
-
- int i = selected + 1;
+ CGlobal *global = CGlobal::Instance();
+ std::vector<CMessage *> messages = global->get_messages();
+ int max = messages.size();
+ int selected = global->get_selected_message();
- while (i != selected) {
- if (i >= max)
- break;
+ int i = selected + 1;
- CMessage cur = messages[i];
- std::string format = cur.format();
- if (strstr(format.c_str(), str) != NULL) {
- global->set_selected_message(i);
- break;
- }
- i += 1;
+ while (i != selected) {
+ if (i >= max)
+ break;
- if (i >= max)
- i = 0;
+ CMessage *cur = messages[i];
+ std::string format = cur->format();
+ if (strstr(format.c_str(), str) != NULL) {
+ global->set_selected_message(i);
+ break;
}
- return 0;
+ i += 1;
+
+ if (i >= max)
+ i = 0;
+ }
+ return 0;
}
View
@@ -167,19 +167,19 @@ std::string * CGlobal::get_index_format()
/**
* Get all selected folders.
*/
-std::vector < std::string > CGlobal::get_selected_folders()
+std::vector<std::string> CGlobal::get_selected_folders()
{
return (m_selected_folders);
}
/**
* Get all folders.
*/
-std::vector < CMaildir > CGlobal::get_all_folders()
+std::vector<CMaildir> CGlobal::get_all_folders()
{
- std::vector < CMaildir > maildirs;
+ std::vector<CMaildir> maildirs;
- std::vector < std::string > folders =
+ std::vector<std::string> folders =
CMaildir::getFolders(*m_maildir_prefix);
std::vector < std::string >::iterator it;
for (it = folders.begin(); it != folders.end(); ++it) {
@@ -195,14 +195,14 @@ std::vector < CMaildir > CGlobal::get_all_folders()
std::vector < CMaildir > CGlobal::get_folders()
{
CGlobal *global = CGlobal::Instance();
- std::vector < CMaildir > folders = global->get_all_folders();
+ std::vector<CMaildir> folders = global->get_all_folders();
+ std::vector<CMaildir> display;
std::string * filter = global->get_maildir_limit();
- std::vector < CMaildir > display;
/**
* Filter the folders to those we can display
*/
- std::vector < CMaildir >::iterator it;
+ std::vector<CMaildir>::iterator it;
for (it = folders.begin(); it != folders.end(); ++it) {
CMaildir x = *it;
@@ -226,16 +226,16 @@ std::vector < CMaildir > CGlobal::get_folders()
/**
* My sort function: sort CMessages by most recent to oldest.
*/
-bool my_sort(CMessage a, CMessage b)
+bool my_sort(CMessage *a, CMessage *b)
{
/**
* Stat both files.
*/
struct stat us;
struct stat them;
- std::string us_path = a.path();
- std::string them_path = b.path();
+ std::string us_path = a->path();
+ std::string them_path = b->path();
if (stat(us_path.c_str(), &us) < 0)
return 0;
@@ -250,46 +250,46 @@ bool my_sort(CMessage a, CMessage b)
/**
* Get all messages from the currently selected folders.
*/
-std::vector < CMessage > CGlobal::get_messages()
+std::vector<CMessage *> CGlobal::get_messages()
{
/**
* Get the selected maildirs.
*/
- CGlobal *global = CGlobal::Instance();
- std::vector < std::string > folders = global->get_selected_folders();
+ CGlobal *global = CGlobal::Instance();
+ std::vector<std::string> folders = global->get_selected_folders();
/**
* The sum of all messages we're going to display.
*/
- std::vector < CMessage > messages;
+ std::vector<CMessage *> messages;
/**
* For each selected maildir read the messages.
*/
- std::vector < std::string >::iterator it;
- for (it = folders.begin(); it != folders.end(); ++it) {
+ std::vector<std::string>::iterator it;
+ for (it = folders.begin(); it != folders.end(); ++it) {
/**
* get the messages from this folder.
*/
- CMaildir tmp = CMaildir(*it);
- std::vector < CMessage > contents = tmp.getMessages();
+ CMaildir tmp = CMaildir(*it);
+ std::vector<CMessage *> contents = tmp.getMessages();
/**
* Append to the list of messages combined.
*/
- std::vector < CMessage >::iterator mit;
- for (mit = contents.begin(); mit != contents.end(); ++mit) {
- messages.push_back(*mit);
- }
+ std::vector<CMessage *>::iterator mit;
+ for (mit = contents.begin(); mit != contents.end(); ++mit) {
+ messages.push_back(*mit);
}
+ }
- /*
- * Sort?
- */
- std::sort(messages.begin(), messages.end(), my_sort);
+ /*
+ * Sort?
+ */
+ std::sort(messages.begin(), messages.end(), my_sort);
- return (messages);
+ return (messages);
}
@@ -314,7 +314,7 @@ void CGlobal::add_folder(std::string path)
*/
bool CGlobal::remove_folder(std::string path)
{
- std::vector < std::string >::iterator it;
+ std::vector<std::string>::iterator it;
/**
* Find the folder.
View
@@ -58,22 +58,22 @@ class CGlobal
/**
* Get all folders.
*/
- std::vector < CMaildir > get_all_folders();
+ std::vector<CMaildir> get_all_folders();
/**
* Get all folders which match the current mode: new/all/pattern
*/
- std::vector < CMaildir > get_folders();
+ std::vector<CMaildir> get_folders();
/**
* Get all selected folders.
*/
- std::vector < std::string > get_selected_folders();
+ std::vector<std::string> get_selected_folders();
/**
* Get all messages from the currently-selected folders.
*/
- std::vector < CMessage > get_messages();
+ std::vector<CMessage *> get_messages();
/**
* Remove all selected folders.
View
@@ -142,56 +142,57 @@ std::vector < std::string > CMaildir::getFolders(std::string path)
}
/**
- * Get the messages in the folder.
+ * Get each messages in the folder.
*/
-std::vector < CMessage > CMaildir::getMessages()
+std::vector <CMessage *> CMaildir::getMessages()
{
/**
* See what filter we have available.
*/
- CGlobal *global = CGlobal::Instance();
- std::string *filter = global->get_index_limit();
-
- std::vector < CMessage > result;
+ CGlobal *global = CGlobal::Instance();
+ std::string *filter = global->get_index_limit();
+ std::vector<CMessage*> result;
dirent *de;
DIR *dp;
- dp = opendir((m_path + "/cur").c_str());
- if (dp) {
- while (true) {
- de = readdir(dp);
- if (de == NULL)
- break;
+ /**
+ * Directories we search.
+ */
+ std::vector < std::string > dirs;
+ dirs.push_back(m_path + "/cur");
+ dirs.push_back(m_path + "/new");
- if (!CMaildir::is_directory (std::string(m_path + "/cur/" + de->d_name)))
- {
- CMessage t = CMessage(std::string(m_path + "/cur/" + de->d_name));
- if ( strcmp(filter->c_str(), "all" ) == 0 )
- result.push_back(t);
- if ( ( strcmp(filter->c_str(), "new") == 0 ) && t.is_new() )
- result.push_back(t);
- }
- }
- closedir(dp);
- }
+ /**
+ * For each directory.
+ */
+ std::vector < std::string >::iterator it;
+ for (it = dirs.begin(); it != dirs.end(); ++it) {
- dp = opendir((m_path + "/new").c_str());
- if (dp) {
- while (true) {
- de = readdir(dp);
- if (de == NULL)
- break;
+ std::string path = *it;
+
+ dp = opendir(path.c_str());
+ if (dp) {
+
+ while (true) {
+
+ de = readdir(dp);
+ if (de == NULL)
+ break;
- if (!CMaildir::is_directory (std::string(m_path + "/new/" + de->d_name)))
- {
- CMessage t = CMessage(std::string(m_path + "/new/" + de->d_name));
+ if (!CMaildir::is_directory (std::string(path + de->d_name))) {
+ CMessage *t = new CMessage(std::string(path + de->d_name));
+
+ /**
+ * TODO: This will go away.
+ */
if ( strcmp(filter->c_str(), "all" ) == 0 )
result.push_back(t);
- if ( ( strcmp(filter->c_str(), "new") == 0 ) && t.is_new() )
+ if ( ( strcmp(filter->c_str(), "new") == 0 ) && t->is_new() )
result.push_back(t);
}
+ }
+ closedir(dp);
}
- closedir(dp);
}
return result;
}
View
@@ -85,7 +85,7 @@ class CMaildir
/**
* Get each message in the folder.
*/
- std::vector < CMessage > getMessages();
+ std::vector <CMessage *> getMessages();
/**
* Count files in a directory.
View
@@ -174,7 +174,7 @@ void CScreen::drawIndex()
* Get all messages from the currently selected maildirs.
*/
CGlobal *global = CGlobal::Instance();
- std::vector < CMessage > messages = global->get_messages();
+ std::vector<CMessage*> messages = global->get_messages();
/**
* If we have no messages report that.
@@ -216,7 +216,7 @@ void CScreen::drawIndex()
*/
CMessage *cur = NULL;
if ((row + selected) < count)
- cur = &messages[row + selected];
+ cur = messages[row + selected];
bool unread = false;
if ( cur != NULL ) {
@@ -276,7 +276,7 @@ void CScreen::drawMessage()
* Get all messages from the currently selected maildirs.
*/
CGlobal *global = CGlobal::Instance();
- std::vector < CMessage > messages = global->get_messages();
+ std::vector<CMessage *> messages = global->get_messages();
/**
* The number of items we've found, vs. the size of the screen.
@@ -295,7 +295,7 @@ void CScreen::drawMessage()
CMessage *cur = NULL;
if ((selected) < count)
- cur = &messages[ selected];
+ cur = messages[ selected];
else
{
clear();

0 comments on commit e6ad44b

Please sign in to comment.