/* * encode a null-terminated string of wide characters into the buffer */ size_t utf8_ptr::setwcharsz(const wchar_t *src, size_t bufsiz) { size_t outbytes; /* loop through the source and store the characters */ for (outbytes = 0 ; *src != 0 ; ++src) { size_t curbytes; /* figure out how many bytes we need for this character */ curbytes = s_wchar_size(*src); /* add it to the total output size */ outbytes += curbytes; /* if we have room, add it to the buffer */ if (bufsiz >= curbytes) { /* store it */ setch(*src); /* deduct this space from the remaining buffer size */ bufsiz -= outbytes; } else { /* * there's no room for this - make sure we don't store * anything more (since we might have room for a shorter * character later, but that would put a gap in the output * string - better to just truncate here) */ bufsiz = 0; } } /* * Add the null terminator, if there's room, but do not increment * our pointer - we want to leave our pointer pointing at the null * terminator. Include the null terminator's size (one byte) in the * result length (even if we don't have room to store it). */ ++outbytes; if (bufsiz > 0) *p_ = '\0'; /* return the total output size used (or needed) */ return outbytes; }
/* * encode a string of wide characters into the buffer */ size_t utf8_ptr::setwchars(const wchar_t *src, size_t src_count, size_t bufsiz) { size_t outbytes; /* loop through the source and store the characters */ for (outbytes = 0 ; src_count > 0 ; --src_count, ++src) { size_t curbytes; /* figure out how many bytes we need for this character */ curbytes = s_wchar_size(*src); /* add it to the total output size */ outbytes += curbytes; /* if we have room, add it to the buffer */ if (bufsiz >= curbytes) { /* store it */ setch(*src); /* deduct this space from the remaining buffer size */ bufsiz -= outbytes; } else { /* * there's no room for this - make sure we don't store * anything more (since we might have room for a shorter * character later, but that would put a gap in the output * string - better to just truncate here) */ bufsiz = 0; } } /* return the total output size used (or needed) */ return outbytes; }
Tchar getch(void) { int k; Tchar i, j; g0: if (ch) { i = ch; if (cbits(i) == '\n') nlflg++; ch = 0; return(i); } if (nlflg) return('\n'); i = getch0(); if (ismot(i)) return(i); k = cbits(i); if (k >= sizeof(gchtab)/sizeof(gchtab[0]) || gchtab[k] == 0) /* nothing special */ return(i); if (k != ESC) { if (k == '\n') { nlflg++; if (ip == 0) numtabp[CD].val++; /* line number */ return(k); } if (k == FLSS) { copyf++; raw++; i = getch0(); if (!fi) flss = i; copyf--; raw--; goto g0; } if (k == RPT) { setrpt(); goto g0; } if (!copyf) { if (k == 'f' && lg && !lgf) { i = getlg(i); return(i); } if (k == fc || k == tabch || k == ldrch) { if ((i = setfield(k)) == 0) goto g0; else return(i); } if (k == '\b') { i = makem(-width(' ' | chbits)); return(i); } } return(i); } k = cbits(j = getch0()); if (ismot(j)) return(j); switch (k) { case 'n': /* number register */ setn(); goto g0; case '$': /* argument indicator */ seta(); goto g0; case '*': /* string indicator */ setstr(); goto g0; case '{': /* LEFT */ i = LEFT; goto gx; case '}': /* RIGHT */ i = RIGHT; goto gx; case '"': /* comment */ while (cbits(i = getch0()) != '\n') ; if (ip == 0) numtabp[CD].val++; /* line number */ nlflg++; return(i); /* experiment: put it here instead of copy mode */ case '(': /* special char name \(xx */ case 'C': /* \C'...' */ if ((i = setch(k)) == 0) goto g0; goto gx; case ESC: /* double backslash */ i = eschar; goto gx; case 'e': /* printable version of current eschar */ i = PRESC; goto gx; case '\n': /* concealed newline */ numtabp[CD].val++; goto g0; case ' ': /* unpaddable space */ i = UNPAD; goto gx; case '\'': /* \(aa */ i = ACUTE; goto gx; case '`': /* \(ga */ i = GRAVE; goto gx; case '_': /* \(ul */ i = UNDERLINE; goto gx; case '-': /* current font minus */ i = MINUS; goto gx; case '&': /* filler */ i = FILLER; goto gx; case 'c': /* to be continued */ i = CONT; goto gx; case '!': /* transparent indicator */ i = XPAR; goto gx; case 't': /* tab */ i = '\t'; return(i); case 'a': /* leader (SOH) */ /* old: *pbp++ = LEADER; goto g0; */ i = LEADER; return i; case '%': /* ohc */ i = OHC; return(i); case 'g': /* return format of a number register */ setaf(); /* should this really be in copy mode??? */ goto g0; case '.': /* . */ i = '.'; gx: setsfbits(i, sfbits(j)); return(i); } if (copyf) { *pbp++ = j; return(eschar); } switch (k) { case 'f': /* font indicator */ setfont(0); goto g0; case 's': /* size indicator */ setps(); goto g0; case 'v': /* vert mot */ numerr.type = numerr.escarg = 0; numerr.esc = k; if (i = vmot()) { return(i); } goto g0; case 'h': /* horiz mot */ numerr.type = numerr.escarg = 0; numerr.esc = k; if (i = hmot()) return(i); goto g0; case '|': /* narrow space */ if (NROFF) goto g0; return(makem((int)(EM)/6)); case '^': /* half narrow space */ if (NROFF) goto g0; return(makem((int)(EM)/12)); case 'w': /* width function */ setwd(); goto g0; case 'p': /* spread */ spread++; goto g0; case 'N': /* absolute character number */ numerr.type = numerr.escarg = 0; numerr.esc = k; if ((i = setabs()) == 0) goto g0; return i; case 'H': /* character height */ numerr.type = numerr.escarg = 0; numerr.esc = k; return(setht()); case 'S': /* slant */ numerr.type = numerr.escarg = 0; numerr.esc = k; return(setslant()); case 'z': /* zero with char */ return(setz()); case 'l': /* hor line */ numerr.type = numerr.escarg = 0; numerr.esc = k; setline(); goto g0; case 'L': /* vert line */ numerr.type = numerr.escarg = 0; numerr.esc = k; setvline(); goto g0; case 'D': /* drawing function */ numerr.type = numerr.escarg = 0; numerr.esc = k; setdraw(); goto g0; case 'X': /* \X'...' for copy through */ setxon(); goto g0; case 'b': /* bracket */ setbra(); goto g0; case 'o': /* overstrike */ setov(); goto g0; case 'k': /* mark hor place */ if ((k = findr(getsn())) != -1) { numtabp[k].val = numtabp[HP].val; } goto g0; case '0': /* number space */ return(makem(width('0' | chbits))); case 'x': /* extra line space */ numerr.type = numerr.escarg = 0; numerr.esc = k; if (i = xlss()) return(i); goto g0; case 'u': /* half em up */ case 'r': /* full em up */ case 'd': /* half em down */ return(sethl(k)); default: return(j); } /* NOTREACHED */ }