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