星期五, 1月 03, 2014

檢查CSV格式是否正確

//result: 0=正確CSV格式 1=檔案不存在 2=每列行數不一致
function isCSVFormat(sFileName: string): integer;
var
  slLine, slLineItem: TStringList;
  sLine: String;
  iLineItemCount: integer;
begin
  Result := 0;
  if not FileExists(sFileName) then
  begin
    Result := 1;  //檔案不存在
    exit;
  end;

  iLineItemCount := -1;
  slLine := TStringList.Create;
  slLineItem := TStringList.Create;
  try
    slLine.LoadFromFile(sFileName);
    for sLine in slLine do
    begin
      slLineItem.Delimiter := ',';
      slLineItem.StrictDelimiter := True;
      slLineItem.DelimitedText := sLine;
      if (iLineItemCount<>-1) and (iLineItemCount<>slLineItem.Count) then
      begin
        Result := 2;   //每列行數不一致
        break;
      end;
      iLineItemCount := slLineItem.Count;
    end;
  finally
    slLineItem.Free;
    slLine.Free;
  end;

end;

//result: 0=正確格式 1=第一列有欄位名稱重覆  2=第一列欄位不能空字串(包括空格, tab, 全形)  3=每列行數不一致
function isExactFormat(var slLine: TStringList): integer;
var
  slLineItem: TStringList;
  sLine: String;
  iLineItemCount: integer;
  i,j: integer;
begin
  Result := 0;

  iLineItemCount := -1;
  slLineItem := TStringList.Create;
  try
    for sLine in slLine do
    begin
      slLineItem.Delimiter := ',';
      slLineItem.StrictDelimiter := True;
      slLineItem.DelimitedText := sLine;
      if (iLineItemCount=-1) then
      begin
        for i := 0 to slLineItem.Count - 2 do
          for j := i+1 to slLineItem.Count - 1 do
            if CompareText(slLineItem.Strings[i], slLineItem.Strings[j])=0 then //大小寫視為相同
            begin
              Result := 1;   //第一列有欄位名稱重覆
              break;
            end;
        for i := 0 to slLineItem.Count - 1 do
          if Length(Trim(StringReplace(slLineItem.Strings[i],' ','',[rfReplaceAll])))=0 then
          begin
            Result := 2;   //第一列欄位不能空字串(包括空格, tab, 全形)
            break;
          end;
      end
      else if (iLineItemCount<>slLineItem.Count) then
      begin
        Result := 3;   //每列行數不一致
        break;
      end;
      iLineItemCount := slLineItem.Count;
    end;
  finally
    slLineItem.Free;
  end;

end;

檢查是否為正確的Mac Address格式

function isMacAdrFormat(str: String): boolean;
begin
  Result := False;
  if Length(str) <> 17 then
    Exit;
  Result := ((str[1] in ['0'..'9', 'A'..'F']) and
             (str[2] in ['0'..'9', 'A'..'F']) and
             (str[3] = '-') and
             (str[4] in ['0'..'9', 'A'..'F']) and
             (str[5] in ['0'..'9', 'A'..'F']) and
             (str[6] = '-') and
             (str[7] in ['0'..'9', 'A'..'F']) and
             (str[8] in ['0'..'9', 'A'..'F']) and
             (str[9] = '-') and
             (str[10] in ['0'..'9', 'A'..'F']) and
             (str[11] in ['0'..'9', 'A'..'F']) and
             (str[12] = '-') and
             (str[13] in ['0'..'9', 'A'..'F']) and
             (str[14] in ['0'..'9', 'A'..'F']) and
             (str[15] = '-') and
             (str[16] in ['0'..'9', 'A'..'F']) and
             (str[17] in ['0'..'9', 'A'..'F']));

end;