test driver modifs

This commit is contained in:
dockes 2006-11-09 08:59:40 +00:00
parent df7b8711a1
commit cbaa54cde1

View File

@ -1,5 +1,5 @@
#ifndef lint #ifndef lint
static char rcsid[] = "@(#$Id: mimeparse.cpp,v 1.15 2006-11-07 18:28:48 dockes Exp $ (C) 2004 J.F.Dockes"; static char rcsid[] = "@(#$Id: mimeparse.cpp,v 1.16 2006-11-09 08:59:40 dockes Exp $ (C) 2004 J.F.Dockes";
#endif #endif
/* /*
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -741,110 +741,186 @@ time_t rfc2822DateToUxTime(const string& dt)
using namespace std; using namespace std;
extern bool rfc2231_decode(const string& in, string& out, string& charset); extern bool rfc2231_decode(const string& in, string& out, string& charset);
extern time_t rfc2822DateToUxTime(const string& date); extern time_t rfc2822DateToUxTime(const string& date);
static const char *thisprog;
static char usage [] =
"-p: header value and parameter test\n"
"-q: qp decoding\n"
"-b: base64\n"
"-7: rfc2047\n"
"-1: rfc2331\n"
"-t: date time\n"
" \n\n"
;
static void
Usage(void)
{
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
exit(1);
}
static int op_flags;
#define OPT_MOINS 0x1
#define OPT_p 0x2
#define OPT_q 0x4
#define OPT_b 0x8
#define OPT_7 0x10
#define OPT_1 0x20
#define OPT_t 0x40
int int
main(int argc, const char **argv) main(int argc, const char **argv)
{ {
#if 0 int count = 10;
//const char *tr = "text/html; charset=utf-8; otherparam=garb";
const char *tr = "text/html;charset = UTF-8 ; otherparam=garb; \n" thisprog = argv[0];
"QUOTEDPARAM=\"quoted value\""; argc--; argv++;
// const char *tr = "application/x-stuff;" while (argc > 0 && **argv == '-') {
// "title*0*=us-ascii'en'This%20is%20even%20more%20;" (*argv)++;
//"title*1*=%2A%2A%2Afun%2A%2A%2A%20;" if (!(**argv))
//"title*2=\"isn't it!\""; /* Cas du "adb - core" */
Usage();
while (**argv)
switch (*(*argv)++) {
case 'p': op_flags |= OPT_p; break;
case 'q': op_flags |= OPT_q; break;
case 'b': op_flags |= OPT_b; break;
case '1': op_flags |= OPT_1; break;
case '7': op_flags |= OPT_7; break;
case 't': op_flags |= OPT_t; break;
default: Usage(); break;
}
b1: argc--; argv++;
}
MimeHeaderValue parsed; if (argc != 0)
Usage();
if (!parseMimeHeaderValue(tr, parsed)) { if (op_flags & OPT_p) {
fprintf(stderr, "PARSE ERROR\n"); // Mime header value and parameters extraction
} const char *tr[] = {
"text/html;charset = UTF-8 ; otherparam=garb; \n"
"QUOTEDPARAM=\"quoted value\"",
printf("Field value: [%s]\n", parsed.value.c_str()); "application/x-stuff;"
map<string, string>::iterator it; "title*0*=us-ascii'en'This%20is%20even%20more%20;"
for (it = parsed.params.begin();it != parsed.params.end();it++) { "title*1*=%2A%2A%2Afun%2A%2A%2A%20;"
if (it == parsed.params.begin()) "title*2=\"isn't it!\""
printf("Parameters:\n"); };
printf(" [%s] = [%s]\n", it->first.c_str(), it->second.c_str());
}
#elif 0
const char *qp = "=41=68 =e0 boire=\r\n continue 1ere\ndeuxieme\n\r3eme "
"agrave is: '=E0' probable skipped decode error: =\n"
"Actual decode error =xx this wont show";
string out; for (unsigned int i = 0; i < sizeof(tr) / sizeof(char *); i++) {
if (!qp_decode(string(qp), out)) { MimeHeaderValue parsed;
fprintf(stderr, "qp_decode returned error\n"); if (!parseMimeHeaderValue(tr[i], parsed)) {
} fprintf(stderr, "PARSE ERROR for [%s]\n", tr[i]);
printf("Decoded: '%s'\n", out.c_str()); }
#elif 0 printf("Field value: [%s]\n", parsed.value.c_str());
//'C'est à boire qu'il nous faut éviter l'excès.' map<string, string>::iterator it;
//'Deuxième ligne' for (it = parsed.params.begin();it != parsed.params.end();it++) {
//'Troisième ligne' if (it == parsed.params.begin())
//'Et la fin (pas de nl). ' printf("Parameters:\n");
const char *b64 = printf(" [%s] = [%s]\n", it->first.c_str(), it->second.c_str());
}
}
} else if (op_flags & OPT_q) {
// Quoted printable stuff
const char *qp =
"=41=68 =e0 boire=\r\n continue 1ere\ndeuxieme\n\r3eme "
"agrave is: '=E0' probable skipped decode error: =\n"
"Actual decode error =xx this wont show";
string out;
if (!qp_decode(string(qp), out)) {
fprintf(stderr, "qp_decode returned error\n");
}
printf("Decoded: '%s'\n", out.c_str());
} else if (op_flags & OPT_b) {
// Base64
//'C'est à boire qu'il nous faut éviter l'excès.'
//'Deuxième ligne'
//'Troisième ligne'
//'Et la fin (pas de nl). '
const char *b64 =
"Qydlc3Qg4CBib2lyZSBxdSdpbCBub3VzIGZhdXQg6XZpdGVyIGwnZXhj6HMuCkRldXhp6G1l\r\n" "Qydlc3Qg4CBib2lyZSBxdSdpbCBub3VzIGZhdXQg6XZpdGVyIGwnZXhj6HMuCkRldXhp6G1l\r\n"
"IGxpZ25lClRyb2lzaehtZSBsaWduZQpFdCBsYSBmaW4gKHBhcyBkZSBubCkuIA==\r\n"; "IGxpZ25lClRyb2lzaehtZSBsaWduZQpFdCBsYSBmaW4gKHBhcyBkZSBubCkuIA==\r\n";
string out; string out;
if (!base64_decode(string(b64), out)) { if (!base64_decode(string(b64), out)) {
fprintf(stderr, "base64_decode returned error\n"); fprintf(stderr, "base64_decode returned error\n");
exit(1); exit(1);
} }
printf("Decoded: '%s'\n", out.c_str()); printf("Decoded: [%s]\n", out.c_str());
#elif 0
char line [1024];
string out;
bool res;
while (fgets(line, 1023, stdin)) {
int l = strlen(line);
if (l == 0)
continue;
line[l-1] = 0;
fprintf(stderr, "Line: [%s]\n", line);
#if 0 #if 0
res = rfc2047_decode(line, out); string coded, decoded;
#else const char *fname = "/tmp/recoll_decodefail";
string charset; if (!file_to_string(fname, coded)) {
res = rfc2231_decode(line, out, charset); fprintf(stderr, "Cant read %s\n", fname);
exit(1);
}
if (!base64_decode(coded, decoded)) {
fprintf(stderr, "base64_decode returned error\n");
exit(1);
}
printf("Decoded: [%s]\n", decoded.c_str());
#endif #endif
if (res)
fprintf(stderr, "Out: [%s]\n", out.c_str());
else
fprintf(stderr, "Decoding failed\n");
}
#elif 0
string coded, decoded;
const char *fname = "/tmp/recoll_decodefail";
if (!file_to_string(fname, coded)) {
fprintf(stderr, "Cant read %s\n", fname);
exit(1);
}
if (!base64_decode(coded, decoded)) { } else if (op_flags & (OPT_7|OPT_1)) {
fprintf(stderr, "base64_decode returned error\n"); // rfc2047
exit(1); char line [1024];
} string out;
printf("Decoded: [%s]\n", decoded.c_str()); bool res;
#elif 1 while (fgets(line, 1023, stdin)) {
{ int l = strlen(line);
time_t t; if (l == 0)
continue;
line[l-1] = 0;
fprintf(stderr, "Line: [%s]\n", line);
string charset;
if (op_flags & OPT_7) {
res = rfc2047_decode(line, out);
} else {
res = rfc2231_decode(line, out, charset);
}
if (res)
fprintf(stderr, "Out: [%s] cs %s\n", out.c_str(), charset.c_str());
else
fprintf(stderr, "Decoding failed\n");
}
} else if (op_flags & OPT_t) {
time_t t;
const char *dates[] = { const char *dates[] = {
" Wed, 13 Sep 2006 11:40:26 -0700 (PDT)", " Wed, 13 Sep 2006 11:40:26 -0700 (PDT)",
" Mon, 3 Jul 2006 09:51:58 +0200", " Mon, 3 Jul 2006 09:51:58 +0200",
" Wed, 13 Sep 2006 08:19:48 GMT-07:00", " Wed, 13 Sep 2006 08:19:48 GMT-07:00",
" Wed, 13 Sep 2006 11:40:26 -0700 (PDT)", " Wed, 13 Sep 2006 11:40:26 -0700 (PDT)",
" Sat, 23 Dec 89 19:27:12 EST", " Sat, 23 Dec 89 19:27:12 EST",
" 13 Jan 90 08:23:29 GMT"}; " 13 Jan 90 08:23:29 GMT"};
for (unsigned int i = 0; i <sizeof(dates) / sizeof(char *); i++) { for (unsigned int i = 0; i <sizeof(dates) / sizeof(char *); i++) {
t = rfc2822DateToUxTime(dates[i]); t = rfc2822DateToUxTime(dates[i]);
} struct tm *tm = localtime(&t);
} char datebuf[100];
#endif strftime(datebuf, 99, "&nbsp;%Y-%m-%d&nbsp;%H:%M:%S %z", tm);
printf("[%s] -> [%s]\n", dates[i], datebuf);
}
printf("Enter date:\n");
char line [1024];
while (fgets(line, 1023, stdin)) {
int l = strlen(line);
if (l == 0) continue;
line[l-1] = 0;
t = rfc2822DateToUxTime(line);
struct tm *tm = localtime(&t);
char datebuf[100];
strftime(datebuf, 99, "&nbsp;%Y-%m-%d&nbsp;%H:%M:%S %z", tm);
printf("[%s] -> [%s]\n", line, datebuf);
}
}
exit(0);
} }
#endif // TEST_MIMEPARSE #endif // TEST_MIMEPARSE