/* Copyright (C) 2001-2002 Kenichi Suto
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "defs.h"
#include "global.h"
#include <iconv.h>
#include <langinfo.h>
#include <wchar.h>
void hex_dump(gchar *buf){
gchar *p =buf;
while(*p != '\0'){
g_print("%02x ", (unsigned char)*p);
p++;
}
g_print("\n");
}
static gchar *locale2jis(gchar *inbuf){
iconv_t cd;
const char* ocode = "euc-jp";
const char* icode;
// const char* ocode = "iso-2022-jp";
// const char* ocode = "shift_jis";
int r = 0;
size_t isize;
size_t osize;
char *outbuf;
guchar *eucbuf=NULL;
guchar *euc_p;
guchar *jisbuf=NULL;
guchar *jis_p;
gint i;
icode = nl_langinfo(CODESET);
if(strcasecmp(icode, "EUC-JP") == 0){
euc_p = inbuf;
} else {
cd = iconv_open( ocode, icode );
if( (int)cd == -1 )
return(NULL);
isize = strlen(inbuf);
osize = isize * 4;
eucbuf = outbuf = malloc(osize);
r = iconv(cd, &inbuf, &isize, &outbuf, &osize);
iconv_close(cd);
if(r == 1){
perror("iconv");
return(NULL);
}
euc_p = eucbuf;
}
jis_p = jisbuf = malloc(strlen(euc_p)*2);
while(*euc_p != '\0'){
if(isalnum(*euc_p)){
*jis_p = 0x23;
jis_p ++;
*jis_p = *euc_p;
jis_p ++;
}
else if(iseuckanji(euc_p)){
*jis_p = *euc_p - 0x80;
jis_p ++;
euc_p++;
*jis_p = *euc_p - 0x80;
jis_p ++;
}
else {
*jis_p = 0x21;
jis_p ++;
*jis_p = 0x29;
jis_p ++;
}
euc_p ++;
}
*jis_p = '\0';
if(eucbuf != NULL)
free(eucbuf);
for(i=0;;i++){
if(jisbuf[i] == '\0')
break;
}
return(jisbuf);
}
static gchar *do_iconv(const gchar *icode, const gchar *ocode, gchar *inbuf){
iconv_t cd;
int r = 0;
int buflen;
size_t isize;
size_t osize;
char *outbuf;
char *result;
cd = iconv_open( ocode, icode );
if( (int)cd == -1 )
return(NULL);
hex_dump(inbuf);
isize = strlen(inbuf);
osize = buflen = isize * 2;
result = outbuf = malloc(osize);
r = iconv(cd, &inbuf, &isize, &outbuf, &osize);
iconv_close(cd);
result[buflen - osize] = '\0';
g_print("buflen = %d osize = %d\n", buflen, osize);
if(r != 0){
perror("iconv");
result[0] = '\0';
return(result);
}
hex_dump(result);
return(result);
}
gchar *locale2euc(gchar *inbuf){
const char* icode;
const char* ocode = "euc-jp"; // "iso-2022-jp" or "shift_jis";
icode = nl_langinfo(CODESET);
if(strcasecmp(icode, "EUC-JP") == 0){
return(strdup(inbuf));
}
return(do_iconv(icode, ocode, inbuf));
}
gchar *euc2locale(gchar *inbuf){
const char* icode = "euc-jp"; // "iso-2022-jp" or "shift_jis";
const char* ocode;
ocode = nl_langinfo(CODESET);
if(strcasecmp(ocode, "EUC-JP") == 0){
return(strdup(inbuf));
}
return(do_iconv(icode, ocode, inbuf));
}
inline gboolean isjisp(gchar *buff){
g_assert(buff != NULL);
if((buff[0] >= 0x21) && (buff[0] <= 0x74) &&
(buff[1] >= 0x21) && (buff[1] <= 0x7E))
return(TRUE);
return(FALSE);
}
syntax highlighted by Code2HTML, v. 0.9.1