source: tags/initial/GDE/SUPPORT/Flatio.c

Last change on this file was 2, checked in by oldcode, 24 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 KB
Line 
1#include <malloc.h>
2#include <stdio.h>
3#define TRUE 1
4#define FALSE 0
5#define MAX(a,b) ((a)>(b)?(a):(b))
6#define MIN(a,b) ((a)<(b)?(a):(b))
7
8struct data_format
9{
10        int length;
11        char *nuc;
12        int offset;
13        char name[64];
14        char type;
15};
16
17
18char *Calloc2(count,size)
19int count,size;
20{
21        char *temp;
22
23        temp = (char*)calloc(count,size);
24        if(temp == NULL)
25        {
26                fprintf(stdout,"Error in Calloc\n");
27                exit(-1);
28        }
29        else
30                return(temp);
31}
32
33char *Realloc(block,size)
34char *block;
35int size;
36{
37    char *temp;
38    temp =(char*)realloc(block,size);
39    if(temp == NULL)
40    {
41                fprintf(stdout,"Error in Calloc\n");
42                exit(-1);
43    }
44    else
45        return(temp);
46}
47
48
49
50int ReadFlat(file,align,maxseqs)
51FILE *file;
52struct data_format align[];
53int maxseqs;
54{
55        int j,len=0, count=-1,offset;
56        unsigned maxlen = 1024;
57        char Inline[1025];
58
59        if(file == NULL)
60                Errorout("Cannot open data file");
61
62        for(;fgets(Inline,1024,file) != NULL;)
63        {
64                Inline[strlen(Inline)-1] = '\0';
65                switch(Inline[0])
66                {
67                        case '>':
68                        case '#':
69                        case '%':
70                        case '"':
71                        case '@':
72                                offset = 0;
73                                for(j=0;j<strlen(Inline);j++)
74                                {
75                                        if(Inline[j] == '(')
76                                        {
77                                                sscanf((char*)
78                                                        (Inline+j+1),"%d",
79                                                        &offset);
80                                                Inline[j] = '\0';
81                                        }
82                                }
83
84                                if(count != -1)
85                                {
86                                        align[count].length = len;
87                                        align[count].nuc[len] = '\0';
88                                        maxlen = len;
89                                }
90
91                                count++;
92                                if(count > maxseqs)
93                                     Errorout("Sorry, alignment is too large");
94
95                                align[count].nuc = Calloc2(maxlen,sizeof(char));
96                                align[count].type = Inline[0];
97                                align[count].offset = offset;
98                                if( align[count].nuc == NULL)
99                                        Errorout("Calloc problem");
100
101                                sscanf((char*)(Inline+1),"%s",
102                                        align[count].name);
103                                len = 0;
104                                break;
105                        default:
106                                if(len+strlen(Inline) > maxlen)
107                                {
108                                        maxlen = (maxlen+strlen(Inline))*2;
109                                        align[count].nuc =
110                                        Realloc(align[count].nuc, maxlen);
111                                }
112                                for(j=0;j<strlen(Inline);j++)
113                                        align[count].nuc[j+len] = Inline[j];
114                                len += strlen(Inline);
115                                break;
116                }
117        }
118        if(count == -1) exit(1);
119
120        align[count].length = len;
121        align[count].nuc[len] = '\0';
122        return(++count);
123}
124
125
126Errorout(string)
127char *string;
128{
129        fprintf(stderr,"%s\n",string);
130        exit(1);
131}
132
133WriteData(file,data,count)
134FILE *file;
135struct data_format data[];
136int count;
137{
138        int i,j;
139        for(j = 0 ; j<count;j++)
140        {
141                if(data[j].offset)
142                        fprintf(file,"\n%c%s(%d)",data[j].type,data[j].name,
143                        data[j].offset);
144                else
145                        fprintf(file,"\n%c%s",data[j].type,data[j].name);
146
147                for(i=0;i<data[j].length;i++)
148                {
149                        if(i%60 == 0)
150                                fputc('\n',file);
151                        fputc(data[j].nuc[i],file);
152                }
153        }
154        return;
155}
156
157
158ErrorOut(code,string)
159int code;
160char *string;
161{
162        if (code == 0)
163        {
164                fprintf(stderr,"Error:%s\n",string);
165                exit(1);
166        }
167        return;
168}
Note: See TracBrowser for help on using the repository browser.