Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

more stringpager manipulations

  • Loading branch information...
commit 45bab18b487eb0c4659f2dcd5e9f572afa9868cb 1 parent 8d6fe39
@dyfet authored
Showing with 96 additions and 0 deletions.
  1. +1 −0  ChangeLog
  2. +67 −0 corelib/memory.cpp
  3. +28 −0 inc/ucommon/memory.h
View
1  ChangeLog
@@ -1,5 +1,6 @@
from ucommon 5.2.2 to 5.2.3
- sort specific string collation functions added
+- more stringpager manipulations possible
from ucommon 5.2.1 to 5.2.2
- use strcoll for sorting when possible
View
67 corelib/memory.cpp
@@ -268,6 +268,52 @@ void stringpager::clear(void)
last = NULL;
}
+void stringpager::push(const char *text)
+{
+ if(!text)
+ text = "";
+
+ size_t size = strlen(text) + 1;
+ caddr_t mem = (caddr_t)memalloc::_alloc(sizeof(member));
+ char *str = (char *)memalloc::_alloc(size);
+
+ strcpy(str, text);
+ member *node;
+
+ node = new(mem) member(&root, str);
+ if(!last)
+ last = node;
+ ++members;
+}
+
+const char *stringpager::pop(void)
+{
+ const char *out = NULL;
+
+ if(!root)
+ return NULL;
+
+ if(root == last) {
+ out = last->text;
+ root = last = NULL;
+ members = 0;
+ return out;
+ }
+
+ linked_pointer<member> np = root;
+ while(is(np)) {
+ if(np->next == last) {
+ out = last->text;
+ last = *np;
+ np->next = NULL;
+ --members;
+ break;
+ }
+ np.next();
+ }
+ return out;
+}
+
void stringpager::add(const char *text)
{
if(!text)
@@ -323,6 +369,27 @@ void stringpager::sort(void)
delete list;
}
+void stringpager::release(char **idx)
+{
+ if(!idx)
+ return;
+
+ delete[] idx;
+}
+
+char **stringpager::index(stringpager& list)
+{
+ unsigned index = 0;
+ char **data = new char *[list.members + 1];
+ linked_pointer<member> mp = list.root;
+ while(is(mp)) {
+ data[index++] = (char *)mp->text;
+ mp.next();
+ }
+ data[index] = NULL;
+ return data;
+}
+
DirPager::DirPager() :
stringpager()
{
View
28 inc/ucommon/memory.h
@@ -299,6 +299,24 @@ class __EXPORT stringpager : protected memalloc
void add(const char *text);
/**
+ * Add text to front of list.
+ * @param text to add.
+ */
+ void push(const char *text);
+
+ /**
+ * Remove element from front of list. Does not release memory.
+ * @return text removed.
+ */
+ const char *pull(void);
+
+ /**
+ * Remove element from back of list. Does not release memory.
+ * @return text removed.
+ */
+ const char *pop(void);
+
+ /**
* Add list to list. This is a list of string pointers terminated with
* NULL.
* @param list of text to add.
@@ -347,6 +365,16 @@ class __EXPORT stringpager : protected memalloc
*/
void sort(void);
+ /**
+ * Fetch index.
+ */
+ static char **index(stringpager& pager);
+
+ /**
+ * Release index.
+ */
+ static void release(char **idx);
+
private:
member *last;
};
Please sign in to comment.
Something went wrong with that request. Please try again.