示例#1
0
		public static bool ReadVariables(string FileName, Circuit owner)
		{
			Dictionary<string,double> CList;
			string[] words,keys;
			string line = "";
			char[] delimiterChars = { ' ', ',', '\t' };
			double val = 0;
			
			
			if(owner == null)
				CList = Program.Globals;
			else
				CList = ((composite)owner).Locals;
		
			Console.WriteLine("\n   Reading variables from {0}:", FileName);
			StreamReader reader = new StreamReader(FileName);
            if (!StringReader.FindString("<variables>", reader)){
                Console.WriteLine("WARNING! Variable list is missing.");
				reader.Dispose();
                return true;
            }
			
			while ((line = reader.ReadLine()) != null){
				line = line.Trim();
                words = StringReader.TrimWords(line.Split(delimiterChars));//read the type and the name
				
				#region "check for empty/terminator lines"
                if (line.StartsWith("<end>")) //finish at the end of group
                    break;
                if (line.StartsWith("#") || line.Length == 0 || words.Length == 0 ) //ignore empty lines/comments/timer
                    continue;
				#endregion
			
				#region "parse the line"
				for(int i=0;i<words.Length;i++){
					keys = StringReader.TrimWords(words[i].Split('=')); //divide the string,
					if(keys.Length != 2){
						Console.WriteLine("ERROR! Expected varible=value pair.");
						return false;
					}
					//now we have a pair of strings... the second must be a number!
					if(!double.TryParse(keys[1], out val)){
						Console.WriteLine("ERROR! Value of variable {0} is not a number.",keys[0]);
						return false;
					}
					//now check if the variable was already declared
					if(CList.ContainsKey(keys[0])){
						Console.WriteLine("ERROR! Variable {0} was already declared.",keys[0]);
						return false;
					}
					
					CList.Add(keys[0],val);	
				}
				#endregion
			}
				
			return true;
		}