File: readafa.cpp

package info (click to toggle)
piler 0~20140707-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 360 kB
  • sloc: cpp: 5,369; makefile: 39
file content (86 lines) | stat: -rwxr-xr-x 1,767 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "piler2.h"

char *ReadAFA(FILE *f, int *ptrSeqLength, int *ptrSeqCount)
	{
	rewind(f);
	int FileSize = GetFileSize(f);
	int BufferSize = FileSize;
	char *Buffer = all(char, BufferSize);

	char prev_c = '\n';
	bool InLabel = false;
	int Pos = 0;
	int SeqStart = 0;
	int SeqCount = 0;
	int SeqLength;

	for (;;)
		{
		int c = fgetc(f);
		if (EOF == c)
			{
			if (feof(f))
				break;
			Quit("Stream error");
			}
		if (InLabel)
			{
			if (c == '\r')
				continue;
			if ('\n' == c)
				InLabel = false;
			}
		else
			{
			if ('>' == c && '\n' == prev_c)
				{
				int ThisSeqLength = Pos - SeqStart;
				if (0 == SeqCount)
					;
				else if (1 == SeqCount)
					SeqLength = ThisSeqLength;
				else if (SeqCount > 1)
					{
					if (SeqLength != ThisSeqLength)
						Quit("ReadAFA: %u seqs, sequence lengths differ (a) %d %d",
						  SeqCount, SeqLength, ThisSeqLength);
					}
				++SeqCount;
				SeqStart = Pos;
				InLabel = true;
				}
			else if (!isspace(c))
				{
				if (Pos >= BufferSize)
					Quit("ReadAFA: buffer too small");
				Buffer[Pos++] = (c);
				}
			}
		prev_c = c;
		}

	int ThisSeqLength = Pos - SeqStart;
	if (1 == SeqCount)
		SeqLength = ThisSeqLength;
	else
		{
		if (SeqLength != ThisSeqLength)
			Quit("ReadAFA: %u seqs, sequence lengths differ (b) %d %d",
			  SeqCount, SeqLength, ThisSeqLength);
		}

	*ptrSeqCount = SeqCount;
	*ptrSeqLength = SeqLength;

	if (Pos != SeqCount*SeqLength)
		Quit("ReadAFA: Internal error");
	return Buffer;
	}

char *ReadAFA(const char FileName[], int *ptrSeqLength, int *ptrSeqCount)
	{
	FILE *f = OpenStdioFile(FileName);
	char *Seqs = ReadAFA(f, ptrSeqLength, ptrSeqCount);
	fclose(f);
	return Seqs;
	}