Skip to content

API.CString

Ivan Semenkov edited this page Oct 24, 2021 · 4 revisions

Table of contents

About

API.CString is a wrapper around C language API cstring char * value.

uses
  utils.api.cstring;
  
type
  API = class
  type
    CString = class
  end;

PAnsiStringWrapper

PAnsiStringWrapper class is a thin wrapper around PAnsiChar which delete allocated memory on destructor.

uses
  utils.api.cstring;
  
type
  API = class
  type
    PAnsiStringWrapper = class
  end;  

Value

Return stored in wrapper PAnsiChar value.

property Value : PAnsiChar;

Length

Return stored in wrapper string length.

property Length : Cardinal;

Create

A new wrapper can be created by call its constructor.

Create a empty string.

constructor Create;
Example
uses
  utils.api.cstring;
  
var
  str : API.CString;
  
begin
  str := API.CString.Create;
  
  FreeAndNil(str);
end;

Create new string by pascal string type.

constructor Create (AString : String);
Example
uses
  utils.api.cstring;
  
var
  str : API.CString;
  
begin
  str := API.CString.Create('some string value');
  
  FreeAndNil(str);
end;

Create new string by PAnsiChar type.

constructor Create (AString : PAnsiChar);
Example
uses
  utils.api.cstring;
  
var
  str : API.CString;
  
begin
  str := API.CString.Create(PAnsiChar('some string value'));
  
  FreeAndNil(str);
end;

ToString

Convert stored value to correct pascal string value.

function ToString : String;
Example
uses
  utils.api.cstring;
  
var
  str : API.CString;
  
begin
  str := API.CString.Create('some string value');
  writeln(str.ToString);
  
  FreeAndNil(str);
end;

ToPAnsiChar

Convert stored value to correct PAnsiChar value.

function ToPAnsiChar : PAnsiChar;
Example
uses
  utils.api.cstring;
  
var
  str : API.CString;
  
begin
  str := API.CString.Create('some string value');
  writeln(str.ToPAnsiString);
  
  FreeAndNil(str);
end;

ToUniquePAnsiChar

Allocate memory for new string and convert stored value to correct PAnsiChar value.

function ToUniquePAnsiChar : PAnsiStringWrapper;
Example
uses
  utils.api.cstring;
  
var
  str : API.CString;
  
begin
  str := API.CString.Create('some string value');
  writeln(str.ToUniquePAnsiString.Value);
  
  FreeAndNil(str);
end;

Length

Return stored string value length.

function Length : Integer;
Example
uses
  utils.api.cstring;
  
var
  str : API.CString;
  
begin
  str := API.CString.Create('some string value');
  writeln(str.Length);
  
  FreeAndNil(str);
end;
Clone this wiki locally