Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit parseCSV;
- interface
- uses Classes, SysUtils, Winapi.Windows;
- type
- // Уточнить размерность полей
- TCSVLine = record // Для Маяка
- ctype : byte;
- widget : byte;
- elements_numb : byte;
- range_min : extended;
- range_typical : extended;
- range_max : extended;
- data_size : byte;
- name_full : string[255];
- name : string[255];
- units : string[255];
- desc : string[255];
- enums_numb : byte;
- // _unknown: byte;
- end;
- TPCSVLine= ^TCSVLine;
- TCSVData = array of TCSVLine;
- TCSVParser = class
- private
- const CSV_DELIMITER = ';'; // Разделитель полей в CSV файле
- private
- _iLength: integer;
- _pData: TCSVData;
- _pStrParser: TstringList;
- private
- procedure _parseLine(const _sLine: string);
- function GetData(Const _iIndex: integer): TCSVLine;
- public
- property Count: integer read _iLength;
- Property Data[const _iIndex: integer]: TCSVLine read GetData; default;
- public
- procedure ParseFile(const _sPath: string); overload;
- procedure ParseFile(const _pList: TStringList); overload;
- public
- constructor Create; overload;
- destructor Destroy; override;
- end;
- // Для АТГС
- TCSVLine_ATGS = record
- number_telem : string[255];
- name_telem : string[255];
- param1 : string[255];
- param2 : string[255];
- param3 : string[255];
- param4 : string[255];
- param5 : string[255];
- param6 : string[255];
- param7 : string[255];
- param8 : string[255];
- param9 : string[255];
- param10 : string[255];
- param11 : string[255];
- param12 : string[255];
- param13 : string[255];
- param14 : string[255];
- param15 : string[255];
- param16 : string[255];
- param17 : string[255];
- param18 : string[255];
- param19 : string[255];
- param20 : string[255];
- param21 : string[255];
- param22 : string[255];
- param23 : string[255];
- param24 : string[255];
- param25 : string[255];
- end;
- TPCSVLine_ATGS= ^TCSVLine_ATGS;
- TCSVData_ATGS = array of TCSVLine_ATGS;
- TCSVParser_ATGS = class
- private
- const CSV_DELIMITER_ATGS = ';'; // Разделитель полей в CSV файле
- private
- _iLength_ATGS: integer;
- _pData_ATGS: TCSVData_ATGS;
- _pStrParser_ATGS: TstringList;
- private
- procedure _parseLine_ATGS(const _sLine_ATGS: string);
- function GetData_ATGS(Const _iIndex_ATGS: integer): TCSVLine_ATGS;
- public
- property Count_ATGS: integer read _iLength_ATGS;
- Property Data_ATGS[const _iIndex_ATGS: integer]: TCSVLine_ATGS read GetData_ATGS; default;
- public
- procedure ParseFile_ATGS(const _sPath_ATGS: string); overload;
- procedure ParseFile_ATGS(const _pList_ATGS: TStringList); overload;
- public
- constructor Create; overload;
- destructor Destroy; override;
- end;
- implementation
- function s2b(const _s: string): byte;
- begin
- Result := ord(_s[1]) - 48; // ord('0') == 48
- end;
- procedure TCSVParser._parseLine(const _sLine: string);
- var
- i : Integer;
- begin
- Self._pStrParser.DelimitedText := _sLine;
- SetLength(Self._pData, Self.Count + 1);
- Self._pData[Self.Count].ctype := s2b(Self._pStrParser[1]);
- Self._pData[Self.Count].widget := s2b(Self._pStrParser[2]);
- Self._pData[Self.Count].elements_numb := s2b(Self._pStrParser[3]);
- Self._pData[Self.Count].range_min := StrToFloatDef(Self._pStrParser[4], 0);
- Self._pData[Self.Count].range_typical := StrToFloatDef(Self._pStrParser[5], 0);
- Self._pData[Self.Count].range_max := StrToFloatDef(Self._pStrParser[6], 0);
- Self._pData[Self.Count].data_size := s2b(Self._pStrParser[7]);
- Self._pData[Self.Count].name_full := Self._pStrParser[8];
- Self._pData[Self.Count].name := Self._pStrParser[9];
- Self._pData[Self.Count].units := Self._pStrParser[10];
- Self._pData[Self.Count].desc := Self._pStrParser[11];
- Self._pData[Self.Count].enums_numb := s2b(Self._pStrParser[12]);
- // Self._pData[Self.Count]._unknown := s2b(Self._pStrParser[13]);
- Inc(Self._iLength);
- end;
- function TCSVParser.GetData(const _iIndex: integer): TCSVLine;
- begin
- if ((_iIndex < 0) or (_iIndex >= Self.Count)) then raise ERangeError.Create('List index out of bounse');
- Result := Self._pData[_iIndex];
- end;
- procedure TCSVParser.ParseFile(const _sPath: string);
- var
- pFile: TStringList;
- begin
- pFile := TStringList.Create;
- pFile.LoadFromFile(_sPath);
- Self.ParseFile(pFile);
- pFile.Free;
- end;
- procedure TCSVParser.ParseFile(const _pList: TStringList);
- var
- sLine: string;
- begin
- for sLine in _pList do begin
- // TODO: Add callback to prevent gui locking
- Self._parseLine(sLine);
- end;
- end;
- constructor TCSVParser.Create;
- begin
- inherited;
- Self._iLength := 0;
- SetLength(Self._pData, Self._iLength);
- Self._pStrParser := TstringList.Create;
- Self._pStrParser.Delimiter := CSV_DELIMITER;
- Self._pStrParser.StrictDelimiter:= True;
- end;
- destructor TCSVParser.Destroy;
- begin
- Self._pStrParser.Free;
- SetLength(Self._pData, 0);
- inherited;
- end;
- // ДЛЯ АТГС
- procedure TCSVParser_ATGS._parseLine_ATGS(const _sLine_ATGS: string);
- var
- i : Integer;
- begin
- Self._pStrParser_ATGS.DelimitedText := _sLine_ATGS;
- SetLength(Self._pData_ATGS, Self.Count_ATGS + 1);
- Self._pData_ATGS[Self.Count_ATGS].number_telem := Self._pStrParser_ATGS[0];
- Self._pData_ATGS[Self.Count_ATGS].name_telem := Self._pStrParser_ATGS[1];
- Self._pData_ATGS[Self.Count_ATGS].param1 := Self._pStrParser_ATGS[2];
- Self._pData_ATGS[Self.Count_ATGS].param2 := Self._pStrParser_ATGS[3];
- Self._pData_ATGS[Self.Count_ATGS].param3 := Self._pStrParser_ATGS[4];
- Self._pData_ATGS[Self.Count_ATGS].param4 := Self._pStrParser_ATGS[5];
- Self._pData_ATGS[Self.Count_ATGS].param5 := Self._pStrParser_ATGS[6];
- Self._pData_ATGS[Self.Count_ATGS].param6 := Self._pStrParser_ATGS[7];
- Self._pData_ATGS[Self.Count_ATGS].param7 := Self._pStrParser_ATGS[8];
- Self._pData_ATGS[Self.Count_ATGS].param8 := Self._pStrParser_ATGS[9];
- Self._pData_ATGS[Self.Count_ATGS].param9 := Self._pStrParser_ATGS[10];
- Self._pData_ATGS[Self.Count_ATGS].param10 := Self._pStrParser_ATGS[11];
- Self._pData_ATGS[Self.Count_ATGS].param11 := Self._pStrParser_ATGS[12];
- Self._pData_ATGS[Self.Count_ATGS].param12 := Self._pStrParser_ATGS[13];
- Self._pData_ATGS[Self.Count_ATGS].param13 := Self._pStrParser_ATGS[14];
- Self._pData_ATGS[Self.Count_ATGS].param14 := Self._pStrParser_ATGS[15];
- Self._pData_ATGS[Self.Count_ATGS].param15 := Self._pStrParser_ATGS[16];
- Self._pData_ATGS[Self.Count_ATGS].param16 := Self._pStrParser_ATGS[17];
- Self._pData_ATGS[Self.Count_ATGS].param17 := Self._pStrParser_ATGS[18];
- Self._pData_ATGS[Self.Count_ATGS].param18 := Self._pStrParser_ATGS[19];
- Self._pData_ATGS[Self.Count_ATGS].param19 := Self._pStrParser_ATGS[20];
- Self._pData_ATGS[Self.Count_ATGS].param20 := Self._pStrParser_ATGS[21];
- Self._pData_ATGS[Self.Count_ATGS].param21 := Self._pStrParser_ATGS[22];
- Self._pData_ATGS[Self.Count_ATGS].param22 := Self._pStrParser_ATGS[23];
- Self._pData_ATGS[Self.Count_ATGS].param23 := Self._pStrParser_ATGS[24];
- Self._pData_ATGS[Self.Count_ATGS].param24 := Self._pStrParser_ATGS[25];
- Self._pData_ATGS[Self.Count_ATGS].param25 := Self._pStrParser_ATGS[26];
- Inc(Self._iLength_ATGS);
- end;
- function TCSVParser_ATGS.GetData_ATGS(const _iIndex_ATGS: integer): TCSVLine_ATGS;
- begin
- if ((_iIndex_ATGS < 0) or (_iIndex_ATGS >= Self.Count_ATGS)) then raise ERangeError.Create('List index out of bounse');
- Result := Self._pData_ATGS[_iIndex_ATGS];
- end;
- procedure TCSVParser_ATGS.ParseFile_ATGS(const _sPath_ATGS: string);
- var
- pFile: TStringList;
- begin
- pFile := TStringList.Create;
- pFile.LoadFromFile(_sPath_ATGS);
- Self.ParseFile_ATGS(pFile);
- pFile.Free;
- end;
- procedure TCSVParser_ATGS.ParseFile_ATGS(const _pList_ATGS: TStringList);
- var
- sLine: string;
- begin
- for sLine in _pList_ATGS do begin
- // TODO: Add callback to prevent gui locking
- Self._parseLine_ATGS(sLine);
- end;
- end;
- constructor TCSVParser_ATGS.Create;
- begin
- inherited;
- Self._iLength_ATGS := 0;
- SetLength(Self._pData_ATGS, Self._iLength_ATGS);
- Self._pStrParser_ATGS := TstringList.Create;
- Self._pStrParser_ATGS.Delimiter := CSV_DELIMITER_ATGS;
- Self._pStrParser_ATGS.StrictDelimiter:= True;
- end;
- destructor TCSVParser_ATGS.Destroy;
- begin
- Self._pStrParser_ATGS.Free;
- SetLength(Self._pData_ATGS, 0);
- inherited;
- end;
- end.
Add Comment
Please, Sign In to add comment