#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <cups/http.h>
int
main(int argc,
char *argv[])
{
http_t *http;
http_status_t status;
char line[1024],
*lineptr,
buffer[2048],
*bufptr,
length[16];
int bytes;
FILE *in,
*out;
if (argc != 3)
{
fputs("Usage: translate outfile language\n", stderr);
return (1);
}
if ((in = fopen("C/cups_C", "r")) == NULL)
{
perror("translate: Unable to open input file");
return (1);
}
if ((out = fopen(argv[1], "w")) == NULL)
{
perror("translate: Unable to create output file");
fclose(in);
return (1);
}
fgets(line, sizeof(line), in);
fputs("iso-8859-1\n", out);
while (fgets(line, sizeof(line), in) != NULL)
{
lineptr = line + strlen(line) - 1;
if (*lineptr == '\n')
*lineptr = '\0';
lineptr = line;
while (isdigit(*lineptr))
putc(*lineptr++, out);
while (isspace(*lineptr))
putc(*lineptr++, out);
if (*lineptr == '\0')
{
putc('\n', out);
continue;
}
sprintf(buffer, "doit=done&lp=en_%s&urltext=[", argv[2]);
bufptr = buffer + strlen(buffer);
while (*lineptr)
{
if (*lineptr == ' ')
*bufptr++ = '+';
else if (*lineptr < ' ' || *lineptr == '%')
{
sprintf(bufptr, "%%%02X", *lineptr & 255);
bufptr += 3;
}
else
*bufptr++ = *lineptr;
lineptr ++;
}
*bufptr++ = '&';
*bufptr = '\0';
sprintf(length, "%d", bufptr - buffer);
if ((http = httpConnect("dns.easysw.com", 80)) == NULL)
{
perror("translate: Unable to contact proxy server");
fclose(in);
fclose(out);
return (1);
}
lineptr = line;
while (isdigit(*lineptr))
lineptr ++;
while (isspace(*lineptr))
lineptr ++;
printf("%s = ", lineptr);
fflush(stdout);
http->version = HTTP_1_0;
httpClearFields(http);
httpSetField(http, HTTP_FIELD_CONTENT_TYPE,
"application/x-www-form-urlencoded");
httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, length);
if (httpPost(http, "http://babelfish.altavista.digital.com/cgi-bin/translate?"))
httpPost(http, "http://babelfish.altavista.digital.com/cgi-bin/translate?");
httpWrite(http, buffer, bufptr - buffer);
while ((status = httpUpdate(http)) == HTTP_CONTINUE);
if (status == HTTP_OK)
{
int sawparen = 0;
int skipws = 1;
int sawbracket = 0;
while ((bytes = httpRead(http, buffer, sizeof(buffer))) > 0)
{
buffer[bytes] = '\0';
for (bufptr = buffer; *bufptr; bufptr ++)
{
if (*bufptr == '>')
sawbracket = 0;
else if (*bufptr == '<')
{
sawbracket = 1;
if (sawparen)
break;
}
else if (*bufptr == '[' && !sawbracket)
sawparen = 1;
else if (sawparen)
{
if (skipws)
{
if (!isspace(*bufptr))
{
skipws = 0;
*bufptr = toupper(*bufptr);
}
}
if (!skipws)
{
if (*bufptr == '\n')
{
putc(' ', out);
putchar(' ');
}
else
{
putc(*bufptr, out);
putchar(*bufptr);
}
}
}
}
if (sawparen && sawbracket)
break;
}
httpFlush(http);
putc('\n', out);
putchar('\n');
}
else
{
printf("HTTP error %d\n", status);
fprintf(out, "%s\n", lineptr);
httpFlush(http);
}
httpClose(http);
}
fclose(in);
fclose(out);
return (0);
}