How to initialize a "Text" variable?

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

How to initialize a "Text" variable?

Felipe Monteiro de Carvalho
Hello,

I have the following variable that I need to initialize:

yyinput: Text;

I searched, but I can't find anything that explains what this is, or
how to initialize it. Is the Text type the same as TextFile?

I would like to set this variables to the contents of a file, so I
tryed the following, but it crashes with runtime error 123 ( 'No
medium found' ) and no error message.

  AssignFile(lexlib.yyinput, AFileName);
  Reset(lexlib.yyinput) ;

  fmlscan.yylex();

  CloseFile(lexlib.yyinput) ;

thanks,
--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to initialize a "Text" variable?

Wanderlan Santos dos Anjos
Hello Felipe,

Yes, Text is the same as TextFile.

Use:
Rewrite for create a new file for writing.
Reset for open a file already created for reading.
Append for open a file already created for writing.


--
Att,

Wanderlan Santos dos Anjos
(061) 3345-5496; 9133-3804



_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to initialize a "Text" variable?

Jonas Maebe-2
In reply to this post by Felipe Monteiro de Carvalho

On 15 Mar 2008, at 12:34, Felipe Monteiro de Carvalho wrote:

> I have the following variable that I need to initialize:
>
> yyinput: Text;
>
> I searched, but I can't find anything that explains what this is, or
> how to initialize it. Is the Text type the same as TextFile?

Yes (although more the other way around: Text is a standard Pascal  
type, while TextFile is an alias for Text introduced in Delphi).

> I would like to set this variables to the contents of a file, so I
> tryed the following, but it crashes with runtime error 123 ( 'No
> medium found' ) and no error message.
>
>  AssignFile(lexlib.yyinput, AFileName);

Normally, you would use plain assign() with Text files, but AssignFile  
maps through to that.

>  Reset(lexlib.yyinput) ;
>
>  fmlscan.yylex();
>
>  CloseFile(lexlib.yyinput) ;

Same remark as for assign() here (close vs closefile)

So your error is unrelated to the use of assign(file)/close(file), it  
is something else.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to initialize a "Text" variable?

Felipe Monteiro de Carvalho
In reply to this post by Wanderlan Santos dos Anjos
Ok, thanks, the initial problem was that the filename calculation
logic was wrong.

thanks,
--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to initialize a "Text" variable?

Felipe Monteiro de Carvalho
Is it possible to set a Text file to nowhere? I mean, lexlib has a
textfile called yyoutput, where it will throw output. I don't want to
have this output at all, and lexlib doesn't allow me to choose that.

yyoutput is declared simply as Text

I would initially try setting it to zero, but that's StdIO.

thanks,
--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to initialize a "Text" variable?

Michael Van Canneyt


On Sun, 16 Mar 2008, Felipe Monteiro de Carvalho wrote:

> Is it possible to set a Text file to nowhere? I mean, lexlib has a
> textfile called yyoutput, where it will throw output. I don't want to
> have this output at all, and lexlib doesn't allow me to choose that.
>
> yyoutput is declared simply as Text
>
> I would initially try setting it to zero, but that's StdIO.

Assign yyoutput to a dummy stream, which simply skips output.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to initialize a "Text" variable?

Felipe Monteiro de Carvalho
On Sun, Mar 16, 2008 at 8:16 AM, Michael Van Canneyt
<[hidden email]> wrote:
>  Assign yyoutput to a dummy stream, which simply skips output.

Could you explain better how can this be implemented?

The only think I know about streams is TStream.

thanks,
--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to initialize a "Text" variable?

Michael Van Canneyt


On Sun, 16 Mar 2008, Felipe Monteiro de Carvalho wrote:

> On Sun, Mar 16, 2008 at 8:16 AM, Michael Van Canneyt
> <[hidden email]> wrote:
> >  Assign yyoutput to a dummy stream, which simply skips output.
>
> Could you explain better how can this be implemented?
>
> The only think I know about streams is TStream.

Exactly:

-----------------------------------------------------------------------

uses lexlib, Classes, StreamIO;

Type
  TNullStream = Class(TStream)
    function Read(var Buffer; Count : LongInt) : Longint; override;
    function Write(const Buffer; Count : LongInt) : LongInt; override;
    function Seek(Offset: Longint; Origin: Word): Longint; override;
  end;

Function TNullStream.Read(var Buffer; Count : LongInt) : Longint;
begin
  Result:=Count;
end;

Function TNullStream.Write(const Buffer; Count : LongInt) : LongInt;

begin
  Result:=Count;
end;

function TNullStream.Seek(Offset: Longint; Origin: Word): Longint;

begin
  Result:=0;
end;


Var
  Null : TNullStream;

begin
  Null:=TNullstream.Create;
  assignstream(yyoutput,Null);
  // blah blah
  FreeAndNil(Null);
end.
-----------------------------------------------------------------------

That's it. Come to think of it,I may add TNullStream to the streamex unit or so.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to initialize a "Text" variable?

Tomas Hajny
In reply to this post by Felipe Monteiro de Carvalho
On Sun, March 16, 2008 12:14, Felipe Monteiro de Carvalho wrote:
> On Sun, Mar 16, 2008 at 8:16 AM, Michael Van Canneyt
> <[hidden email]> wrote:
>>  Assign yyoutput to a dummy stream, which simply skips output.
>
> Could you explain better how can this be implemented?
>
> The only think I know about streams is TStream.

I'm not sure if mixing lexlib (which is AFAIK written using procedural TP
style code) with streams is a good idea. Depending on your platform, you
could probably simply assign the text file name of 'NUL' or '/dev/nul'.
Admittedly, I don't know whether this kind of device exists for all
platforms (e.g. Symbian ;-) ).

Tomas


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

RE: How to initialize a "Text" variable?

Sam Liddicott
In reply to this post by Felipe Monteiro de Carvalho
Find the definition of the "text" type and see that it contains pointers to read and write functions.

They need replacing in yor instance to pointers to functions that do nothing.

The original Pascal file access is object oriented after all!

Sam


-----Original Message-----
From: Tomas Hajny <[hidden email]>
Sent: 17 March 2008 07:17
To: FPC-Pascal users discussions <[hidden email]>
Subject: Re: [fpc-pascal] How to initialize a "Text" variable?

On Sun, March 16, 2008 12:14, Felipe Monteiro de Carvalho wrote:
> On Sun, Mar 16, 2008 at 8:16 AM, Michael Van Canneyt
> <[hidden email]> wrote:
>>  Assign yyoutput to a dummy stream, which simply skips output.
>
> Could you explain better how can this be implemented?
>
> The only think I know about streams is TStream.

I'm not sure if mixing lexlib (which is AFAIK written using procedural TP
style code) with streams is a good idea. Depending on your platform, you
could probably simply assign the text file name of 'NUL' or '/dev/nul'.
Admittedly, I don't know whether this kind of device exists for all
platforms (e.g. Symbian ;-) ).

Tomas


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal