File: teal.cpp

package info (click to toggle)
turqstat 2.2.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,768 kB
  • ctags: 1,148
  • sloc: cpp: 16,876; perl: 250; makefile: 193; sh: 8
file content (157 lines) | stat: -rw-r--r-- 4,255 bytes parent folder | download | duplicates (2)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Teal - Text Encoder ALgorithm :-)
//
// A small utility that converts text files from one character set to another
// using Turquoise SuperStat conversion routines
// Version 1.0
//
// Copyright (c) 2001 Peter Karlsson
//
// $Id: teal.cpp,v 1.3 2001/06/10 21:06:46 peter Exp $
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#include <config.h>
#include <iostream>
#include <stdio.h>
#ifdef HAVE_GETOPT_IN_UNISTD
# include <unistd.h>
#elif defined(HAVE_GETOPT_IN_GETOPT)
# include <getopt.h>
#else
# include "utility.h"
#endif

#include "convert.h"

int main(int argc, char *argv[])
{
    const char *input = "-", *output = "-",
#if defined(__EMX__) || defined(__CYGWIN__) || defined (__MINGW32__)
               *inset = "iso-8859-1", *outset = "ibm850";
#else
               *inset = "ibm850", *outset = "iso-8859-1";
#endif
    int verbose = 1;
    bool listsets = false;

    // Handle arguments
    int c;
    while (EOF != (c = getopt(argc, argv, "i:o:f:t:ql")))
    {
        switch (c)
        {
            case 'i': input  = optarg; break;
            case 'o': output = optarg; break;
            case 'q': verbose --;      break;
            case 'f': inset  = optarg; break;
            case 't': outset = optarg; break;
            case 'l': listsets = true; break;
            default:
                cout << "Usage: " << argv[0] <<
                        " [-q] [-f from-charset] [-t to-charset]"
                        " [-i infile] [-o outfile]"
                     << endl;
                cout << "       " << argv[0] << " -l" << endl;
                return 0;
        }
    }

    if (listsets)
    {
        cout << "Known Fidonet character set names: " << endl;
        CharsetEnumerator fidonames(CharsetEnumerator::Fidonet);
        const char *name_p;
        while ((name_p = fidonames.Next())) cout << name_p << ' ';
        cout << endl;
        cout << endl;

        cout << "Known MIME character set names: " << endl;
        CharsetEnumerator mimenames(CharsetEnumerator::Usenet);
        while ((name_p = mimenames.Next())) cout << name_p << ' ';
        cout << endl;
        cout << endl;

        return 0;
    }

    if (argc > optind)
    {
        input = argv[optind];
        if (argc > optind + 1)
        {
            output = argv[optind + 1];
        }
    }

    // Create decoder and encoder objects
    Decoder *decoder_p = Decoder::GetDecoderByName(inset);
    Encoder *encoder_p = Encoder::GetEncoderByName(outset);

    // Open files (or connect to stdin/stdout)
    FILE *in, *out;
    if (strcmp(input, "-"))
    {
        in = fopen(input, "r");
        if (!in)
        {
            perror("Cannot open input");
            return 1;
        }
        if (verbose > 0)
        {
            fprintf(stderr, "Converting \"%s\"(%s)", input, inset);
        }
    }
    else
    {
        in = stdin;
        if (verbose > 0)
        {
            fprintf(stderr, "Converting standard input(%s)", inset);
        }
    }
    if (strcmp(output, "-"))
    {
        out = fopen(output, "w");
        if (!out)
        {
            perror("Cannot open output");
            return 1;
        }
        if (verbose > 0)
        {
            fprintf(stderr, " to \"%s\"(%s)\n", output, outset);
        }
    }
    else
    {
        out = stdout;
        if (verbose > 0)
        {
            fprintf(stderr, " to standard output(%s)", outset);
        }
    }

    // Convert
    char buf[1024];
    int len;
    while (0 != (len = fread(buf, 1, 1024, in)))
    {
        fputs(encoder_p->Encode(decoder_p->Decode(string(buf, len))).c_str(),
              out);
    }

    fclose(in);
    fclose(out);
}