Commit eb9e1ecf eb9e1ecf546af4d96de809e62081062e82e6d692 by Wojciech Polak

Improve libmu_cpp.

* libmu_cpp/address.cc, libmu_cpp/envelope.cc, libmu_cpp/header.cc,
libmu_cpp/url.cc: Use sget instead of aget.
* libmu_cpp/list.cc (mulist_to_stl): New function.
(List::to_stl): New method.
* include/mailutils/cpp/list.h: Likewise.
1 parent b4b7a4a6
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
21 #ifndef _MUCPP_LIST_H 21 #ifndef _MUCPP_LIST_H
22 #define _MUCPP_LIST_H 22 #define _MUCPP_LIST_H
23 23
24 #include <list>
24 #include <errno.h> 25 #include <errno.h>
25 #include <mailutils/list.h> 26 #include <mailutils/list.h>
26 #include <mailutils/cpp/error.h> 27 #include <mailutils/cpp/error.h>
...@@ -32,6 +33,8 @@ typedef int (*mu_list_comparator_t) (const void*, const void*); ...@@ -32,6 +33,8 @@ typedef int (*mu_list_comparator_t) (const void*, const void*);
32 namespace mailutils 33 namespace mailutils
33 { 34 {
34 35
36 std::list<void*> mulist_to_stl (mu_list_t mu_list);
37
35 class Iterator; 38 class Iterator;
36 39
37 class List 40 class List
...@@ -71,6 +74,8 @@ class List ...@@ -71,6 +74,8 @@ class List
71 size_t count (); 74 size_t count ();
72 size_t size (); 75 size_t size ();
73 76
77 std::list<void*> to_stl ();
78
74 inline void* operator [] (size_t index) { 79 inline void* operator [] (size_t index) {
75 return this->get (index); 80 return this->get (index);
76 } 81 }
......
...@@ -95,8 +95,8 @@ Address :: get_count () ...@@ -95,8 +95,8 @@ Address :: get_count ()
95 std::string 95 std::string
96 Address :: get_email (size_t n) 96 Address :: get_email (size_t n)
97 { 97 {
98 char *buf = NULL; 98 const char* buf = NULL;
99 int status = mu_address_aget_email (addr, n, &buf); 99 int status = mu_address_sget_email (addr, n, &buf);
100 if (status == EINVAL) 100 if (status == EINVAL)
101 throw Address::EInval ("Address::get_email", status); 101 throw Address::EInval ("Address::get_email", status);
102 else if (status == ENOENT) 102 else if (status == ENOENT)
...@@ -108,8 +108,8 @@ Address :: get_email (size_t n) ...@@ -108,8 +108,8 @@ Address :: get_email (size_t n)
108 std::string 108 std::string
109 Address :: get_local_part (size_t n) 109 Address :: get_local_part (size_t n)
110 { 110 {
111 char *buf = NULL; 111 const char* buf = NULL;
112 int status = mu_address_aget_local_part (addr, n, &buf); 112 int status = mu_address_sget_local_part (addr, n, &buf);
113 if (status == EINVAL) 113 if (status == EINVAL)
114 throw Address::EInval ("Address::get_local_part", status); 114 throw Address::EInval ("Address::get_local_part", status);
115 else if (status == ENOENT) 115 else if (status == ENOENT)
...@@ -121,8 +121,8 @@ Address :: get_local_part (size_t n) ...@@ -121,8 +121,8 @@ Address :: get_local_part (size_t n)
121 std::string 121 std::string
122 Address :: get_domain (size_t n) 122 Address :: get_domain (size_t n)
123 { 123 {
124 char *buf = NULL; 124 const char* buf = NULL;
125 int status = mu_address_aget_domain (addr, n, &buf); 125 int status = mu_address_sget_domain (addr, n, &buf);
126 if (status == EINVAL) 126 if (status == EINVAL)
127 throw Address::EInval ("Address::get_domain", status); 127 throw Address::EInval ("Address::get_domain", status);
128 else if (status == ENOENT) 128 else if (status == ENOENT)
...@@ -134,8 +134,8 @@ Address :: get_domain (size_t n) ...@@ -134,8 +134,8 @@ Address :: get_domain (size_t n)
134 std::string 134 std::string
135 Address :: get_personal (size_t n) 135 Address :: get_personal (size_t n)
136 { 136 {
137 char *buf = NULL; 137 const char* buf = NULL;
138 int status = mu_address_aget_personal (addr, n, &buf); 138 int status = mu_address_sget_personal (addr, n, &buf);
139 if (status == EINVAL) 139 if (status == EINVAL)
140 throw Address::EInval ("Address::get_personal", status); 140 throw Address::EInval ("Address::get_personal", status);
141 else if (status == ENOENT) 141 else if (status == ENOENT)
...@@ -147,8 +147,8 @@ Address :: get_personal (size_t n) ...@@ -147,8 +147,8 @@ Address :: get_personal (size_t n)
147 std::string 147 std::string
148 Address :: get_comments (size_t n) 148 Address :: get_comments (size_t n)
149 { 149 {
150 char *buf = NULL; 150 const char* buf = NULL;
151 int status = mu_address_aget_comments (addr, n, &buf); 151 int status = mu_address_sget_comments (addr, n, &buf);
152 if (status == EINVAL) 152 if (status == EINVAL)
153 throw Address::EInval ("Address::get_comments", status); 153 throw Address::EInval ("Address::get_comments", status);
154 else if (status == ENOENT) 154 else if (status == ENOENT)
...@@ -160,8 +160,8 @@ Address :: get_comments (size_t n) ...@@ -160,8 +160,8 @@ Address :: get_comments (size_t n)
160 std::string 160 std::string
161 Address :: get_route (size_t n) 161 Address :: get_route (size_t n)
162 { 162 {
163 char *buf = NULL; 163 const char* buf = NULL;
164 int status = mu_address_aget_route (addr, n, &buf); 164 int status = mu_address_sget_route (addr, n, &buf);
165 if (status == EINVAL) 165 if (status == EINVAL)
166 throw Address::EInval ("Address::get_route", status); 166 throw Address::EInval ("Address::get_route", status);
167 else if (status == ENOENT) 167 else if (status == ENOENT)
......
...@@ -42,28 +42,20 @@ Envelope :: Envelope (const mu_envelope_t env) ...@@ -42,28 +42,20 @@ Envelope :: Envelope (const mu_envelope_t env)
42 std::string 42 std::string
43 Envelope :: get_sender () 43 Envelope :: get_sender ()
44 { 44 {
45 char* c_val = NULL; 45 const char* buf = NULL;
46 46 int status = mu_envelope_sget_sender (env, &buf);
47 int status = mu_envelope_aget_sender (env, &c_val);
48 if (status) 47 if (status)
49 throw Exception ("Envelope::get_sender", status); 48 throw Exception ("Envelope::get_sender", status);
50 49 return std::string (buf);
51 std::string val (c_val);
52 free (c_val);
53 return val;
54 } 50 }
55 51
56 std::string 52 std::string
57 Envelope :: get_date () 53 Envelope :: get_date ()
58 { 54 {
59 char* c_val; 55 const char* buf = NULL;
60 56 int status = mu_envelope_sget_date (env, &buf);
61 int status = mu_envelope_aget_date (env, &c_val);
62 if (status) 57 if (status)
63 throw Exception ("Envelope::get_date", status); 58 throw Exception ("Envelope::get_date", status);
64 59 return std::string (buf);
65 std::string val (c_val);
66 free (c_val);
67 return val;
68 } 60 }
69 61
......
...@@ -42,31 +42,27 @@ Header :: Header (const mu_header_t hdr) ...@@ -42,31 +42,27 @@ Header :: Header (const mu_header_t hdr)
42 std::string 42 std::string
43 Header :: get_value (const std::string& name) 43 Header :: get_value (const std::string& name)
44 { 44 {
45 char* c_val; 45 const char* buf = NULL;
46 46
47 int status = mu_header_aget_value (hdr, name.c_str (), &c_val); 47 int status = mu_header_sget_value (hdr, name.c_str (), &buf);
48 if (status) 48 if (status)
49 throw Exception ("Header::get_value", status); 49 throw Exception ("Header::get_value", status);
50 50
51 std::string val (c_val); 51 return std::string (buf);
52 free (c_val);
53 return val;
54 } 52 }
55 53
56 std::string 54 std::string
57 Header :: get_value (const std::string& name, const std::string& def) 55 Header :: get_value (const std::string& name, const std::string& def)
58 { 56 {
59 char* c_val; 57 const char* buf = NULL;
60 58
61 int status = mu_header_aget_value (hdr, name.c_str (), &c_val); 59 int status = mu_header_sget_value (hdr, name.c_str (), &buf);
62 if (status == MU_ERR_NOENT) 60 if (status == MU_ERR_NOENT)
63 return std::string (def); 61 return std::string (def);
64 else if (status) 62 else if (status)
65 throw Exception ("Header::get_value", status); 63 throw Exception ("Header::get_value", status);
66 64
67 std::string val (c_val); 65 return std::string (buf);
68 free (c_val);
69 return val;
70 } 66 }
71 67
72 size_t 68 size_t
......
...@@ -22,6 +22,29 @@ ...@@ -22,6 +22,29 @@
22 22
23 using namespace mailutils; 23 using namespace mailutils;
24 24
25 std::list<void*>
26 mailutils :: mulist_to_stl (mu_list_t mu_list)
27 {
28 size_t list_count;
29 std::list<void *> list;
30
31 if (!mu_list)
32 return list;
33
34 int status = mu_list_count (mu_list, &list_count);
35 if (status)
36 return list;
37
38 for (int i = 0; i < list_count; i++)
39 {
40 void *item = NULL;
41 status = mu_list_get (mu_list, i, &item);
42 if (!status && item)
43 list.push_back (item);
44 }
45 return list;
46 }
47
25 // 48 //
26 // List 49 // List
27 // 50 //
...@@ -199,3 +222,9 @@ List :: set_destroy_item (void (*mu_destroy_item) (void *item)) ...@@ -199,3 +222,9 @@ List :: set_destroy_item (void (*mu_destroy_item) (void *item))
199 throw Exception ("List::set_destroy_item", status); 222 throw Exception ("List::set_destroy_item", status);
200 } 223 }
201 224
225 std::list<void*>
226 List :: to_stl ()
227 {
228 return mulist_to_stl (mu_list);
229 }
230
......
...@@ -74,8 +74,8 @@ Url :: get_port () ...@@ -74,8 +74,8 @@ Url :: get_port ()
74 std::string 74 std::string
75 Url :: get_scheme () 75 Url :: get_scheme ()
76 { 76 {
77 char *buf = NULL; 77 const char* buf = NULL;
78 int status = mu_url_aget_scheme (url, &buf); 78 int status = mu_url_sget_scheme (url, &buf);
79 if (status == MU_ERR_NOENT) 79 if (status == MU_ERR_NOENT)
80 return ""; 80 return "";
81 else if (status) 81 else if (status)
...@@ -86,8 +86,8 @@ Url :: get_scheme () ...@@ -86,8 +86,8 @@ Url :: get_scheme ()
86 std::string 86 std::string
87 Url :: get_user () 87 Url :: get_user ()
88 { 88 {
89 char *buf = NULL; 89 const char* buf = NULL;
90 int status = mu_url_aget_user (url, &buf); 90 int status = mu_url_sget_user (url, &buf);
91 if (status == MU_ERR_NOENT) 91 if (status == MU_ERR_NOENT)
92 return ""; 92 return "";
93 else if (status) 93 else if (status)
...@@ -98,8 +98,8 @@ Url :: get_user () ...@@ -98,8 +98,8 @@ Url :: get_user ()
98 std::string 98 std::string
99 Url :: get_passwd () 99 Url :: get_passwd ()
100 { 100 {
101 char *buf = NULL; 101 const char* buf = NULL;
102 int status = mu_url_aget_passwd (url, &buf); 102 int status = mu_url_sget_passwd (url, &buf);
103 if (status == MU_ERR_NOENT) 103 if (status == MU_ERR_NOENT)
104 return ""; 104 return "";
105 else if (status) 105 else if (status)
...@@ -110,8 +110,8 @@ Url :: get_passwd () ...@@ -110,8 +110,8 @@ Url :: get_passwd ()
110 std::string 110 std::string
111 Url :: get_auth () 111 Url :: get_auth ()
112 { 112 {
113 char *buf = NULL; 113 const char* buf = NULL;
114 int status = mu_url_aget_auth (url, &buf); 114 int status = mu_url_sget_auth (url, &buf);
115 if (status == MU_ERR_NOENT) 115 if (status == MU_ERR_NOENT)
116 return ""; 116 return "";
117 else if (status) 117 else if (status)
...@@ -122,8 +122,8 @@ Url :: get_auth () ...@@ -122,8 +122,8 @@ Url :: get_auth ()
122 std::string 122 std::string
123 Url :: get_host () 123 Url :: get_host ()
124 { 124 {
125 char *buf = NULL; 125 const char* buf = NULL;
126 int status = mu_url_aget_host (url, &buf); 126 int status = mu_url_sget_host (url, &buf);
127 if (status == MU_ERR_NOENT) 127 if (status == MU_ERR_NOENT)
128 return ""; 128 return "";
129 else if (status) 129 else if (status)
...@@ -134,8 +134,8 @@ Url :: get_host () ...@@ -134,8 +134,8 @@ Url :: get_host ()
134 std::string 134 std::string
135 Url :: get_path () 135 Url :: get_path ()
136 { 136 {
137 char *buf = NULL; 137 const char* buf = NULL;
138 int status = mu_url_aget_path (url, &buf); 138 int status = mu_url_sget_path (url, &buf);
139 if (status == MU_ERR_NOENT) 139 if (status == MU_ERR_NOENT)
140 return ""; 140 return "";
141 else if (status) 141 else if (status)
......