Debian Codegraph @

src/tools/uchardet.cpp

raw
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is Mozilla Universal charset detector code.
15 *
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2001
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * BYVoid <[email protected]>
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37 #include "../uchardet.h"
38 #include <getopt.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #ifndef VERSION
44 #define VERSION "Unknown"
45 #endif
46 #define BUFFER_SIZE 65536
47
48 static char buffer[BUFFER_SIZE];
49
50 static void detect(FILE * fp)
51 {
52 uchardet_t handle = uchardet_new();
53
54 while (1)
55 {
56 size_t len = fread(buffer, 1, BUFFER_SIZE, fp);
57 if (len == 0)
58 break;
59 int retval = uchardet_handle_data(handle, buffer, len);
60 if (retval != 0)
61 {
62 fprintf(stderr, "Handle data error.\n");
63 exit(1);
64 }
65 }
66 uchardet_data_end(handle);
67
68 const char * charset = uchardet_get_charset(handle);
69 if (*charset)
70 printf("%s\n", charset);
71 else
72 printf("unknown\n");
73
74 uchardet_delete(handle);
75 }
76
77 static void show_version()
78 {
79 printf("\n");
80 printf("uchardet Command Line Tool\n");
81 printf("Version %s\n", VERSION);
82 printf("\n");
83 printf("Authors: %s\n", "BYVoid, Jehan");
84 printf("Bug Report: %s\n", "https://gitlab.freedesktop.org/uchardet/uchardet/-/issues");
85 printf("\n");
86 }
87
88 static void show_usage()
89 {
90 show_version();
91 printf("Usage:\n");
92 printf(" uchardet [Options] [File]...\n");
93 printf("\n");
94 printf("Options:\n");
95 printf(" -v, --version Print version and build information.\n");
96 printf(" -h, --help Print this help.\n");
97 printf("\n");
98 }
99
100 int main(int argc, char ** argv)
101 {
102 static struct option longopts[] =
103 {
104 { "version", no_argument, NULL, 'v' },
105 { "help", no_argument, NULL, 'h' },
106 { 0, 0, 0, 0 },
107 };
108 bool end_options = false;
109
110 static int oc;
111 while((oc = getopt_long(argc, argv, "vh", longopts, NULL)) != -1)
112 {
113 switch (oc)
114 {
115 case 'v':
116 show_version();
117 return 0;
118 case 'h':
119 show_usage();
120 return 0;
121 case '?':
122 printf("Please use %s --help.\n", argv[0]);
123 return 1;
124 }
125 }
126
127 FILE * f = stdin;
128 int error_seen = 0;
129 if (argc < 2 ||
130 (argc == 2 && strcmp(argv[1], "--") == 0))
131 {
132 // No file arg, use stdin by default
133 detect(f);
134 }
135 for (int i = 1; i < argc; i++)
136 {
137 const char *filename = argv[i];
138
139 if (! end_options && strcmp(filename, "--") == 0)
140 {
141 end_options = true;
142 continue;
143 }
144
145 f = fopen(filename, "r");
146 if (f == NULL)
147 {
148 perror(filename);
149 error_seen = 1;
150 continue;
151 }
152 if (argc > 2)
153 {
154 printf("%s: ", filename);
155 }
156 detect(f);
157 }
158
159 return error_seen;
160 }