fpc/rtl/inc/typefile.inc
Jonas Maebe df6a2dce00 + unicodestring support for assign/erase/rename
+ codepage support for textrec/filerec and the above routines
  * textrec/filerec now store the filename by default using widechar. It is
    possible to switch back to ansichars using the FPC_ANSI_TEXTFILEREC define.
    In that case, from now on the filename will always be stored in
    DefaultFileSystemEncoding
  * fixed potential buffer overflows and non-null-terminated file names in
    textrec/filerec
  + dodirseparators(pwidechar), changed the dodirseparators(pchar/pwidechar)
    parameters into var-parameters and gave those routines an extra parameter
    that indicates whether the p(wide)char can be changed in place if
    necessary or whether a copy must be made first (avoids us having to make
    all strings always unique everywhere, because they might be changed on
    some platforms via a pchar)
  * do_open/do_erase/do_rename got extra boolean parameters indicating whether
    the passed pchars point to data that can be freely changed (to pass on to
    dodirseparators() if applicable)
  * objects.pp: force assign(pchar) to be called, because
    assign(array[0..255]) cannot choose between pchar and rawbytestring
    versions (and removing the pchar version means that assign(pchar) will
    be mapped to assign(shortstring) in {$h-})
  * fixed up some routines in other units that depend on the format of
    the textrec/filerec.name field

git-svn-id: branches/cpstrrtl@25137 -
2013-07-19 16:30:51 +00:00

146 lines
3.6 KiB
PHP

{
This file is part of the Free Pascal Run time library.
Copyright (c) 1999-2000 by the Free Pascal development team
See the File COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{****************************************************************************
subroutines for typed file handling
****************************************************************************}
{$ifdef FPC_HAS_FEATURE_WIDESTRINGS}
Procedure Assign(out f:TypedFile;const Name: UnicodeString);
{
Assign Name to file f so it can be used with the file routines
}
Begin
Assign(UnTypedFile(f),Name);
End;
{$endif FPC_HAS_FEATURE_WIDESTRINGS}
{$ifdef FPC_HAS_FEATURE_ANSISTRINGS}
Procedure Assign(out f:TypedFile;const Name: RawByteString);
{
Assign Name to file f so it can be used with the file routines
}
Begin
Assign(UnTypedFile(f),Name);
End;
{$endif FPC_HAS_FEATURE_ANSISTRINGS}
Procedure Assign(out f:TypedFile;const Name: ShortString);
{
Assign Name to file f so it can be used with the file routines
}
Begin
Assign(UnTypedFile(f),Name);
End;
Procedure Assign(out f:TypedFile;const p:PAnsiChar);
Begin
Assign(UnTypedFile(f),p);
end;
Procedure Assign(out f:TypedFile;const c:AnsiChar);
Begin
Assign(UnTypedFile(f),c);
end;
Procedure fpc_reset_typed(var f : TypedFile;Size : Longint);[Public,IOCheck, Alias:'FPC_RESET_TYPED']; compilerproc;
Begin
Reset(UnTypedFile(f),Size);
End;
Procedure fpc_rewrite_typed(var f : TypedFile;Size : Longint);[Public,IOCheck, Alias:'FPC_REWRITE_TYPED']; compilerproc;
Begin
Rewrite(UnTypedFile(f),Size);
End;
{$ifdef FPC_HAS_FEATURE_RANDOM}
{ this code is duplicated in the iso7185 unit }
Procedure DoAssign(var t : TypedFile);
Begin
Assign(t,'fpc_'+HexStr(random(1000000000),8)+'.tmp');
End;
{$else FPC_HAS_FEATURE_RANDOM}
{ this code is duplicated in the iso7185 unit }
Procedure DoAssign(var t : TypedFile);
const
start : dword = 0;
Begin
Assign(t,'fpc_'+HexStr(start,8)+'.tmp');
inc(start);
End;
{$endif FPC_HAS_FEATURE_RANDOM}
Procedure fpc_reset_typed_iso(var f : TypedFile;Size : Longint);[Public,IOCheck, Alias:'FPC_RESET_TYPED_ISO']; compilerproc;
Begin
If InOutRes <> 0 then
exit;
{ create file name? }
if FileRec(f).mode=0 then
DoAssign(f);
Reset(UnTypedFile(f),Size);
End;
Procedure fpc_rewrite_typed_iso(var f : TypedFile;Size : Longint);[Public,IOCheck, Alias:'FPC_REWRITE_TYPED_ISO']; compilerproc;
Begin
If InOutRes <> 0 then
exit;
{ create file name? }
if FileRec(f).mode=0 then
DoAssign(f);
Rewrite(UnTypedFile(f),Size);
End;
Procedure fpc_typed_write(TypeSize : Longint;var f : TypedFile;const Buf);[IOCheck, Public, Alias :'FPC_TYPED_WRITE']; compilerproc;
Begin
If InOutRes <> 0 then
exit;
case fileRec(f).mode of
fmOutPut,fmInOut:
Do_Write(FileRec(f).Handle,@Buf,TypeSize);
fmInput: inOutRes := 105;
else inOutRes := 103;
end;
End;
Procedure fpc_typed_read(TypeSize : Longint;var f : TypedFile;out Buf);[IOCheck, Public, Alias :'FPC_TYPED_READ']; compilerproc;
var
Result : Longint;
Begin
If InOutRes <> 0 then
exit;
case FileRec(f).mode of
fmInput,fmInOut:
begin
Result:=Do_Read(FileRec(f).Handle,@Buf,TypeSize);
If Result<TypeSize Then
InOutRes:=100
end;
fmOutPut: inOutRes := 104
else inOutRes := 103;
end;
End;