1 /*
2  * This file is part of gtkD.
3  * 
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  * 
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  * 
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 module utils.DefReader;
21 
22 
23 //debug=file;
24 
25 /**
26  * Reads and processes the API defintion file
27  * Stores global values:
28  * - license text
29  * - 
30  */
31 public class DefReader
32 {
33 	
34 	private import std.file;
35 	private import std.string;
36 	private import std.conv;
37 	
38 	private import std.stdio;
39 
40 	private import utils.GtkDClass;
41 	
42 	string fileName;
43 	string[] lines;
44 	
45 	string fullLine;
46 	string key;
47 	string value;
48 	
49 	int currLine = 0;
50 	
51 	/**
52 	 * Creates a new DegReader.
53 	 * Reads the entire definition file
54 	 * Params:
55 	 *    	fileName = 	The file name of the file containing the conversion definition
56 	 */
57 	this ( string fileName )
58 	{
59 		this.fileName = fileName;
60 		debug(file)writefln("DefReader.ctor fileName = %s", fileName);
61 		lines = std..string.splitLines(cast(string) std.file.read(fileName));
62 	}
63 
64 	public override string toString()
65 	{
66 		string str;
67 		str ~= "\n[DefReader]"
68 				~ "\nfileName = " ~ fileName
69 				~ "\ncurrLine = " ~ to!(string)(currLine)
70 				~ "\nfullLine = " ~ fullLine
71 				~ "\nkey      = " ~ key
72 				~ "\nvalue    + " ~ value
73 				;
74 		return str;
75 	}
76 	
77 	string getFileName()
78 	{
79 		return fileName;
80 	}
81 	
82 	/**
83 	 * Gets the next key/value pair.
84 	 * both key and value a stripped of non visible start and ending chars
85 	 * Returns: The key after read the next key/value pair
86 	 */
87 	string next(bool skipEmpty = true)
88 	{
89 		key.length = 0;
90 		value.length = 0;
91 		string line;
92 		if ( currLine < lines.length )
93 		{
94 			fullLine = lines[currLine++];
95 			line = std..string.strip(fullLine);
96 			int commentCount = 0;
97 			while ( skipEmpty
98 					&& (commentCount > 0  || line.length ==0 || line[0] == '#' || GtkDClass.startsWith(line, "#*") )
99 					&& currLine < lines.length
100 					)
101 			{
102 				if ( GtkDClass.startsWith(line, "#*") )
103 				{
104 					++commentCount;
105 				}
106 				else if ( GtkDClass.startsWith(line, "*#") )
107 				{
108 					--commentCount;
109 				}
110 
111 				fullLine = lines[currLine++];
112 				line = std..string.strip(fullLine);
113 			}
114 		}
115 		
116 		if ( line.length > 0 )
117 		{
118 			sizediff_t pos = std..string.indexOf(line, ':');
119 			if ( pos > 0 )
120 			{
121 				key = std..string.strip(line[0 .. pos]);
122 				value = std..string.strip(line[pos+1 .. line.length]);
123 			}
124 		}
125 		else
126 		{
127 			key.length = 0;
128 			value.length = 0;
129 		}
130 		
131 		//writefln("key=%s value=%s",key,value);
132 		
133 		return key;
134 	}
135 	
136 	/**
137 	 * Gets the key of the current key/value pair
138 	 * Returns: The current key
139 	 */
140 	string getKey()
141 	{
142 		return key;
143 	}
144 	
145 	/**
146 	 * Gets the value of the current key/value pair
147 	 * Returns: The current value
148 	 */
149 	string getValue()
150 	{
151 		return value;
152 	}
153 
154 	
155 	bool getValueBit()
156 	{
157 		return std..string.indexOf(" 1 ok OK Ok true TRUE True Y y yes YES Yes ", value) > 0;
158 	}
159 	
160 	string getFullLine()
161 	{
162 		return fullLine;
163 	}
164 	/**
165 	 * Gets the current line number
166 	 * Returns: The current line number
167 	 */
168 	int getLineNumber()
169 	{
170 		return currLine;
171 	}
172 	
173 }