# S-Code User Level Functions

```int s_wd(int s);
int s_punct(int s);
int s_digit(int s);
int s_space(int s);
```

s_digit 檢查是否為數目字符號 --- 0 代表不是, 1 代表是半形的阿拉伯數目字, 2 代表是全形的阿拉伯數目字. s_space 檢查是否為空白符號 --- 0 代表不是, 1 代表是 ASCII 碼被 中的 isspace 定義成空白者, 應該包括 space, formfeed, newline, carriage return, tab, vertical tab; 2 代表是全形的空格. 注意, 在 CNS 碼中, 有兩個碼看起來都是空格. 一個是 2121, 這個才是空格; 另一個是 256D, 這個應該是注音符號的一聲聲調符號.

```int s_len(int *ss);
int s_width(int *ss);
```

```int char2s(unsigned char *cs, int *ss, int catg);
```

```int s_getline(FILE *fp, int *ss, int lim, int catg)
```

```int s_putstr(int *ss, FILE *fp, int catg);
```

```#include "s_code.h"
#include <ctype.h>
#define iny(n) (((BL0 <= n) && (n <= BL1)) || ((BH0 <= n) && (n <= BH1)))

int s_wd(int s) {
return ((s&SM) == 0) ? 1 : 2;
}

int s_punct(int s) {
int c=0, t;
if ((s&SM) == 0)
if (ispunct(s) != 0)
c = 1;
else if (s_gets(s) == SF) {
t = (s&TM);
if ((0x01000000 < t) && (t < 0x10000000)) {
switch(t) {
case PUNRF: c=3; break;
case PUNRL: c=4; break;
case PUNCF: c=5; break;
case PUNCL: c=6; break;
default:    c=2; break;
}
}
}
return c;
}

int s_digit(int s) {
int c=0;
if ( ((s&SM) == 0) && (isdigit(s) != 0) )
c = 1;
else if ( (s_gets(s) == SF) && ((s&TM) == NUMARB) )
c = 2;
return c;
}

int s_space(int s) {
int c=0;
if ( ((s&SM) == 0) && (isspace(s) != 0) )
c = 1;
else if ( (s_gets(s) == SF) && ((s&TM) == WHITE) )
c = 2;
return c;
}

int s_len(int *s) {
int *t;
t=s;
while (*t++ != 0);
return t-s-1;
}

int s_width(int *s) {
int *t, x, n=0;
t=s;
while ((x = *t++) != 0)
n += s_wd(x);
return n;
}

int char2s(char *cs, int *ss, int catg) {
void *v;
unsigned char *c, *s;
int *t, x;

v = cs;
c = (unsigned char*) v;
s = c;
t = ss;
switch(catg) {
case BIG5:
while ((x = *c++) != '\0') {
if ( ((BX0 <= x) && (x <= BX1)) && iny(*c) )
*t++ = big5_s(x, *c++);
else
*t++ = x;
}
*t = 0;
break;
default:
fprintf(stderr, "ERROR. char2s: unknown category %d.\n", catg);
exit(5);
}
return (c-s)-1;
}

int s_getline(FILE *fp, int *ss, int lim, int catg) {
int *s, len=lim, c, n=0;
s = ss;
while (--lim>1 && (c=s_getc(fp, catg)) != EOF && c != '\n')
*s++ = c;
if (c == '\n')
*s++ = c;
else if (lim == 1) {
*s++ = '\n';
fprintf(stderr, "WARNING. s_getline: Line too long, splitted.\n");
}
*s = 0;
return s - ss;
}

int s_putstr(int *ss, FILE *fp, int catg) {
int *s, x, n;
s = ss;
while ((x=*s++) != 0)
if (s_putc(x, fp, catg) == EOF)
return EOF;
else
n += s_wd(x);
return n;
}

```

Created: Jan 14, 1996
Last Revised: Jan 14, 1996