Exemple #1
0
 public INIForm(String file, String section, INIEntry entry, String Default)
 {
     _entry   = entry;
     _Default = Default;
     InitializeComponent();
     this.label1.Text   = file;
     this.label2.Text   = "[" + section + "]";
     this.label3.Text   = entry.Name + " =";
     this.textBox1.Text = Default;
     this.label4.Text   = "The value in the INI below has not been found\nPlease enter a good value or accept the default.";
 }
Exemple #2
0
        internal void OpenFile(string file)
        {
            long file_length;

            //this.file = file;
            if (system_base_file == null)
            {
                if (!System.IO.Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "/xperdex"))
                {
                    System.IO.Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "/xperdex");
                }
                system_base_file = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "/xperdex/xperdex.ini";
            }

            if (file == null)
            {
                file = system_base_file;
            }
            this.file = file;

            // if second character is a ":"  ... c:...
            // if first character is a / or \ it's an absolue
            // path, and should not be used...
            if ((file[1] != ':') &&
                (file[0] != '/') &&
                (file[0] != '\\'))
            {
                // dont' use the path if it's not set (otherwise get leaing slash filename)
                if (system_base_path != null)
                {
                    file = system_base_path + "/" + file;
                }
                this.file = file;
            }

            byte[]     data;
            FileStream fs = null;

            try
            {
                if (System.IO.File.Exists(file))
                {
                    fs = new FileStream(file, FileMode.Open);
                }
                else
                {
                    filename = file;                     // save this so we can write it back out later...
                    crypt    = false;
                    fs       = new FileStream(file, FileMode.OpenOrCreate);
                }
            }
            catch (FileNotFoundException)
            {
                filename = file;                 // save this so we can write it back out later...
                crypt    = false;
                fs       = new FileStream(file, FileMode.OpenOrCreate);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                crypt = true;
                return;
            }
            loading     = true;
            filename    = file;          // save this so we can write it back out later...
            file_length = fs.Length;
            data        = new byte[file_length];
            BinaryReader br = new BinaryReader(fs);

            byte[] buffer = new byte[file_length];
            br.Read(buffer, 0, Convert.ToInt32(file_length));
            if (buffer.Length > 0)
            {
                if (buffer[0] != '[')
                {
                    crypt = true;
                    for (int i = 0; i < file_length; i++)
                    {
                        data[i] = (byte)((buffer[i] - 20) & 0xFF);
                    }
                }
                else
                {
                    crypt = false;
                    for (int i = 0; i < file_length; i++)
                    {
                        data[i] = buffer[i];
                    }
                }

                {
                    INISection   current_section = null;
                    INIEntry     current_entry   = null;
                    StringWriter sr = new StringWriter();

                    int        ofs;
                    read_state state = read_state.find_section_entry;
                    for (ofs = 0; ofs < data.Length; ofs++)
                    {
                        switch (data[ofs])
                        {
                        case (byte)'[':
                            if (state == read_state.find_section_entry)
                            {
                                state = read_state.read_section;
                            }
                            break;

                        case (byte)']':
                            current_section = new INISection(this, sr.ToString());
                            sr.GetStringBuilder().Length = 0;
                            Add(current_section);
                            state = read_state.find_section_entry;
                            break;

                        case (byte)'\r':
                            break;

                        case (byte)'\n':
                            if (state == read_state.find_value)
                            {
                                state = read_state.find_section_entry;
                                sr.GetStringBuilder().Length = 0;
                            }
                            else if (state == read_state.read_Value)
                            {
                                current_entry.Value = sr.ToString();
                                sr.GetStringBuilder().Length = 0;
                                state = read_state.find_section_entry;
                            }
                            // else we ignore it - cause we're in find_section? or find other?
                            break;

                        case (byte)'=':
                            if (state == read_state.find_value)
                            {
                                if (current_section != null)
                                {
                                    StringBuilder sb = sr.GetStringBuilder();
                                    while (sb.ToString().Substring(sb.Length - 1, 1) == " ")
                                    {
                                        sb.Length--;
                                    }
                                    current_entry = new INIEntry(current_section, sr.ToString());
                                    sr.GetStringBuilder().Length = 0;
                                    current_section.Add(current_entry);
                                    state = read_state.read_Value;
                                }
                                else
                                {
                                    Console.WriteLine("= found while not in a section!");
                                }
                            }
                            else if (state == read_state.read_Value)
                            {
                                sr.Write(Convert.ToChar(data[ofs]));
                            }
                            break;

                        case (byte)' ':
                        case (byte)'\t':
                            if (state == read_state.find_section_entry)
                            {
                                continue;                                 // ignore spaces if not collecting something useful.
                            }
                            sr.Write(Convert.ToChar(data[ofs]));
                            break;

                        default:
                            switch (state)
                            {
                            case read_state.find_value:
                            case read_state.read_section:
                            case read_state.read_Value:
                                sr.Write(Convert.ToChar(data[ofs]));
                                break;

                            case read_state.find_section_entry:
                                state = read_state.find_value;
                                sr.Write(Convert.ToChar(data[ofs]));
                                break;
                            }
                            break;
                        }
                    }
                }
            }
            fs.Close();
            br.Close();

            if (file == system_base_file && system_base_path == null)
            {
                system_base_path = this["System"]["INI Path"];
                if (system_base_path == null || system_base_path == "")
                {
                    system_base_path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "/xperdex";
                    this["System"]["INI Path"].Value = system_base_path;
                }
            }
            loading = false;
        }
Exemple #3
0
 internal INITreeNode(INIEntry e)
 {
     this.e    = e;
     this.Text = this.ToString();
 }