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.
25 ***************************************************************************/
27 #include <exec/libraries.h>
28 #include <libraries/codesets.h>
29 #include <proto/codesets.h>
30 #include <proto/exec.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
;
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)
46 #define GETINTERFACE(iface, base) TRUE
47 #define DROPINTERFACE(iface)
50 struct codeset
*srcCodeset
;
51 struct codeset
*destCodeset
;
53 int main(int argc
, char **argv
)
61 fprintf(stderr
, "Usage: %s <source codeset> <destination codeset> <source file>\n", argv
[0]);
64 if((CodesetsBase
= OpenLibrary(CODESETSNAME
,CODESETSVER
)) &&
65 GETINTERFACE(ICodesets
, CodesetsBase
))
67 srcCodeset
= CodesetsFind(argv
[1], CSA_FallbackToDefault
, FALSE
, TAG_DONE
);
70 destCodeset
= CodesetsFind(argv
[2], CSA_FallbackToDefault
, FALSE
, TAG_DONE
);
73 buf
= AllocMem(BUF_SIZE
, MEMF_CLEAR
);
77 f
= fopen(argv
[3], "r");
80 fread(buf
, BUF_SIZE
-1, 1, f
);
82 destbuf
= CodesetsConvertStr(CSA_SourceCodeset
, (IPTR
)srcCodeset
,
83 CSA_DestCodeset
, (IPTR
)destCodeset
,
84 CSA_Source
, (IPTR
)buf
,
85 CSA_DestLenPtr
, (IPTR
)&destlen
,
89 fprintf(stderr
, "Result length: %u\n", (unsigned int)destlen
);
90 fwrite(destbuf
, destlen
, 1, stdout
);
92 CodesetsFreeA(destbuf
, NULL
);
95 fprintf(stderr
, "Failed to convert text!\n");
97 FreeMem(buf
, BUF_SIZE
);
100 fprintf(stderr
, "Failed to allocate %d bytes for buffer\n", BUF_SIZE
);
103 fprintf(stderr
, "Unknown destination codeset %s\n", argv
[2]);
106 fprintf(stderr
, "Unknown source codeset %s\n", argv
[1]);
108 DROPINTERFACE(ICodesets
);
109 CloseLibrary(CodesetsBase
);
112 fprintf(stderr
, "Failed to open codesets.library!\n");