summaryrefslogtreecommitdiff
path: root/utils/synutils/utility.h
blob: d3f52ee0ce32a775640bf626e401bd68f7aadfe3 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * utility.h
 *
 *  Created on: Jun 24, 2013
 *      Author: lijunhui
 */

#ifndef UTILITY_H_
#define UTILITY_H_


#include <zlib.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>

using namespace std;

typedef std::unordered_map<std::string, int> MapString2Int;
typedef std::unordered_map<std::string, float> MapString2Float;
typedef std::unordered_map<std::string, float>::iterator MapString2FloatIterator;

using namespace std;

struct SFReader {
	SFReader( ){ }
	virtual ~SFReader( ){ }

	virtual bool fnReadNextLine( char *pszLine, int* piLength ) = 0;
	virtual bool fnReadNextLine(string& strLine) = 0;
};

struct STxtFileReader: public SFReader {
	STxtFileReader(const char* pszFname) {
		m_fpIn = fopen(pszFname, "r");
		assert(m_fpIn != NULL);
	}
	~STxtFileReader() {
		if (m_fpIn != NULL)
			fclose(m_fpIn);
	}

	bool fnReadNextLine(char *pszLine, int* piLength) {
			if (feof(m_fpIn) == true)
				return false;

			int iLen;

			pszLine[ 0 ] = '\0';

			fgets(pszLine, 10001, m_fpIn);
			iLen = strlen(pszLine);
			if (iLen == 0) return false;
			while (iLen > 0 && pszLine[iLen - 1] > 0 && pszLine[iLen -1] < 33) {
				pszLine[ iLen - 1 ] = '\0';
				iLen--;
			}

			if ( piLength != NULL )
				(*piLength) = iLen;

			return true;
		}

	bool fnReadNextLine(string& strLine) {
		char *pszLine = new char[10001];
		bool bOut = fnReadNextLine(pszLine, NULL);
		if (bOut) strLine = string(pszLine);
		else strLine = string("");
		delete [] pszLine;

		return bOut;
	}
private:
	FILE *m_fpIn;
};

struct SGZFileReader: public SFReader {
	SGZFileReader(const char* pszFname) {
		m_fpIn = gzopen( pszFname, "r" );
		assert(m_fpIn != NULL);
	}
	~SGZFileReader() {
		if (m_fpIn != NULL)
			gzclose(m_fpIn);
	}

	bool fnReadNextLine(char *pszLine, int* piLength) {
		if ( m_fpIn == NULL )
			exit( 0 );
		if ( gzeof( m_fpIn ) == true )
			return false;

		int iLen;

		pszLine[ 0 ] = '\0';

		gzgets( m_fpIn, pszLine, 10001 );
		iLen = strlen( pszLine );
		while ( iLen > 0 && pszLine[ iLen - 1 ] > 0 && pszLine[ iLen -1 ] < 33 )
		{
			pszLine[ iLen - 1 ] = '\0';
			iLen--;
		}

		if ( piLength != NULL )
			(*piLength) = iLen;

		return true;
	}

	bool fnReadNextLine(string& strLine) {
		char *pszLine = new char[10001];
		bool bOut = fnReadNextLine(pszLine, NULL);
		if (bOut) strLine = string(pszLine);
		else strLine = string("");
		delete [] pszLine;

		return bOut;
	}
private:
	gzFile m_fpIn;
};


#endif /* UTILITY_H_ */