Add Debug class to libmu_cpp.
* include/mailutils/cpp/debug.h, libmu_cpp/debug.cc: New files. * examples/cpp/mimetest.cc, examples/cpp/sfrom.cc, libmu_cpp/header.cc, include/mailutils/cpp/header.h: Accept default header value if missing.
Showing
10 changed files
with
140 additions
and
5 deletions
... | @@ -74,7 +74,12 @@ main (int argc, char **argv) | ... | @@ -74,7 +74,12 @@ main (int argc, char **argv) |
74 | 74 | ||
75 | MailboxDefault mbox (argv[i]); | 75 | MailboxDefault mbox (argv[i]); |
76 | 76 | ||
77 | /* Debugging trace. FIXME: ADD MISSING */ | 77 | /* Debugging trace. */ |
78 | if (debug) | ||
79 | { | ||
80 | Debug debug = mbox.get_debug (); | ||
81 | debug.set_level (MU_DEBUG_LEVEL_UPTO (MU_DEBUG_PROT)); | ||
82 | } | ||
78 | 83 | ||
79 | /* Open the mailbox for reading only. */ | 84 | /* Open the mailbox for reading only. */ |
80 | mbox.open (); | 85 | mbox.open (); |
... | @@ -89,7 +94,7 @@ main (int argc, char **argv) | ... | @@ -89,7 +94,7 @@ main (int argc, char **argv) |
89 | 94 | ||
90 | cout << "Message: " << i << endl; | 95 | cout << "Message: " << i << endl; |
91 | cout << "From: " << hdr[MU_HEADER_FROM] << endl; | 96 | cout << "From: " << hdr[MU_HEADER_FROM] << endl; |
92 | cout << "Subject: " << hdr[MU_HEADER_SUBJECT] << endl; | 97 | cout << "Subject: " << hdr.get_value (MU_HEADER_SUBJECT, "[none]") << endl; |
93 | cout << "Number of parts in message - " << msg.get_num_parts () << endl; | 98 | cout << "Number of parts in message - " << msg.get_num_parts () << endl; |
94 | cout << "Total message size - " | 99 | cout << "Total message size - " |
95 | << msg.size () << "/" << msg.lines () << endl; | 100 | << msg.size () << "/" << msg.lines () << endl; |
... | @@ -162,7 +167,7 @@ message_display_parts (Message& msg, int indent) | ... | @@ -162,7 +167,7 @@ message_display_parts (Message& msg, int indent) |
162 | 167 | ||
163 | Header hdr = part.get_header (); | 168 | Header hdr = part.get_header (); |
164 | string from = hdr[MU_HEADER_FROM]; | 169 | string from = hdr[MU_HEADER_FROM]; |
165 | string subject = hdr[MU_HEADER_SUBJECT]; | 170 | string subject = hdr.get_value (MU_HEADER_SUBJECT, "[none]"); |
166 | 171 | ||
167 | cout << setw (indent) << setfill (' ') | 172 | cout << setw (indent) << setfill (' ') |
168 | << "Encapsulated message : " << from << "\t" << subject << endl; | 173 | << "Encapsulated message : " << from << "\t" << subject << endl; | ... | ... |
... | @@ -43,7 +43,7 @@ int main (int argc, char* argv[]) | ... | @@ -43,7 +43,7 @@ int main (int argc, char* argv[]) |
43 | Message msg = mbox[msgno]; | 43 | Message msg = mbox[msgno]; |
44 | Header hdr = msg.get_header (); | 44 | Header hdr = msg.get_header (); |
45 | cout << hdr[MU_HEADER_FROM] << " " | 45 | cout << hdr[MU_HEADER_FROM] << " " |
46 | << hdr[MU_HEADER_SUBJECT] << endl; | 46 | << hdr.get_value (MU_HEADER_SUBJECT, "[none]") << endl; |
47 | } | 47 | } |
48 | 48 | ||
49 | mbox.close (); | 49 | mbox.close (); | ... | ... |
include/mailutils/cpp/debug.h
0 → 100644
1 | /* | ||
2 | GNU Mailutils -- a suite of utilities for electronic mail | ||
3 | Copyright (C) 2009 Free Software Foundation, Inc. | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Lesser General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 3 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Lesser General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Lesser General | ||
16 | Public License along with this library; if not, write to the | ||
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301 USA | ||
19 | */ | ||
20 | |||
21 | #ifndef _MUCPP_DEBUG_H | ||
22 | #define _MUCPP_DEBUG_H | ||
23 | |||
24 | #include <string> | ||
25 | #include <mailutils/debug.h> | ||
26 | |||
27 | namespace mailutils | ||
28 | { | ||
29 | |||
30 | class Debug | ||
31 | { | ||
32 | protected: | ||
33 | mu_debug_t debug; | ||
34 | |||
35 | public: | ||
36 | Debug (); | ||
37 | Debug (const mu_debug_t); | ||
38 | |||
39 | void set_level (const mu_log_level_t level); | ||
40 | }; | ||
41 | |||
42 | } | ||
43 | |||
44 | #endif // not _MUCPP_DEBUG_H | ||
45 |
... | @@ -37,6 +37,7 @@ class Header | ... | @@ -37,6 +37,7 @@ class Header |
37 | Header (const mu_header_t); | 37 | Header (const mu_header_t); |
38 | 38 | ||
39 | std::string get_value (const std::string& name); | 39 | std::string get_value (const std::string& name); |
40 | std::string get_value (const std::string& name, const std::string& def); | ||
40 | size_t size (); | 41 | size_t size (); |
41 | size_t lines (); | 42 | size_t lines (); |
42 | 43 | ... | ... |
... | @@ -22,6 +22,7 @@ | ... | @@ -22,6 +22,7 @@ |
22 | #define _MUCPP_MAILBOX_H | 22 | #define _MUCPP_MAILBOX_H |
23 | 23 | ||
24 | #include <mailutils/mailbox.h> | 24 | #include <mailutils/mailbox.h> |
25 | #include <mailutils/cpp/debug.h> | ||
25 | #include <mailutils/cpp/message.h> | 26 | #include <mailutils/cpp/message.h> |
26 | 27 | ||
27 | namespace mailutils | 28 | namespace mailutils |
... | @@ -37,6 +38,8 @@ class MailboxBase | ... | @@ -37,6 +38,8 @@ class MailboxBase |
37 | void open (int flag); | 38 | void open (int flag); |
38 | void close (); | 39 | void close (); |
39 | 40 | ||
41 | Debug& get_debug (); | ||
42 | |||
40 | size_t messages_count (); | 43 | size_t messages_count (); |
41 | Message& get_message (size_t num); | 44 | Message& get_message (size_t num); |
42 | 45 | ... | ... |
libmu_cpp/debug.cc
0 → 100644
1 | /* | ||
2 | GNU Mailutils -- a suite of utilities for electronic mail | ||
3 | Copyright (C) 2009 Free Software Foundation, Inc. | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Lesser General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 3 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Lesser General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Lesser General | ||
16 | Public License along with this library; if not, write to the | ||
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301 USA | ||
19 | */ | ||
20 | |||
21 | #include <mailutils/cpp/debug.h> | ||
22 | #include <mailutils/cpp/error.h> | ||
23 | #include <errno.h> | ||
24 | |||
25 | using namespace mailutils; | ||
26 | |||
27 | // | ||
28 | // Debug | ||
29 | // | ||
30 | |||
31 | Debug :: Debug () | ||
32 | { | ||
33 | this->debug = NULL; | ||
34 | } | ||
35 | |||
36 | Debug :: Debug (const mu_debug_t debug) | ||
37 | { | ||
38 | if (debug == 0) | ||
39 | throw Exception ("Debug::Debug", EINVAL); | ||
40 | |||
41 | this->debug = debug; | ||
42 | } | ||
43 | |||
44 | void | ||
45 | Debug :: set_level (const mu_log_level_t level) | ||
46 | { | ||
47 | int status = mu_debug_set_level (debug, level); | ||
48 | if (status) | ||
49 | throw Exception ("Debug::set_level", status); | ||
50 | } | ||
51 |
... | @@ -30,6 +30,7 @@ using namespace mailutils; | ... | @@ -30,6 +30,7 @@ using namespace mailutils; |
30 | 30 | ||
31 | Header :: Header () | 31 | Header :: Header () |
32 | { | 32 | { |
33 | this->hdr = NULL; | ||
33 | } | 34 | } |
34 | 35 | ||
35 | Header :: Header (const mu_header_t hdr) | 36 | Header :: Header (const mu_header_t hdr) |
... | @@ -54,6 +55,22 @@ Header :: get_value (const std::string& name) | ... | @@ -54,6 +55,22 @@ Header :: get_value (const std::string& name) |
54 | return val; | 55 | return val; |
55 | } | 56 | } |
56 | 57 | ||
58 | std::string | ||
59 | Header :: get_value (const std::string& name, const std::string& def) | ||
60 | { | ||
61 | char* c_val; | ||
62 | |||
63 | int status = mu_header_aget_value (hdr, name.c_str (), &c_val); | ||
64 | if (status == MU_ERR_NOENT) | ||
65 | return std::string (def); | ||
66 | else if (status) | ||
67 | throw Exception ("Header::get_value", status); | ||
68 | |||
69 | std::string val (c_val); | ||
70 | free (c_val); | ||
71 | return val; | ||
72 | } | ||
73 | |||
57 | size_t | 74 | size_t |
58 | Header :: size () | 75 | Header :: size () |
59 | { | 76 | { | ... | ... |
... | @@ -19,7 +19,6 @@ | ... | @@ -19,7 +19,6 @@ |
19 | */ | 19 | */ |
20 | 20 | ||
21 | #include <mailutils/cpp/mailbox.h> | 21 | #include <mailutils/cpp/mailbox.h> |
22 | #include <mailutils/cpp/message.h> | ||
23 | #include <mailutils/cpp/error.h> | 22 | #include <mailutils/cpp/error.h> |
24 | #include <errno.h> | 23 | #include <errno.h> |
25 | 24 | ||
... | @@ -53,6 +52,18 @@ MailboxBase :: close () | ... | @@ -53,6 +52,18 @@ MailboxBase :: close () |
53 | throw Exception ("MailboxBase::close", status); | 52 | throw Exception ("MailboxBase::close", status); |
54 | } | 53 | } |
55 | 54 | ||
55 | Debug& | ||
56 | MailboxBase :: get_debug () | ||
57 | { | ||
58 | mu_debug_t c_dbg; | ||
59 | |||
60 | int status = mu_mailbox_get_debug (mbox, &c_dbg); | ||
61 | if (status) | ||
62 | throw Exception ("MailboxBase::get_debug", status); | ||
63 | |||
64 | return *new Debug (c_dbg); | ||
65 | } | ||
66 | |||
56 | size_t | 67 | size_t |
57 | MailboxBase :: messages_count () | 68 | MailboxBase :: messages_count () |
58 | { | 69 | { | ... | ... |
-
Please register or sign in to post a comment