libmu_cpp's new example.
Showing
8 changed files
with
911 additions
and
0 deletions
examples/cpp/addr.cc
0 → 100644
1 | /* | ||
2 | GNU Mailutils -- a suite of utilities for electronic mail | ||
3 | Copyright (C) 2004 Free Software Foundation, Inc. | ||
4 | |||
5 | GNU Mailutils is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | GNU Mailutils 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 | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNU Mailutils; if not, write to the Free Software | ||
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
18 | */ | ||
19 | |||
20 | #include <iostream> | ||
21 | #include <mailutils/cpp/mailutils.h> | ||
22 | |||
23 | #include <mailutils/filter.h> | ||
24 | #include <mailutils/mutil.h> | ||
25 | |||
26 | using namespace std; | ||
27 | using namespace mailutils; | ||
28 | |||
29 | static int | ||
30 | parse (const char *str) | ||
31 | { | ||
32 | size_t no = 0; | ||
33 | size_t count = 0; | ||
34 | std::string buf; | ||
35 | |||
36 | mu_set_user_email_domain ("localhost"); | ||
37 | |||
38 | try { | ||
39 | Address address (str); | ||
40 | count = address.GetCount (); | ||
41 | cout << str << "=> count " << count << endl; | ||
42 | |||
43 | for (no = 1; no <= count; no++) | ||
44 | { | ||
45 | bool isgroup = address.IsGroup (no); | ||
46 | cout << no << " "; | ||
47 | |||
48 | if (isgroup) | ||
49 | cout << "group " << address.GetPersonal (no) << endl; | ||
50 | else | ||
51 | cout << "email " << address.GetEmail (no) << endl; | ||
52 | |||
53 | if (!isgroup) | ||
54 | cout << " personal " << address.GetPersonal (no) << endl; | ||
55 | |||
56 | cout << " comments " << address.GetComments (no) << endl; | ||
57 | cout << " local-part " << address.GetLocalPart (no) | ||
58 | << " domain " << address.GetDomain (no) << endl; | ||
59 | cout << " route " << address.GetRoute (no) << endl; | ||
60 | } | ||
61 | } | ||
62 | catch (Exception& e) { | ||
63 | cerr << e.Method () << ": " << e.MsgError () << endl; | ||
64 | } | ||
65 | |||
66 | cout << endl; | ||
67 | return 0; | ||
68 | } | ||
69 | |||
70 | static int | ||
71 | parseinput (void) | ||
72 | { | ||
73 | char buf[BUFSIZ]; | ||
74 | |||
75 | while (!cin.getline (buf, sizeof (buf)).eof ()) | ||
76 | { | ||
77 | parse (buf); | ||
78 | } | ||
79 | |||
80 | return 0; | ||
81 | } | ||
82 | |||
83 | int | ||
84 | main (int argc, const char *argv[]) | ||
85 | { | ||
86 | argc = 1; | ||
87 | |||
88 | if (!argv[argc]) | ||
89 | return parseinput (); | ||
90 | |||
91 | for (; argv[argc]; argc++) | ||
92 | { | ||
93 | if (strcmp (argv[argc], "-") == 0) | ||
94 | parseinput (); | ||
95 | else | ||
96 | parse (argv[argc]); | ||
97 | } | ||
98 | |||
99 | return 0; | ||
100 | } | ||
101 |
examples/cpp/http.cc
0 → 100644
1 | /* | ||
2 | GNU Mailutils -- a suite of utilities for electronic mail | ||
3 | Copyright (C) 2004 Free Software Foundation, Inc. | ||
4 | |||
5 | GNU Mailutils is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | GNU Mailutils 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 | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNU Mailutils; if not, write to the Free Software | ||
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
18 | */ | ||
19 | |||
20 | /* This is an example program to illustrate the use of stream functions. | ||
21 | It connects to a remote HTTP server and prints the contents of its | ||
22 | index page */ | ||
23 | |||
24 | #include <iostream> | ||
25 | #include <mailutils/cpp/mailutils.h> | ||
26 | |||
27 | using namespace std; | ||
28 | using namespace mailutils; | ||
29 | |||
30 | std::string wbuf = "GET / HTTP/1.0\r\n\r\n"; | ||
31 | std::string rbuf; | ||
32 | |||
33 | int | ||
34 | main () | ||
35 | { | ||
36 | int off = 0; | ||
37 | |||
38 | try { | ||
39 | TcpStream stream ("www.gnu.org", 80, MU_STREAM_NONBLOCK); | ||
40 | |||
41 | connect_again: | ||
42 | try { | ||
43 | stream.Open (); | ||
44 | } | ||
45 | catch (Stream::EAgain) { | ||
46 | stream.Wait (MU_STREAM_READY_WR); | ||
47 | goto connect_again; | ||
48 | } | ||
49 | |||
50 | write_again: | ||
51 | try { | ||
52 | string wbuf (wbuf, off, wbuf.length ()); | ||
53 | stream << wbuf; | ||
54 | } | ||
55 | catch (Stream::EAgain) { | ||
56 | stream.Wait (MU_STREAM_READY_WR); | ||
57 | off += stream.GetWriten (); | ||
58 | goto write_again; | ||
59 | } | ||
60 | |||
61 | if (stream.GetWriten () != wbuf.length ()) | ||
62 | { | ||
63 | cerr << "stream.GetWriten() != wbuf length" << endl; | ||
64 | exit (1); | ||
65 | } | ||
66 | |||
67 | read_again: | ||
68 | do | ||
69 | { | ||
70 | try { | ||
71 | stream >> rbuf; | ||
72 | } | ||
73 | catch (Stream::EAgain) { | ||
74 | stream.Wait (MU_STREAM_READY_RD); | ||
75 | goto read_again; | ||
76 | } | ||
77 | cout << rbuf.substr (0, stream.GetReadn ()); | ||
78 | } | ||
79 | while (stream.GetReadn ()); | ||
80 | } | ||
81 | catch (Exception& e) { | ||
82 | cerr << e.Method () << ": " << e.MsgError () << endl; | ||
83 | exit (1); | ||
84 | } | ||
85 | |||
86 | return 0; | ||
87 | } | ||
88 |
examples/cpp/iconv.cc
0 → 100644
1 | /* | ||
2 | GNU Mailutils -- a suite of utilities for electronic mail | ||
3 | Copyright (C) 2004 Free Software Foundation, Inc. | ||
4 | |||
5 | GNU Mailutils is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | GNU Mailutils 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 | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNU Mailutils; if not, write to the Free Software | ||
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
18 | */ | ||
19 | |||
20 | #include <iostream> | ||
21 | #include <mailutils/cpp/mailutils.h> | ||
22 | |||
23 | using namespace std; | ||
24 | using namespace mailutils; | ||
25 | |||
26 | int | ||
27 | main (int argc, char **argv) | ||
28 | { | ||
29 | size_t total = 0; | ||
30 | char buffer[80]; | ||
31 | |||
32 | if (argc != 3) | ||
33 | { | ||
34 | cerr << "usage: " << argv[0] << " from-code to-code" << endl; | ||
35 | return 1; | ||
36 | } | ||
37 | |||
38 | try { | ||
39 | StdioStream *in = new StdioStream(stdin, 0); | ||
40 | in->Open (); | ||
41 | |||
42 | FilterStream cvt; | ||
43 | cvt.IconvCreate (*in, (string)argv[1], (string)argv[2], 0, mu_fallback_none); | ||
44 | cvt.Open (); | ||
45 | delete in; | ||
46 | |||
47 | StdioStream out (stdout, 0); | ||
48 | out.Open (); | ||
49 | |||
50 | do { | ||
51 | cvt.Read (buffer, sizeof (buffer), total); | ||
52 | out.SequentialWrite (buffer, cvt.GetReadn ()); | ||
53 | total += cvt.GetReadn (); | ||
54 | } while (cvt.GetReadn ()); | ||
55 | |||
56 | out.Flush (); | ||
57 | delete in; | ||
58 | } | ||
59 | catch (Exception& e) { | ||
60 | cerr << e.Method () << ": " << e.MsgError () << endl; | ||
61 | exit (1); | ||
62 | } | ||
63 | |||
64 | return 0; | ||
65 | } |
examples/cpp/listop.cc
0 → 100644
1 | /* | ||
2 | GNU Mailutils -- a suite of utilities for electronic mail | ||
3 | Copyright (C) 2004 Free Software Foundation, Inc. | ||
4 | |||
5 | GNU Mailutils is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | GNU Mailutils 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 | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNU Mailutils; if not, write to the Free Software | ||
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
18 | */ | ||
19 | |||
20 | #include <iostream> | ||
21 | #include <cstdlib> | ||
22 | #include <mailutils/cpp/mailutils.h> | ||
23 | #include <mailutils/argcv.h> | ||
24 | |||
25 | using namespace std; | ||
26 | using namespace mailutils; | ||
27 | |||
28 | void | ||
29 | usage (int code) | ||
30 | { | ||
31 | cout << "usage: listop [item..]" << endl; | ||
32 | exit (code); | ||
33 | } | ||
34 | |||
35 | void | ||
36 | print (List& lst) | ||
37 | { | ||
38 | Iterator itr (lst); | ||
39 | |||
40 | for (itr.First (); !itr.IsDone (); itr++) | ||
41 | { | ||
42 | char* text = (char *) itr.Current (); | ||
43 | cout << text << endl; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | void | ||
48 | next (Iterator* itr, char *arg) | ||
49 | { | ||
50 | int skip = arg ? strtoul (arg, NULL, 0) : 1; | ||
51 | |||
52 | if (skip == 0) | ||
53 | cout << "next arg?" << endl; | ||
54 | while (skip--) | ||
55 | itr->Next (); | ||
56 | } | ||
57 | |||
58 | void | ||
59 | del (List& lst, int argc, char **argv) | ||
60 | { | ||
61 | int rc; | ||
62 | if (argc == 1) | ||
63 | { | ||
64 | cerr << "del arg?" << endl; | ||
65 | return; | ||
66 | } | ||
67 | |||
68 | while (--argc) | ||
69 | { | ||
70 | try { | ||
71 | lst.Remove (strdup (*++argv)); | ||
72 | } | ||
73 | catch (Exception& e) { | ||
74 | cerr << e.Method () << ": " << e.MsgError () << endl; | ||
75 | } | ||
76 | } | ||
77 | } | ||
78 | |||
79 | void | ||
80 | add (List& lst, int argc, char **argv) | ||
81 | { | ||
82 | int rc; | ||
83 | |||
84 | if (argc == 1) | ||
85 | { | ||
86 | cerr << "add arg?" << endl; | ||
87 | return; | ||
88 | } | ||
89 | |||
90 | while (--argc) | ||
91 | { | ||
92 | try { | ||
93 | lst.Append (strdup (*++argv)); | ||
94 | } | ||
95 | catch (Exception& e) { | ||
96 | cerr << e.Method () << ": " << e.MsgError () << endl; | ||
97 | } | ||
98 | } | ||
99 | } | ||
100 | |||
101 | void | ||
102 | prep (List& lst, int argc, char **argv) | ||
103 | { | ||
104 | int rc; | ||
105 | if (argc == 1) | ||
106 | { | ||
107 | cerr << "add arg?" << endl; | ||
108 | return; | ||
109 | } | ||
110 | |||
111 | while (--argc) | ||
112 | { | ||
113 | try { | ||
114 | lst.Prepend (strdup (*++argv)); | ||
115 | } | ||
116 | catch (Exception& e) { | ||
117 | cerr << e.Method () << ": " << e.MsgError () << endl; | ||
118 | } | ||
119 | } | ||
120 | } | ||
121 | |||
122 | void | ||
123 | repl (List& lst, int argc, char **argv) | ||
124 | { | ||
125 | int rc; | ||
126 | if (argc != 3) | ||
127 | { | ||
128 | cerr << "repl src dst?" << endl; | ||
129 | return; | ||
130 | } | ||
131 | |||
132 | try { | ||
133 | lst.Replace (argv[1], strdup (argv[2])); | ||
134 | } | ||
135 | catch (Exception& e) { | ||
136 | cerr << e.Method () << ": " << e.MsgError () << endl; | ||
137 | } | ||
138 | } | ||
139 | |||
140 | #define NITR 4 | ||
141 | |||
142 | void | ||
143 | iter (int *pnum, int argc, char** argv) | ||
144 | { | ||
145 | int n; | ||
146 | if (argc != 2) | ||
147 | { | ||
148 | cerr << "iter num?" << endl; | ||
149 | return; | ||
150 | } | ||
151 | |||
152 | n = strtoul (argv[1], NULL, 0); | ||
153 | if (n < 0 || n >= NITR) | ||
154 | { | ||
155 | cerr << "iter [0-3]?" << endl; | ||
156 | return; | ||
157 | } | ||
158 | *pnum = n; | ||
159 | } | ||
160 | |||
161 | void | ||
162 | find (Iterator* itr, char* arg) | ||
163 | { | ||
164 | char *text; | ||
165 | if (!arg) | ||
166 | { | ||
167 | cerr << "find item?" << endl; | ||
168 | return; | ||
169 | } | ||
170 | |||
171 | itr->Current ((void**) &text); | ||
172 | for (itr->First (); !itr->IsDone (); itr->Next ()) | ||
173 | { | ||
174 | char *item; | ||
175 | |||
176 | itr->Current ((void**) &item); | ||
177 | if (!strcmp (arg, item)) | ||
178 | return; | ||
179 | } | ||
180 | |||
181 | cerr << arg << " not in list" << endl; | ||
182 | |||
183 | for (itr->First (); !itr->IsDone (); itr->Next ()) | ||
184 | { | ||
185 | char *item; | ||
186 | |||
187 | itr->Current ((void**) &item); | ||
188 | if (!strcmp (text, item)) | ||
189 | return; | ||
190 | } | ||
191 | } | ||
192 | |||
193 | void | ||
194 | help () | ||
195 | { | ||
196 | cout << "next [count]\n"; | ||
197 | cout << "first\n"; | ||
198 | cout << "find item\n"; | ||
199 | cout << "del item [item...]\n"; | ||
200 | cout << "add item [item...]\n"; | ||
201 | cout << "prep item [item...]\n"; | ||
202 | cout << "repl old_item new_item\n"; | ||
203 | cout << "print\n"; | ||
204 | cout << "quit\n"; | ||
205 | cout << "iter num\n"; | ||
206 | cout << "help\n"; | ||
207 | cout << "NUMBER\n"; | ||
208 | } | ||
209 | |||
210 | void | ||
211 | shell (List& lst) | ||
212 | { | ||
213 | int rc; | ||
214 | int num = 0; | ||
215 | Iterator* itr[NITR]; | ||
216 | |||
217 | for (num = 0; num < NITR; num++) | ||
218 | { | ||
219 | itr[num] = new Iterator (lst); | ||
220 | itr[num]->First (); | ||
221 | } | ||
222 | |||
223 | num = 0; | ||
224 | while (1) | ||
225 | { | ||
226 | char *text; | ||
227 | char buf[80]; | ||
228 | int argc; | ||
229 | char **argv; | ||
230 | |||
231 | try { | ||
232 | itr[num]->Current ((void**) &text); | ||
233 | } | ||
234 | catch (Exception& e) { | ||
235 | cerr << e.Method () << ": " << e.MsgError () << endl; | ||
236 | } | ||
237 | |||
238 | cout << num << ":(" << (text ? text : "NULL") << ")> "; | ||
239 | if (cin.getline (buf, sizeof (buf)).eof ()) | ||
240 | return; | ||
241 | |||
242 | rc = argcv_get (buf, "", "#", &argc, &argv); | ||
243 | if (rc) | ||
244 | cerr << "argcv_get: " << rc << endl; | ||
245 | |||
246 | if (argc > 0) | ||
247 | { | ||
248 | if (!strcmp (argv[0], "next")) | ||
249 | next (itr[num], argv[1]); | ||
250 | else if (!strcmp (argv[0], "first")) | ||
251 | itr[num]->First (); | ||
252 | else if (!strcmp (argv[0], "del")) | ||
253 | del (lst, argc, argv); | ||
254 | else if (!strcmp (argv[0], "add")) | ||
255 | add (lst, argc, argv); | ||
256 | else if (!strcmp (argv[0], "prep")) | ||
257 | prep (lst, argc, argv); | ||
258 | else if (!strcmp (argv[0], "repl")) | ||
259 | repl (lst, argc, argv); | ||
260 | else if (!strcmp (argv[0], "print")) | ||
261 | print (lst); | ||
262 | else if (!strcmp (argv[0], "quit")) | ||
263 | return; | ||
264 | else if (!strcmp (argv[0], "iter")) | ||
265 | iter (&num, argc, argv); | ||
266 | else if (!strcmp (argv[0], "find")) | ||
267 | find (itr[num], argv[1]); | ||
268 | else if (!strcmp (argv[0], "help")) | ||
269 | help (); | ||
270 | else if (argc == 1) | ||
271 | { | ||
272 | char* p; | ||
273 | size_t n = strtoul (argv[0], &p, 0); | ||
274 | if (*p != 0) | ||
275 | cerr << "?" << endl; | ||
276 | else | ||
277 | { | ||
278 | try { | ||
279 | text = (char*) lst[n]; | ||
280 | } | ||
281 | catch (Exception& e) { | ||
282 | cerr << e.Method () << ": " << e.MsgError () << endl; | ||
283 | } | ||
284 | |||
285 | // else | ||
286 | cout << text << endl; | ||
287 | } | ||
288 | } | ||
289 | else | ||
290 | cerr << "?" << endl; | ||
291 | } | ||
292 | argcv_free (argc, argv); | ||
293 | } | ||
294 | } | ||
295 | |||
296 | static int | ||
297 | string_comp (const void* item, const void* value) | ||
298 | { | ||
299 | return strcmp ((const char*) item, (const char*) value); | ||
300 | } | ||
301 | |||
302 | int | ||
303 | main (int argc, char **argv) | ||
304 | { | ||
305 | int rc; | ||
306 | |||
307 | while ((rc = getopt (argc, argv, "h")) != EOF) | ||
308 | switch (rc) | ||
309 | { | ||
310 | case 'h': | ||
311 | usage (0); | ||
312 | |||
313 | default: | ||
314 | usage (1); | ||
315 | } | ||
316 | |||
317 | argc -= optind; | ||
318 | argv += optind; | ||
319 | |||
320 | try { | ||
321 | List lst; | ||
322 | lst.SetComparator (string_comp); | ||
323 | |||
324 | while (argc--) | ||
325 | { | ||
326 | lst.Append (*argv++); | ||
327 | } | ||
328 | |||
329 | shell (lst); | ||
330 | } | ||
331 | catch (Exception& e) { | ||
332 | cerr << e.Method () << ": " << e.MsgError () << endl; | ||
333 | } | ||
334 | |||
335 | return 0; | ||
336 | } | ||
337 |
examples/cpp/mailcap.cc
0 → 100644
1 | /* | ||
2 | GNU Mailutils -- a suite of utilities for electronic mail | ||
3 | Copyright (C) 2004 Free Software Foundation, Inc. | ||
4 | |||
5 | GNU Mailutils is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | GNU Mailutils 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 | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNU Mailutils; if not, write to the Free Software | ||
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
18 | */ | ||
19 | |||
20 | #include <iostream> | ||
21 | #include <mailutils/cpp/mailutils.h> | ||
22 | |||
23 | using namespace std; | ||
24 | using namespace mailutils; | ||
25 | |||
26 | int | ||
27 | main (int argc, char **argv) | ||
28 | { | ||
29 | int status = 0; | ||
30 | char* file = "/etc/mailcap"; | ||
31 | |||
32 | try { | ||
33 | FileStream stream ((std::string) file, MU_STREAM_READ); | ||
34 | stream.Open (); | ||
35 | |||
36 | Mailcap mailcap (stream); | ||
37 | |||
38 | int i; | ||
39 | size_t count = 0; | ||
40 | string buffer; | ||
41 | |||
42 | count = mailcap.GetCount (); | ||
43 | for (i = 1; i <= count; i++) | ||
44 | { | ||
45 | size_t j; | ||
46 | size_t fields_count = 0; | ||
47 | |||
48 | cout << "entry[" << i << "]\n"; | ||
49 | |||
50 | MailcapEntry entry = mailcap.GetEntry (i); | ||
51 | |||
52 | /* typefield. */ | ||
53 | buffer = entry.GetTypeField (); | ||
54 | cout << "\ttypefield: " << buffer << endl; | ||
55 | |||
56 | /* view-command. */ | ||
57 | buffer = entry.GetViewCommand (); | ||
58 | cout << "\tview-command: " << buffer << endl; | ||
59 | |||
60 | /* fields. */ | ||
61 | fields_count = entry.FieldsCount (); | ||
62 | for (j = 1; j <= fields_count; j++) | ||
63 | { | ||
64 | try { | ||
65 | buffer = entry.GetField (j); | ||
66 | } | ||
67 | catch (Exception& e) { | ||
68 | cerr << e.Method () << ": cannot retrieve field " | ||
69 | << j << ": " << e.MsgError () << endl; | ||
70 | } | ||
71 | cout << "\tfields[" << j << "]: " << buffer << endl; | ||
72 | } | ||
73 | |||
74 | cout << endl; | ||
75 | } | ||
76 | } | ||
77 | catch (Exception& e) { | ||
78 | cerr << e.Method () << ": " << e.MsgError () << endl; | ||
79 | exit (1); | ||
80 | } | ||
81 | |||
82 | return 0; | ||
83 | } |
examples/cpp/murun.cc
0 → 100644
1 | /* | ||
2 | GNU Mailutils -- a suite of utilities for electronic mail | ||
3 | Copyright (C) 2004 Free Software Foundation, Inc. | ||
4 | |||
5 | GNU Mailutils is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | GNU Mailutils 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 | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNU Mailutils; if not, write to the Free Software | ||
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
18 | */ | ||
19 | |||
20 | #include <iostream> | ||
21 | #include <mailutils/cpp/mailutils.h> | ||
22 | #include <mailutils/argcv.h> | ||
23 | |||
24 | using namespace std; | ||
25 | using namespace mailutils; | ||
26 | |||
27 | static char *progname; | ||
28 | |||
29 | static void | ||
30 | read_and_print (Stream *in, Stream& out) | ||
31 | { | ||
32 | char buffer[128]; | ||
33 | |||
34 | in->SequentialReadLine (buffer, sizeof (buffer)); | ||
35 | while (in->GetReadn ()) | ||
36 | { | ||
37 | out.SequentialWrite (buffer, in->GetReadn ()); | ||
38 | in->SequentialReadLine (buffer, sizeof (buffer)); | ||
39 | } | ||
40 | } | ||
41 | |||
42 | Stream * | ||
43 | createFilter (bool read_stdin, char *cmdline, int flags) | ||
44 | { | ||
45 | try { | ||
46 | if (read_stdin) | ||
47 | { | ||
48 | StdioStream *in = new StdioStream (stdin, 0); | ||
49 | in->Open (); | ||
50 | FilterProgStream *stream = new FilterProgStream (cmdline, in); | ||
51 | stream->Open (); | ||
52 | return stream; | ||
53 | } | ||
54 | else | ||
55 | { | ||
56 | ProgStream *stream = new ProgStream (cmdline, flags); | ||
57 | stream->Open (); | ||
58 | return stream; | ||
59 | } | ||
60 | } | ||
61 | catch (Exception& e) { | ||
62 | cerr << progname << ": cannot create program filter stream: " | ||
63 | << e.Method () << ": " << e.MsgError () << endl; | ||
64 | exit (1); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | int | ||
69 | main (int argc, char *argv[]) | ||
70 | { | ||
71 | int i = 1; | ||
72 | int read_stdin = 0; | ||
73 | int flags = MU_STREAM_READ; | ||
74 | char *cmdline; | ||
75 | Stream *stream, out; | ||
76 | |||
77 | progname = argv[0]; | ||
78 | |||
79 | if (argc > 1 && strcmp (argv[i], "--stdin") == 0) | ||
80 | { | ||
81 | read_stdin = 1; | ||
82 | flags |= MU_STREAM_WRITE; | ||
83 | i++; | ||
84 | } | ||
85 | |||
86 | if (i == argc) | ||
87 | { | ||
88 | cerr << "Usage: " << argv[0] << " [--stdin] progname [args]" << endl; | ||
89 | exit (1); | ||
90 | } | ||
91 | |||
92 | argcv_string (argc - i, &argv[i], &cmdline); | ||
93 | |||
94 | stream = createFilter (read_stdin, cmdline, flags); | ||
95 | |||
96 | try { | ||
97 | StdioStream out (stdout, 0); | ||
98 | out.Open (); | ||
99 | |||
100 | read_and_print (stream, out); | ||
101 | |||
102 | delete stream; | ||
103 | } | ||
104 | catch (Exception& e) { | ||
105 | cerr << e.Method () << ": " << e.MsgError () << endl; | ||
106 | exit (1); | ||
107 | } | ||
108 | |||
109 | return 0; | ||
110 | } | ||
111 |
examples/cpp/sfrom.cc
0 → 100644
1 | /* | ||
2 | GNU Mailutils -- a suite of utilities for electronic mail | ||
3 | Copyright (C) 2004 Free Software Foundation, Inc. | ||
4 | |||
5 | GNU Mailutils is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | GNU Mailutils 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 | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNU Mailutils; if not, write to the Free Software | ||
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
18 | */ | ||
19 | |||
20 | #include <iostream> | ||
21 | #include <mailutils/cpp/mailutils.h> | ||
22 | |||
23 | #include <mailutils/registrar.h> // tmp | ||
24 | #include <mailutils/list.h> // tmp | ||
25 | |||
26 | using namespace std; | ||
27 | using namespace mailutils; | ||
28 | |||
29 | int main (int argc, char* argv[]) | ||
30 | { | ||
31 | size_t total = 0; | ||
32 | |||
33 | if (argc == 1) | ||
34 | exit (0); | ||
35 | |||
36 | mu_register_local_mbox_formats(); | ||
37 | |||
38 | Message msg; | ||
39 | Header hdr; | ||
40 | |||
41 | try { | ||
42 | |||
43 | MailboxDefault mbox (argv[1]); | ||
44 | |||
45 | mbox.Open (MU_STREAM_READ); | ||
46 | total = mbox.MessagesCount (); | ||
47 | cout << "Total: " << total << endl; | ||
48 | |||
49 | for (int msgno = 1; msgno <= total; msgno++) | ||
50 | { | ||
51 | msg = mbox[msgno]; | ||
52 | hdr = msg.GetHeader (); | ||
53 | cout << hdr[MU_HEADER_FROM] << " " | ||
54 | << hdr[MU_HEADER_SUBJECT] << endl; | ||
55 | } | ||
56 | |||
57 | mbox.Close (); | ||
58 | } | ||
59 | catch (Exception& e) { | ||
60 | cerr << e.Method () << ": " << e.MsgError () << endl; | ||
61 | exit (1); | ||
62 | } | ||
63 | |||
64 | return 0; | ||
65 | } | ||
66 |
examples/cpp/url-parse.cc
0 → 100644
1 | /* | ||
2 | GNU Mailutils -- a suite of utilities for electronic mail | ||
3 | Copyright (C) 2004 Free Software Foundation, Inc. | ||
4 | |||
5 | GNU Mailutils is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2, or (at your option) | ||
8 | any later version. | ||
9 | |||
10 | GNU Mailutils 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 | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNU Mailutils; if not, write to the Free Software | ||
17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
18 | */ | ||
19 | |||
20 | #include <iostream> | ||
21 | #include <mailutils/cpp/mailutils.h> | ||
22 | |||
23 | #include <cstdlib> | ||
24 | #include <cstring> | ||
25 | |||
26 | using namespace std; | ||
27 | using namespace mailutils; | ||
28 | |||
29 | int | ||
30 | main () | ||
31 | { | ||
32 | char str[1024]; | ||
33 | |||
34 | again: | ||
35 | while (!cin.getline (str, sizeof (str)).eof ()) | ||
36 | { | ||
37 | if (strspn (str, " \t") == strlen (str)) | ||
38 | continue; /* skip empty lines */ | ||
39 | |||
40 | try { | ||
41 | Url url (str); | ||
42 | url.Parse (); | ||
43 | |||
44 | cout << "\tscheme <" << url.GetScheme () << ">" << endl; | ||
45 | cout << "\tuser <" << url.GetUser () << ">" << endl; | ||
46 | cout << "\tpasswd <" << url.GetPasswd () << ">" << endl; | ||
47 | cout << "\tauth <" << url.GetAuth () << ">" << endl; | ||
48 | cout << "\thost <" << url.GetHost () << ">" << endl; | ||
49 | cout << "\tport " << url.GetPort () << endl; | ||
50 | cout << "\tpath <" << url.GetPath () << ">" << endl; | ||
51 | cout << "\tquery <" << url.GetQuery () << ">" << endl; | ||
52 | } | ||
53 | catch (Exception& e) { | ||
54 | cerr << e.Method () << ": " << e.MsgError () << endl; | ||
55 | goto again; | ||
56 | } | ||
57 | } | ||
58 | return 0; | ||
59 | } | ||
60 |
-
Please register or sign in to post a comment