Results 1 to 5 of 5

Thread: cross-platform temp filename generator?

  1. #1

    cross-platform temp filename generator?

    Hi all,
    I was wondering if there is a cross-platform method of generating a temporary filename, one that would work with Delphi AND freepascal (whatever platform)?

    I currently have tried coding this (not run it yet though):

    [pascal]Function TPackFile.GetTemporaryFileName : AnsiString;
    Const
    cValidChars = 'abcdefghijklmnopqrztuvwxyz1234567890!@#$%()_+&#91 ;]{}';
    cValidCharsLen = Length(cValidChars);
    Var
    i : Integer;
    Seed : LongInt;
    Begin
    Seed := RandSeed;
    Randomize;
    SetLength(Result,Random(20) + 10);
    For i := 1 To Length(Result) Do Result[i] := cValidChars[Random(cValidCharsLen) + 1];
    RandSeed := Seed;
    End;[/pascal]

    Does this code seem ok to generate random filenames?

    cheers,
    paul
    Games:
    Seafox


    Pages:
    Syntax Error Software itch.io page

    Online Chess
    http://gameknot.com/#paul_nicholls

  2. #2

    Re: cross-platform temp filename generator?

    I missing checking if file already exists, like

    Code:
    if FileExists(Result) then Result := GetTemporaryFileName()
    and maybe it was useful to add a suffix as optional parameter to function.

    Thomas

  3. #3

    Re: cross-platform temp filename generator?

    I see 2 things that may not work as intended:

    1) Randomize is not supposed to be called more than once per program runtime, it uses GetTickCount for seed if i recall, and if you call this function more than once per 1 millisecond it will generate same name.
    I suggest scrapping all seed and Randomize() code out.

    2) I don't think setlength() is proper use for strings, consider
    (init)
    Result := '';
    (for loop)
    Result := Result+cValidChars[Random(cValidCharsLen) + 1];

  4. #4

    Re: cross-platform temp filename generator?

    Quote Originally Posted by User137
    I see 2 things that may not work as intended:

    1) Randomize is not supposed to be called more than once per program runtime, it uses GetTickCount for seed if i recall, and if you call this function more than once per 1 millisecond it will generate same name.
    I suggest scrapping all seed and Randomize() code out.

    2) I don't think setlength() is proper use for strings, consider
    (init)
    Result := '';
    (for loop)
    Result := Result+cValidChars[Random(cValidCharsLen) + 1];
    setlength is ok for strings
    From brazil (:

    Pascal pownz!

  5. #5

    Re: cross-platform temp filename generator?

    Thanks for the hints/tips people
    cheers,
    Paul
    Games:
    Seafox


    Pages:
    Syntax Error Software itch.io page

    Online Chess
    http://gameknot.com/#paul_nicholls

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •