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