#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define bool char
#define MAX 1000000
char buf[MAX + 2];
char fin[9000],fout[9000];
int main(int argc, char* argv[])
{
if(argc >= 2){
fprintf(stderr,"這個程式不需要額外參數\n");
return 2;
}
bool toolong=0,newsec=1,seccom=1;
bool inok=0;
int len;
int c;
FILE *in,*out;
while(1){
do{
fprintf(stderr,"輸入檔名稱:");
fgets(fin, 8999, stdin);
len=strlen(fin);
if(fin[len-1]=='\n'){
fin[len-1]='\0';
}else{
while(getchar()!='\n');
}
if((in=fopen(fin,"r"))==NULL){
perror(fin);
inok=0;
}else{
inok=1;
}
}while(!inok);
do{
fprintf(stderr,"輸出檔名稱:");
fgets(fout, 8999, stdin);
len=strlen(fout);
if(fout[len-1]=='\n'){
fout[len-1]='\0';
}else{
while(getchar()!='\n');
}
if((out=fopen(fout,"w"))==NULL){
perror(fin);
inok=0;
}else{
inok=1;
}
}while(!inok);
toolong=0,newsec=1,seccom=1;
while(1){
fgets(buf, MAX + 1, in);
if(feof(in)){
break;
}
toolong=0,seccom=0;
len=strlen(buf);
if(len == 0){
fprintf(stderr,"發生不明錯誤,立即中止!\n");
return 1;
}
if(len == 1){
if(newsec){
seccom=1;
continue;
}
fprintf(out,");\n");
newsec=1;
seccom=1;
continue;
}
if(buf[len - 1] != '\n'){
toolong=1;
}else{
buf[len - 1]='\0';
}
if(newsec){
fprintf(out,"document.write(\n"
"\'%s",buf);
newsec=0;
}else{
fprintf(out,"+\'%s",buf);
}
if(toolong){
do{
fgets(buf, MAX + 1, in);
len=strlen(buf);
if(buf[len - 1] == '\n'){
buf[len - 1]='\0';
toolong=0;
}
fprintf(out,"%s",buf);
}while(toolong);
}
fprintf(out,"\'\n");
}
if(!seccom){
fprintf(out,");\n");
}
fclose(in);
if(fclose(out)){
perror(fout);
}
fprintf(stderr,"轉換下一個檔案?[Y] ");
if((c=getchar()) == 'n' || c == 'N'){
return 0;
}else{
if(c!='\n'){
while(getchar()!='\n');
}
}
}
return 0;
}