Changeset View
Changeset View
Standalone View
Standalone View
contrib/less/cvt.c
Show All 14 Lines | |||||
#include "charset.h" | #include "charset.h" | ||||
extern int utf_mode; | extern int utf_mode; | ||||
/* | /* | ||||
* Get the length of a buffer needed to convert a string. | * Get the length of a buffer needed to convert a string. | ||||
*/ | */ | ||||
public int | public int | ||||
cvt_length(len, ops) | cvt_length(int len, int ops) | ||||
int len; | |||||
int ops; | |||||
{ | { | ||||
if (utf_mode) | if (utf_mode) | ||||
/* | /* | ||||
* Just copying a string in UTF-8 mode can cause it to grow | * Just copying a string in UTF-8 mode can cause it to grow | ||||
* in length. | * in length. | ||||
* Four output bytes for one input byte is the worst case. | * Four output bytes for one input byte is the worst case. | ||||
*/ | */ | ||||
len *= 4; | len *= 4; | ||||
return (len + 1); | return (len + 1); | ||||
} | } | ||||
/* | /* | ||||
* Allocate a chpos array for use by cvt_text. | * Allocate a chpos array for use by cvt_text. | ||||
*/ | */ | ||||
public int * | public int * | ||||
cvt_alloc_chpos(len) | cvt_alloc_chpos(int len) | ||||
int len; | |||||
{ | { | ||||
int i; | int i; | ||||
int *chpos = (int *) ecalloc(sizeof(int), len); | int *chpos = (int *) ecalloc(sizeof(int), len); | ||||
/* Initialize all entries to an invalid position. */ | /* Initialize all entries to an invalid position. */ | ||||
for (i = 0; i < len; i++) | for (i = 0; i < len; i++) | ||||
chpos[i] = -1; | chpos[i] = -1; | ||||
return (chpos); | return (chpos); | ||||
} | } | ||||
/* | /* | ||||
* Convert text. Perform the transformations specified by ops. | * Convert text. Perform the transformations specified by ops. | ||||
* Returns converted text in odst. The original offset of each | * Returns converted text in odst. The original offset of each | ||||
* odst character (when it was in osrc) is returned in the chpos array. | * odst character (when it was in osrc) is returned in the chpos array. | ||||
*/ | */ | ||||
public void | public void | ||||
cvt_text(odst, osrc, chpos, lenp, ops) | cvt_text(char *odst, char *osrc, int *chpos, int *lenp, int ops) | ||||
char *odst; | |||||
char *osrc; | |||||
int *chpos; | |||||
int *lenp; | |||||
int ops; | |||||
{ | { | ||||
char *dst; | char *dst; | ||||
char *edst = odst; | char *edst = odst; | ||||
char *src; | char *src; | ||||
register char *src_end; | char *src_end; | ||||
LWCHAR ch; | LWCHAR ch; | ||||
if (lenp != NULL) | if (lenp != NULL) | ||||
src_end = osrc + *lenp; | src_end = osrc + *lenp; | ||||
else | else | ||||
src_end = osrc + strlen(osrc); | src_end = osrc + strlen(osrc); | ||||
for (src = osrc, dst = odst; src < src_end; ) | for (src = osrc, dst = odst; src < src_end; ) | ||||
{ | { | ||||
int src_pos = (int) (src - osrc); | int src_pos = (int) (src - osrc); | ||||
int dst_pos = (int) (dst - odst); | int dst_pos = (int) (dst - odst); | ||||
ch = step_char(&src, +1, src_end); | ch = step_char((constant char **)&src, +1, src_end); | ||||
if ((ops & CVT_BS) && ch == '\b' && dst > odst) | if ((ops & CVT_BS) && ch == '\b' && dst > odst) | ||||
{ | { | ||||
/* Delete backspace and preceding char. */ | /* Delete backspace and preceding char. */ | ||||
do { | do { | ||||
dst--; | dst--; | ||||
} while (dst > odst && | } while (dst > odst && | ||||
!IS_ASCII_OCTET(*dst) && !IS_UTF8_LEAD(*dst)); | !IS_ASCII_OCTET(*dst) && !IS_UTF8_LEAD(*dst)); | ||||
} else if ((ops & CVT_ANSI) && IS_CSI_START(ch)) | } else if ((ops & CVT_ANSI) && IS_CSI_START(ch)) | ||||
Show All 26 Lines |