示例#1
0
        private void loadINI()
        {
            iniContent.Clear();
            StreamReader sr = null;

            try { sr = new StreamReader(File.OpenRead(fileName), encoding); } catch { }
            INILine.setSection = "";
            if (sr != null)
            {
                while (!sr.EndOfStream)
                {
                    INILine        line = new INILine(sr.ReadLine());
                    INIVariableDef vd   = getVarDefn(line.section, line.key);
                    line.defVal    = vd.defValue;
                    line.varType   = vd.type;
                    line.varDict   = vd.dict;
                    line.varBool   = vd.boolType;
                    line.useMinMax = vd.useMinMax;
                    line.max       = vd.max;
                    line.min       = vd.min;
                    iniContent.Add(line);
                }
                sr.Close();
            }
        }
示例#2
0
 public INIFile(string file, INIVariableDef[] varDefn, bool saveDef)
 {
     this.fileName = file;
     this.varDefn = varDefn;
     this.saveDef = saveDef;
     this.iniContent = new List<INILine>();
     loadINI();
 }
示例#3
0
        private INIVariableDef getVarDefn(string variable)
        {
            INIVariableDef ret = varDefn[0];

            foreach (INIVariableDef defn in varDefn)
            {
                if (defn.var != null && variable.CompareTo(defn.var) == 0)
                {
                    return(defn);
                }
            }
            return(varDefn[0]);
        }
示例#4
0
        private void loadINI()
        {
            iniContent.Clear();

            // Explicitly test for NUL instead of catching exceptions
            if (string.Equals(fileName, "NUL", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            // Open file and check for unwanted UTF-8 BOM
            try {
                using (var f = File.OpenRead(fileName)) {
                    byte[] preamble = new byte[3];
                    f.Read(preamble, 0, 3);

                    // Mark for resaving without BOM if present, seek back to start if BOM not present
                    if (preamble[0] == 0xEF && preamble[1] == 0xBB && preamble[2] == 0xBF)
                    {
                        modified = true;
                    }
                    else
                    {
                        f.Seek(0, SeekOrigin.Begin);
                    }

                    // Switch to text stream
                    using (var sr = new StreamReader(f, encoding)) {
                        INILine.setSection = "";

                        while (!sr.EndOfStream)
                        {
                            INILine        line = new INILine(sr.ReadLine());
                            INIVariableDef vd   = getVarDefn(line.section, line.key);
                            line.defVal    = vd.defValue;
                            line.varType   = vd.type;
                            line.varDict   = vd.dict;
                            line.varBool   = vd.boolType;
                            line.useMinMax = vd.useMinMax;
                            line.max       = vd.max;
                            line.min       = vd.min;
                            iniContent.Add(line);
                        }
                    }
                }
            } catch (FileNotFoundException) {
                // Assume generating new INI
            }
        }
示例#5
0
        private bool isDefault(string section, string key, string sValue)
        {
            INIVariableDef vd     = getVarDefn(section, key);
            string         iValue = sValue.ToLower(Statics.Culture);

            if (vd.defValue != null)
            {
                string def = vd.defValue.ToLower(Statics.Culture);
                if (iValue == def)
                {
                    return(true);
                }
                bool   useDict = false;
                double dVal = double.NegativeInfinity, dDef = double.PositiveInfinity;
                if (vd.dict != null)
                {
                    foreach (KeyValuePair <string, double> dict in vd.dict)
                    {
                        if (dict.Key.ToLower(Statics.Culture) == iValue)
                        {
                            dVal    = dict.Value;
                            useDict = true;
                            break;
                        }
                    }
                    if (useDict)
                    {
                        foreach (KeyValuePair <string, double> dict in vd.dict)
                        {
                            if (dict.Key.ToLower(Statics.Culture) == def)
                            {
                                dDef = dict.Value;
                                break;
                            }
                        }
                    }
                    if (dVal == dDef)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
示例#6
0
        private void loadINI()
        {
            iniContent.Clear();

            // Open file and check for unwanted UTF-8 BOM
            var f = File.OpenRead(fileName);

            byte[] preamble = new byte[3];
            f.Read(preamble, 0, 3);

            // Mark for resaving without BOM if present, seek back to start if BOM not present
            if (preamble[0] == 0xEF && preamble[1] == 0xBB && preamble[2] == 0xBF)
            {
                modified = true;
            }
            else
            {
                f.Seek(0, SeekOrigin.Begin);
            }

            // Switch to text stream
            var sr = new StreamReader(f, encoding);

            INILine.setSection = "";

            if (sr != null)
            {
                while (!sr.EndOfStream)
                {
                    INILine        line = new INILine(sr.ReadLine());
                    INIVariableDef vd   = getVarDefn(line.section, line.key);
                    line.defVal    = vd.defValue;
                    line.varType   = vd.type;
                    line.varDict   = vd.dict;
                    line.varBool   = vd.boolType;
                    line.useMinMax = vd.useMinMax;
                    line.max       = vd.max;
                    line.min       = vd.min;
                    iniContent.Add(line);
                }
                sr.Close();
            }
        }
示例#7
0
        private bool isDefault(string section, string key, double dValue)
        {
            INIVariableDef vd = getVarDefn(section, key);

            if (vd.defValue != null)
            {
                string def     = vd.defValue.ToLower(Statics.Culture);
                bool   useDict = false;
                double dDef    = double.NaN;
                if (vd.dict != null)
                {
                    foreach (KeyValuePair <string, double> dict in vd.dict)
                    {
                        if (dict.Key.ToLower(Statics.Culture) == def)
                        {
                            dDef    = dict.Value;
                            useDict = true;
                            break;
                        }
                    }
                    if (useDict && dValue == dDef)
                    {
                        return(true);
                    }
                }
                try {
                    // dDef = Convert.ToDouble(def);
                    dDef = double.Parse(def, Statics.NumFormat);
                    if (dValue == dDef)
                    {
                        return(true);
                    }
                } catch {
                }
            }
            return(false);
        }
示例#8
0
 public INIFile(string file, INIVariableDef[] varDefn)
     : this(file, varDefn, false)
 {
 }
示例#9
0
        public bool isKeySet(string variable)
        {
            INIVariableDef vd = getVarDefn(variable);

            return(isKeySet(vd.section, vd.key));
        }
示例#10
0
 public INIFile(string file, INIVariableDef[] varDefn, Encoding encoding, bool saveDef)
     : base(file, varDefn, encoding, saveDef)
 {
 }
示例#11
0
 public INIFile(string file, INIVariableDef[] varDefn, Encoding encoding)
     : this(file, varDefn, encoding, false)
 {
 }
示例#12
0
 public INIFile(string file, INIVariableDef[] varDefn, Encoding encoding)
     : base(file, varDefn, encoding)
 {
 }
示例#13
0
        private void keySet(string section, string key, bool useString, string sValue, double dValue)
        {
            List <string> sections = new List <string>();
            List <string> keys     = new List <string>();
            List <string> comments = new List <string>();

            foreach (INILine line in iniContent)
            {
                sections.Add(line.section);
                keys.Add(line.iKey);
                comments.Add(line.comment);
            }
            bool           isDef      = useString ? isDefault(section, key, sValue) : isDefault(section, key, dValue);
            INIVariableDef vd         = getVarDefn(section, key);
            string         iSection   = section.ToLower(Statics.Culture);
            string         iKey       = key.ToLower(Statics.Culture);
            int            firstIndex = sections.IndexOf(iSection);
            int            lastIndex  = sections.LastIndexOf(iSection);

            INILine.setSection = section;
            INILine tmp = new INILine(key + "=");

            tmp.defVal    = vd.defValue;
            tmp.varType   = vd.type;
            tmp.varDict   = vd.dict;
            tmp.varBool   = vd.boolType;
            tmp.useMinMax = vd.useMinMax;
            tmp.min       = vd.min;
            tmp.max       = vd.max;
            if (useString)
            {
                tmp.sValue = sValue;
            }
            else
            {
                if (vd.precision != -1)
                {
                    dValue = double.Parse(dValue.ToString("F" + vd.precision.ToString()));
                }
                tmp.dValue = dValue;
            }
            if (firstIndex == -1)
            {
                if (!saveDef && isDef)
                {
                    return;
                }
                if (iniContent.Count > 0)
                {
                    INILine line = iniContent[iniContent.Count - 1];
                    if (line.entry != "" || line.comment != "")
                    {
                        INILine.setSection = line.section;
                        iniContent.Add(new INILine(""));
                        firstIndex++;
                    }
                }
                INILine.setSection = section;
                iniContent.Add(new INILine("[" + section + "]"));
                iniContent.Add(tmp);
                iniContent.Add(new INILine(""));
            }
            else
            {
                int index = keys.IndexOf(iKey, firstIndex, lastIndex - firstIndex);
                if (index >= firstIndex && index <= lastIndex)
                {
                    if (!saveDef && isDef && comments[index] == "")
                    {
                        iniContent.RemoveAt(index);
                    }
                    else if (useString && iniContent[index].value == sValue || !useString && iniContent[index].dValue == dValue)
                    {
                        return;
                    }
                    else if (useString)
                    {
                        iniContent[index].sValue = sValue;
                    }
                    else
                    {
                        iniContent[index].dValue = dValue;
                    }
                }
                else
                {
                    if (!saveDef && isDef)
                    {
                        return;
                    }
                    INILine line = iniContent[lastIndex++];
                    if (line.entry != "" || line.comment != "")
                    {
                        iniContent.Insert(lastIndex, new INILine(""));
                        iniContent.Insert(lastIndex, tmp);
                    }
                    else
                    {
                        iniContent.Insert(lastIndex - 1, tmp);
                    }
                }
            }
            modified = true;
        }
示例#14
0
 public INIFile(string file, INIVariableDef[] varDefn)
     : base(file, varDefn)
 {
 }
示例#15
0
        public void setKey(string variable, string sValue)
        {
            INIVariableDef vd = getVarDefn(variable);

            keySet(vd.section, vd.key, true, sValue, double.NaN);
        }
示例#16
0
        public string getKeyString(string variable)
        {
            INIVariableDef vd = getVarDefn(variable);

            return(getKeyString(vd.section, vd.key));
        }
示例#17
0
 public INIFile(INIVariableDef[] varDefn)
 {
     this.fileName = "";
     this.varDefn = varDefn;
     this.saveDef = false;
     this.iniContent = new List<INILine>();
     INILine.setSection = "";
     foreach(INIVariableDef vd in varDefn) {
         if (vd.section == null) continue;
         INILine line = new INILine("");
         line.section = vd.section.ToLower();
         line.key = vd.key;
         line.value = vd.defValue;
         line.defVal = vd.defValue;
         line.varType = vd.type;
         line.varDict = vd.dict;
         line.varBool = vd.boolType;
         line.useMinMax = vd.useMinMax;
         line.max = vd.max;
         line.min = vd.min;
         iniContent.Add(line);
     }
 }
示例#18
0
        public double getKeyValue(string variable)
        {
            INIVariableDef vd = getVarDefn(variable);

            return(getKeyValue(vd.section, vd.key));
        }
示例#19
0
        public void setKey(string variable, bool bValue)
        {
            INIVariableDef vd = getVarDefn(variable);

            keySet(vd.section, vd.key, false, null, bValue ? 1 : 0);
        }
示例#20
0
        public void setKey(string variable, double dValue)
        {
            INIVariableDef vd = getVarDefn(variable);

            keySet(vd.section, vd.key, false, null, dValue);
        }
示例#21
0
 public INIFile(string file, INIVariableDef[] varDefn, bool saveDef)
     : base(file, varDefn, saveDef)
 {
 }