add cescape stuff and two tests

master
leitner 22 years ago
parent a8f6a1c121
commit 056760c5f1

@ -0,0 +1,25 @@
#include <string.h>
#include "buffer.h"
#include "textcode.h"
void cescape(const char* c) {
char* buf=alloca(strlen(c)*5+1);
buffer_put(buffer_1,buf,fmt_cescape(buf,c,strlen(c)));
buffer_putnlflush(buffer_1);
}
main(int argc,char* argv[]) {
int i;
for (i=1; i<argc; ++i) {
cescape(argv[i]);
}
if (argc<2) {
char src[1024];
int len=read(0,src,sizeof(src)-1);
if (len==-1) return(1);
src[len]=0;
cescape(src);
}
return 0;
}

@ -0,0 +1,21 @@
#include "array.h"
#include "textcode.h"
#include <assert.h>
array a;
int main() {
char buf[256];
int i;
for (i=0; i<256; ++i) buf[i]=i;
fmt_to_array(fmt_uuencoded,&a,buf,256);
assert(!array_failed(&a));
write(1,array_start(&a),array_bytes(&a));
array_trunc(&a);
fmt_to_array(fmt_base64,&a,buf,256);
assert(!array_failed(&a));
write(1,array_start(&a),array_bytes(&a)); write(1,"\n",1);
array_trunc(&a);
}

@ -0,0 +1,46 @@
#include "fmt.h"
#include "textcode.h"
#include "str.h"
#include "haveinline.h"
unsigned long fmt_cescape(char* dest,const char* src,unsigned long len) {
register const unsigned char* s=(const unsigned char*) src;
unsigned long written=0,i;
char c;
for (i=0; i<len; ++i) {
switch (s[i]) {
case '\a': c='a'; goto doescape;
case '\b': c='b'; goto doescape;
case '\e': c='e'; goto doescape;
case '\f': c='f'; goto doescape;
case '\n': c='n'; goto doescape;
case '\r': c='r'; goto doescape;
case '\t': c='t'; goto doescape;
case '\v': c='v'; goto doescape;
case '\\':
c='\\';
doescape:
if (dest) {
dest[written]='\\';
dest[written+1]=c;
}
written+=2;
break;
default:
if (s[i]<' ') {
if (dest) {
dest[written]='\\';
dest[written+1]='x';
dest[written+2]=fmt_tohex(s[i]>>4);
dest[written+3]=fmt_tohex(s[i]&0xf);
}
written+=4;
} else {
if (dest) dest[written]=s[i];
++written;
}
break;
}
}
return written;
}

@ -0,0 +1,42 @@
#include "fmt.h"
#include "textcode.h"
#include "scan.h"
unsigned long scan_cescape(const char *src,char *dest,unsigned long *destlen) {
register const unsigned char* s=(const unsigned char*) src;
unsigned long written=0,i;
char c;
for (i=0; s[i]; ++i) {
if ((c=s[i])=='\\') {
switch (s[i+1]) {
case 'a': c='\a'; break;
case 'b': c='\b'; break;
case 'e': c='\e'; break;
case 'f': c='\f'; break;
case 'n': c='\n'; break;
case 'r': c='\r'; break;
case 't': c='\t'; break;
case 'v': c='\v';
case '\\': break;
case 'x':
{
unsigned char a,b;
a=scan_fromhex(s[i+2]);
b=scan_fromhex(s[i+3]);
if (a<16 && b<16) {
c=(a<<4)+b;
i+=2;
}
}
break;
default:
--i;
}
++i;
}
dest[written]=c;
++written;
}
*destlen=written;
return i;
}
Loading…
Cancel
Save