Browse Source

add html encoders

add vd (mldonkey support program) as test for socket API
master
Felix von Leitner 19 years ago
parent
commit
4a830369db
  1. 1
      CHANGES
  2. 36
      test/vd.c
  3. 3
      textcode.h
  4. 24
      textcode/fmt_html.c
  5. 36
      textcode/scan_html.c

1
CHANGES

@ -10,6 +10,7 @@
make install forgot to install ndelay.h
fix typos in several man pages (Hynek Schlawack)
add stralloc versions of textcode API (Kai Ruemmler)
add html to textcode ('<' to '&lt;' etc)
0.14:
avoid bus errors in byte_copy

36
test/vd.c

@ -0,0 +1,36 @@
#include "socket.h"
#include "buffer.h"
#include <unistd.h>
#include <stdio.h>
main() {
int s=socket_tcp4();
char line[1024];
char buf[4096];
int l;
int header=1;
buffer filein;
buffer_init(&filein,read,s,buf,sizeof buf);
if (socket_connect4(s,"\x7f\x00\x00\x01",4000)) {
perror("connect");
return 1;
}
write(s,"vd\nq\n",5);
for (;;) {
line[0]=0;
if ((l=buffer_getline(&filein,line,(sizeof line)-1))==0 && line[l]!='\n')
break;
else {
line[l+1]=0;
if (!header) {
if (strcmp(line,"\e[7mMLdonkey command-line:\e[2;37;0m\n") &&
strcmp(line,"\e[2;37;0m\e[7mMLdonkey command-line:\e[2;37;0m\n") &&
strncmp(line,"> ",2))
buffer_put(buffer_1,line,l+1);
}
if (!strcmp(line,"Use \e[31m?\e[2;37;0m for help\n")) header=0;
if (!strcmp(line,"Use ? for help\n")) header=0;
}
}
buffer_flush(buffer_1);
}

3
textcode.h

@ -9,6 +9,8 @@ unsigned int fmt_quotedprintable(char* dest,const char* src,unsigned int len);
unsigned int fmt_urlencoded(char* dest,const char* src,unsigned int len);
unsigned int fmt_yenc(char* dest,const char* src,unsigned int len);
unsigned int fmt_hexdump(char* dest,const char* src,unsigned int len);
/* this changes '<' to '&lt;' and '&' to '&amp;' */
unsigned int fmt_html(char* dest,const char* src,unsigned int len);
/* These read one line from src, decoded it, and write the result to
* dest. The number of decoded bytes is written to destlen. dest
@ -19,6 +21,7 @@ unsigned int scan_quotedprintable(const char *src,char *dest,unsigned int *destl
unsigned int scan_urlencoded(const char *src,char *dest,unsigned int *destlen);
unsigned int scan_yenc(const char *src,char *dest,unsigned int *destlen);
unsigned int scan_hexdump(const char *src,char *dest,unsigned int *destlen);
unsigned int scan_html(const char *src,char *dest,unsigned int *destlen);
#ifdef STRALLOC_H
/* These take len bytes from src and write them in encoded form to sa.

24
textcode/fmt_html.c

@ -0,0 +1,24 @@
#include "fmt.h"
#include "textcode.h"
#include "str.h"
#include "haveinline.h"
unsigned int fmt_html(char* dest,const char* src,unsigned int len) {
register const unsigned char* s=(const unsigned char*) src;
unsigned long written=0,i;
const char* seq;
for (i=0; i<len; ++i) {
switch (s[i]) {
case '&': seq="&amp;"; goto doit;
case '<': seq="&lt;"; goto doit;
case '>': seq="&gt;"; goto doit;
case '\n':
seq="<br>";
doit:
written+=fmt_str(dest?dest+written:0,"&gt;");
break;
default: if (dest) dest[written]=s[i]; ++written; break;
}
}
return written;
}

36
textcode/scan_html.c

@ -0,0 +1,36 @@
#include "fmt.h"
#include "textcode.h"
#include "haveinline.h"
#include "case.h"
unsigned int scan_html(const char *src,char *dest,unsigned int *destlen) {
register const unsigned char* s=(const unsigned char*) src;
unsigned long written=0,i;
for (i=0; s[i]; ++i) {
if (s[i]=='&') {
if (case_starts(s+i+1,"amp;")) {
dest[written]='&';
i+=4;
} else if (case_starts(s+i+1,"lt;")) {
dest[written]='<';
i+=3;
} else if (case_starts(s+i+1,"gt;")) {
dest[written]='>';
i+=3;
}
} else if (s[i]=='<') {
if (case_starts(s+i+1,"br>")) {
dest[written]='\n';
i+=3;
} else if (case_starts(s+i+1,"p>")) {
dest[written]='\n'; ++written;
dest[written]='\n';
i+=3;
}
} else
dest[written]=s[i];
++written;
}
*destlen=written;
return i;
}
Loading…
Cancel
Save