Parent: [c14b34] (diff)

Child: [d392d3] (diff)

Download this file

mimeparse.cpp    101 lines (82 with data), 2.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
#ifndef lint
static char rcsid[] = "@(#$Id: mimeparse.cpp,v 1.2 2005-03-17 14:02:06 dockes Exp $ (C) 2004 J.F.Dockes";
#endif
#ifndef TEST_MIMEPARSE
#include <string>
#include <ctype.h>
#include <stdio.h>
#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;
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 <string>
#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<string, string>::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