grub2: bring back build of aros-side grub2 tools
[AROS.git] / workbench / libs / codesets / developer / examples / Convert.c
blob2175b8b6c35714169c89898622c0f3e56a150024
1 /***************************************************************************
3 codesets.library - Amiga shared library for handling different codesets
4 Copyright (C) 2001-2005 by Alfonso [alfie] Ranieri <alforan@tin.it>.
5 Copyright (C) 2005-2014 codesets.library Open Source Team
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 codesets.library project: http://sourceforge.net/projects/codesetslib/
19 Most of the code included in this file was relicensed from GPL to LGPL
20 from the source code of SimpleMail (http://www.sf.net/projects/simplemail)
21 with full permissions by its authors.
23 $Id$
25 ***************************************************************************/
27 #include <exec/libraries.h>
28 #include <libraries/codesets.h>
29 #include <proto/codesets.h>
30 #include <proto/exec.h>
32 #include <stdio.h>
34 /* This is just a very quickly written test, not a full-featured convertor */
35 #define BUF_SIZE 102400
37 struct Library *CodesetsBase = NULL;
38 #if defined(__amigaos4__)
39 struct CodesetsIFace *ICodesets = NULL;
40 #endif
42 #if defined(__amigaos4__)
43 #define GETINTERFACE(iface, base) (iface = (APTR)GetInterface((struct Library *)(base), "main", 1L, NULL))
44 #define DROPINTERFACE(iface) (DropInterface((struct Interface *)iface), iface = NULL)
45 #else
46 #define GETINTERFACE(iface, base) TRUE
47 #define DROPINTERFACE(iface)
48 #endif
50 struct codeset *srcCodeset;
51 struct codeset *destCodeset;
53 int main(int argc, char **argv)
55 char *buf, *destbuf;
56 ULONG destlen;
57 FILE *f;
59 if (argc < 4)
61 fprintf(stderr, "Usage: %s <source codeset> <destination codeset> <source file>\n", argv[0]);
62 return 0;
64 if((CodesetsBase = OpenLibrary(CODESETSNAME,CODESETSVER)) &&
65 GETINTERFACE(ICodesets, CodesetsBase))
67 srcCodeset = CodesetsFind(argv[1], CSA_FallbackToDefault, FALSE, TAG_DONE);
68 if (srcCodeset)
70 destCodeset = CodesetsFind(argv[2], CSA_FallbackToDefault, FALSE, TAG_DONE);
71 if (destCodeset)
73 buf = AllocMem(BUF_SIZE, MEMF_CLEAR);
75 if (buf)
77 f = fopen(argv[3], "r");
78 if (f)
80 fread(buf, BUF_SIZE-1, 1, f);
81 fclose(f);
82 destbuf = CodesetsConvertStr(CSA_SourceCodeset, (IPTR)srcCodeset,
83 CSA_DestCodeset, (IPTR)destCodeset,
84 CSA_Source, (IPTR)buf,
85 CSA_DestLenPtr, (IPTR)&destlen,
86 TAG_DONE);
87 if (destbuf)
89 fprintf(stderr, "Result length: %u\n", (unsigned int)destlen);
90 fwrite(destbuf, destlen, 1, stdout);
91 fputc('\n', stderr);
92 CodesetsFreeA(destbuf, NULL);
94 else
95 fprintf(stderr, "Failed to convert text!\n");
97 FreeMem(buf, BUF_SIZE);
99 else
100 fprintf(stderr, "Failed to allocate %d bytes for buffer\n", BUF_SIZE);
102 else
103 fprintf(stderr, "Unknown destination codeset %s\n", argv[2]);
105 else
106 fprintf(stderr, "Unknown source codeset %s\n", argv[1]);
108 DROPINTERFACE(ICodesets);
109 CloseLibrary(CodesetsBase);
111 else
112 fprintf(stderr, "Failed to open codesets.library!\n");
114 return 0;