Switch to unified view

a b/src/utils/mimeparse.cpp
1
#ifndef lint
2
static char rcsid[] = "@(#$Id: mimeparse.cpp,v 1.1 2005-01-26 11:45:55 dockes Exp $ (C) 2004 J.F.Dockes";
3
#endif
4
5
#ifndef TEST_MIMEPARSE
6
7
#include <string>
8
#include <ctype.h>
9
#include <stdio.h>
10
11
#include "mimeparse.h"
12
13
using namespace std;
14
#define WHITE " \t\n"
15
16
static void stripw_lc(string &in)
17
{
18
    // fprintf(stderr, "In: '%s'\n", in.c_str());
19
    string::size_type pos, pos1;
20
    pos = in.find_first_not_of(WHITE);
21
    if (pos == string::npos) {
22
  // All white
23
  in = "";
24
  return;
25
    }
26
    in.replace(0, pos, "");
27
    pos1 = in.find_last_not_of(WHITE); 
28
    if (pos1 != in.length() -1)
29
  in  = in.replace(pos1+1, string::npos, "");
30
    string::iterator i;
31
    for (i = in.begin(); i != in.end(); i++)
32
  *i = tolower(*i);
33
}
34
35
MimeHeaderValue parseMimeHeaderValue(const string &ein)
36
{
37
    string in = ein;
38
    MimeHeaderValue out;
39
    string::size_type pos, pos1;
40
41
    pos = in.find_first_not_of(WHITE);
42
    if (pos == string::npos)
43
  return out;
44
    in = in.substr(pos, string::npos);
45
    if ((pos = in.find_first_of(";")) == string::npos) {
46
  out.value = in;
47
  return out;
48
    } 
49
    out.value = in.substr(0, pos);
50
    stripw_lc(out.value);
51
    in = in.substr(pos+1, string::npos);
52
    for (;;) {
53
  // Skip whitespace
54
  if ((pos = in.find_first_not_of(WHITE)) == string::npos)
55
      return out;
56
  in = in.substr(pos, string::npos);
57
58
  if ((pos = in.find_first_of("=")) == string::npos)
59
      return out;
60
  string pname = in.substr(0, pos);
61
  stripw_lc(pname);
62
  in = in.substr(pos+1, string::npos);
63
64
  pos = in.find_first_of(";");
65
  string pvalue = in.substr(0, pos);
66
  stripw_lc(pvalue);
67
  out.params[pname] = pvalue;
68
  if (pos == string::npos)
69
      return out;
70
  in = in.substr(pos+1, string::npos);
71
    }
72
73
    return out;
74
75
}
76
77
#else 
78
79
#include <string>
80
#include "mimeparse.h"
81
using namespace std;
82
int
83
main(int argc, const char **argv)
84
{
85
86
    MimeHeaderValue parsed;
87
88
    //    const char *tr = "text/html; charset=utf-8; otherparam=garb";
89
    const char *tr = "text/html;charset = UTF-8 ; otherparam=garb;";
90
91
    parsed = parseMimeHeaderValue(tr);
92
    
93
    printf("'%s' \n", parsed.value.c_str());
94
    map<string, string>::iterator it;
95
    for (it = parsed.params.begin();it != parsed.params.end();it++) {
96
  printf("  '%s' = '%s'\n", it->first.c_str(), it->second.c_str());
97
    }
98
}
99
100
#endif // TEST_MIMEPARSE