• Domingo 28 de Abril de 2024, 18:47

Autor Tema:  duda en editor de archivos ini  (Leído 1075 veces)

U2_Caparzo

  • Miembro activo
  • **
  • Mensajes: 45
  • Nacionalidad: cl
  • Super duper divertido xDD
    • Ver Perfil
duda en editor de archivos ini
« en: Jueves 12 de Abril de 2012, 04:02 »
0
Hola a todos, bueno, he estado tratando de aplicar un poco lo que he leido en unos tutoriales, y decidi hacer un editor de archivos ini entre otras cosas, pero tengo un problema: La velocidad, con 200 campos(campo=valor, a esa parte me refiero) tarda entre 400 y 900 milisegundos, lo cual encuentro que es demasiado.

esto es lo que hice(disculpen el que lo haga en ingles, pero me acostumbre)


Código: [Seleccionar]
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using IniHandler;

namespace IniHandler
{
    struct Item
    {
        public string Section;
        public string Field;
    }
}

namespace IniManager
{
    class IniManager
    {
        byte COUNT = 0;
        string SECTION, FILE;
        string[] SECTIONS = new string[255];
        Dictionary<Item, string> ITEMS;
        StreamReader READER;
        bool READING = true;

        public IniManager(string _FILE)
        {
            COUNT = 0;
            FILE = _FILE;
            ITEMS = new Dictionary<Item, string>();
            FILL();
            READING = true;
        }

        void FILL()
        {
            string[] Lines = File.ReadAllLines(FILE);
            foreach (string Line in Lines)
            {
                ANALYZELINE(Lines);
            }
            //for (ushort i = 0; i < LINES.Length; i++)
            //{
            //    ANALYZELINE(LINES[i]);
            //}
        }

        void ANALYZELINE(string LINE)
        {
            if (LINE.Length > 0)
            {
                if (LINE[0] == '[')
                {
                    SECTIONS[COUNT] = LINE;
                    COUNT++;
                    SECTION = LINE;
                }
                else
                {
                    Item i = new Item();
                    i.Section = SECTION;
                    string[] SPLIT = LINE.Split('=');
                    i.Field = SPLIT[0];
                    ITEMS.Add(i, SPLIT[1]);
                }
            }
            else
                return;
        }

        public Item GetItem(string Section, string Field)
        {
            Item i = new Item();
            i.Section = Section;
            i.Field = Field;
            return i;
        }

        public string ReadString(string Section, string Field)
        {
            string Value;
            Item I = new Item();
            I.Section = '[' + Section + ']';
            I.Field = Field;
            ITEMS.TryGetValue(I, out Value);
            return Value;
        }

        public void Write(string Section, string Field, object Value)
        {
            StringBuilder BUILDER = new StringBuilder();
            Item I = GetItem(Section, Field);
            if (!ITEMS.ContainsKey(I))
            {
                ITEMS.Add(I, Value.ToString());
            }
            else
            {
                ITEMS.Remove(I);
                ITEMS.Add(I, Value.ToString());
            }
            for (byte i = 0; i <= COUNT; i++)
            {
                string section = SECTIONS[i];
                BUILDER.AppendLine(section);
                foreach(KeyValuePair<Item, string> item in ITEMS)
                {
                    if (item.Key.Section != section)
                        continue;
                    else
                    {
                        BUILDER.AppendLine(item.Key.Field + '=' + item.Value);
                    }
                }
            }
            File.WriteAllText(FILE, BUILDER.ToString());
        }
    }
}



ojala puedan ayudarme a mejorar esto, ya que obviamente debe ser una pesima forma por la cantidad de tiempo que toma, y que es exactamente lo que falla, tengo una idea pero la verdad no se si lo es, que seria el crear masivamente objetos a partir de la estructura "Item"

Gracias de antemano
« última modificación: Jueves 12 de Abril de 2012, 18:12 por ProfesorX »
Lo dificil se hace... lo imposible se intenta
Todos somos muy ignorantes. Lo que ocurre es que no todos ignoramos las mismas cosas.(Frase de Albert Einstein)