Exemplo n.º 1
0
 //**********************************************************************
 //								CSVCompare()
 //
 //		Compare a field to a search value using a particular criteria.
 //**********************************************************************
 static bool CSVCompare(string fieldValue, string target, CSVCompareCriteria criteria)
 {
     if (criteria == CSVCompareCriteria.CC_ExactString)
     {
         return(fieldValue == target);
     }
     else if (criteria == CSVCompareCriteria.CC_ApproxString)
     {
         return(fieldValue.ToLower() == target.ToLower());
     }
     else if (criteria == CSVCompareCriteria.CC_Integer)
     {
         return(atoi(fieldValue) == atoi(target));
     }
     return(false);
 }
Exemplo n.º 2
0
        //**********************************************************************
        //							CSVGetField()
        //
        //		The all-in-one function to fetch a particular field value
        //		from a CSV file. Note this function will return an empty
        //		string, rather than NULL if it fails to find the desired
        //		value for some reason. The caller can't establish that the
        //		fetch failed.
        //**********************************************************************
        public static string CSVGetField(string filename, string keyFieldName, string keyFieldValue,
                                         CSVCompareCriteria criteria, string targetField)
        {
            // --------------------------------------------------------------------
            //		Find the table.
            // --------------------------------------------------------------------
            CSVTable table = CSVAccess(filename);

            if (table == null)
            {
                return("");
            }

            // --------------------------------------------------------------------
            //		Find the correct record.
            // --------------------------------------------------------------------
            string[] record = CSVScanFileByName(filename, keyFieldName, keyFieldValue, criteria);
            if (record == null)
            {
                return("");
            }

            // --------------------------------------------------------------------
            //		Figure out which field we want out of this.
            // --------------------------------------------------------------------
            int iTargetField = CSVGetFileFieldId(filename, targetField);

            if (iTargetField < 0)
            {
                return("");
            }

            if (iTargetField >= record.Length)
            {
                return("");
            }

            return(record[iTargetField]);
        }
Exemplo n.º 3
0
		//**********************************************************************
		//							CSVGetField()
		//
		//		The all-in-one function to fetch a particular field value
		//		from a CSV file. Note this function will return an empty
		//		string, rather than NULL if it fails to find the desired
		//		value for some reason. The caller can't establish that the
		//		fetch failed.
		//**********************************************************************
		public static string CSVGetField(string filename, string keyFieldName, string keyFieldValue,
			CSVCompareCriteria criteria, string targetField)
		{
			// --------------------------------------------------------------------
			//		Find the table.
			// --------------------------------------------------------------------
			CSVTable table=CSVAccess(filename);
			if(table==null) return "";

			// --------------------------------------------------------------------
			//		Find the correct record.
			// --------------------------------------------------------------------
			string[] record=CSVScanFileByName(filename, keyFieldName, keyFieldValue, criteria);
			if(record==null) return "";

			// --------------------------------------------------------------------
			//		Figure out which field we want out of this.
			// --------------------------------------------------------------------
			int iTargetField=CSVGetFileFieldId(filename, targetField);
			if(iTargetField<0) return "";

			if(iTargetField>=record.Length) return "";

			return record[iTargetField];
		}
Exemplo n.º 4
0
		//**********************************************************************
		//							CSVScanFileByName()
		//
		//		Same as CSVScanFile(), but using a field name instead of a
		//		field number.
		//**********************************************************************
		public static string[] CSVScanFileByName(string filename, string keyFieldName, string value, CSVCompareCriteria criteria)
		{
			int keyField=CSVGetFileFieldId(filename, keyFieldName);
			if(keyField==-1) return null;

			return CSVScanFile(filename, keyField, value, criteria);
		}
Exemplo n.º 5
0
		//**********************************************************************
		//							CSVScanFile()
		//
		//		Scan a whole file using criteria similar to above, but also
		//		taking care of file opening and closing.
		//**********************************************************************
		public static string[] CSVScanFile(string filename, int keyField, string value, CSVCompareCriteria criteria)
		{
			// --------------------------------------------------------------------
			//		Get access to the table.
			// --------------------------------------------------------------------
			if(keyField<0) return null;

			CSVTable table=CSVAccess(filename);
			if(table==null) return null;

			CSVIngest(filename);

			// --------------------------------------------------------------------
			//		Does the current record match the criteria? If so, just
			//		return it again.
			// --------------------------------------------------------------------
			if(keyField>=0&&keyField<table.recFields.Length&&CSVCompare(value, table.recFields[keyField], criteria))
				return table.recFields;

			// --------------------------------------------------------------------
			//		Scan the file from the beginning, replacing the "current
			//		record" in our structure with the one that is found.
			// --------------------------------------------------------------------
			if(table.cache!=null) table.recFields=CSVScanLinesIngested(table, keyField, value, criteria);
			else
			{
				table.file.BaseStream.Seek(0, SeekOrigin.Begin);
				CSVReadParseLine(table.file); // throw away the header line

				table.recFields=CSVScanLines(table.file, keyField, value, criteria);
			}

			return table.recFields;
		}
Exemplo n.º 6
0
		//**********************************************************************
		//						CSVScanLinesIngested()
		//
		//		Read the file scanline for lines where the key field equals
		//		the indicated value with the suggested comparison criteria.
		//		Return the first matching line split into fields.
		//**********************************************************************
		static string[] CSVScanLinesIngested(CSVTable table, int keyField, string value, CSVCompareCriteria criteria)
		{
			int nTestValue=0;
			if(criteria==CSVCompareCriteria.CC_Integer) nTestValue=atoi(value);

			// --------------------------------------------------------------------
			//		Short cut for indexed files.
			// --------------------------------------------------------------------
			if(keyField==0&&criteria==CSVCompareCriteria.CC_Integer&&table.index!=null)
			{
				if(table.index.ContainsKey(nTestValue)) return table.index[nTestValue];
				return null;
			}

			// --------------------------------------------------------------------
			//		Scan from in-core lines.
			// --------------------------------------------------------------------
			foreach(string[] line in table.cache)
			{
				if(line.Length<keyField+1) continue; // not selected

				bool selected=false;
				if(criteria==CSVCompareCriteria.CC_Integer&&atoi(line[keyField])==nTestValue)
					selected=true;
				else selected=CSVCompare(line[keyField], value, criteria);

				if(selected) return line;
			}

			return null;
		}
Exemplo n.º 7
0
		//**********************************************************************
		//								CSVScanLines()
		//
		//		Read the file scanline for lines where the key field equals
		//		the indicated value with the suggested comparison criteria.
		//		Return the first matching line split into fields.
		//**********************************************************************
		public static string[] CSVScanLines(StreamReader file, int keyField, string value, CSVCompareCriteria criteria)
		{
			int testValue=atoi(value);
			for(;;)
			{
				string[] fields=CSVReadParseLine(file);
				if(fields==null) return null;
				if(fields.Length<keyField+1) continue; // not selected

				bool selected=false;
				if(criteria==CSVCompareCriteria.CC_Integer&&atoi(fields[keyField])==testValue)
					selected=true;
				else selected=CSVCompare(fields[keyField], value, criteria);

				if(selected) return fields;
			}
		}
Exemplo n.º 8
0
		//**********************************************************************
		//								CSVCompare()
		//
		//		Compare a field to a search value using a particular criteria.
		//**********************************************************************
		static bool CSVCompare(string fieldValue, string target, CSVCompareCriteria criteria)
		{
			if(criteria==CSVCompareCriteria.CC_ExactString) return fieldValue==target;
			else if(criteria==CSVCompareCriteria.CC_ApproxString) return fieldValue.ToLower()==target.ToLower();
			else if(criteria==CSVCompareCriteria.CC_Integer) return atoi(fieldValue)==atoi(target);
			return false;
		}
Exemplo n.º 9
0
        //**********************************************************************
        //							CSVScanFileByName()
        //
        //		Same as CSVScanFile(), but using a field name instead of a
        //		field number.
        //**********************************************************************
        public static string[] CSVScanFileByName(string filename, string keyFieldName, string value, CSVCompareCriteria criteria)
        {
            int keyField = CSVGetFileFieldId(filename, keyFieldName);

            if (keyField == -1)
            {
                return(null);
            }

            return(CSVScanFile(filename, keyField, value, criteria));
        }
Exemplo n.º 10
0
        //**********************************************************************
        //							CSVScanFile()
        //
        //		Scan a whole file using criteria similar to above, but also
        //		taking care of file opening and closing.
        //**********************************************************************
        public static string[] CSVScanFile(string filename, int keyField, string value, CSVCompareCriteria criteria)
        {
            // --------------------------------------------------------------------
            //		Get access to the table.
            // --------------------------------------------------------------------
            if (keyField < 0)
            {
                return(null);
            }

            CSVTable table = CSVAccess(filename);

            if (table == null)
            {
                return(null);
            }

            CSVIngest(filename);

            // --------------------------------------------------------------------
            //		Does the current record match the criteria? If so, just
            //		return it again.
            // --------------------------------------------------------------------
            if (keyField >= 0 && keyField < table.recFields.Length && CSVCompare(value, table.recFields[keyField], criteria))
            {
                return(table.recFields);
            }

            // --------------------------------------------------------------------
            //		Scan the file from the beginning, replacing the "current
            //		record" in our structure with the one that is found.
            // --------------------------------------------------------------------
            if (table.cache != null)
            {
                table.recFields = CSVScanLinesIngested(table, keyField, value, criteria);
            }
            else
            {
                table.file.BaseStream.Seek(0, SeekOrigin.Begin);
                CSVReadParseLine(table.file);                 // throw away the header line

                table.recFields = CSVScanLines(table.file, keyField, value, criteria);
            }

            return(table.recFields);
        }
Exemplo n.º 11
0
        //**********************************************************************
        //						CSVScanLinesIngested()
        //
        //		Read the file scanline for lines where the key field equals
        //		the indicated value with the suggested comparison criteria.
        //		Return the first matching line split into fields.
        //**********************************************************************
        static string[] CSVScanLinesIngested(CSVTable table, int keyField, string value, CSVCompareCriteria criteria)
        {
            int nTestValue = 0;

            if (criteria == CSVCompareCriteria.CC_Integer)
            {
                nTestValue = atoi(value);
            }

            // --------------------------------------------------------------------
            //		Short cut for indexed files.
            // --------------------------------------------------------------------
            if (keyField == 0 && criteria == CSVCompareCriteria.CC_Integer && table.index != null)
            {
                if (table.index.ContainsKey(nTestValue))
                {
                    return(table.index[nTestValue]);
                }
                return(null);
            }

            // --------------------------------------------------------------------
            //		Scan from in-core lines.
            // --------------------------------------------------------------------
            foreach (string[] line in table.cache)
            {
                if (line.Length < keyField + 1)
                {
                    continue;                                        // not selected
                }
                bool selected = false;
                if (criteria == CSVCompareCriteria.CC_Integer && atoi(line[keyField]) == nTestValue)
                {
                    selected = true;
                }
                else
                {
                    selected = CSVCompare(line[keyField], value, criteria);
                }

                if (selected)
                {
                    return(line);
                }
            }

            return(null);
        }
Exemplo n.º 12
0
        //**********************************************************************
        //								CSVScanLines()
        //
        //		Read the file scanline for lines where the key field equals
        //		the indicated value with the suggested comparison criteria.
        //		Return the first matching line split into fields.
        //**********************************************************************
        public static string[] CSVScanLines(StreamReader file, int keyField, string value, CSVCompareCriteria criteria)
        {
            int testValue = atoi(value);

            for (;;)
            {
                string[] fields = CSVReadParseLine(file);
                if (fields == null)
                {
                    return(null);
                }
                if (fields.Length < keyField + 1)
                {
                    continue;                                          // not selected
                }
                bool selected = false;
                if (criteria == CSVCompareCriteria.CC_Integer && atoi(fields[keyField]) == testValue)
                {
                    selected = true;
                }
                else
                {
                    selected = CSVCompare(fields[keyField], value, criteria);
                }

                if (selected)
                {
                    return(fields);
                }
            }
        }