Programlama yapalım ve Öğrenelim. - Delphi Eğitim188
  Ana Sayfa
  .NET Eğitim Notları
  Visual C# .NET Örnek Kodları
  VisualBasic.NET Örnek Kodları
  J# Örnekleri
  ASP.NET Örnek Kodları
  Delphi Eğitim
  => Delphi Eğitim1
  => Delphi Eğitim2
  => Delphi Eğitim3
  => Delphi Eğitim4
  => Delphi Eğitim5
  => Delphi Eğitim6
  => Delphi Eğitim7
  => Delphi Eğitim8
  => Delphi Eğitim9
  => Delphi Eğitim10
  => Delphi Eğitim11
  => Delphi Eğitim13
  => Delphi Eğitim14
  => Delphi Eğitim15
  => Delphi Eğitim16
  => Delphi Eğitim17
  => Delphi Eğitim18
  => Delphi Eğitim19
  => Delphi Eğitim20
  => Delphi Eğitim21
  => Delphi Eğitim22
  => Delphi Eğitim23
  => Delphi Eğitim24
  => Delphi Eğitim25
  => Delphi Eğitim26
  => Delphi Eğitim27
  => Delphi Eğitim28
  => Delphi Eğitim29
  => Delphi Eğitim30
  => Delphi Eğtim31
  => Delphi Eğitim32
  => Delphi Eğitim33
  => Delphi Eğitim34
  => Delphi Eğitim35
  => Delphi Eğitim36
  => Delphi Eğitim37
  => Delphi Eğitim38
  => Delphi Eğitim39
  => Delphi Eğitim40
  => Delphi Eğitim41
  => Delphi Eğitim42
  => Delphi Eğitim43
  => Delphi Eğitim44
  => Delphi Eğitim45
  => Delphi Eğitim46
  => Delphi Eğitim47
  => Delphi Eğitim48
  => Delphi Eğitim49
  => Delphi Eğitim50
  => Delphi Eğitim51
  => Delphi Eğitim52
  => Delphi Eğitim53
  => Delphi Eğitim54
  => Delphi Eğitim55
  => Delphi Eğitim56
  => Delphi Eğitim57
  => Delphi Eğitim58
  => Delphi Eğitim59
  => Delphi Eğitim60
  => Delphi Eğitim61
  => Delphi Eğitim62
  => Delphi Eğitim63
  => Delphi Eğitim64
  => Delphi Eğitim65
  => Delphi Eğitim66
  => Delphi Eğitim67
  => Delphi Eğitim68
  => Delphi Eğitim69
  => Delphi Eğitim70
  => Delphi Eğitim71
  => Delphi Eğitim72
  => Delphi Eğitim73
  => Delphi Eğitim74
  => Delphi Eğitim75
  => Delphi Eğitim76
  => Delphi Eğitim77
  => Delphi Eğitim78
  => Delphi Eğitim79
  => Delphi Eğitim80
  => Delphi Eğitim81
  => Delphi Eğitim82
  => Delphi Eğitim83
  => Delphi Eğitim84
  => Delphi Eğitim85
  => Delphi Eğitim86
  => Delphi Eğitim87
  => Delphi Eğitim88
  => Delphi Eğitim89
  => Delphi Eğitim90
  => Delphi Eğitim91
  => Delphi Eğitim92
  => Delphi Eğitim93
  => Delphi Eğitim94
  => Delphi Eğitim95
  => Delphi Eğitim96
  => Delphi Eğitim97
  => Delphi Eğitim98
  => Delphi Eğitim99
  => Delphi Eğitim100
  => Delphi Eğitim101
  => Delphi Eğitim102
  => Delphi Eğitim103
  => Delphi Eğitim104
  => Delphi Eğitim105
  => Delphi Eğitim106
  => Delphi Eğitim107
  => Delphi Eğitim108
  => Delphi Eğitim109
  => Delphi Eğitim110
  => Delphi Eğitim111
  => Delphi Eğitim112
  => Delphi Eğitim113
  => Delphi Eğitim114
  => Delphi Eğitim115
  => Delphi Eğitim116
  => Delphi Eğitim117
  => Delphi Eğitim118
  => Delphi Eğitim119
  => Delphi Eğitim120
  => Delphi Eğitim121
  => Delphi Eğitim122
  => Delphi Eğitim123
  => Delphi Eğitim124
  => Delphi Eğitim125
  => Delphi Eğitim126
  => Delphi Eğitim127
  => Delphi Eğitim128
  => Delphi Eğitim129
  => Delphi Eğitim130
  => Delphi Eğitim131
  => Delphi Eğitim132
  => Delphi Eğitim133
  => Delphi Eğitim134
  => Delphi Eğitim135
  => Delphi Eğitim136
  => Delphi Eğitim137
  => Delphi Eğitim138
  => Delphi Eğitim139
  => Delphi Eğitim140
  => Delphi Eğitim141
  => Delphi Eğitim142
  => Delphi Eğitim143
  => Delphi Eğitim144
  => Delphi Eğitim145
  => Delphi Eğitim146
  => Delphi eğitim147
  => Delphi Eğitim148
  => Delphi Eğitim149
  => Delphi Eğitim150
  => Delphi Eğitim151
  => Delphi Eğitim152
  => Delphi Eğitim153
  => Delphi Eğitim154
  => Delphi Eğitim155
  => Delphi Eğitim156
  => Delphi Eğitim157
  => Delphi Eğitim158
  => Delphi Eğitim159
  => Delphi Eğitim160
  => Delphi Eğitim161
  => Delphi Eğitim162
  => Delphi Eğitim164
  => Delphi Eğitim165
  => Delphi Eğitim166
  => Delphi Eğitim167
  => Delphi Eğitim168
  => Delphi Eğitim169
  => Delphi Eğitim170
  => Delphi Eğitim171
  => Delphi Eğitim172
  => Delphi Eğitim173
  => Delphi Eğitim174
  => Delphi Eğitim175
  => Delphi Eğitim176
  => Delphi Eğitim177
  => Delphi Eğitim178
  => Delphi Eğitim179
  => Delphi Eğitim180
  => Delphi Eğitim181
  => Delphi Eğitim182
  => Delphi Eğitim183
  => Delphi Eğitim184
  => Delphi Eğitim185
  => Delphi Eğitim186
  => Delphi Eğitim187
  => Delphi Eğitim188
  => Delphi Eğitim189
  => Delphi Eğitim190
  => Delphi Eğitim191
  => Delphi Eğitim192
  => Delphi Eğitim193
  => Delphi Eğitim194
  => Delphi Eğitim195
  => Delphi Eğitim196
  => Delphi Eğitim197
  => Delphi Eğitim198
  => Delphi Eğitim199
  => Delphi Eğitim200
  => Delphi Eğitim201
  => Delphi Eğitim202
  => Delphi Eğitim203
  => Delphi Eğitim204
  => Delphi Eğitim205
  => Delphi Eğitim206
  => Delphi Eğitim207
  => Delphi Eğitim208
  => Delphi Eğitim209
  => Delphi Eğitim210
  => Delphi Eğitim211
  => Delphi Eğitim212
  => Delphi Eğitim213
  => Delphi Eğitim214
  => Delphi Eğitim215
  => Delphi Eğitim216
  => Delphi Eğitim217
  => Delphi Eğitim218
  => Delphi Eğitim219
  => Delphi Eğitim220
  => Delphi Eğitim221
  => Delphi Eğitim222
  => Delphi Eğitim223
  => Delphi Eğitim224
  => Delphi Eğitim225
  => Delphi Eğitim226
  => Delphi Eğitim227
  => Delphi Eğitim228
  => Delphi Eğitim229
  => Delphi Eğitim230
  => Delphi Eğitim231
  => Delphi Eğitim232
  => Delphi Eğitim233
  => Delphi Eğitim234
  => Delphi Eğitim235
  => Delphi Eğitim236
  => Delphi Eğitim237
  => Delphi Eğitim238
  => Delphi Eğitim239
  => Delphi Eğitim240
  => Delphi Eğitim241
  => Delphi Eğitim242
  İletişim

neoturk: ...Convert a csv file to xml ?...

> I am trying to write an application that converts a CSV(or similar)it to

> an XML one.The application looks for a character(the comma - - or anything

  > else specified in an Edit box - -), adds a starting and ending tag to the

> line, and writes the line to the new XML file. in the end I should get an

> XML file with the various elements.

 

{Your task has a number of subtasks.

 

The first is parsing the input file into lines. You can leave that to a

Tstringlist, if the files you need to handle are not in the

multimegabyte size range. If they are you would be best served by using

the good old Pascal Textfile routines, where a simple ReadLn( filevar, S

) gets you a line.

 

The second is parsing a line into its elements, based on a separator

character between the elements. This is also not so difficult to do,

especially if you don't need to deal with quoted elements that may

contain the separator. Search the newsgroup archives for "SplitString"

for an example. Tstringlist.Delimitedtext may be of use here, but be

warned that it considers any character <= #32 as a separator *in

addition* to what you define as Delimiter. It can deal with quoted

elements, though.

 

The second subtask would end with a TStringlist instance containing the

elements to store into the XML file for one line of the input file. This

is the input for the third task: to create a first-level XML element

containing the data. To write valid XML you need not only deal with

proper nesting of XML tags, you also have to properly represent some

characters that have special meaning in XML ('<' and '&' for instance).

I can recommend Berend de Boers xml_generator class

http://www.pobox.com/~berend/delphi for this task, it deals with all the

nastiness behind the scenes and produces syntactically correct XML

without the overhead of a DOM model implementation.

 

There is something else you need: a list of column names, one name for

each "column" in your XML file. These names will become the node names

for the subnodes of the produced XML. Depending on your input files you

may be able to get these names from the first line (which often is a

header line giving the column names).

 

Here is sketch (untested!) of the conversion routine: }

 

type

  {: Callback for CSVToXML. If given the callback will be called

    after each processed line.

    @Param currentline is the 0-based number of the processed line

    @Param totallines is the total number of lines. This may be a

      raw estimate if the file is not completly loaded in memory.

    @Returns true to continue processing, false to stop it. }

  TProgressNotification =

    function(currentline, totallines: Integer): Boolean of object;

 

{-- CSVToXML ----------------------------------------------------------}

{: Convert a delimiter-separated file of data to XML

@Param csvfilename is the file to convert

@Param xmlfilename is the xml file to create

@Param aSeparator is the separator for the data

@Param aRootNodeName is the name to use for the root node of the XML

  file.

@Param columnnames is an optional list of column names to use as subnode

  names. If this parameter is nil the first line of the data file must

  contain a header line with the names to use.

@Param onProgress is an optional callback to call afte each processed

  line.

@Precondition  csvfilename exists

}{ Created 17.3.2003 by P. Below

-----------------------------------------------------------------------}

 

procedure CSVToXML(const csvfilename, xmlfilename: string;

  const aSeparator: Char;

  const aRootNodeName: string;

  const columnnames: TStrings = nil;

  const onProgress: TProgressNotification = nil);

 

  function DoProgress(currentline, totallines: Integer): Boolean;

  begin

    if Assigned(onProgress) then

      Result := onProgress(currentline, totallines)

    else

      Result := true;

  end;

 

  procedure WriteDataline(const line: string; header: TStringlist; xml: TXMLGenerator);

  var

    elements: TStringlist;

    i, max: Integer;

  begin

    elements := TStringlist.Create;

    try

      elements.Delimiter := aSeparator;

      elements.Delimitedtext := line;

      if elements.count > header.count then

        max := header.count

      else

        max := elements.count;

      for i := 0 to max - 1 do begin

        xml.StartTag(header[i]);

        xml.AddData(elements[i]);

        xml.StopTag;

      end; { For }

    finally

      elements.Free;

    end;

  end;

 

  procedure WriteData(data: TStringlist; xml: TXMLGenerator);

  var

    header: TStringlist;

    firstline: Integer;

    i: Integer;

  begin

    header := Tstringlist.Create;

    try

      firstline := 0;

      if assigned(columnnames) then

        header.Assign(columnnames)

      else begin

        header.Delimiter := aSeparator;

        header.DelimitedText := data[0];

        firstline := 1;

      end; { Else }

      for i := firstline to data.count - 1 do begin

        WriteDataline(data[i], header, xml);

        if not DoProgress(i, data.count) then

          Break;

      end; { For }

    finally

      header.Free;

    end;

  end;

 

  procedure SaveStringToFile(const S, filename: string);

  var

    fs: TFilestream;

  begin

    fs := TFileStream.Create(filename, fmCreate);

    try

      if Length(S) > 0 then

        fs.WriteBuffer(S[1], Length(S));

    finally

      fs.free

    end;

  end; { SaveStringToFile }

 

 

var

  xml: TXMLGenerator; // from xml_generator unit by Berend de Boers

  datafile: Tstringlist;

begin { CSVToXML }

  if not FileExists(csvfilename) then

    raise Exception.CreateFmt('Input file %s not found', [csvfilename]);

  datafile := Tstringlist.Create;

  try

    datafile.LoadfromFile(csvfilename);

    xml := TXMLGenerator.CreateWithEncoding(16 * 1024, encISO_8859_1);

    try

      xml.StartTag(aRootNodeName);

      if datafile.count > 0 then

        WriteData(datafile, xml);

      xml.StopTag;

      SaveStringToFile(xml.AsLatin1, xmlfilename);

    finally

      xml.Free;

    end;

  finally

    datafile.free;

  end;

end; { CSVToXML }

 

Delphi - .....................................

 

neoturk: ...Use pathcombine - extractrelativepath ?...

{

Question:

 

Could anyone point me to any function that can receive

an HTML style relative path as its input and return a full path as its

output based on the exe's current location, e.g

 

Input: '....test2.dat'

Output: C:folder1test2.dat

 

(assuming that the executable is running from

 C:folder1folder2folder3Project1.exe)

 

}

 

{

 

Answer:

 

PathCombine() Concatenates two strings that represent properly

formed paths into one path, as well as any relative path pieces.

}

 

function PathCombine(lpszDest: PChar; const lpszDir, lpszFile: PChar):

PChar; stdcall; external 'shlwapi.dll' name 'PathCombineA';

function PathCombineA(lpszDest: PAnsiChar; const lpszDir, lpszFile:

PAnsiChar): PAnsiChar; stdcall; external 'shlwapi.dll';

function PathCombineW(lpszDest: PWideChar; const lpszDir, lpszFile:

PWideChar): PWideChar; stdcall; external 'shlwapi.dll';

 

{

 Requires Windows 2000 (or Windows NT 4.0 with Internet Explorer 4.0 or later);

 Requires Windows 98 (or Windows 95 with Internet Explorer 4.0 or later)

}

 

procedure TForm1.FormCreate(Sender: TObject);

var

  dest: string;

  BaseFile, RelativePath: String;

begin

  BaseFile := 'C:folder1folder2folder3';

  RelativePath := '....test2.dat';

  SetLength(dest, MAX_PATH);

  PathCombine(@dest[1], PChar(ExtractFilePath(BaseFile)), PChar(RelativePath));

  SetLength(dest, StrLen(@Dest[1]));

  ShowMessage(dest);

end;

 

{

  The ExtractRelativePath function takes a base directory but the

  ExpandFilename function does the expansion based on the current

  directory not a given base path.

}

 

RelativePath := ExtractRelativePath(ExtractFilePath(BaseFile), TargetFile);

 

Delphi - .....................................

 

neoturk: ...Use pathcombine - extractrelativepath ?...

{

Question:

 

Could anyone point me to any function that can receive

an HTML style relative path as its input and return a full path as its

output based on the exe's current location, e.g

 

Input: '....test2.dat'

Output: C:folder1test2.dat

 

(assuming that the executable is running from

 C:folder1folder2folder3Project1.exe)

 

}

 

{

 

Answer:

 

PathCombine() Concatenates two strings that represent properly

formed paths into one path, as well as any relative path pieces.

}

 

function PathCombine(lpszDest: PChar; const lpszDir, lpszFile: PChar):

PChar; stdcall; external 'shlwapi.dll' name 'PathCombineA';

function PathCombineA(lpszDest: PAnsiChar; const lpszDir, lpszFile:

PAnsiChar): PAnsiChar; stdcall; external 'shlwapi.dll';

function PathCombineW(lpszDest: PWideChar; const lpszDir, lpszFile:

PWideChar): PWideChar; stdcall; external 'shlwapi.dll';

 

{

 Requires Windows 2000 (or Windows NT 4.0 with Internet Explorer 4.0 or later);

 Requires Windows 98 (or Windows 95 with Internet Explorer 4.0 or later)

}

 

procedure TForm1.FormCreate(Sender: TObject);

var

  dest: string;

  BaseFile, RelativePath: String;

begin

  BaseFile := 'C:folder1folder2folder3';

  RelativePath := '....test2.dat';

  SetLength(dest, MAX_PATH);

  PathCombine(@dest[1], PChar(ExtractFilePath(BaseFile)), PChar(RelativePath));

  SetLength(dest, StrLen(@Dest[1]));

  ShowMessage(dest);

end;

 

{

  The ExtractRelativePath function takes a base directory but the

  ExpandFilename function does the expansion based on the current

  directory not a given base path.

}

 

RelativePath := ExtractRelativePath(ExtractFilePath(BaseFile), TargetFile);

 

Delphi - .....................................

 

neoturk: ...List all files in a directory ?...

procedure ListFileDir(Path: string; FileList: TStrings);

var

  SR: TSearchRec;

begin

  if FindFirst(Path + '*.*', faAnyFile, SR) = 0 then

  begin

    repeat

      if (SR.Attr <> faDirectory) then

      begin

        FileList.Add(SR.Name);

      end;

    until FindNext(SR) <> 0;

    FindClose(SR);

  end;

end;

 

procedure TForm1.Button1Click(Sender: TObject);

begin

  ListFileDir('C:WINDOWS', ListBox1.Items);

end;

 

Delphi - .....................................

 

neoturk: ...List all files in a directory ?...

procedure ListFileDir(Path: string; FileList: TStrings);

var

  SR: TSearchRec;

begin

  if FindFirst(Path + '*.*', faAnyFile, SR) = 0 then

  begin

    repeat

      if (SR.Attr <> faDirectory) then

      begin

        FileList.Add(SR.Name);

      end;

    until FindNext(SR) <> 0;

    FindClose(SR);

  end;

end;

 

procedure TForm1.Button1Click(Sender: TObject);

begin

  ListFileDir('C:WINDOWS', ListBox1.Items);

end;

 

Delphi - .....................................

 

neoturk: ...Get the image size of a jpg, gif and png image file ?...

unit ImgSize;

 

interface

 

uses Classes;

 

 

procedure GetJPGSize(const sFile: string; var wWidth, wHeight: Word);

procedure GetPNGSize(const sFile: string; var wWidth, wHeight: Word);

procedure GetGIFSize(const sGIFFile: string; var wWidth, wHeight: Word);

 

 

implementation

 

uses SysUtils;

 

function ReadMWord(f: TFileStream): Word;

type

  TMotorolaWord = record

    case Byte of

      0: (Value: Word);

      1: (Byte1, Byte2: Byte);

  end;

var

  MW: TMotorolaWord;

begin

  { It would probably be better to just read these two bytes in normally }

  { and then do a small ASM routine to swap them.  But we aren't talking }

  { about reading entire files, so I doubt the performance gain would be }

  { worth the trouble. }

  f.read(MW.Byte2, SizeOf(Byte));

  f.read(MW.Byte1, SizeOf(Byte));

  Result := MW.Value;

end;

 

procedure GetJPGSize(const sFile: string; var wWidth, wHeight: Word);

const

  ValidSig: array[0..1] of Byte = ($FF, $D8);

  Parameterless = [$01, $D0, $D1, $D2, $D3, $D4, $D5, $D6, $D7];

var

  Sig: array[0..1] of byte;

  f: TFileStream;

  x: integer;

  Seg: byte;

  Dummy: array[0..15] of byte;

  Len: word;

  ReadLen: LongInt;

begin

  FillChar(Sig, SizeOf(Sig), #0);

  f := TFileStream.Create(sFile, fmOpenRead);

  try

    ReadLen := f.read(Sig[0], SizeOf(Sig));

 

    for x := Low(Sig) to High(Sig) do

      if Sig[x] <> ValidSig[x] then ReadLen := 0;

 

    if ReadLen > 0 then

    begin

      ReadLen := f.read(Seg, 1);

      while (Seg = $FF) and (ReadLen > 0) do

      begin

        ReadLen := f.read(Seg, 1);

        if Seg <> $FF then

        begin

          if (Seg = $C0) or (Seg = $C1) then

          begin

            ReadLen := f.read(Dummy[0], 3); { don't need these bytes }

            wHeight := ReadMWord(f);

            wWidth  := ReadMWord(f);

          end

          else

          begin

            if not (Seg in Parameterless) then

            begin

              Len := ReadMWord(f);

              f.Seek(Len - 2, 1);

              f.read(Seg, 1);

            end

            else

              Seg := $FF; { Fake it to keep looping. }

          end;

        end;

      end;

    end;

  finally

    f.Free;

  end;

end;

 

procedure GetPNGSize(const sFile: string; var wWidth, wHeight: Word);

type

  TPNGSig = array[0..7] of Byte;

const

  ValidSig: TPNGSig = (137,80,78,71,13,10,26,10);

var

  Sig: TPNGSig;

  f: tFileStream;

  x: integer;

begin

  FillChar(Sig, SizeOf(Sig), #0);

  f := TFileStream.Create(sFile, fmOpenRead);

  try

    f.read(Sig[0], SizeOf(Sig));

    for x := Low(Sig) to High(Sig) do

      if Sig[x] <> ValidSig[x] then Exit;

    f.Seek(18, 0);

    wWidth := ReadMWord(f);

    f.Seek(22, 0);

    wHeight := ReadMWord(f);

  finally

    f.Free;

  end;

end;

 

 

procedure GetGIFSize(const sGIFFile: string; var wWidth, wHeight: Word);

type

  TGIFHeader = record

    Sig: array[0..5] of char;

    ScreenWidth, ScreenHeight: Word;

    Flags, Background, Aspect: Byte;

  end;

 

  TGIFImageBlock = record

    Left, Top, Width, Height: Word;

    Flags: Byte;

  end;

var

  f: file;

  Header: TGifHeader;

  ImageBlock: TGifImageBlock;

  nResult: integer;

  x: integer;

  c: char;

  DimensionsFound: boolean;

begin

  wWidth  := 0;

  wHeight := 0;

 

  if sGifFile = '' then

    Exit;

 

  {$I-}

  FileMode := 0;   { read-only }

  AssignFile(f, sGifFile);

  reset(f, 1);

  if IOResult <> 0 then

    { Could not open file }

    Exit;

 

  { Read header and ensure valid file. }

  BlockRead(f, Header, SizeOf(TGifHeader), nResult);

  if (nResult <> SizeOf(TGifHeader)) or (IOResult <> 0) or

    (StrLComp('GIF', Header.Sig, 3) <> 0) then

  begin

    { Image file invalid }

    Close(f);

    Exit;

  end;

 

  { Skip color map, if there is one }

  if (Header.Flags and $80) > 0 then

  begin

    x := 3 * (1 shl ((Header.Flags and 7) + 1));

    Seek(f, x);

    if IOResult <> 0 then

    begin

      { Color map thrashed }

      Close(f);

      Exit;

    end;

  end;

 

  DimensionsFound := False;

  FillChar(ImageBlock, SizeOf(TGIFImageBlock), #0);

  { Step through blocks. }

  BlockRead(f, c, 1, nResult);

  while (not EOF(f)) and (not DimensionsFound) do

  begin

    case c of

      ',': { Found image }

        begin

          BlockRead(f, ImageBlock, SizeOf(TGIFImageBlock), nResult);

          if nResult <> SizeOf(TGIFImageBlock) then

          begin

            { Invalid image block encountered }

            Close(f);

            Exit;

          end;

          wWidth := ImageBlock.Width;

          wHeight := ImageBlock.Height;

          DimensionsFound := True;

        end;

      'ÿ': { Skip }

        begin

          { NOP }

        end;

      { nothing else.  just ignore }

    end;

    BlockRead(f, c, 1, nResult);

  end;

  Close(f);

  {$I+}

end;

 

end.

 

Delphi - .....................................

 

neoturk: ...Get the image size of a jpg, gif and png image file ?...

unit ImgSize;

 

interface

 

uses Classes;

 

 

procedure GetJPGSize(const sFile: string; var wWidth, wHeight: Word);

procedure GetPNGSize(const sFile: string; var wWidth, wHeight: Word);

procedure GetGIFSize(const sGIFFile: string; var wWidth, wHeight: Word);

 

 

implementation

 

uses SysUtils;

 

function ReadMWord(f: TFileStream): Word;

type

  TMotorolaWord = record

    case Byte of

      0: (Value: Word);

      1: (Byte1, Byte2: Byte);

  end;

var

  MW: TMotorolaWord;

begin

  { It would probably be better to just read these two bytes in normally }

  { and then do a small ASM routine to swap them.  But we aren't talking }

  { about reading entire files, so I doubt the performance gain would be }

  { worth the trouble. }

  f.read(MW.Byte2, SizeOf(Byte));

  f.read(MW.Byte1, SizeOf(Byte));

  Result := MW.Value;

end;

 

procedure GetJPGSize(const sFile: string; var wWidth, wHeight: Word);

const

  ValidSig: array[0..1] of Byte = ($FF, $D8);

  Parameterless = [$01, $D0, $D1, $D2, $D3, $D4, $D5, $D6, $D7];

var

  Sig: array[0..1] of byte;

  f: TFileStream;

  x: integer;

  Seg: byte;

  Dummy: array[0..15] of byte;

  Len: word;

  ReadLen: LongInt;

begin

  FillChar(Sig, SizeOf(Sig), #0);

  f := TFileStream.Create(sFile, fmOpenRead);

  try

    ReadLen := f.read(Sig[0], SizeOf(Sig));

 

    for x := Low(Sig) to High(Sig) do

      if Sig[x] <> ValidSig[x] then ReadLen := 0;

 

    if ReadLen > 0 then

    begin

      ReadLen := f.read(Seg, 1);

      while (Seg = $FF) and (ReadLen > 0) do

      begin

        ReadLen := f.read(Seg, 1);

        if Seg <> $FF then

        begin

          if (Seg = $C0) or (Seg = $C1) then

          begin

            ReadLen := f.read(Dummy[0], 3); { don't need these bytes }

            wHeight := ReadMWord(f);

            wWidth  := ReadMWord(f);

          end

          else

          begin

            if not (Seg in Parameterless) then

            begin

              Len := ReadMWord(f);

              f.Seek(Len - 2, 1);

              f.read(Seg, 1);

            end

            else

              Seg := $FF; { Fake it to keep looping. }

          end;

        end;

      end;

    end;

  finally

    f.Free;

  end;

end;

 

procedure GetPNGSize(const sFile: string; var wWidth, wHeight: Word);

type

  TPNGSig = array[0..7] of Byte;

const

  ValidSig: TPNGSig = (137,80,78,71,13,10,26,10);

var

  Sig: TPNGSig;

  f: tFileStream;

  x: integer;

begin

  FillChar(Sig, SizeOf(Sig), #0);

  f := TFileStream.Create(sFile, fmOpenRead);

  try

    f.read(Sig[0], SizeOf(Sig));

    for x := Low(Sig) to High(Sig) do

      if Sig[x] <> ValidSig[x] then Exit;

    f.Seek(18, 0);

    wWidth := ReadMWord(f);

    f.Seek(22, 0);

    wHeight := ReadMWord(f);

  finally

    f.Free;

  end;

end;

 

 

procedure GetGIFSize(const sGIFFile: string; var wWidth, wHeight: Word);

type

  TGIFHeader = record

    Sig: array[0..5] of char;

    ScreenWidth, ScreenHeight: Word;

    Flags, Background, Aspect: Byte;

  end;

 

  TGIFImageBlock = record

    Left, Top, Width, Height: Word;

    Flags: Byte;

  end;

var

  f: file;

  Header: TGifHeader;

  ImageBlock: TGifImageBlock;

  nResult: integer;

  x: integer;

  c: char;

  DimensionsFound: boolean;

begin

  wWidth  := 0;

  wHeight := 0;

 

  if sGifFile = '' then

    Exit;

 

  {$I-}

  FileMode := 0;   { read-only }

  AssignFile(f, sGifFile);

  reset(f, 1);

  if IOResult <> 0 then

    { Could not open file }

    Exit;

 

  { Read header and ensure valid file. }

  BlockRead(f, Header, SizeOf(TGifHeader), nResult);

  if (nResult <> SizeOf(TGifHeader)) or (IOResult <> 0) or

    (StrLComp('GIF', Header.Sig, 3) <> 0) then

  begin

    { Image file invalid }

    Close(f);

    Exit;

  end;

 

  { Skip color map, if there is one }

  if (Header.Flags and $80) > 0 then

  begin

    x := 3 * (1 shl ((Header.Flags and 7) + 1));

    Seek(f, x);

    if IOResult <> 0 then

    begin

      { Color map thrashed }

      Close(f);

      Exit;

    end;

  end;

 

  DimensionsFound := False;

  FillChar(ImageBlock, SizeOf(TGIFImageBlock), #0);

  { Step through blocks. }

  BlockRead(f, c, 1, nResult);

  while (not EOF(f)) and (not DimensionsFound) do

  begin

    case c of

      ',': { Found image }

        begin

          BlockRead(f, ImageBlock, SizeOf(TGIFImageBlock), nResult);

          if nResult <> SizeOf(TGIFImageBlock) then

          begin

            { Invalid image block encountered }

            Close(f);

            Exit;

          end;

          wWidth := ImageBlock.Width;

          wHeight := ImageBlock.Height;

          DimensionsFound := True;

        end;

      'ÿ': { Skip }

        begin

          { NOP }

        end;

      { nothing else.  just ignore }

    end;

    BlockRead(f, c, 1, nResult);

  end;

  Close(f);

  {$I+}

end;

 

end.

 

Delphi - .....................................

 

neoturk: ...Save a stringgrid as a csv file ?...

function TForm1.SaveToCSV:Boolean;

var

  SD : TSaveDialog;

  I : Integer;

  CSV : TStrings;

  FileName : String;

begin

  Try

  // Filedialog erzeugen

  SD := TSaveDialog.Create(Self);

  SD.Filter := 'CSV-Trennzeichen getrennt (*.csv)|*.CSV';

  //Filedialog ausführen

  If SD.Execute = True Then

  Begin

    //Filename zuweisen

    FileName := SD.FileName;

    If Copy(FileName,Pos('.',FileName),Length(FileName)-Pos('.',FileName)+1) <> '.csv' Then FileName := FileName + '.csv';

    Screen.Cursor := crHourGlass;

    //Stringliste erzeugen

    CSV := TStringList.Create;

    Try

      //Stringliste füllen

      For I := 0 To Grid.RowCount - 1 Do CSV.Add(Grid.Rows[I].CommaText);

      //CSV speichern

      CSV.SaveToFile(FileName);

      Result := True;

    Finally

      CSV.Free;

    End;

  End;

 

  Finally

    SD.Free;

    Screen.Cursor := crDefault;

  End;

end;

 

//SaveToCSV ausführen (sample call)

procedure TForm1.BtnSaveClick(Sender: TObject);

begin

   SaveToCSV;

end;

 

Delphi - .....................................

 

neoturk: ...Save a stringgrid as a csv file ?...

function TForm1.SaveToCSV:Boolean;

var

  SD : TSaveDialog;

  I : Integer;

  CSV : TStrings;

  FileName : String;

begin

  Try

  // Filedialog erzeugen

  SD := TSaveDialog.Create(Self);

  SD.Filter := 'CSV-Trennzeichen getrennt (*.csv)|*.CSV';

  //Filedialog ausführen

  If SD.Execute = True Then

  Begin

    //Filename zuweisen

    FileName := SD.FileName;

    If Copy(FileName,Pos('.',FileName),Length(FileName)-Pos('.',FileName)+1) <> '.csv' Then FileName := FileName + '.csv';

    Screen.Cursor := crHourGlass;

    //Stringliste erzeugen

    CSV := TStringList.Create;

    Try

      //Stringliste füllen

      For I := 0 To Grid.RowCount - 1 Do CSV.Add(Grid.Rows[I].CommaText);

      //CSV speichern

      CSV.SaveToFile(FileName);

      Result := True;

    Finally

      CSV.Free;

    End;

  End;

 

  Finally

    SD.Free;

    Screen.Cursor := crDefault;

  End;

end;

 

//SaveToCSV ausführen (sample call)

procedure TForm1.BtnSaveClick(Sender: TObject);

begin

   SaveToCSV;

end;

 

Delphi - .....................................

 

neoturk: ...Check if a file is a text file or a binary one ?...

function IsTextFile(const sFile: TFileName): boolean;

//Created By Marcelo Castro - from Brazil

 

var

 oIn: TFileStream;

 iRead: Integer;

 iMaxRead: Integer;

 iData: Byte;

 dummy:string;

begin

 result:=true;

 dummy :='';

 oIn := TFileStream.Create(sFile, fmOpenRead or fmShareDenyNone);

 try

   iMaxRead := 1000;  //only text the first 1000 bytes

   if iMaxRead > oIn.Size then

     iMaxRead := oIn.Size;

   for iRead := 1 to iMaxRead do

   begin

     oIn.Read(iData, 1);

     if (idata) > 127 then result:=false;

   end;

 finally

   FreeAndNil(oIn);

 end;

end;

 

(* ----- Sample call ----- *)

 

procedure TForm1.Button1Click(Sender: TObject);

begin

  if OpenDialog1.Execute then

  begin

  if IsTextFile(OpenDialog1.FileName) then

  showmessage('is ascii')

  else showmessage('is BinaryFile')

  end;

 

end;

Bu web sitesi ücretsiz olarak Bedava-Sitem.com ile oluşturulmuştur. Siz de kendi web sitenizi kurmak ister misiniz?
Ücretsiz kaydol