Skip to content

Commit

Permalink
ptexenc: Add ptenc_get_command_line_args() etc. (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-tk committed May 25, 2018
1 parent 5796f2d commit dea8920
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
134 changes: 134 additions & 0 deletions source/texk/ptexenc/ptexenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -935,4 +935,138 @@ int nkf_close(FILE *fp) {
}
return fclose(fp);
}


unsigned char *ptenc_from_utf8_string_to_internal_enc(const unsigned char *is)
{
int i;
long u = 0, j, len;
int i1 = EOF, i2 = EOF, i3 = EOF, i4 = EOF;
unsigned char *buf, *buf_bak;
long first_bak, last_bak;

if (terminal_enc != ENC_UTF8 || is_internalUPTEX()) return NULL;
buf_bak = buffer;
first_bak = first;
last_bak = last;

len = strlen(is)+1;
buffer = buf = xmalloc(len);
first = last = 0;

for (i=0; i<strlen(is); i++) {
i1 = is[i];
switch (UTF8length(i1)) {
case 1:
buffer[last++] = i1; /* ASCII */
if (i1 == '\0') goto end;
continue;
case 2:
i2 = is[++i]; if (i2 == '\0') break;
u = UTF8BtoUCS(i1, i2);
break;
case 3:
i2 = is[++i]; if (i2 == '\0') break;
i3 = is[++i]; if (i3 == '\0') break;
u = UTF8CtoUCS(i1, i2, i3);
if (u == U_BOM) continue; /* just ignore */
if (u == U_VOICED && combin_voiced_sound(false)) continue;
if (u == U_SEMI_VOICED && combin_voiced_sound(true)) continue;
break;
case 4:
i2 = is[++i]; if (i2 == '\0') break;
i3 = is[++i]; if (i3 == '\0') break;
i4 = is[++i]; if (i4 == '\0') break;
u = UTF8DtoUCS(i1, i2, i3, i4);
break;
default:
u = U_REPLACEMENT_CHARACTER;
break;
}

j = toBUFF(fromUCS(u));
if (j == 0) { /* can't represent in EUC/SJIS */
if (last+4>=len) buffer = xrealloc(buffer, len=last+64);
write_hex(i1);
if (i2 != '\0') write_hex(i2);
if (i3 != '\0') write_hex(i3);
if (i4 != '\0') write_hex(i4);
} else {
write_multibyte(j);
}
i2 = i3 = i4 = '\0';
}
buffer[last] = '\0';
end:
buffer = buf_bak;
first = first_bak;
last = last_bak;
return buf;
}

unsigned char *ptenc_from_internal_enc_string_to_utf8(const unsigned char *is)
{
int i;
long u = 0, len;
int i1 = EOF, i2 = EOF;
unsigned char *buf, *buf_bak;
long first_bak, last_bak;

if (terminal_enc != ENC_UTF8 || is_internalUPTEX()) return NULL;
buf_bak = buffer;
first_bak = first;
last_bak = last;

len = strlen(is)+1;
buffer = buf = xmalloc(len*1.5);
first = last = 0;

for (i=0; i<strlen(is); i++) {
i1 = is[i];
switch (multibytelen(i1)) {
case 1:
buffer[last++] = i1; /* ASCII */
if (i1 == '\0') goto end;
continue;
case 2:
i2 = is[++i]; if (i2 == '\0') break;
u = JIStoUCS2(toJIS(HILO(i1,i2)));
break;
default:
u = U_REPLACEMENT_CHARACTER;
break;
}

write_multibyte(UCStoUTF8(u));
}
buffer[last] = '\0';
end:
buffer = buf_bak;
first = first_bak;
last = last_bak;
return buf;
}

int ptenc_get_command_line_args(int *p_ac, char ***p_av)
{
int i, argc;
char **argv;

get_terminal_enc();
if (terminal_enc == ENC_UTF8 && !is_internalUPTEX()) {
argc = *p_ac;
argv = xmalloc(sizeof(char *)*(argc+1));
for (i=0; i<argc; i++) {
argv[i] = ptenc_from_utf8_string_to_internal_enc((*p_av)[i]);
#ifdef DEBUG
fprintf(stderr, "Commandline arguments %d:(%s)\n", i, argv[i]);
#endif /* DEBUG */
}
argv[argc] = NULL;
*p_av = argv;
return terminal_enc;
}
return 0;
}

#endif /* !WIN32 */
3 changes: 3 additions & 0 deletions source/texk/ptexenc/ptexenc/ptexenc.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ extern PTENCDLL void clear_infile_enc(FILE *fp);
extern PTENCDLL void nkf_disable(void);
extern PTENCDLL FILE *nkf_open(const char *path, const char *mode);
extern PTENCDLL int nkf_close(FILE *fp);
extern PTENCDLL unsigned char *ptenc_from_utf8_string_to_internal_enc(const unsigned char *is);
extern PTENCDLL unsigned char *ptenc_from_internal_enc_string_to_utf8(const unsigned char *is);
extern PTENCDLL int ptenc_get_command_line_args(int *p_ac, char ***p_av);
#endif

#endif /* PTEXENC_PTEXENC_H */

0 comments on commit dea8920

Please sign in to comment.