2 ********************************************************************************
3 * Copyright (C) 2016 and later: Unicode, Inc. and others.
4 * License & terms of use: http://www.unicode.org/copyright.html#License
5 ********************************************************************************
6 ********************************************************************************
7 * Copyright (C) 2005-2006, International Business Machines
8 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
12 #include "unicode/utypes.h"
13 #include "unicode/ucsdet.h"
18 #define BUFFER_SIZE 8192
20 int main(int argc, char *argv[])
22 static char buffer[BUFFER_SIZE];
26 printf("Usage: %s [filename]...\n", argv[0]);
30 for(arg = 1; arg < argc; arg += 1) {
32 char *filename = argv[arg];
33 int32_t inputLength, match, matchCount = 0;
34 UCharsetDetector* csd;
35 const UCharsetMatch **csm;
36 UErrorCode status = U_ZERO_ERROR;
42 file = fopen(filename, "rb");
45 printf("Cannot open file \"%s\"\n\n", filename);
49 printf("%s:\n", filename);
51 inputLength = (int32_t) fread(buffer, 1, BUFFER_SIZE, file);
55 csd = ucsdet_open(&status);
56 ucsdet_setText(csd, buffer, inputLength, &status);
58 csm = ucsdet_detectAll(csd, &matchCount, &status);
60 for(match = 0; match < matchCount; match += 1) {
61 const char *name = ucsdet_getName(csm[match], &status);
62 const char *lang = ucsdet_getLanguage(csm[match], &status);
63 int32_t confidence = ucsdet_getConfidence(csm[match], &status);
65 if (lang == NULL || strlen(lang) == 0) {
69 printf("%s (%s) %d\n", name, lang, confidence);