BBS水木清华站∶精华区

发信人: energy (长白山), 信区: Linux        
标  题: CJK+Hyperref中文目录的处理程序 
发信站: BBS 水木清华站 (Wed Dec  1 01:11:17 1999) 
 
该程序用于把pdflatex生成的*.out文件进行处理, 从*.aux文件中读取目录文本, 
写入*.out文件中, 并在头一行加上\let\WriteBookMarks\relax 
处理之后生成中间文件*.tmp, 程序里用system的笨办法复制为*.out. 
命令行参数为要处理的文件名, 注意别加扩展名. 
下面是源代码, 请eggs等大虾试试. 
 
aux2out.c 
 
#include <stdio.h> 
#include <string.h> 
#include <process.h> 
 
void errmsg() 
{   printf("File format not recognized\n"); 

 
int main(int argc, char *argv[]) 
{    
    FILE *f_aux=0, *f_out=0, *f_tmp=0; 
     
    char aux_fname[256]; 
    char out_fname[256]; 
    char tmp_fname[256]; 
    char line[256], toc[256], buf[256]; 
    char *str1,*str2; 
     
        int n; 
     
        if (argc!=2) 
    {   printf("Usage:\n\t aux2out file \nNo file extention needed.\n"); 
        return 0; 
    } 
     
    strcpy(aux_fname,argv[1]);    strcat(aux_fname,".aux"); 
    strcpy(out_fname,argv[1]);    strcat(out_fname,".out"); 
    strcpy(tmp_fname,argv[1]);    strcat(tmp_fname,".tmp"); 
     
    f_aux = fopen( aux_fname, "rt" ); 
    if (f_aux == NULL) 
    {   printf("File %s not found\n",aux_fname); 
        return 0; 
    } 
     
    f_out = fopen( out_fname, "rt" ); 
    if (f_aux == NULL) 
    {   printf("File %s not found\n",out_fname); 
        return 0; 
    } 
 
    f_tmp = fopen( tmp_fname, "wt" ); 
    if (f_aux == NULL) 
    {   printf("File %s can't create\n",tmp_fname); 
        return 0; 
    } 
    fputs("\\let\\WriteBookMarks\\relax\n",f_tmp); 
     
loop: 
    /*read a line from AUX file*/ 
    str1 = fgets( line, 256, f_aux); 
    if (str1==NULL) goto over; 
     
    /*find a toc line*/ 
    if (strstr(line,"\\@writefile{toc}{\\contentsline")==NULL) goto loop; 
     
    /*find the start of toc: the 3rd '}' */ 
    for (n=0;n<3;n++) 
    { str1 = strstr(str1,"}"); str1++; 
      if (str1==NULL){ errmsg(); return 0; } 
    } 
 
    /*find the end of toc: the next '}'*/ 
    str2 = strstr(str1,"}"); 
    if (str2==NULL) { errmsg(); return 0; } 
     
    /*get the toc string into str1, and save it*/ 
    *str2=0;  strcpy(toc, str1); 
     
    /*read line from the OUT file*/ 
loop2: 
    str1 = fgets( line, 256, f_out); 
    if (str1==NULL) goto over; 
     
    /*find a toc line*/ 
    if (strstr(line,"\\BOOKMARK")==NULL) goto loop2; 
     
    /*find the start of toc: the first '}' */ 
    str1 = strstr(str1,"}"); 
    if (str1==NULL){ errmsg(); return 0; } 
        str1+=2; 
     
    /*find the rest of the toc. {} may not be empty*/ 
        str2 = strstr(str1,"}"); 
    if (str2==NULL){ errmsg(); return 0; } 
 
    /*save the rest of line*/ 
    strcpy(buf, str2); 
     
    /*insert toc*/ 
    strcpy(str1, toc); 
     
    /*add the rest of line*/ 
    strcat(line,buf); 
     
    /*write to TMP file*/ 
    fputs(line,f_tmp); 
     
    goto loop; 
     
over: 
    fclose(f_aux); fclose(f_out); fclose(f_tmp); 
 
/*The follows is to rename TMP to OUT, and delete TMP*/ 
/*May not work on Unix/Linux, modify if needed*/ 
        sprintf(line,"copy %s %s", tmp_fname, out_fname); 
        system(line); 
 
        sprintf(line,"del %s", tmp_fname); 
        system(line); 
 
        return 1; 

 
 
-- 
※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 202.192.242.206] 

BBS水木清华站∶精华区