From c14b34647ac9dd714788f25f6355e6e031bdc392 Mon Sep 17 00:00:00 2001 From: dockes Date: Wed, 26 Jan 2005 11:45:55 +0000 Subject: [PATCH] mime header parsing embryo --- src/utils/mimeparse.cpp | 100 ++++++++++++++++++++++++++++++++++++++++ src/utils/mimeparse.h | 17 +++++++ 2 files changed, 117 insertions(+) create mode 100644 src/utils/mimeparse.cpp create mode 100644 src/utils/mimeparse.h diff --git a/src/utils/mimeparse.cpp b/src/utils/mimeparse.cpp new file mode 100644 index 00000000..57f51c1c --- /dev/null +++ b/src/utils/mimeparse.cpp @@ -0,0 +1,100 @@ +#ifndef lint +static char rcsid[] = "@(#$Id: mimeparse.cpp,v 1.1 2005-01-26 11:45:55 dockes Exp $ (C) 2004 J.F.Dockes"; +#endif + +#ifndef TEST_MIMEPARSE + +#include +#include +#include + +#include "mimeparse.h" + +using namespace std; +#define WHITE " \t\n" + +static void stripw_lc(string &in) +{ + // fprintf(stderr, "In: '%s'\n", in.c_str()); + string::size_type pos, pos1; + pos = in.find_first_not_of(WHITE); + if (pos == string::npos) { + // All white + in = ""; + return; + } + in.replace(0, pos, ""); + pos1 = in.find_last_not_of(WHITE); + if (pos1 != in.length() -1) + in = in.replace(pos1+1, string::npos, ""); + string::iterator i; + for (i = in.begin(); i != in.end(); i++) + *i = tolower(*i); +} + +MimeHeaderValue parseMimeHeaderValue(const string &ein) +{ + string in = ein; + MimeHeaderValue out; + string::size_type pos, pos1; + + pos = in.find_first_not_of(WHITE); + if (pos == string::npos) + return out; + in = in.substr(pos, string::npos); + if ((pos = in.find_first_of(";")) == string::npos) { + out.value = in; + return out; + } + out.value = in.substr(0, pos); + stripw_lc(out.value); + in = in.substr(pos+1, string::npos); + for (;;) { + // Skip whitespace + if ((pos = in.find_first_not_of(WHITE)) == string::npos) + return out; + in = in.substr(pos, string::npos); + + if ((pos = in.find_first_of("=")) == string::npos) + return out; + string pname = in.substr(0, pos); + stripw_lc(pname); + in = in.substr(pos+1, string::npos); + + pos = in.find_first_of(";"); + string pvalue = in.substr(0, pos); + stripw_lc(pvalue); + out.params[pname] = pvalue; + if (pos == string::npos) + return out; + in = in.substr(pos+1, string::npos); + } + + return out; + +} + +#else + +#include +#include "mimeparse.h" +using namespace std; +int +main(int argc, const char **argv) +{ + + MimeHeaderValue parsed; + + // const char *tr = "text/html; charset=utf-8; otherparam=garb"; + const char *tr = "text/html;charset = UTF-8 ; otherparam=garb;"; + + parsed = parseMimeHeaderValue(tr); + + printf("'%s' \n", parsed.value.c_str()); + map::iterator it; + for (it = parsed.params.begin();it != parsed.params.end();it++) { + printf(" '%s' = '%s'\n", it->first.c_str(), it->second.c_str()); + } +} + +#endif // TEST_MIMEPARSE diff --git a/src/utils/mimeparse.h b/src/utils/mimeparse.h new file mode 100644 index 00000000..3c96dc3b --- /dev/null +++ b/src/utils/mimeparse.h @@ -0,0 +1,17 @@ +#ifndef _MIME_H_INCLUDED_ +#define _MIME_H_INCLUDED_ +/* @(#$Id: mimeparse.h,v 1.1 2005-01-26 11:45:55 dockes Exp $ (C) 2004 J.F.Dockes */ + +#include +#include + +// Simple (no quoting) mime value parser. Input: "value; pn1=pv1; pn2=pv2 +class MimeHeaderValue { + public: + std::string value; + std::map params; +}; +extern MimeHeaderValue parseMimeHeaderValue(const std::string &in); + + +#endif /* _MIME_H_INCLUDED_ */