Rename ffopen and ffclose to gmx_ff*.
[alexxy/gromacs.git] / src / contrib / hrefify.c
1 /*
2  * 
3  *                This source code is part of
4  * 
5  *                 G   R   O   M   A   C   S
6  * 
7  *          GROningen MAchine for Chemical Simulations
8  * 
9  *                        VERSION 3.3.99_development_20071104
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2006, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  * 
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  * 
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  * 
30  * For more info, check our website at http://www.gromacs.org
31  * 
32  * And Hey:
33  * Groningen Machine for Chemical Simulation
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <math.h>
40 #include <string.h> 
41 #include <ctype.h>
42 #include "smalloc.h"
43 #include "typedefs.h"
44 #include "macros.h"
45 #include "string2.h"
46 #include "gromacs/fileio/confio.h"
47 #include "vec.h"
48 #include "gromacs/commandline/pargs.h"
49 #include "copyrite.h"
50 #include "gromacs/fileio/pdbio.h"
51 #include "gromacs/fileio/strdb.h"
52
53 gmx_bool isword(char c)
54 {
55   return (isalnum(c) || (c=='-') || (c=='_'));
56 }
57
58 char *strncasestr(char *line,char *str)
59 {
60   char *dum;
61
62   dum=line;
63   if (dum) {
64     while (strlen(dum) && strncasecmp(dum,str,strlen(str)))
65       dum++;
66     if (strlen(dum)==0)
67       dum=NULL;
68   }
69
70   return dum;
71 }
72
73 char *strstr_href(char *line,gmx_bool *bInHREF,int *i_dat,int n_dat,char **dat)
74 {
75   char *start,*found,*href=NULL;
76   gmx_bool bIn;
77   int i;
78
79   found=NULL;
80   *i_dat=-1;
81   bIn=*bInHREF;
82   start=line;
83   do {
84     if (bIn) {
85       while (strlen(start) && (strncasecmp(start,"</a",3) != 0))
86         start++;
87       if (strlen(start)>0) {
88         start+=3;
89         bIn=FALSE;
90       }
91     }
92     else {
93       href=strncasestr(start,"<a href");
94       if (href)
95         bIn=TRUE;
96       i=0;
97       while((i<n_dat) && !found) {
98         found=strncasestr(start,dat[i]);
99         if (found) {
100           if (href && (found>href))
101             found=NULL;
102           else {
103             if (((found!=start) && isword(found[-1])) || 
104                 isword(found[strlen(dat[i])])) 
105               found=NULL;
106             else
107               *i_dat=i;
108           }
109           i++;
110         }
111       }
112     }
113   } while (strlen(start) && !found && href);
114   *bInHREF=bIn;
115
116   return found;
117 }
118
119 int main(int argc, char *argv[])
120 {
121   static char *desc[] = {
122     "[TT]hrefify[tt] adds href's for all the words in the input file which are not",
123     "already hyperlinked and which appear in the file specified with the",
124     "option [TT]-l[tt].[PAR]",
125     "If the href's should call a script, text can be added",
126     "with the [TT]-t[tt] option."
127   };
128
129   int n;
130   
131   char **text,**str,line[1024],*ptr,*ref,
132     start[STRLEN],word[STRLEN],end[STRLEN];
133   int n_text,n_str,i_str;
134   gmx_bool bInHREF,bIn;
135   
136   FILE    *fp;
137   char    title[STRLEN];
138   int     i,l,n_repl;
139   t_filenm fnm[] = {
140     { efDAT, "-l", "links", ffLIBRD },
141   };
142 #define NFILE asize(fnm)
143   static char *in=NULL,*out=NULL,*excl=NULL,*link_text=NULL;
144   static gmx_bool peratom=FALSE;
145   t_pargs pa[] = {
146     { "-f", FALSE, etSTR, { &in } , "HTML input" },
147     { "-o", FALSE, etSTR, { &out } , "HTML output" },
148     { "-e", FALSE, etSTR, { &excl } , "Exclude a string from HREF's, "
149       "when this option is not set, the filename without path and extension "
150       "will be excluded from HREF's"},
151     { "-t", FALSE, etSTR, { &link_text } , "Insert a string in front of the "
152       "href file name, useful for scripts" }
153   };
154   
155   CopyRight(stderr,argv[0]);
156   parse_common_args(&argc,argv,0,NFILE,fnm,asize(pa),pa,
157                     asize(desc),desc,0,NULL);
158
159   if (!in || !out)
160     gmx_fatal(FARGS,"Input or output filename is not set");
161
162   n_text = get_file(in, &text);
163   fprintf(stderr,"Read %d lines from %s\n",n_text,in);
164
165   n_str=get_file(ftp2fn(efDAT,NFILE,fnm),&str);  
166   fprintf(stderr,"Read %d strings %s\n",n_str,ftp2fn(efDAT,NFILE,fnm));
167   if (!excl) {
168     for (i=strlen(in)-1; i>0 && in[i-1]!='/'; i--);
169     excl=strdup(in+i);
170     for(i=strlen(excl)-1; i>0 && (excl[i]!='.'); i--);
171     if (excl[i]=='.')
172       excl[i]='\0';
173   }
174   fprintf(stderr,"Excluding '%s' from references\n",excl);
175   for(l=0; l<n_str && strcasecmp(str[l],excl); l++);
176   if (l<n_str) {
177     for(i=l+1; i<n_str; i++)
178       str[i-1]=str[i];
179     n_str--;
180   }
181
182   if (!link_text)
183     link_text=strdup("\0");
184   else
185     fprintf(stderr,"Adding '%s' to href's\n",link_text);
186
187   fp=gmx_ffopen(out,"w");
188
189   n_repl=0;
190   i_str=-1;
191   bInHREF=FALSE;
192   for(l=0; l<n_text; l++) {
193     strcpy(line,text[l]);
194     do {
195       bIn=bInHREF;
196       ptr=strstr_href(line,&bIn,&i_str,n_str,str);
197       if (ptr) {
198         ref=ptr;
199         if ((ref!=line) && (ref[-1]=='.')) {
200           ref--;
201           while((ref>line) && isword(ref[-1]))
202             ref--;
203         }
204         strcpy(start,line);
205         start[ref-line]='\0';
206         strcpy(word,ref);
207         word[ptr-ref+strlen(str[i_str])]='\0';
208         strcpy(end,ptr+strlen(str[i_str]));
209         sprintf(line,"%s<a href=\"%s%s.html\">%s</a>%s",
210                 start,link_text,str[i_str],word,end);
211         fprintf(stderr,"line %d: %s\n",l+1,str[i_str]);
212         n_repl++;
213       }
214     } while (ptr);
215     bInHREF=bIn;
216     fprintf(fp,"%s\n",line);
217   }
218
219   gmx_ffclose(fp);
220   
221   fprintf(stderr,"Added %d HTML references\n",n_repl);
222
223   return 0;
224 }
225