private void newToolStripMenuItem_Click(object sender, EventArgs e) { long head = -1; string name; SaveFileDialog newFile = new SaveFileDialog(); newFile.Title = "New File"; newFile.DefaultExt = "bin"; newFile.Filter = "Bin Files (*.bin)| *.bin"; newFile.AddExtension = true; if (newFile.ShowDialog() == DialogResult.OK) { toolStripStatusLabel1.Text = "File created at: " + newFile.FileName; file = new AuxFile(); name = newFile.FileName; file.fileName = name; file.stream = new FileStream(name, FileMode.Create); if (file.stream != null) { BinaryWriter binaryWriter = new BinaryWriter(file.stream); binaryWriter.Write(head); binaryWriter.Close(); } else { MessageBox.Show("The file cannot be created."); } } }
private void openToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog open = new OpenFileDialog(); open.Filter = "Bin Files (*.bin)| *.bin"; if (open.ShowDialog() == DialogResult.OK) { file = new AuxFile(); if (file.stream != null) { file.stream.Close(); } name = open.FileName; file.fileName = name; file.stream = new FileStream(name, FileMode.Open); BinaryReader reader = new BinaryReader(file.stream, Encoding.UTF8); if (file.stream.Length != 0)//Mientras no sea fin de archivo { file.stream.Position = 0; long head = reader.ReadInt64(); long pos = head; if (head != -1) //Si hay entidades { pos = head; //Declarar un auxiliar while (pos != -1) //Mientras exista minimo una entidad { file.stream.Position = pos; //Asignar la cabecera a la posicion del archivo Entity newEntity = new Entity(); //Carga entidad newEntity.name = reader.ReadChars(30); newEntity.entityDir = reader.ReadInt64(); newEntity.attributeDir = reader.ReadInt64(); newEntity.dataDir = reader.ReadInt64(); newEntity.nextDir = reader.ReadInt64(); string name = new string(newEntity.name); comboBox1.Items.Add(name); //Agrega la entidad creada al combobox para visualizar if (newEntity.attributeDir != -1) //si existe atributos en la entidad { Attribute newAtt = new Attribute(); //= CapturAt(nuevaEnt.DirAtrib);//Carga atributo file.stream.Position = newEntity.attributeDir; newAtt.name = reader.ReadChars(30); newAtt.type = reader.ReadChar(); newAtt.length = reader.ReadInt32(); newAtt.attributeDir = reader.ReadInt64(); newAtt.indexType = reader.ReadInt32(); newAtt.indexDir = reader.ReadInt64(); newAtt.nextAttDir = reader.ReadInt64(); newEntity.attributes.Add(newAtt); /*if (newAtt.indexType != -1) * { * * R.BaseStream.Seek(newAtt.indexDir, SeekOrigin.Begin); * if (newAtt.iIndice == 2) * { * for (int p = 0; p < 10; p++) * { * Mpk = new Pk(); * if (newAtt.cTipo == 'C') * { * auxPk = string.Empty; * NomPk = R.ReadChars(newAtt.iLongitud); * for (int i = 0; i < NomPk.Count(); i++) * { * auxPk += NomPk[i]; * } * Mpk.oClave = auxPk; * Mpk.lDireccion = R.ReadInt64(); * ent.ListPk.Add(Mpk); * } * else * { * auxPk = string.Empty; * Mpk.oClave = R.ReadInt32(); * Mpk.lDireccion = R.ReadInt64(); * ent.ListPk.Add(Mpk); * } * } * } * * if (newAtt.iIndice == 3) * { * for (int f = 0; f < 5; f++) * { * Mfk = new Fk(); * if (newAtt.cTipo == 'C') * { * auxFk = string.Empty; * NomFk = R.ReadChars(newAtt.iLongitud); * for (int i = 0; i < NomFk.Count(); i++) * { * auxFk += NomFk[i]; * } * Mfk.oClave = auxFk; * for (int d = 0; d < 10; d++) * { * Mfk.lDirecciones.Add(R.ReadInt64()); * } * ent.ListFk.Add(Mfk); * * } * else * { * Mfk.oClave = R.ReadInt32(); * for (int d = 0; d < 10; d++) * { * Mfk.lDirecciones.Add(R.ReadInt64()); * } * ent.ListFk.Add(Mfk); * } * } * } * if (newAtt.iIndice == 4) * { * Nodo n; * Nodo nuevo = LeeNodo(newAtt.lDirIndice); * ent.listNodos.Add(nuevo); * if (nuevo.cTipo == 'R') * { * for (int i = 0; i < nuevo.iDatos.Count + 1; i++) * { * n = LeeNodo(nuevo.lDirecciones[i]); * ent.listNodos.Add(n); * if (n.cTipo == 'I') * { * for (int j = 0; j < n.lDirecciones.Count; j++) * { * Nodo nh = LeeNodo(n.lDirecciones[j]); * ent.listNodos.Add(nh); * } * } * } * } * } * } * /*if (newAtt.lDirSigAtributo != -1) * { * R.BaseStream.Seek(newAtt.lDirSigAtributo, SeekOrigin.Begin); * }*/ while (newAtt.nextAttDir != -1) // Mientras exista otro atributo { Attribute aux = new Attribute(); file.stream.Position = newAtt.nextAttDir; aux.name = reader.ReadChars(30); aux.type = reader.ReadChar(); aux.length = reader.ReadInt32(); aux.attributeDir = reader.ReadInt64(); aux.indexType = reader.ReadInt32(); aux.indexDir = reader.ReadInt64(); aux.nextAttDir = reader.ReadInt64(); newAtt = aux; newEntity.attributes.Add(newAtt);//Agrega el siguiente atributo } } entities.Add(newEntity); //Agrega la entidad a memoria addDataAttribute(newEntity); pos = newEntity.nextDir; //Asigna a pos la dirreccion de la siguiente entidad } } else { MessageBox.Show("ARCHIVO VACIO"); } } else { MessageBox.Show("ARCHIVO VACIO"); } addData(); //Muestra los datos de la entidad en el data grid reader.Close(); //Cierra la lectura actual file.stream.Close(); //Cierra la secuencia actual toolStripStatusLabel1.Text = "File " + name + " opened."; } }