Cleaned up memory usage in gmx traj and trjconv
[gromacs.git] / src / contrib / hrefify.c
bloba21f14abae9d6db6d015e7161d582c03a400d511
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.
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.
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.
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.
30 * For more info, check our website at http://www.gromacs.org
32 * And Hey:
33 * Groningen Machine for Chemical Simulation
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
39 #include <math.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include "gromacs/utility/smalloc.h"
43 #include "typedefs.h"
44 #include "macros.h"
45 #include "gromacs/utility/cstringutil.h"
46 #include "gromacs/fileio/confio.h"
47 #include "gromacs/math/vec.h"
48 #include "gromacs/commandline/pargs.h"
49 #include "copyrite.h"
50 #include "gromacs/fileio/pdbio.h"
51 #include "gromacs/utility/strdb.h"
53 gmx_bool isword(char c)
55 return (isalnum(c) || (c=='-') || (c=='_'));
58 char *strncasestr(char *line,char *str)
60 char *dum;
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;
70 return dum;
73 char *strstr_href(char *line,gmx_bool *bInHREF,int *i_dat,int n_dat,char **dat)
75 char *start,*found,*href=NULL;
76 gmx_bool bIn;
77 int i;
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;
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;
109 i++;
113 } while (strlen(start) && !found && href);
114 *bInHREF=bIn;
116 return found;
119 int main(int argc, char *argv[])
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."
129 int n;
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;
136 FILE *fp;
137 char title[STRLEN];
138 int i,l,n_repl;
139 t_filenm fnm[] = {
140 { efDAT, "-l", "links", ffLIBRD },
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" }
155 CopyRight(stderr,argv[0]);
156 parse_common_args(&argc,argv,0,NFILE,fnm,asize(pa),pa,
157 asize(desc),desc,0,NULL);
159 if (!in || !out)
160 gmx_fatal(FARGS,"Input or output filename is not set");
162 n_text = get_file(in, &text);
163 fprintf(stderr,"Read %d lines from %s\n",n_text,in);
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';
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--;
182 if (!link_text)
183 link_text=strdup("\0");
184 else
185 fprintf(stderr,"Adding '%s' to href's\n",link_text);
187 fp=gmx_ffopen(out,"w");
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--;
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++;
214 } while (ptr);
215 bInHREF=bIn;
216 fprintf(fp,"%s\n",line);
219 gmx_ffclose(fp);
221 fprintf(stderr,"Added %d HTML references\n",n_repl);
223 return 0;