diff options
author | Wojciech Polak <polak@gnu.org> | 2004-11-17 21:33:18 +0000 |
---|---|---|
committer | Wojciech Polak <polak@gnu.org> | 2004-11-17 21:33:18 +0000 |
commit | ed306cd08e2a39cc9219e1cab0f915022ac31023 (patch) | |
tree | 04acf74611336d561ffeabfe4a96f9d7288748c5 /examples | |
parent | 7e90b785792197188501d8dfc78f8e3057634796 (diff) | |
download | mailutils-ed306cd08e2a39cc9219e1cab0f915022ac31023.tar.gz mailutils-ed306cd08e2a39cc9219e1cab0f915022ac31023.tar.bz2 |
libmu_cpp's new example.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/cpp/addr.cc | 101 | ||||
-rw-r--r-- | examples/cpp/http.cc | 88 | ||||
-rw-r--r-- | examples/cpp/iconv.cc | 65 | ||||
-rw-r--r-- | examples/cpp/listop.cc | 337 | ||||
-rw-r--r-- | examples/cpp/mailcap.cc | 83 | ||||
-rw-r--r-- | examples/cpp/murun.cc | 111 | ||||
-rw-r--r-- | examples/cpp/sfrom.cc | 66 | ||||
-rw-r--r-- | examples/cpp/url-parse.cc | 60 |
8 files changed, 911 insertions, 0 deletions
diff --git a/examples/cpp/addr.cc b/examples/cpp/addr.cc new file mode 100644 index 000000000..2043412d4 --- /dev/null +++ b/examples/cpp/addr.cc @@ -0,0 +1,101 @@ +/* + GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 2004 Free Software Foundation, Inc. + + GNU Mailutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU Mailutils is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Mailutils; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include <iostream> +#include <mailutils/cpp/mailutils.h> + +#include <mailutils/filter.h> +#include <mailutils/mutil.h> + +using namespace std; +using namespace mailutils; + +static int +parse (const char *str) +{ + size_t no = 0; + size_t count = 0; + std::string buf; + + mu_set_user_email_domain ("localhost"); + + try { + Address address (str); + count = address.GetCount (); + cout << str << "=> count " << count << endl; + + for (no = 1; no <= count; no++) + { + bool isgroup = address.IsGroup (no); + cout << no << " "; + + if (isgroup) + cout << "group " << address.GetPersonal (no) << endl; + else + cout << "email " << address.GetEmail (no) << endl; + + if (!isgroup) + cout << " personal " << address.GetPersonal (no) << endl; + + cout << " comments " << address.GetComments (no) << endl; + cout << " local-part " << address.GetLocalPart (no) + << " domain " << address.GetDomain (no) << endl; + cout << " route " << address.GetRoute (no) << endl; + } + } + catch (Exception& e) { + cerr << e.Method () << ": " << e.MsgError () << endl; + } + + cout << endl; + return 0; +} + +static int +parseinput (void) +{ + char buf[BUFSIZ]; + + while (!cin.getline (buf, sizeof (buf)).eof ()) + { + parse (buf); + } + + return 0; +} + +int +main (int argc, const char *argv[]) +{ + argc = 1; + + if (!argv[argc]) + return parseinput (); + + for (; argv[argc]; argc++) + { + if (strcmp (argv[argc], "-") == 0) + parseinput (); + else + parse (argv[argc]); + } + + return 0; +} + diff --git a/examples/cpp/http.cc b/examples/cpp/http.cc new file mode 100644 index 000000000..296190940 --- /dev/null +++ b/examples/cpp/http.cc @@ -0,0 +1,88 @@ +/* + GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 2004 Free Software Foundation, Inc. + + GNU Mailutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU Mailutils is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Mailutils; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +/* This is an example program to illustrate the use of stream functions. + It connects to a remote HTTP server and prints the contents of its + index page */ + +#include <iostream> +#include <mailutils/cpp/mailutils.h> + +using namespace std; +using namespace mailutils; + +std::string wbuf = "GET / HTTP/1.0\r\n\r\n"; +std::string rbuf; + +int +main () +{ + int off = 0; + + try { + TcpStream stream ("www.gnu.org", 80, MU_STREAM_NONBLOCK); + + connect_again: + try { + stream.Open (); + } + catch (Stream::EAgain) { + stream.Wait (MU_STREAM_READY_WR); + goto connect_again; + } + + write_again: + try { + string wbuf (wbuf, off, wbuf.length ()); + stream << wbuf; + } + catch (Stream::EAgain) { + stream.Wait (MU_STREAM_READY_WR); + off += stream.GetWriten (); + goto write_again; + } + + if (stream.GetWriten () != wbuf.length ()) + { + cerr << "stream.GetWriten() != wbuf length" << endl; + exit (1); + } + + read_again: + do + { + try { + stream >> rbuf; + } + catch (Stream::EAgain) { + stream.Wait (MU_STREAM_READY_RD); + goto read_again; + } + cout << rbuf.substr (0, stream.GetReadn ()); + } + while (stream.GetReadn ()); + } + catch (Exception& e) { + cerr << e.Method () << ": " << e.MsgError () << endl; + exit (1); + } + + return 0; +} + diff --git a/examples/cpp/iconv.cc b/examples/cpp/iconv.cc new file mode 100644 index 000000000..35c7445eb --- /dev/null +++ b/examples/cpp/iconv.cc @@ -0,0 +1,65 @@ +/* + GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 2004 Free Software Foundation, Inc. + + GNU Mailutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU Mailutils is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Mailutils; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include <iostream> +#include <mailutils/cpp/mailutils.h> + +using namespace std; +using namespace mailutils; + +int +main (int argc, char **argv) +{ + size_t total = 0; + char buffer[80]; + + if (argc != 3) + { + cerr << "usage: " << argv[0] << " from-code to-code" << endl; + return 1; + } + + try { + StdioStream *in = new StdioStream(stdin, 0); + in->Open (); + + FilterStream cvt; + cvt.IconvCreate (*in, (string)argv[1], (string)argv[2], 0, mu_fallback_none); + cvt.Open (); + delete in; + + StdioStream out (stdout, 0); + out.Open (); + + do { + cvt.Read (buffer, sizeof (buffer), total); + out.SequentialWrite (buffer, cvt.GetReadn ()); + total += cvt.GetReadn (); + } while (cvt.GetReadn ()); + + out.Flush (); + delete in; + } + catch (Exception& e) { + cerr << e.Method () << ": " << e.MsgError () << endl; + exit (1); + } + + return 0; +} diff --git a/examples/cpp/listop.cc b/examples/cpp/listop.cc new file mode 100644 index 000000000..c88d1bbb9 --- /dev/null +++ b/examples/cpp/listop.cc @@ -0,0 +1,337 @@ +/* + GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 2004 Free Software Foundation, Inc. + + GNU Mailutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU Mailutils is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Mailutils; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include <iostream> +#include <cstdlib> +#include <mailutils/cpp/mailutils.h> +#include <mailutils/argcv.h> + +using namespace std; +using namespace mailutils; + +void +usage (int code) +{ + cout << "usage: listop [item..]" << endl; + exit (code); +} + +void +print (List& lst) +{ + Iterator itr (lst); + + for (itr.First (); !itr.IsDone (); itr++) + { + char* text = (char *) itr.Current (); + cout << text << endl; + } +} + +void +next (Iterator* itr, char *arg) +{ + int skip = arg ? strtoul (arg, NULL, 0) : 1; + + if (skip == 0) + cout << "next arg?" << endl; + while (skip--) + itr->Next (); +} + +void +del (List& lst, int argc, char **argv) +{ + int rc; + if (argc == 1) + { + cerr << "del arg?" << endl; + return; + } + + while (--argc) + { + try { + lst.Remove (strdup (*++argv)); + } + catch (Exception& e) { + cerr << e.Method () << ": " << e.MsgError () << endl; + } + } +} + +void +add (List& lst, int argc, char **argv) +{ + int rc; + + if (argc == 1) + { + cerr << "add arg?" << endl; + return; + } + + while (--argc) + { + try { + lst.Append (strdup (*++argv)); + } + catch (Exception& e) { + cerr << e.Method () << ": " << e.MsgError () << endl; + } + } +} + +void +prep (List& lst, int argc, char **argv) +{ + int rc; + if (argc == 1) + { + cerr << "add arg?" << endl; + return; + } + + while (--argc) + { + try { + lst.Prepend (strdup (*++argv)); + } + catch (Exception& e) { + cerr << e.Method () << ": " << e.MsgError () << endl; + } + } +} + +void +repl (List& lst, int argc, char **argv) +{ + int rc; + if (argc != 3) + { + cerr << "repl src dst?" << endl; + return; + } + + try { + lst.Replace (argv[1], strdup (argv[2])); + } + catch (Exception& e) { + cerr << e.Method () << ": " << e.MsgError () << endl; + } +} + +#define NITR 4 + +void +iter (int *pnum, int argc, char** argv) +{ + int n; + if (argc != 2) + { + cerr << "iter num?" << endl; + return; + } + + n = strtoul (argv[1], NULL, 0); + if (n < 0 || n >= NITR) + { + cerr << "iter [0-3]?" << endl; + return; + } + *pnum = n; +} + +void +find (Iterator* itr, char* arg) +{ + char *text; + if (!arg) + { + cerr << "find item?" << endl; + return; + } + + itr->Current ((void**) &text); + for (itr->First (); !itr->IsDone (); itr->Next ()) + { + char *item; + + itr->Current ((void**) &item); + if (!strcmp (arg, item)) + return; + } + + cerr << arg << " not in list" << endl; + + for (itr->First (); !itr->IsDone (); itr->Next ()) + { + char *item; + + itr->Current ((void**) &item); + if (!strcmp (text, item)) + return; + } +} + +void +help () +{ + cout << "next [count]\n"; + cout << "first\n"; + cout << "find item\n"; + cout << "del item [item...]\n"; + cout << "add item [item...]\n"; + cout << "prep item [item...]\n"; + cout << "repl old_item new_item\n"; + cout << "print\n"; + cout << "quit\n"; + cout << "iter num\n"; + cout << "help\n"; + cout << "NUMBER\n"; +} + +void +shell (List& lst) +{ + int rc; + int num = 0; + Iterator* itr[NITR]; + + for (num = 0; num < NITR; num++) + { + itr[num] = new Iterator (lst); + itr[num]->First (); + } + + num = 0; + while (1) + { + char *text; + char buf[80]; + int argc; + char **argv; + + try { + itr[num]->Current ((void**) &text); + } + catch (Exception& e) { + cerr << e.Method () << ": " << e.MsgError () << endl; + } + + cout << num << ":(" << (text ? text : "NULL") << ")> "; + if (cin.getline (buf, sizeof (buf)).eof ()) + return; + + rc = argcv_get (buf, "", "#", &argc, &argv); + if (rc) + cerr << "argcv_get: " << rc << endl; + + if (argc > 0) + { + if (!strcmp (argv[0], "next")) + next (itr[num], argv[1]); + else if (!strcmp (argv[0], "first")) + itr[num]->First (); + else if (!strcmp (argv[0], "del")) + del (lst, argc, argv); + else if (!strcmp (argv[0], "add")) + add (lst, argc, argv); + else if (!strcmp (argv[0], "prep")) + prep (lst, argc, argv); + else if (!strcmp (argv[0], "repl")) + repl (lst, argc, argv); + else if (!strcmp (argv[0], "print")) + print (lst); + else if (!strcmp (argv[0], "quit")) + return; + else if (!strcmp (argv[0], "iter")) + iter (&num, argc, argv); + else if (!strcmp (argv[0], "find")) + find (itr[num], argv[1]); + else if (!strcmp (argv[0], "help")) + help (); + else if (argc == 1) + { + char* p; + size_t n = strtoul (argv[0], &p, 0); + if (*p != 0) + cerr << "?" << endl; + else + { + try { + text = (char*) lst[n]; + } + catch (Exception& e) { + cerr << e.Method () << ": " << e.MsgError () << endl; + } + + // else + cout << text << endl; + } + } + else + cerr << "?" << endl; + } + argcv_free (argc, argv); + } +} + +static int +string_comp (const void* item, const void* value) +{ + return strcmp ((const char*) item, (const char*) value); +} + +int +main (int argc, char **argv) +{ + int rc; + + while ((rc = getopt (argc, argv, "h")) != EOF) + switch (rc) + { + case 'h': + usage (0); + + default: + usage (1); + } + + argc -= optind; + argv += optind; + + try { + List lst; + lst.SetComparator (string_comp); + + while (argc--) + { + lst.Append (*argv++); + } + + shell (lst); + } + catch (Exception& e) { + cerr << e.Method () << ": " << e.MsgError () << endl; + } + + return 0; +} + diff --git a/examples/cpp/mailcap.cc b/examples/cpp/mailcap.cc new file mode 100644 index 000000000..f26035549 --- /dev/null +++ b/examples/cpp/mailcap.cc @@ -0,0 +1,83 @@ +/* + GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 2004 Free Software Foundation, Inc. + + GNU Mailutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU Mailutils is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Mailutils; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include <iostream> +#include <mailutils/cpp/mailutils.h> + +using namespace std; +using namespace mailutils; + +int +main (int argc, char **argv) +{ + int status = 0; + char* file = "/etc/mailcap"; + + try { + FileStream stream ((std::string) file, MU_STREAM_READ); + stream.Open (); + + Mailcap mailcap (stream); + + int i; + size_t count = 0; + string buffer; + + count = mailcap.GetCount (); + for (i = 1; i <= count; i++) + { + size_t j; + size_t fields_count = 0; + + cout << "entry[" << i << "]\n"; + + MailcapEntry entry = mailcap.GetEntry (i); + + /* typefield. */ + buffer = entry.GetTypeField (); + cout << "\ttypefield: " << buffer << endl; + + /* view-command. */ + buffer = entry.GetViewCommand (); + cout << "\tview-command: " << buffer << endl; + + /* fields. */ + fields_count = entry.FieldsCount (); + for (j = 1; j <= fields_count; j++) + { + try { + buffer = entry.GetField (j); + } + catch (Exception& e) { + cerr << e.Method () << ": cannot retrieve field " + << j << ": " << e.MsgError () << endl; + } + cout << "\tfields[" << j << "]: " << buffer << endl; + } + + cout << endl; + } + } + catch (Exception& e) { + cerr << e.Method () << ": " << e.MsgError () << endl; + exit (1); + } + + return 0; +} diff --git a/examples/cpp/murun.cc b/examples/cpp/murun.cc new file mode 100644 index 000000000..31368782d --- /dev/null +++ b/examples/cpp/murun.cc @@ -0,0 +1,111 @@ +/* + GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 2004 Free Software Foundation, Inc. + + GNU Mailutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU Mailutils is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Mailutils; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include <iostream> +#include <mailutils/cpp/mailutils.h> +#include <mailutils/argcv.h> + +using namespace std; +using namespace mailutils; + +static char *progname; + +static void +read_and_print (Stream *in, Stream& out) +{ + char buffer[128]; + + in->SequentialReadLine (buffer, sizeof (buffer)); + while (in->GetReadn ()) + { + out.SequentialWrite (buffer, in->GetReadn ()); + in->SequentialReadLine (buffer, sizeof (buffer)); + } +} + +Stream * +createFilter (bool read_stdin, char *cmdline, int flags) +{ + try { + if (read_stdin) + { + StdioStream *in = new StdioStream (stdin, 0); + in->Open (); + FilterProgStream *stream = new FilterProgStream (cmdline, in); + stream->Open (); + return stream; + } + else + { + ProgStream *stream = new ProgStream (cmdline, flags); + stream->Open (); + return stream; + } + } + catch (Exception& e) { + cerr << progname << ": cannot create program filter stream: " + << e.Method () << ": " << e.MsgError () << endl; + exit (1); + } +} + +int +main (int argc, char *argv[]) +{ + int i = 1; + int read_stdin = 0; + int flags = MU_STREAM_READ; + char *cmdline; + Stream *stream, out; + + progname = argv[0]; + + if (argc > 1 && strcmp (argv[i], "--stdin") == 0) + { + read_stdin = 1; + flags |= MU_STREAM_WRITE; + i++; + } + + if (i == argc) + { + cerr << "Usage: " << argv[0] << " [--stdin] progname [args]" << endl; + exit (1); + } + + argcv_string (argc - i, &argv[i], &cmdline); + + stream = createFilter (read_stdin, cmdline, flags); + + try { + StdioStream out (stdout, 0); + out.Open (); + + read_and_print (stream, out); + + delete stream; + } + catch (Exception& e) { + cerr << e.Method () << ": " << e.MsgError () << endl; + exit (1); + } + + return 0; +} + diff --git a/examples/cpp/sfrom.cc b/examples/cpp/sfrom.cc new file mode 100644 index 000000000..cfbf65b9b --- /dev/null +++ b/examples/cpp/sfrom.cc @@ -0,0 +1,66 @@ +/* + GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 2004 Free Software Foundation, Inc. + + GNU Mailutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU Mailutils is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Mailutils; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include <iostream> +#include <mailutils/cpp/mailutils.h> + +#include <mailutils/registrar.h> // tmp +#include <mailutils/list.h> // tmp + +using namespace std; +using namespace mailutils; + +int main (int argc, char* argv[]) +{ + size_t total = 0; + + if (argc == 1) + exit (0); + + mu_register_local_mbox_formats(); + + Message msg; + Header hdr; + + try { + + MailboxDefault mbox (argv[1]); + + mbox.Open (MU_STREAM_READ); + total = mbox.MessagesCount (); + cout << "Total: " << total << endl; + + for (int msgno = 1; msgno <= total; msgno++) + { + msg = mbox[msgno]; + hdr = msg.GetHeader (); + cout << hdr[MU_HEADER_FROM] << " " + << hdr[MU_HEADER_SUBJECT] << endl; + } + + mbox.Close (); + } + catch (Exception& e) { + cerr << e.Method () << ": " << e.MsgError () << endl; + exit (1); + } + + return 0; +} + diff --git a/examples/cpp/url-parse.cc b/examples/cpp/url-parse.cc new file mode 100644 index 000000000..97fcc105e --- /dev/null +++ b/examples/cpp/url-parse.cc @@ -0,0 +1,60 @@ +/* + GNU Mailutils -- a suite of utilities for electronic mail + Copyright (C) 2004 Free Software Foundation, Inc. + + GNU Mailutils is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU Mailutils is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Mailutils; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include <iostream> +#include <mailutils/cpp/mailutils.h> + +#include <cstdlib> +#include <cstring> + +using namespace std; +using namespace mailutils; + +int +main () +{ + char str[1024]; + + again: + while (!cin.getline (str, sizeof (str)).eof ()) + { + if (strspn (str, " \t") == strlen (str)) + continue; /* skip empty lines */ + + try { + Url url (str); + url.Parse (); + + cout << "\tscheme <" << url.GetScheme () << ">" << endl; + cout << "\tuser <" << url.GetUser () << ">" << endl; + cout << "\tpasswd <" << url.GetPasswd () << ">" << endl; + cout << "\tauth <" << url.GetAuth () << ">" << endl; + cout << "\thost <" << url.GetHost () << ">" << endl; + cout << "\tport " << url.GetPort () << endl; + cout << "\tpath <" << url.GetPath () << ">" << endl; + cout << "\tquery <" << url.GetQuery () << ">" << endl; + } + catch (Exception& e) { + cerr << e.Method () << ": " << e.MsgError () << endl; + goto again; + } + } + return 0; +} + |