예제 #1
0
        public void TestListFind()
        {
            CList <int> l1 = new CList <int>();

            for (int i = 0; i <= 9; i++)
            {
                l1.Append(i);
            }
            Assert.AreEqual(l1.Find(12), -1);
            Assert.AreEqual(l1.Find(1), 1);
            Assert.AreEqual(l1.Find(8), 8);
        }
예제 #2
0
        //reads from an arbitrary file, and adds the circuits either to the program list or to the composites subcircuits
        public static bool ReadCircuits(string FileName, Circuit owner)
        {
			List<Circuit> CList;
            string[] words;
			string line = "";
            char[] delimiterChars = { ' ', ',', '\t' };
			Type CircType;
			Circuit circ;
			
			
			if(owner == null)
				CList = Program.Circuits; //if we are reading the main input file
			else
				CList = ((composite)owner).SubCircuits; //if we are reading a composite setup
			
            Console.WriteLine("\n   Reading circuits from {0} (composite?{1}):", FileName,owner!=null);

            StreamReader reader = new StreamReader(FileName);
            if (!StringReader.FindString("<circuits>", reader)){
                Console.WriteLine("FATAL! Circuit list is missing.");
                return false;
            }
			
            Console.WriteLine("   Reading circuit list:\n");

			//check if there is a main timer (but not for composites)
			#region "get main timer"
			if(owner == null){
				if(!StringReader.FindStringNoEnd("timer", reader, "<circuits>", ref line, FileName) ){
					Console.WriteLine("FATAL! The main timer circuit is not present.");
					return false;
				}
				line = line.Trim();
				words = StringReader.TrimWords(line.Split(delimiterChars));//read the type and the name
				words[1]="time"; //force it to be called time
				mytimer = (timer)Activator.CreateInstance(myCircuitTypes[words[0]], new object[]{words});
				mytimer.SetUp();
				CList.Insert(0, mytimer ); //ACHTUNG! timer goes first!
				reader.Dispose();
				reader = new StreamReader(FileName);
				StringReader.FindString("<circuits>", reader);
			}
			#endregion
			
            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 || line.StartsWith("timer") ) //ignore empty lines/comments/timer
                    continue;
                if (words[0].StartsWith("#"))
                    continue;
				if (words.Length < 2){
					Console.WriteLine("FATAL! Two fields are always required!");
					return false;
				}
				#endregion
				
                #region "check restricted names"
                if ( (words[1] == "time") && (words[0] != "timer") ){
                    Console.WriteLine("FATAL! Only a timer circuit can be named 'timer'.");
                    return false;
                }
                if (words[1] == "scanner"){
                    Console.WriteLine("FATAL! Name 'scanner' is reserved for the scanner controller.");
                    return false;
                }
                if (words[1] == "cantilever"){
                    Console.WriteLine("FATAL! Name 'cantilever' is reserved for the cantilever.");
                    return false;
                }
                #endregion
                
				#region "check if name/type are good"
				//check if present in the circuit types list
				if(!myCircuitTypes.ContainsKey(words[0])){
					Console.WriteLine("ERROR! Circuit type {0} does not exist.",words[0]);
					return false;
				}
				
				//check if the name is already used
				if(CList.Find(c => c.Name == words[1]) != null){
					Console.WriteLine("ERROR! A circuit named {0} was already initialized.",words[1]);
					return false;
				}
				#endregion
								
				CircType = myCircuitTypes[words[0]]; //get the circuit type
				if(owner == null){
					words[0] = "program";
				}else{
					words[0] = owner.Name;
				}
				
				#region "check that there is only one scripter"
				if(CircType == typeof(Scripter)){
					for(int i=0; i<CList.Count; i++){
						if((CList[i].GetType() == typeof(Scripter)) || (CList[i].GetType() == typeof(MainScanner))){
							Console.WriteLine("ERROR! Only one scripter is allowed!");
							return false;
						}
					}
				}
				#endregion
				
				//Console.WriteLine("INPUT READER: w0 {0}",words[0]);
				circ = (Circuit)Activator.CreateInstance(CircType, new object[]{words});
				circ.Owner = owner; // set the owner
				circ.SetUp(); //do the setup routine
				CList.Add( circ ); //add the circuit to the list
				
				if(CircType == typeof(composite))
					((composite)circ).composite_Init();
				
				if(CircType == typeof(Scripter)){
					if(!(((Scripter)circ).ReadScript(FileName)))
						return false;
				}
				
            }
			
			//add all the output feeds of the circuit to a global list of feeds. ONLY FOR COMPOSITES
			if(owner != null)
				foreach(Circuit listedcirc in CList)
					foreach(Channel outchan in listedcirc.Output)
						Program.AllFeeds.Add(outchan.Signal);
			
            Console.WriteLine("---Circuits Read.\n");
            reader.Dispose();
            return true;
        }