1 /* @source embossdata application
2 **
3 ** Finds or fetches the data files read in by the EMBOSS programs
4 **
5 ** @author Copyright (C) Gary Williams (gwilliam@hgmp.mrc.ac.uk)
6 ** @modified Alan Bleasby (ableasby@hgmp.mrc.ac.uk) for recursion
7 ** @modified Alan Bleasby (ableasby@hgmp.mrc.ac.uk) to remove HOME dependency
8 ** @@
9 **
10 ** This program is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License
12 ** as published by the Free Software Foundation; either version 2
13 ** of the License, or (at your option) any later version.
14 **
15 ** This program is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ** GNU General Public License for more details.
19 **
20 ** You should have received a copy of the GNU General Public License
21 ** along with this program; if not, write to the Free Software
22 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23 ******************************************************************************/
24 
25 
26 #include "emboss.h"
27 
28 
29 
30 
31 static void embossdata_check_dir(const AjPStr d, AjPFile outf);
32 static void embossdata_check_file(const AjPStr d, const AjPStr file,
33 				  AjPFile outf);
34 static AjPStr embossdata_data_dir(void);
35 
36 
37 
38 
39 /* @prog embossdata ***********************************************************
40 **
41 ** Finds or fetches the data files read in by the EMBOSS programs
42 **
43 ******************************************************************************/
44 
main(int argc,char ** argv)45 int main(int argc, char **argv)
46 {
47     AjPList rlist = NULL;
48     AjPList flocs = NULL;
49     AjPFile outf = NULL;
50     AjPFile datafile = NULL;
51     AjPFile newdatafile = NULL;
52     AjPStr  t = NULL;
53     AjPStr line = NULL;
54 
55     ajint i;
56 
57 
58     AjPStr filename = NULL;
59     AjBool isname;
60 
61     AjBool fetch;
62     AjBool showall;
63 
64     AjPStr directory = NULL;
65     AjPStr hdir = NULL;
66     AjPStr ddir = NULL;
67     AjPStr path = NULL;
68     AjPStr cmd  = NULL;
69     AjPStr* rstrs = NULL;
70 
71     char *p = NULL;
72 
73     embInit("embossdata", argc, argv);
74 
75     filename = ajAcdGetString("filename");
76     fetch    = ajAcdGetBoolean("fetch");
77     showall  = ajAcdGetToggle("showall");
78     rstrs    = ajAcdGetSelect("reject");
79     outf     = ajAcdGetOutfile("outfile");
80 
81 
82     if(ajStrGetLen(filename))
83 	isname = ajTrue;
84     else
85 	isname = ajFalse;
86 
87 
88     /* Get directory reject list */
89     rlist = ajListNew();
90     if(!ajStrMatchCaseC(rstrs[0],"None"))
91     {
92 	i = 0;
93 	while(rstrs[i])
94 	{
95 	    ajListPush(rlist,(void *)rstrs[i]);
96 	    ++i;
97 	}
98     }
99 
100 
101     /* get the full pathname of the  emboss/data installation directory */
102     ddir = embossdata_data_dir();
103 
104     flocs = ajListNew();
105 
106     /*
107     ** fetch the data from the 'official' EMBOSS
108     ** data directory?
109     */
110     if(fetch)
111     {
112 	ajStrAssignS(&path,ddir);
113 	ajFilelistAddPathWildRecursiveIgnore(flocs, path,filename,rlist);
114 	if(!ajListPop(flocs,(void **)&t))
115 	    ajFatal("The file '%S' does not exist.", filename);
116 
117 	/* fetch it */
118         datafile = ajFileNewInNameS(t);
119         newdatafile = ajFileNewOutNameS(filename);
120         while(ajReadline(datafile, &line))
121             ajWriteline(newdatafile, line);
122         ajFileClose(&datafile);
123         ajFileClose(&newdatafile);
124 
125 	ajFmtPrintF(outf, "File '%S' has been copied successfully.\n", t);
126 	ajStrDel(&t);
127 	ajStrDel(&cmd);
128 	ajStrDel(&line);
129     }
130 
131 
132     /*
133     **  report whether data directories exist (no filename given)
134     **  or whether a specific file is in those directories
135     */
136     if(!fetch && !showall)
137     {
138 	ajFmtPrintF(outf,"# The following directories can contain "
139 		    "EMBOSS data files.\n");
140 	ajFmtPrintF(outf,"# They are searched in the following order "
141 		    "until the file is found.\n");
142 	ajFmtPrintF(outf, "# If the directory does not exist, then this "
143 		    "is noted below.\n");
144 	ajFmtPrintF(outf,"# '.' is the UNIX name for your current "
145 		    "working directory.\n");
146 	ajFmtPrintF(outf,"\n");
147 
148 	ajStrAssignC(&directory, ".");
149 	if(isname)
150 	    embossdata_check_file(directory,filename,outf);
151 	else
152 	    embossdata_check_dir(directory,outf);
153 
154 	/* .embossdata */
155 	ajStrAssignC(&directory, ".embossdata");
156 	if(isname)
157 	    embossdata_check_file(directory,filename,outf);
158 	else
159 	    embossdata_check_dir(directory,outf);
160 
161 	/* HOME */
162 	if((p = getenv("HOME")))
163 	{
164 	    ajStrAssignC(&hdir, p);
165 	    ajStrAssignS(&directory, hdir);
166 	    if(isname)
167 		embossdata_check_file(directory,filename,outf);
168 	    else
169 		embossdata_check_dir(directory,outf);
170 
171 	    /* ~/.embossdata */
172 	    ajStrAssignS(&directory, hdir);
173 	    ajStrAppendC(&directory, "/.embossdata");
174 	    if(isname)
175 		embossdata_check_file(directory,filename,outf);
176 	    else
177 		embossdata_check_dir(directory,outf);
178 	}
179 
180 
181 	/* DATA */
182 	if(isname)
183 	{
184 	    ajStrAssignS(&path,ddir);
185 	    ajFilelistAddPathWildRecursiveIgnore(flocs,path,filename,rlist);
186 
187 	    if(!ajListPop(flocs,(void **)&t))
188 		embossdata_check_file(ddir,filename,outf);
189 	    else
190 	    {
191 		ajFmtPrintF(outf,"File %-60.60S Exists\n",t);
192 		ajStrDel(&t);
193 	    }
194 	}
195 	else
196 	    embossdata_check_dir(ddir,outf);
197     }
198 
199 
200     /* Just show all the files in the EMBOSS Installation data directory */
201     if(showall)
202     {
203 	ajStrAssignS(&path,ddir);
204 	ajDirnamePrintRecursiveIgnore(path,rlist,outf);
205     }
206 
207     ajListFree(&flocs);
208     while(ajListPop(rlist,(void **)&t))
209 	ajStrDel(&t);
210     ajListFree(&rlist);
211     ajStrDel(&path);
212     ajStrDel(&hdir);
213     ajStrDel(&ddir);
214     ajStrDel(&directory);
215     ajStrDel(&cmd);
216     ajStrDel(&filename);
217     ajFileClose(&outf);
218 
219     AJFREE(rstrs);			/* individual strings freed on list */
220 
221     embExit();
222 
223     return 0;
224 }
225 
226 
227 
228 
229 /* @funcstatic embossdata_check_dir *******************************************
230 **
231 ** Undocumented.
232 **
233 ** @param [r] d [const AjPStr] Undocumented
234 ** @param [u] outf [AjPFile] Undocumented
235 ** @@
236 ******************************************************************************/
237 
238 
239 
embossdata_check_dir(const AjPStr d,AjPFile outf)240 static void embossdata_check_dir(const AjPStr d, AjPFile outf)
241 {
242     AjPStr tmpstr;
243     tmpstr = ajStrNewS(d);
244 
245     if(ajDirnameFixExists(&tmpstr))
246 	ajFmtPrintF(outf,"%-60.60S Exists\n",d);
247     else
248 	ajFmtPrintF(outf,"%-60.60S Does not exist\n",d);
249 
250     ajStrDel(&tmpstr);
251 
252     return;
253 }
254 
255 
256 
257 
258 /* @funcstatic embossdata_check_file ******************************************
259 **
260 ** Undocumented.
261 **
262 ** @param [r] d [const AjPStr] Undocumented
263 ** @param [r] file [const AjPStr] Undocumented
264 ** @param [u] outf [AjPFile] Undocumented
265 ** @@
266 ******************************************************************************/
267 
embossdata_check_file(const AjPStr d,const AjPStr file,AjPFile outf)268 static void embossdata_check_file(const AjPStr d, const AjPStr file,
269 				  AjPFile outf)
270 {
271     AjPStr s;
272 
273     s = ajStrNew();
274 
275     ajStrAssignS(&s,d);
276     ajStrAppendC(&s,"/");
277     ajStrAppendS(&s,file);
278     if(ajFilenameExistsRead(s))
279 	ajFmtPrintF(outf,"File %-60.60S Exists\n",s);
280     else
281 	ajFmtPrintF(outf,"File %-60.60S Does not exist\n",s);
282 
283     ajStrDel(&s);
284 
285     return;
286 }
287 
288 
289 
290 
291 /* @funcstatic embossdata_data_dir ********************************************
292 **
293 ** Undocumented.
294 **
295 ** @return [AjPStr] Undocumented
296 ** @@
297 ******************************************************************************/
298 
embossdata_data_dir(void)299 static AjPStr embossdata_data_dir(void)
300 {
301     static AjPStr where = NULL;
302     AjPStr tmp = NULL;
303 
304     where = ajStrNew();
305     tmp   = ajStrNew();
306 
307 
308     if(!ajNamGetValueC("DATA",&tmp))
309     {
310 	ajStrAssignS(&where, ajNamValueInstalldir());
311 	ajDirnameFix(&where);
312 	ajFmtPrintS(&tmp,"%Sshare/EMBOSS/data/",where);
313 
314 	if(!ajDirnameFixExists(&tmp))
315 	{
316 	    ajStrAssignS(&tmp, ajNamValueRootdir());
317 	    if(ajStrGetLen(tmp))
318 		ajStrAppendC(&tmp,"/data");
319 	    else
320 		ajFatal("The EMBOSS 'DATA' directory isn't defined.");
321 	}
322     }
323 
324     ajStrAssignC(&where,ajStrGetPtr(tmp));
325 
326     ajStrDel(&tmp);
327 
328     return where;
329 }
330