📄 qimport3common.pas
字号:
quote_in_quote := false;
Continue;
end
else if is_separator then
begin
quote_in_quote := false;
in_quote := false;
is_first := true;
AStrings.Add(sss);
Continue;
end
else begin
wait_separator := true;
quote_in_quote := false;
Continue;
end;
end;
if is_quote and quote_in_quote then
begin
sss := sss + Quote;
Continue;
end;
if is_quote and in_quote then
begin
quote_in_quote := True;
Continue;
end;
sss := sss + Str[i];
end;
AStrings.Add(sss);
end;
procedure ReplaceTabs(var Str: {$IFDEF QI_UNICODE}WideString{$ELSE}string{$ENDIF});
var
sp: WideString;
i, d, r: Integer;
begin
i := 1;
while i <= Length(Str) do
begin
if Str[i] = #9 then
begin
if (i mod 8) = 0 then
r := i mod 9
else
r := i mod 8;
d := 9 - r;
sp := StringOfChar(' ', d);
QIDelete(Str, i, 1);
QIInsert(sp, Str, i);
i := i + d;
Continue;
end;
Inc(i);
end;
end;
function GetListSeparator: Char;
begin
Result := GetLocaleChar(GetThreadLocale, LOCALE_SLIST, ',');
end;
procedure ClearIniFile(IniFile: TIniFile);
var
AStrings: TStrings;
i: integer;
begin
AStrings := TStringList.Create;
try
IniFile.ReadSections(AStrings);
for i := 0 to AStrings.Count - 1 do
IniFile.EraseSection(AStrings[i]);
finally
AStrings.Free;
end;
end;
// ImportDestination routines
procedure QImportCheckDestination(IsCSV: boolean;
ImportDestination: TQImportDestination; DataSet: TDataSet
{$IFNDEF NOGUI}; DBGrid: TDBGrid;
ListView: TListView;
StringGrid: TStringGrid{$ENDIF});
begin
if IsCSV then Exit;
case ImportDestination of
qidDataSet:
if not Assigned(DataSet) then
raise EQImportError.Create(QImportLoadStr(QIE_NoDataSet));
{$IFNDEF NOGUI}
qidDBGrid:
if not Assigned(DBGrid) then
raise EQImportError.Create(QImportLoadStr(QIE_NoDBGrid));
qidListView:
if not Assigned(ListView) then
raise EQImportError.Create(QImportLoadStr(QIE_NoListView));
qidStringGrid:
if not Assigned(StringGrid) then
raise EQImportError.Create(QImportLoadStr(QIE_NoStringGrid));
{$ENDIF}
end;
end;
function QImportIsDestinationActive(IsCSV: boolean;
ImportDestination: TQImportDestination; DataSet: TDataSet
{$IFNDEF NOGUI}; DBGrid: TDBGrid;
ListView: TListView;
StringGrid: TStringGrid{$ENDIF}): boolean;
begin
Result := false;
if IsCSV then Exit;
case ImportDestination of
qidDataSet: Result := DataSet.Active;
{$IFNDEF NOGUI}
qidDBGrid:
Result := Assigned(DBGrid.DataSource) and Assigned(DBGrid.DataSource.DataSet)and
DBGrid.DataSource.DataSet.Active;
else Result := true;
{$ENDIF}
end;
end;
procedure QImportIsDestinationOpen(IsCSV: boolean;
ImportDestination: TQImportDestination; DataSet: TDataSet
{$IFNDEF NOGUI}; DBGrid: TDBGrid;
ListView: TListView;
StringGrid: TStringGrid{$ENDIF});
begin
if IsCSV then Exit;
case ImportDestination of
qidDataSet: DataSet.Open;
{$IFNDEF NOGUI}
qidDBGrid:
if Assigned(DBGrid.DataSource) and
Assigned(DBGrid.DataSource.DataSet) then
DBGrid.DataSource.DataSet.Open;
{$ENDIF}
end;
end;
procedure QImportIsDestinationClose(IsCSV: boolean;
ImportDestination: TQImportDestination; DataSet: TDataSet
{$IFNDEF NOGUI}; DBGrid: TDBGrid;
ListView: TListView;
StringGrid: TStringGrid{$ENDIF});
begin
if IsCSV then Exit;
case ImportDestination of
qidDataSet: DataSet.Close;
{$IFNDEF NOGUI}
qidDBGrid:
if Assigned(DBGrid.DataSource) and
Assigned(DBGrid.DataSource.DataSet) then
DBGrid.DataSource.DataSet.Close;
{$ENDIF}
end;
end;
function QImportDestinationColCount(IsCSV: boolean;
ImportDestination: TQImportDestination; DataSet: TDataSet
{$IFNDEF NOGUI}; DBGrid: TDBGrid;
ListView: TListView;
StringGrid: TStringGrid{$ENDIF}): integer;
begin
Result := 0;
if IsCSV then Exit;
case ImportDestination of
qidDataSet: Result := DataSet.FieldCount;
{$IFNDEF NOGUI}
qidDBGrid: Result := DBGrid.Columns.Count;
qidListView: Result := ListView.Columns.Count;
qidStringGrid: Result := StringGrid.ColCount;
{$ENDIF}
end;
end;
function QImportDestinationColName(IsCSV: boolean;
ImportDestination: TQImportDestination; DataSet: TDataSet;
{$IFNDEF NOGUI}DBGrid: TDBGrid;
ListView: TListView;
StringGrid: TStringGrid;
GridCaptionRow,{$ENDIF} Index: integer): string;
begin
Result := EmptyStr;
if IsCSV then Exit;
case ImportDestination of
qidDataSet: Result := DataSet.Fields[Index].FieldName;
{$IFNDEF NOGUI}
qidDBGrid: Result := DBGrid.Columns[Index].Title.Caption;
qidListView: Result := ListView.Columns[Index].Caption;
qidStringGrid:
if (GridCaptionRow > -1) and (GridCaptionRow < StringGrid.RowCount) then
Result := StringGrid.Cells[Index, GridCaptionRow]
else Result := IntToStr(Index);
{$ENDIF}
end;
end;
function QImportDestinationAssigned(IsCSV: boolean;
ImportDestination: TQImportDestination; DataSet: TDataSet
{$IFNDEF NOGUI}; DBGrid: TDBGrid;
ListView: TListView;
StringGrid: TStringGrid{$ENDIF}): boolean;
begin
Result := false;
if IsCSV then Exit;
case ImportDestination of
qidDataSet: Result := Assigned(DataSet);
{$IFNDEF NOGUI}
qidDBGrid: Result := Assigned(DBGrid);
qidListView: Result := Assigned(ListView);
qidStringGrid: Result := Assigned(StringGrid);
{$ENDIF}
end;
end;
function QImportDestinationFindColumn(IsCSV: boolean;
ImportDestination: TQImportDestination; DataSet: TDataSet;
{$IFNDEF NOGUI}DBGrid: TDBGrid;
ListView: TListView;
StringGrid: TStringGrid; GridCaptionRow: integer;
{$ENDIF}
const ColName: string): integer;
var
Field: TField;
{$IFNDEF NOGUI}
i: integer;
{$ENDIF}
begin
Result := -1;
if IsCSV then Exit;
case ImportDestination of
qidDataSet: begin
Field := DataSet.FindField(ColName);
if Assigned(Field) then
Result := Field.Index;
end;
{$IFNDEF NOGUI}
qidDBGrid:
for i := 0 to DBGrid.Columns.Count - 1 do
if AnsiCompareText(DBGrid.Columns[i].Title.Caption, ColName) = 0 then
begin
Result := i;
Exit;
end;
qidListView:
for i := 0 to ListView.Columns.Count - 1 do
if AnsiCompareText(ListView.Columns[i].Caption, ColName) = 0 then
begin
Result := i;
Exit;
end;
qidStringGrid: begin
i := StrToIntDef(ColName, -1);
if i > -1 then
begin
if i < StringGrid.ColCount then Result := i;
end
else begin
if GridCaptionRow > -1 then
for i := 0 to StringGrid.ColCount - 1 do
if AnsiCompareStr(StringGrid.Cells[i, GridCaptionRow], ColName) = 0 then
begin
Result := i;
Exit;
end;
end;
end;
{$ENDIF}
end;
end;
function IncludePathDelimiter(const S: string): string;
begin
Result := S;
if not IsPathDelimiter(Result, Length(Result)) then
Result := Result + PathDelim;
end;
function GetNumericString(const Str: string): string;
var
p: integer;
begin
Result := Str;
p := Pos(ThousandSeparator, Result);
while p > 0 do
begin
Delete(Result, p, 1);
p := Pos(ThousandSeparator, Result);
end;
end;
function FormatStrToDateTime(ADateTimeStr, AFormatStr: string): TDateTime;
function NormalizeSubString(const ASubStr: string): string;
var
i: Integer;
begin
Result := '';
for i := 1 to Length(ASubStr) do
begin
if StrToIntDef(ASubStr[i], -1) <> -1 then
Result := Result + ASubStr[i];
end;
end;
var
i, j: Integer;
st: TSystemTime;
fChar, subStr: string;
begin
Result := 0;
if (Length(ADateTimeStr) = 0) or (Length(AFormatStr) < 5) then Exit;
AFormatStr := UpperCase(AFormatStr);
st.wHour := 0;
st.wMinute := 0;
st.wSecond := 0;
st.wMilliSeconds := 0;
while Length(AFormatStr) > 0 do
begin
fChar := Copy(AFormatStr, 1, 1);
if fChar = 'Y' then
begin
i := Pos('YYYY', AFormatStr);
if (i > 0) and (i < Length(ADateTimeStr)) then
begin
subStr := Copy(ADateTimeStr, i, 4);
st.wYear := StrToIntDef(subStr, 0);
if st.wYear = 0 then
begin
subStr := NormalizeSubString(subStr);
st.wYear := StrToIntDef(subStr, 0);
if st.wYear = 0 then Exit;
end;
if (Length(ADateTimeStr) < Length(subStr)) or
(Length(AFormatStr) < 4) then Exit;
Delete(ADateTimeStr, 1, Length(subStr));
Delete(AFormatStr, 1, 4);
end
else begin
i := Pos('YY', AFormatStr);
if (i > 0) and (i < Length(ADateTimeStr)) then
begin
subStr := Copy(ADateTimeStr, i, 2);
st.wYear := StrToIntDef(subStr, 0);
if st.wYear = 0 then
begin
subStr := NormalizeSubString(subStr);
st.wYear := StrToIntDef(subStr, 0);
if st.wYear = 0 then Exit;
end;
st.wYear := 2000 + st.wYear;
if (Length(ADateTimeStr) < Length(subStr)) or
(Length(AFormatStr) < 2) then Exit;
Delete(ADateTimeStr, 1, Length(subStr));
Delete(AFormatStr, 1, 2);
end
else begin
i := Pos('Y', AFormatStr);
if (i > 0) and (i <= Length(ADateTimeStr)) then
begin
subStr := Copy(ADateTimeStr, i, 1);
st.wYear := StrToIntDef(subStr, 0);
if st.wYear = 0 then
begin
subStr := NormalizeSubString(subStr);
st.wYear := StrToIntDef(subStr, 0);
if st.wYear = 0 then Exit;
end;
st.wYear := 2000 + st.wYear;
if (Length(ADateTimeStr) < Length(subStr)) or
(Length(AFormatStr) < 1) then Exit;
Delete(ADateTimeStr, 1, Length(subStr));
Delete(AFormatStr, 1, 1);
end
else
Exit;
end;
end;
end
else if fChar = 'M' then
begin
i := Pos('MMMM', AFormatStr);
if (i > 0) and (i < Length(ADateTimeStr)) then
begin
for j := 1 to 12 do
begin
if Pos(UpperCase(DefLongMonthNames[j]), UpperCase(ADateTimeStr)) > 0 then
begin
st.wMonth := j;
if (Length(ADateTimeStr) < Length(DefLongMonthNames[j])) or
(Length(AFormatStr) < 4) then Exit;
Delete(ADateTimeStr, 1, Length(DefLongMonthNames[j]));
Delete(AFormatStr, 1, 4);
Break
end
else if j = 12 then
Exit;
end;
end
else begin
i := Pos('MMM', AFormatStr);
if (i > 0) and (i < Length(ADateTimeStr)) then
begin
for j := 1 to 12 do
begin
if Pos(UpperCase(DefShortMonthNames[j]), UpperCase(ADateTimeStr)) > 0 then
begin
st.wMonth := j;
if (Length(ADateTimeStr) < Length(DefShortMonthNames[j])) or
(Length(AFormatStr) < 3) then Exit;
Delete(ADateTimeStr, 1, Length(DefShortMonthNames[j]));
Delete(AFormatStr, 1, 3);
Break
end
else if j = 12 then
Exit;
end;
end
else begin
i := Pos('MM', AFormatStr);
if (i > 0) and (i < Length(ADateTimeStr)) then
begin
subStr := Copy(ADateTimeStr, i, 2);
st.wMonth := StrToIntDef(subStr, 0);
if st.wMonth = 0 then
begin
subStr := NormalizeSubString(subStr);
st.wMonth := StrToIntDef(subStr, 0);
if st.wMonth = 0 then Exit;
end;
if (Length(ADateTimeStr) < Length(subStr)) or
(Length(AFormatStr) < 2) then Exit;
Delete(ADateTimeStr, 1, Length(subStr));
Delete(AFormatStr, 1, 2)
end
else begin
i := Pos('M', AFormatStr);
if (i > 0) and (i <= Length(ADateTimeStr)) then
begin
subStr := Copy(ADateTimeStr, i, 1);
st.wMonth := StrToIntDef(subStr, 0);
if st.wMonth = 0 then
begin
subStr := NormalizeSubString(subStr);
st.wMonth := StrToIntDef(subStr, 0);
if st.wMonth = 0 then Exit;
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -