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,27 +741,78 @@ 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();
MimeHeaderValue parsed; while (**argv)
switch (*(*argv)++) {
if (!parseMimeHeaderValue(tr, parsed)) { case 'p': op_flags |= OPT_p; break;
fprintf(stderr, "PARSE ERROR\n"); 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++;
} }
if (argc != 0)
Usage();
if (op_flags & OPT_p) {
// Mime header value and parameters extraction
const char *tr[] = {
"text/html;charset = UTF-8 ; otherparam=garb; \n"
"QUOTEDPARAM=\"quoted value\"",
"application/x-stuff;"
"title*0*=us-ascii'en'This%20is%20even%20more%20;"
"title*1*=%2A%2A%2Afun%2A%2A%2A%20;"
"title*2=\"isn't it!\""
};
for (unsigned int i = 0; i < sizeof(tr) / sizeof(char *); i++) {
MimeHeaderValue parsed;
if (!parseMimeHeaderValue(tr[i], parsed)) {
fprintf(stderr, "PARSE ERROR for [%s]\n", tr[i]);
}
printf("Field value: [%s]\n", parsed.value.c_str()); printf("Field value: [%s]\n", parsed.value.c_str());
map<string, string>::iterator it; map<string, string>::iterator it;
for (it = parsed.params.begin();it != parsed.params.end();it++) { for (it = parsed.params.begin();it != parsed.params.end();it++) {
@ -769,8 +820,12 @@ main(int argc, const char **argv)
printf("Parameters:\n"); printf("Parameters:\n");
printf(" [%s] = [%s]\n", it->first.c_str(), it->second.c_str()); 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 "
} 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" "agrave is: '=E0' probable skipped decode error: =\n"
"Actual decode error =xx this wont show"; "Actual decode error =xx this wont show";
@ -779,7 +834,8 @@ main(int argc, const char **argv)
fprintf(stderr, "qp_decode returned error\n"); fprintf(stderr, "qp_decode returned error\n");
} }
printf("Decoded: '%s'\n", out.c_str()); printf("Decoded: '%s'\n", out.c_str());
#elif 0 } else if (op_flags & OPT_b) {
// Base64
//'C'est à boire qu'il nous faut éviter l'excès.' //'C'est à boire qu'il nous faut éviter l'excès.'
//'Deuxième ligne' //'Deuxième ligne'
//'Troisième ligne' //'Troisième ligne'
@ -793,29 +849,8 @@ main(int argc, const char **argv)
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);
#else
string charset;
res = rfc2231_decode(line, out, charset);
#endif
if (res)
fprintf(stderr, "Out: [%s]\n", out.c_str());
else
fprintf(stderr, "Decoding failed\n");
}
#elif 0
string coded, decoded; string coded, decoded;
const char *fname = "/tmp/recoll_decodefail"; const char *fname = "/tmp/recoll_decodefail";
if (!file_to_string(fname, coded)) { if (!file_to_string(fname, coded)) {
@ -828,8 +863,31 @@ main(int argc, const char **argv)
exit(1); exit(1);
} }
printf("Decoded: [%s]\n", decoded.c_str()); printf("Decoded: [%s]\n", decoded.c_str());
#elif 1 #endif
{
} else if (op_flags & (OPT_7|OPT_1)) {
// rfc2047
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);
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; time_t t;
const char *dates[] = { const char *dates[] = {
@ -842,9 +900,27 @@ main(int argc, const char **argv)
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];
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);
} }
#endif
}
exit(0);
} }
#endif // TEST_MIMEPARSE #endif // TEST_MIMEPARSE