File: MapFile.h

package info (click to toggle)
pose 3.0a3-3
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 15,500 kB
  • ctags: 20,548
  • sloc: ansic: 72,579; cpp: 50,198; perl: 1,336; python: 1,242; sh: 363; makefile: 290
file content (76 lines) | stat: -rw-r--r-- 2,353 bytes parent folder | download
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
/* -*- mode: C++; tab-width: 4 -*- */
/* ================================================================================== */
/* Copyright (c) 1998-1999 3Com Corporation or its subsidiaries. All rights reserved. */
/* ================================================================================== */

#ifndef __MAPFILE_H__
#define __MAPFILE_H__

#include <stdio.h>

// -----------------------------------------------------------------------------
// class MapFile
//
// A MapFile is a class which reads a file of name -> value pair mappings into
// memory, and then presents the data as an associative map
// -----------------------------------------------------------------------------
class MapEntry;

class MapFile
{
public:

	// -----------------------------------------------------------------------------
	// constructor / destructor
	// -----------------------------------------------------------------------------

	MapFile( int buckets = 20 );
	MapFile( const char* filename, int buckets = 20 );
	virtual ~MapFile( void );

	// -----------------------------------------------------------------------------
	// public methods
	// -----------------------------------------------------------------------------

	void parseFile( const char* fname );

	// add mapping to Map. Memory is copied.
	void add( const char* key, const char* value );
	bool remove( const char* key );
	char* find( const char* key );
	int size() { return _count; }
	void dump( FILE* f );

	void setCaseCompare( bool b ) { _useCase = b; }
	bool getCaseCompare() { return _useCase; }

private:

	// -----------------------------------------------------------------------------
	// private methods
	// -----------------------------------------------------------------------------

	void initialize();

	void removeEntry( MapEntry* entry );
	void insertEntry( MapEntry* entry );
	int hash( const char* key );
	MapEntry* lookup( const char* key );

	void scanLine( char* line );
	void parseLine( char* line );
	char* trim( char *s );

	int compare( const char *s1, const char *s2 );

	// -----------------------------------------------------------------------------
	// private data members
	// -----------------------------------------------------------------------------	

	bool _useCase;
	int _numBuckets;
	int _count;
	MapEntry **_buckets;
};

#endif /* __MAPFILE_H__ */