Switch to unified view

a/src/internfile/mimehandler.cpp b/src/internfile/mimehandler.cpp
1
#ifndef lint
1
#ifndef lint
2
static char rcsid[] = "@(#$Id: mimehandler.cpp,v 1.4 2005-01-29 15:41:11 dockes Exp $ (C) 2004 J.F.Dockes";
2
static char rcsid[] = "@(#$Id: mimehandler.cpp,v 1.5 2005-02-01 17:20:05 dockes Exp $ (C) 2004 J.F.Dockes";
3
#endif
3
#endif
4
4
5
#include <iostream>
5
#include <iostream>
6
#include <string>
6
#include <string>
7
using namespace std;
7
using namespace std;
...
...
10
#include "readfile.h"
10
#include "readfile.h"
11
#include "csguess.h"
11
#include "csguess.h"
12
#include "transcode.h"
12
#include "transcode.h"
13
#include "debuglog.h"
13
#include "debuglog.h"
14
#include "smallut.h"
14
#include "smallut.h"
15
#include "html.h"
16
#include "execmd.h"
15
17
18
class MimeHandlerText : public MimeHandler {
19
 public:
16
bool textPlainToDoc(RclConfig *conf, const string &fn, 
20
    bool worker(RclConfig *conf, const string &fn, 
17
           const string &mtype, Rcl::Doc &docout)
21
        const string &mtype, Rcl::Doc &docout);
22
    
23
};
24
25
// Process a plain text file
26
bool MimeHandlerText::worker(RclConfig *conf, const string &fn, 
27
               const string &mtype, Rcl::Doc &docout)
18
{
28
{
19
    string otext;
29
    string otext;
20
    if (!file_to_string(fn, otext))
30
    if (!file_to_string(fn, otext))
21
    return false;
31
    return false;
22
    
32
    
...
...
43
    out.text = utf8;
53
    out.text = utf8;
44
    docout = out;
54
    docout = out;
45
    return true;
55
    return true;
46
}
56
}
47
57
48
// Map of mime types to internal interner functions. This could just as well 
58
class MimeHandlerExec : public MimeHandler {
49
// be an if else if suite inside getMimeHandler(), but this is prettier ?
50
static map<string, MimeHandlerFunc> ihandlers;
51
// Static object to get the map to be initialized at program start.
52
class IHandler_Init {
53
 public:
59
 public:
54
    IHandler_Init() {
60
    list<string> params;
55
  ihandlers["text/plain"] = textPlainToDoc;
61
    virtual ~MimeHandlerExec() {}
56
  ihandlers["text/html"] = textHtmlToDoc;
62
    virtual bool worker(RclConfig *conf, const string &fn, 
57
  // Add new associations here when needed
63
          const string &mtype, Rcl::Doc &docout);
64
65
};
66
67
    
68
// Execute an external program to translate a file from its native format
69
// to html. Then call the html parser to do the actual indexing
70
bool MimeHandlerExec::worker(RclConfig *conf, const string &fn, 
71
               const string &mtype, Rcl::Doc &docout)
72
{
73
    string cmd = params.front();
74
    list<string>::iterator it = params.begin();
75
    list<string>myparams(++it, params.end());
76
    myparams.push_back(fn);
77
78
    string html;
79
    ExecCmd exec;
80
    int status = exec.doexec(cmd, myparams, 0, &html);
81
    if (status) {
82
  LOGDEB(("MimeHandlerExec: command status 0x%x: %s\n", 
83
      status, cmd.c_str()));
84
  return false;
58
    }
85
    }
86
    MimeHandlerHtml hh;
87
    return hh.worker1(conf, fn, html, mtype, docout);
59
};
88
}
60
static IHandler_Init ihandleriniter;
61
89
90
static MimeHandler *mhfact(const string &mime)
91
{
92
    if (!stringlowercmp("text/plain", mime))
93
  return new MimeHandlerText;
94
    else if (!stringlowercmp("text/html", mime))
95
  return new MimeHandlerHtml;
96
    return 0;
97
}
62
98
63
/**
99
/**
64
 * Return handler function for given mime type
100
 * Return handler function for given mime type
65
 */
101
 */
66
MimeHandlerFunc getMimeHandler(const std::string &mtype, ConfTree *mhandlers)
102
MimeHandler *getMimeHandler(const std::string &mtype, ConfTree *mhandlers)
67
{
103
{
68
    // Return handler definition for mime type
104
    // Return handler definition for mime type
69
    string hs;
105
    string hs;
70
    if (!mhandlers->get(mtype, hs, "index")) {
106
    if (!mhandlers->get(mtype, hs, "index")) {
71
    LOGDEB(("getMimeHandler: no handler for %s\n", mtype.c_str()));
107
    LOGDEB(("getMimeHandler: no handler for %s\n", mtype.c_str()));
...
...
80
    return 0;
116
    return 0;
81
    }
117
    }
82
118
83
    // Retrieve handler function according to type
119
    // Retrieve handler function according to type
84
    if (!stringlowercmp("internal", toks[0])) {
120
    if (!stringlowercmp("internal", toks[0])) {
85
  map<string, MimeHandlerFunc>::const_iterator it = 
121
  return mhfact(mtype);
86
      ihandlers.find(mtype);
122
    } else if (!stringlowercmp("dll", toks[0])) {
87
  if (it == ihandlers.end()) {
123
  return 0;
88
      LOGERR(("getMimeHandler: internal handler not found for %s\n",
124
    } else if (!stringlowercmp("exec", toks[0])) {
125
  if (toks.size() < 2) {
126
      LOGERR(("getMimeHandler: bad line for %s: %s\n", mtype.c_str(),
89
            mtype.c_str()));
127
            hs.c_str()));
90
        return 0;
128
        return 0;
91
    }
129
    }
92
  return it->second;
130
  MimeHandlerExec *h = new MimeHandlerExec;
93
    } else if (!stringlowercmp("dll", toks[0])) {
131
  vector<string>::const_iterator it1 = toks.begin();
94
  if (toks.size() != 2)
132
  it1++;
95
      return 0;
133
  for (;it1 != toks.end();it1++)
134
      h->params.push_back(*it1);
96
    return 0;
135
    return h;
97
    } else if (!stringlowercmp("exec", toks[0])) {
98
  if (toks.size() != 2)
99
      return 0;
100
  return 0;
101
    } else {
102
  return 0;
103
    }
136
    }
137
    return 0;
104
}
138
}
105
139
106
/**
140
/**
107
 * Return external viewer exec string for given mime type
141
 * Return external viewer exec string for given mime type
108
 */
142
 */