using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Fire.Container.Exception;
using System.IO;
namespace System.Fire.Container {
///
/// The FileInfoX object contains basic informations about a file
///
public struct FileInfoX {
private string _name;
private string _mime;
private string _extention;
///
/// Creates a new FileInfoX Opbject
///
/// The FileName
public FileInfoX(string FileName) {
this._name = FileName;
this._mime = System.Fire.Mime.getMimeType(FileName);
this._extention = "";
if (FileName.Contains(".") && FileName.LastIndexOf(".") > 0 && FileName.LastIndexOf(".") < FileName.Length) {
this._extention = FileName.Substring(FileName.LastIndexOf(".") + 1).ToUpper();
}
}
///
/// Get or set the file name
///
public string Name {
get {
return this._name;
}
set {
this._name = value;
}
}
///
/// Get or set the file mime
///
public string Mime {
get {
return this._mime;
}
set {
this._mime = value;
}
}
///
/// Get or set the file extention
///
public string Extention {
get {
return this._extention;
}
set {
this._extention = value;
}
}
}
public partial class ContainerList {
private FileStream InfoFile;
private BinaryReader InfoFileR;
private BinaryWriter InfoFileW;
private long FileInfo_Offset;
private FileInfoX ReadFileInfo(long pos) {
FileInfoX nfo = new FileInfoX();
this.InfoFile.Position = pos;
byte version = this.InfoFileR.ReadByte();
if (version == 1) {
byte nameLen = this.InfoFileR.ReadByte();
byte[] tmp_name = this.InfoFileR.ReadBytes(nameLen);
nfo.Name = System.Text.Encoding.UTF8.GetString(tmp_name);
byte mimeLen = this.InfoFileR.ReadByte();
byte[] tmp_mime = this.InfoFileR.ReadBytes(mimeLen);
nfo.Mime = System.Text.Encoding.UTF8.GetString(tmp_mime);
byte extLen = this.InfoFileR.ReadByte();
byte[] tmp_ext = this.InfoFileR.ReadBytes(extLen);
nfo.Extention = System.Text.Encoding.UTF8.GetString(tmp_ext);
}
return nfo;
}
private long WriteFileInfo(FileInfoX nfo) {
this.InfoFile.Seek(0, SeekOrigin.End);
long pos = InfoFile.Position;
byte version = 1;
this.InfoFileW.Write(version);
byte[] tmp_name = System.Text.Encoding.UTF8.GetBytes(nfo.Name);
this.InfoFileW.Write((byte)tmp_name.Length);
this.InfoFileW.Write(tmp_name);
byte[] tmp_mime = System.Text.Encoding.UTF8.GetBytes(nfo.Mime);
this.InfoFileW.Write((byte)tmp_mime.Length);
this.InfoFileW.Write(tmp_mime);
byte[] tmp_ext = System.Text.Encoding.UTF8.GetBytes(nfo.Extention);
this.InfoFileW.Write((byte)tmp_ext.Length);
this.InfoFileW.Write(tmp_ext);
return pos;
}
private void OpenFileInfo(string Path) {
bool bHasFileInfo = false;
if (System.IO.File.Exists(Path)) {
this.InfoFile = new FileStream(Path, FileMode.Open);
if (this.InfoFile.Length > 6) {
this.InfoFileR = new BinaryReader(this.InfoFile);
this.InfoFileW = new BinaryWriter(this.InfoFile);
byte[] Magic = this.InfoFileR.ReadBytes(4);
if (
Magic[0] == 67 /* C */ &&
Magic[1] == 111 /* o */ &&
Magic[2] == 70 /* F */ &&
Magic[3] == 73 /* I */
) {
ushort version = this.InfoFileR.ReadUInt16();
bHasFileInfo = true;
this.FileInfo_Offset = this.InfoFile.Position;
}
}
if (bHasFileInfo == false) {
this.InfoFile.Close();
File.Delete(Path);
}
}
if (!bHasFileInfo) {
this.InfoFile = new FileStream(Path, FileMode.CreateNew);
this.InfoFileR = new BinaryReader(this.InfoFile);
this.InfoFileW = new BinaryWriter(this.InfoFile);
this.InfoFileW.Write(new byte[] { 67, 111, 70, 73 });
this.InfoFileW.Write(Version);
this.FileInfo_Offset = this.InfoFile.Position;
}
}
private void CloseFileInfo() {
this.InfoFile.Close();
}
}
}