⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unaclasses.pas

📁 Voice Commnucation Components for Delphi
💻 PAS
📖 第 1 页 / 共 5 页
字号:
    <BR>NOTE: This class is abstract, do not create instances of it.
  }
  unaIniAbstractStorage = class(unaObject)
  private
    f_section: string;
    //
    f_gate: unaInProcessGate;
    f_lockTimeout: unsigned;
    //
    function getUnsigned(const key: string; defValue: unsigned = 0): unsigned; overload;
    function getInt(const key: string; defValue: int = 0): int; overload;
    function getInt64(const key: string; defValue: int64 = 0): int64; overload;
    function getBool(const key: string; defValue: bool = false): bool; overload;
    //
    function getUnsigned(const section, key: string; defValue: unsigned = 0): unsigned; overload;
    function getInt(const section, key: string; defValue: int = 0): int; overload;
    function getInt64(const section, key: string; defValue: int64 = 0): int64; overload;
    function getBool(const section, key: string; defValue: bool = false): bool; overload;
    function getStringSectionKey(const section, key: string; const defValue: string = ''): string; overload;
    //
    function getSection(): string;
  protected
    function getStringValue(const key: string; const defValue: string = ''): string; virtual; abstract;
    procedure setStringValue(const key: string; const value: string); virtual; abstract;
    //
    function getAsString(): string; virtual; abstract;
    procedure setAsString(const value: string); virtual; abstract;
    //
    function doGetSectionAsText(const sectionName: string): string; virtual; abstract;
    function doSetSectionAsText(const sectionName, value: string): bool; virtual; abstract;
  public
    constructor create(const section: string = 'settings'; lockTimeout: unsigned = 1000);
    procedure AfterConstruction(); override;
    procedure BeforeDestruction(); override;
    //
    procedure setSection(const section: string);
    //
    function get(const key: string; defValue: int = 0): int; overload;
    function get(const key: string; defValue: int64 = 0): int64; overload;
    function get(const key: string; defValue: unsigned = 0): unsigned; overload;
    function get(const key: string; defValue: boolean = false): bool; overload;
    function get(const key: string; const defValue: string = ''): string; overload;
    // -- routines for C++Builder
    function get_int(const key: string; defValue: int = 0): int;
    function get_int64(const key: string; defValue: int64 = 0): int64;
    function get_uint(const key: string; defValue: unsigned = 0): unsigned;
    function get_bool(const key: string; defValue: bool = false): bool;
    function get_string(const key: string; const defValue: string = ''): string;
    // --
    function get(const section, key: string; defValue: int): int; overload;
    function get(const section, key: string; defValue: int64): int64; overload;
    function get(const section, key: string; defValue: unsigned): unsigned; overload;
    function get(const section, key: string; defValue: boolean): bool; overload;
    function get(const section, key: string; const defValue: string): string; overload;
    //
    procedure setValue(const key: string; value: int); overload;
    procedure setValue(const key: string; value: int64); overload;
    procedure setValue(const key: string; value: unsigned); overload;
    procedure setValue(const key: string; value: boolean); overload;
    procedure setValue(const key: string; const value: string); overload;
    // -- routines for C++Builder
    procedure set_int(const key: string; value: int);
    procedure set_int64(const key: string; value: int64);
    procedure set_uint(const key: string; value: unsigned);
    procedure set_bool(const key: string; value: bool);
    procedure set_string(const key: string; const value: string);
    // --
    procedure setValue(const section, key: string; value: int); overload;
    procedure setValue(const section, key: string; value: int64); overload;
    procedure setValue(const section, key: string; value: unsigned); overload;
    procedure setValue(const section, key: string; value: boolean); overload;
    procedure setValue(const section, key: string; const value: string); overload;
    //
    // timeout is in ms
    function waitForValue(const key, value: string; timeout: int): bool;
    //
    function getSectionAsText(const sectionName: string = ''): string;
    function setSectionAsText(const sectionName: string; const value: string): bool; overload;
    function setSectionAsText(const value: string): bool; overload;
    //
    function enter(const section: string; out sectionSave: string; timeout: unsigned = 0): bool; overload;
    function enter(const section: string = ''; timeout: unsigned = 0): bool; overload;
    procedure leave(const sectionSave: string = '');
    //
    property section: string read getSection write setSection;
    property lockTimeout: unsigned read f_lockTimeout write f_lockTimeout;
    {DP:METHOD
      Use carefully, string is created every time this property is touched
    }
    property asString: string read getAsString write setAsString;
  end;


  //
  // -- unaIniFile --
  //

  {DP:CLASS
    Manages values stored in Windows INI files.
  }
  unaIniFile = class(unaIniAbstractStorage)
  private
    f_fileName: wideString;
    //
    procedure setFileName(const value: wideString);
    procedure fixFilePath();
  protected
    function getStringValue(const key: string; const defValue: string = ''): string; override;
    procedure setStringValue(const key: string; const value: string); override;
    //
    function getAsString(): string; override;
    procedure setAsString(const value: string); override;
    //
    function doGetSectionAsText(const sectionName: string): string; override;
    function doSetSectionAsText(const sectionName, value: string): bool; override;
  public
    constructor create(const fileName: wideString = ''; const section: string = 'settings'; lockTimeout: unsigned = 1000; checkFilePath: bool = true);
    procedure AfterConstruction(); override;
    //
    property fileName: wideString read f_fileName write setFileName;
  end;


  //
  // -- unaIniMemorySection --
  //

  {DP:CLASS
    Manages values stored in Windows INI files.
  }
  unaIniMemorySection = class(unaObject)
  private
    f_name: string;
    f_keys: unaStringList;
    f_values: unaStringList;
  public
    constructor create(const name: string);
    //
    procedure AfterConstruction(); override;
    procedure BeforeDestruction(); override;
    //
    procedure addKeyValue(const key, value: string);
    //
    property name: string read f_name;
  end;


  //
  // -- unaIniMemory --
  //

  {DP:CLASS
    Manages values stored in memory in INI format.
  }
  unaIniMemory = class(unaIniAbstractStorage)
  private
    f_sections: unaObjectList;
    //
    procedure parseMemory(memory: pChar; size: unsigned);
    function getSection(const name: string; allowCreation: bool): unaIniMemorySection;
  protected
    function getStringValue(const key: string; const defValue: string = ''): string; override;
    procedure setStringValue(const key: string; const value: string); override;
    //
    function getAsString(): string; override;
    procedure setAsString(const value: string); override;
    //
    function doGetSectionAsText(const sectionName: string): string; override;
    function doSetSectionAsText(const sectionName, value: string): bool; override;
  public
    constructor create(memory: pointer = nil; size: unsigned = 0; const section: string = 'settings'; lockTimeout: unsigned = 1000);
    procedure BeforeDestruction(); override;
    //
    procedure loadFrom(const fileName: wideString);
    procedure saveTo(const fileName: wideString);
  end;



  //
  // -- unaAbstractStream --
  //

  unaAbstractStreamClass = class of unaAbstractStream;

  {DP:CLASS
    Simple First In First Out (FIFO) stream. It is multi-threaded safe, so you can use it from several threads without any special care.
  }
  unaAbstractStream = class(unaObject)
  private
    f_summarySize: unsigned;
    f_isValid: bool;
    f_pos: unsigned;
    f_dataEvent: unaEvent;
  {$IFDEF DEBUG}
    f_title: string;
  {$ENDIF}
    f_lockTimeout: unsigned;
    //
    f_gate: unaInProcessGate;
    function getIsEmpty: bool;
  protected
    function seek(position: unsigned; fromBeggining: bool = true): unsigned; overload; virtual;
    function seek(delta: int): unsigned; overload; virtual;
    function getPosition(): unsigned; virtual;
    function remove2(size: unsigned): unsigned; virtual;
    function clear2(): unaAbstractStream; virtual;
    function write2(buf: pointer; size: unsigned): unsigned; overload; virtual;
    function read2(buf: pointer; size: unsigned; remove: bool = true): unsigned; overload; virtual; abstract;
    //
    function getSize2(): unsigned; virtual;
    function getAvailableSize2(): unsigned; virtual;
  public
    constructor create(lockTimeout: unsigned = 1000{$IFDEF DEBUG}; const title: string = ''{$ENDIF}); virtual;
    procedure AfterConstruction(); override;
    destructor Destroy(); override;
    //
    function clear(): unaAbstractStream;
    function waitForData(timeout: unsigned = 1000): bool;
    function remove(size: unsigned): unsigned;
    //
    function readFrom(const fileName: wideString; offset: unsigned = 0): unsigned; overload;
    function readFrom(source: unaAbstractStream): unsigned; overload;
    //
    function write(buf: pointer; size: unsigned): unsigned; overload;
    function write(value: boolean): unsigned; overload;
    function write(value: byte): unsigned; overload;
    function write(value: word): unsigned; overload;
    function write(value: int): unsigned; overload;
    function write(value: unsigned): unsigned; overload;
    function write(const value: string): unsigned; overload;
    //
    function read(buf: pointer; size: unsigned; remove: bool = true): unsigned; overload;
    function read(def: boolean; remove: bool = true): bool; overload;
    function read(def: byte; remove: bool = true): byte; overload;
    function read(def: word; remove: bool = true): word; overload;
    function read(def: int; remove: bool = true): int; overload;
    function read(def: unsigned; remove: bool = true): unsigned; overload;
    function read(const def: string; remove: bool = true): string; overload;
    //
    function writeTo(const fileName: wideString; append: bool = true; size: unsigned = 0): unsigned; overload;
    function writeTo(dest: unaAbstractStream): unsigned; overload;
    class function copyStream(source, dest: unaAbstractStream): unsigned;
    //
    function getSize(): unsigned;
    function getAvailableSize(): unsigned;
    //
    function enter(timeout: unsigned = INFINITE{$IFDEF DEBUG}; const masterName: string = ''{$ENDIF}): bool;
    procedure leave();
  {$IFDEF DEBUG}
    property gate: unaInProcessGate read f_gate;
  {$ENDIF}
    //
    property isValid: bool read f_isValid;
    property isEmpty: bool read getIsEmpty;
  {$IFDEF DEBUG}
    property title: string read f_title write f_title;
  {$ENDIF}
    property lockTimeout: unsigned read f_lockTimeout write f_lockTimeout default 1000;
  end;


  //
  // -- unaStreamChunk --
  //

  unaStreamChunk = class(unaObject)
  private
    f_buf: pointer;
    f_bufSize: unsigned;
    f_dataSize: unsigned;
    f_offset: unsigned;
  public
    constructor create(buf: pointer; size: unsigned);
    destructor Destroy(); override;
    //
    function getSize(): unsigned;
    function read(buf: pointer; size: unsigned; remove: bool = true): unsigned;
    procedure newData(buf: pointer; size: unsigned);
  end;


  //
  // -- unaMemoryStream --
  //

  {DP:CLASS
    This stream is stored in memory. This implementation does not supports seeking.
  }
  unaMemoryStream = class(unaAbstractStream)
  private
    f_chunks: unaObjectList;
    f_emptyChunks: unaObjectList;
    f_maxCacheSize: unsigned;
  protected
    function seek(position: unsigned; fromBeggining: bool = true): unsigned; overload; override;
    function seek(delta: int): unsigned; overload; override;
    function getPosition(): unsigned; override;
    function remove2(size: unsigned): unsigned; override;
    function clear2(): unaAbstractStream; override;
    //
    function read2(buf: pointer; size: unsigned; remove: bool = true): unsigned; override;
    function write2(buf: pointer; size: unsigned): unsigned; override;
    function getAvailableSize2(): unsigned; override;
  public
    procedure AfterConstruction(); override;
    destructor Destroy(); override;
    //
    function getCrc32(): unsigned;
    //
    function getFirstChunkSize(): unsigned;
    //
    property maxCacheSize: unsigned read f_maxCacheSize write f_maxCacheSize default 10;
  end;


  {DP:CLASS
    This stream is stored in memory. It does support seeking.
  }
  unaMemoryData = class(unaAbstractStream)
  private
    f_data: pointer;
  protected
    function read2(buf: pointer; size: unsigned; remove: bool = true): unsigned; override;
    function write2(buf: pointer; size: unsigned): unsigned; override;
    function remove2(size: unsigned): unsigned; override;
  public
    constructor createData(data: pointer; size: unsigned);
    procedure BeforeDestruction(); override;
    //
    property data: pointer read f_data;
  end;


  //
  // -- unaFileStream --
  //

  {DP:CLASS
    This stream is stored in a file.
  }
  unaFileStream = class(unaAbstractStream)
  private
    f_handle: tHandle;
    f_fileName: wideString;
    f_fileOffset: unsigned;
    f_access: unsigned;
    f_shareMode: unsigned;
    f_loop: bool;
    f_fileFlags: unsigned;
  protected
    function clear2(): unaAbstractStream; override;
    //
    function read2(buf: pointer; size: unsigned; remove: bool = true): unsigned; override;
    function write2(buf: pointer; size: unsigned): unsigned; override;
    {DP:METHOD
      Returns size of file.
    }
    function getSize2(): unsigned; override;
    {DP:METHOD
      Returns number of bytes you can read from the file from current position.
    }
    function getAvailableSize2(): unsigned; override;
  public
    procedure AfterConstruction(); override;
    constructor createStream(const fileName: wideString; access: unsigned = GENERIC_READ + GENERIC_WRITE; shareMode: unsigned = FILE_SHARE_READ + FILE_SHARE_WRITE; loop: bool = false; fileFlags: unsigned = FILE_ATTRIBUTE_NORMAL);
    destructor Destroy(); override;
    //
    function initStream(const fileName: wideString; access: unsigned = GENERIC_READ + GENERIC_WRITE; shareMode: unsigned = FILE_SHARE_READ + FILE_SHARE_WRITE; loop: bool = false; fileFlags: unsigned = FILE_ATTRIBUTE_NORMAL): bool;
    function seek(position: unsigned; fromBeggining: bool = true): unsigned; overload; override;
    function seek(delta: int): unsigned; overload; override;
    function getPosition(): unsigned; override;
    //
    procedure close();
    //
    property fileName: wideString read f_fileName;
    {DP:METHOD
      When reading beyond the end of file, set this property is true, to wrap at 0 offset.
    }
    property loop: bool read f_loop write f_loop;
  end;


  //
  // -- unaResourceStream --
  //

  {DP:CLASS
    This stream is stored in resource.
  }
  unaResourceStream = class(unaAbstractStream)
  private
    f_name: wideString;
    f_resType: pWideChar;
    f_instance: hModule;
    //
    f_resource: hRSRC;
    f_global: hGLOBAL;
    f_data: pointer;
  protected
    function read2(buf: pointer; size: unsigned; remove: bool = true): unsigned; override;
    function write2(buf: pointer; size: unsigned): unsigned; override;
  public
    constructor createRes(const name: wideString; resType: pWideChar = RT_RCDATAW; instance: hModule = 0);
    procedure AfterConstruction(); override;
    procedure BeforeDestruction(); override;
    //
    function find(const name: wideString; resType: pWideChar = RT_RCDATAW; instance: hModule = 0): int;
    //

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -