diff options
author | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2012-09-04 11:42:17 +0300 |
---|---|---|
committer | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2012-09-04 11:42:17 +0300 |
commit | 1ac911b4d59d629cc9e2c73047ca329508377f88 (patch) | |
tree | 23fcff723dd18ff7b84f5a1bf38a15b4dd5a0fcd | |
parent | 3ab3d03b8b603f210d878d352b56442eec14f6af (diff) |
added iconv test code
this code can convert file from one supported by iconv charset to other
-rw-r--r-- | iconv_test.project | 98 | ||||
-rw-r--r-- | iconv_test.workspace | 12 | ||||
-rw-r--r-- | main.cpp | 79 |
3 files changed, 189 insertions, 0 deletions
diff --git a/iconv_test.project b/iconv_test.project new file mode 100644 index 0000000..dc252dd --- /dev/null +++ b/iconv_test.project @@ -0,0 +1,98 @@ +<?xml version="1.0" encoding="utf-8"?> +<CodeLite_Project Name="iconv_test" InternalType="Console"> + <Plugins> + <Plugin Name="qmake"> + <![CDATA[00010001N0005Debug000000000000]]> + </Plugin> + </Plugins> + <Description/> + <Dependencies/> + <VirtualDirectory Name="src"> + <File Name="main.cpp"/> + </VirtualDirectory> + <Settings Type="Executable"> + <GlobalSettings> + <Compiler Options="" C_Options=""> + <IncludePath Value="."/> + </Compiler> + <Linker Options=""> + <LibraryPath Value="."/> + </Linker> + <ResourceCompiler Options=""/> + </GlobalSettings> + <Configuration Name="Debug" CompilerType="gnu g++" DebuggerType="GNU gdb debugger" Type="Executable" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append"> + <Compiler Options="-g;-O0;-Wall" C_Options="-g;-O0;-Wall" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" UseDifferentPCHFlags="no" PCHFlags=""> + <IncludePath Value="."/> + </Compiler> + <Linker Options="" Required="yes"/> + <ResourceCompiler Options="" Required="no"/> + <General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="./Debug" Command="./$(ProjectName)" CommandArguments="-i test -o converted_test -f UTF-8 -t WINDOWS-1251" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes"/> + <Environment EnvVarSetName="<Use Defaults>" DbgSetName="<Use Defaults>"> + <![CDATA[]]> + </Environment> + <Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath=""> + <PostConnectCommands/> + <StartupCommands/> + </Debugger> + <PreBuild/> + <PostBuild/> + <CustomBuild Enabled="no"> + <RebuildCommand/> + <CleanCommand/> + <BuildCommand/> + <PreprocessFileCommand/> + <SingleFileCommand/> + <MakefileGenerationCommand/> + <ThirdPartyToolName>None</ThirdPartyToolName> + <WorkingDirectory/> + </CustomBuild> + <AdditionalRules> + <CustomPostBuild/> + <CustomPreBuild/> + </AdditionalRules> + <Completion EnableCpp11="no"> + <ClangCmpFlagsC/> + <ClangCmpFlags/> + <ClangPP/> + <SearchPaths/> + </Completion> + </Configuration> + <Configuration Name="Release" CompilerType="gnu g++" DebuggerType="GNU gdb debugger" Type="" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append"> + <Compiler Options="-O2;-Wall" C_Options="-O2;-Wall" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" UseDifferentPCHFlags="no" PCHFlags=""> + <IncludePath Value="."/> + </Compiler> + <Linker Options="" Required="yes"/> + <ResourceCompiler Options="" Required="no"/> + <General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="./Release" Command="./$(ProjectName)" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes"/> + <Environment EnvVarSetName="<Use Defaults>" DbgSetName="<Use Defaults>"> + <![CDATA[]]> + </Environment> + <Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath=""> + <PostConnectCommands/> + <StartupCommands/> + </Debugger> + <PreBuild/> + <PostBuild/> + <CustomBuild Enabled="no"> + <RebuildCommand/> + <CleanCommand/> + <BuildCommand/> + <PreprocessFileCommand/> + <SingleFileCommand/> + <MakefileGenerationCommand/> + <ThirdPartyToolName>None</ThirdPartyToolName> + <WorkingDirectory/> + </CustomBuild> + <AdditionalRules> + <CustomPostBuild/> + <CustomPreBuild/> + </AdditionalRules> + <Completion EnableCpp11="no"> + <ClangCmpFlagsC/> + <ClangCmpFlags/> + <ClangPP/> + <SearchPaths/> + </Completion> + </Configuration> + </Settings> +</CodeLite_Project> diff --git a/iconv_test.workspace b/iconv_test.workspace new file mode 100644 index 0000000..9f99f72 --- /dev/null +++ b/iconv_test.workspace @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="utf-8"?> +<CodeLite_Workspace Name="iconv_test" Database="./iconv_test.tags"> + <Project Name="iconv_test" Path="iconv_test.project" Active="Yes"/> + <BuildMatrix> + <WorkspaceConfiguration Name="Debug" Selected="yes"> + <Project Name="iconv_test" ConfigName="Debug"/> + </WorkspaceConfiguration> + <WorkspaceConfiguration Name="Release" Selected="yes"> + <Project Name="iconv_test" ConfigName="Release"/> + </WorkspaceConfiguration> + </BuildMatrix> +</CodeLite_Workspace> diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..56d8523 --- /dev/null +++ b/main.cpp @@ -0,0 +1,79 @@ +#include <unistd.h> +#include <stdio.h> +#include <iconv.h> +#include <string.h> +#include <errno.h> +#include <stdlib.h> + + +int main(int argc, char **argv) +{ + int opt = -1; + char ifile[256], ofile[256], fcharset[32], tcharset[32]; + ifile[0] = 0; + ofile[0] = 0; + fcharset[0] = 0; + tcharset[0] = 0; + while((opt = getopt(argc, argv, "i:o:f:t:")) != -1) + { + switch(opt) + { + case 'i': + strcpy(ifile, optarg); + break; + case 'o': + strcpy(ofile, optarg); + break; + case 'f': + strcpy(fcharset, optarg); + break; + case 't': + strcpy(tcharset, optarg); + break; + default: + printf("usage: %s -ioft\n\t-i input file\n\t-o output file\n\t-f from charset\n\t-t to charset\n", argv[0]); + } + } + iconv_t id = iconv_open(tcharset, fcharset); + if((long)id == -1) + { + printf("failed to create iconv descriptor with error: %s\n", strerror(errno)); + return 1; + } + FILE *in = fopen(ifile, "r"); + if(!in) + { + printf("failed to open input file with error: %s\n", strerror(errno)); + return 1; + } + FILE *out = fopen(ofile, "w"); + if(!out) + { + printf("failed to open input file with error: %s\n", strerror(errno)); + return 1; + } + while(!feof(in)) + { + char *buf = (char*)malloc(4096), *outbuf = (char*)malloc(4096), *op, *ip; + op = outbuf; + ip = buf; + if(!fgets(buf, 4096, in)) + break; + size_t len = strlen(buf), outlen = 4096; + int enc_len = iconv(id, &ip, &len, &op, &outlen); + if(enc_len == -1) + { + printf("failed to convert buffer with error: %s\n", strerror(errno)); + return 1; + } + enc_len = 4096 - outlen; + if((int)fwrite(outbuf, 1, enc_len, out) != enc_len) + { + printf("failed to write data to file with error: %s\n", strerror(errno)); + return 1; + } + free(buf); + free(outbuf); + } + return 0; +} |