Change Eclipse configuration
[jabaws.git] / website / archive / binaries / mac / src / disembl / Tisean_3.0.1 / source_c / routines / search_datafile.c
1 /*
2  *   This file is part of TISEAN
3  *
4  *   Copyright (c) 1998-2007 Rainer Hegger, Holger Kantz, Thomas Schreiber
5  *
6  *   TISEAN is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   TISEAN is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with TISEAN; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 /*Author: Rainer Hegger Last modified: Sep 3, 1999 */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include "tsa.h"
26
27 char check_col(char *col)
28 {
29   int i;
30   
31   for (i=0;i<strlen(col);i++)
32     if (!isdigit((unsigned int)col[i])) {
33       fprintf(stderr,"Column must be a unsigned integer. Ignoring it!\n");
34       return 0;
35     }
36   return 1;
37 }
38
39 char look_for_column(char *name,unsigned int *col)
40 {
41   char *hcol,*hname;
42   char vcol=0;
43   int j,in;
44
45   check_alloc(hname=(char*)calloc(strlen(name)+1,1));
46   check_alloc(hcol=(char*)calloc(strlen(name)+1,1));
47   j=0;
48   while (*(name+j) != '\0') {
49     if (*(name+j) == ',') {
50       in=sscanf(name+j+1,"%s",hcol);
51       if (in > 0)
52         vcol=check_col(hcol);
53       *(name+j)='\0';
54       break;
55     }
56     *(hname+j)=*(name+j);
57     j++;
58   }
59   *col=(unsigned int)atoi(hcol);
60   free(hname);
61   free(hcol);
62
63   return vcol;
64 }
65
66 char* search_datafile(int n,char **names,unsigned int *col,
67                       unsigned int verbosity)
68 {
69   char valid=0,validcol=0;
70   char *retname=NULL;
71   int i;
72   unsigned int hcol;
73   FILE *test;
74
75   for (i=n-1;i>0;i--) {
76     if (names[i] != NULL) {
77       valid=0;
78       if (strcmp(names[i],"-")) {
79         if (col != 0)
80           validcol=look_for_column(names[i],&hcol);
81         test=fopen(names[i],"r");
82         if (test == NULL) {
83           fprintf(stderr,"File %s not found!\n",names[i]);
84         }
85         else {
86           fclose(test);
87           if ((col != 0) && (validcol == 1))
88             *col=hcol;
89           if (col != 0) {
90             if (verbosity&VER_INPUT)
91               fprintf(stderr,"Using %s as datafile, reading column %u\n",
92                       names[i],*col);
93           }
94           else {
95             if (verbosity&VER_INPUT)
96               fprintf(stderr,"Using %s as datafile!\n",names[i]);
97           }
98           check_alloc(retname=(char*)calloc(strlen(names[i])+1,(size_t)1));
99           strcpy(retname,names[i]);
100           names[i]=NULL;
101           return retname;
102         }
103       }
104       else {
105         valid=1;
106         break;
107       }
108     }
109   }
110
111   if (valid == 1) {
112     if (verbosity&VER_INPUT)
113       fprintf(stderr,"Reading input from stdin!\n");
114     return NULL;
115   }
116   
117   if (verbosity&VER_INPUT) {
118     if ((col != 0) && (validcol == 1))
119       fprintf(stderr,"Reading input from stdin, using column %u!\n",*col);
120     else
121       fprintf(stderr,"Reading input from stdin!\n");
122   }
123
124   return NULL;
125 }