Dolphin Smalltalk questions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Dolphin Smalltalk questions

Costas Menico
I would appreciate some assistance with the following questions:

1) Why does an instance of FileStream keep working even after I send a
close to the instance?

E.g.  

x:=FileStream readWrite: 'c:\temp.txt'.
x nextPutAll: 'Hello file'.
x close.
x nextPutAll: 'Hello file again'.
x size.

2) What is the best way to add non-printable ASCII characters to a
string.  For example I would like to add CR or LF or ESC.

For example the following works but it looks convoluted.

x:='My line has a tab at the end' , (9 asCharacter asString).  

Other languages have shortcuts for some of these characters like \x09
in C or #09 in Pascal.

Costas Menico


Reply | Threaded
Open this post in threaded view
|

Re: Dolphin Smalltalk questions

Ian Bartholomew-2
Costas,

> 1) Why does an instance of FileStream keep working even after I send a
> close to the instance?

It's not really working, you just didn't reach the point where it complains.
Replace the fourth line with

x nextPutAll: ((String new: 10000) atAllPut: $?)

and a walkback occurs. In any case, nothing ever gets written to a file.

I suppose there is an argument for putting in a test to ensure that the
FileStream is still open before allowing read/write/any operations but I'm
not sure what you would really gain?

> 2) What is the best way to add non-printable ASCII characters to a
> string.  For example I would like to add CR or LF or ESC.

There are a number of ways, it depends on what you are trying to do -

- Your example could be written

x:='My line has a tab at the end', String tab.

as both #tab and #lineDelimiter are defined as String class methods. You
could add more as needed.

- Line delimiters and tabs can be embedded directly in the literal

x := 'Line    1
Line    2
Line    3'

includes 2 cr/lf's and 3 tabs

- Streams are probably the neatest way for more "unusual" inserts

x := String writeStream
    nextPutAll: 'Word1';
    tab;
    nextPutAll: 'Word2';
    nextPut: 27 asCharacter;
    nextPutAll: 'Word3';
    contents

- String implements #formatMessage so you could use something like
'%1%2!c!%3' formatWith: 'Word1' with: 9 with: 'Word2'

- String implements a (very limited) #sprintf
'%s%c' sprintfWith: 'Word1' with: 9

- If you use it a lot then just write your own. Something like

String>>substitute
    " 'Word1\tword2\eword3\r\n\\\?' substitute "
    | in out |
    in := self readStream.
    out := String writeStream.
    [in atEnd] whileFalse: [
        in peek == $\
            ifTrue: [| convert |
                in next.
                self assert: [in atEnd not].
                convert := ##(LookupTable new
                    at: $t put: 9;
                    at: $e put: 27;
                    at: $n put: 13;
                    at: $r put: 10;
                    at: $\ put: $\ codePoint;
                    yourself) at: in next ifAbsent: [32].
                out nextPut: convert asCharacter]
            ifFalse: [out nextPut: in next]].
    ^out contents

If you really want the ASCII integer values embedded in the string then just
replace the LookupTable with a bit of code that converts the next 1 (or 2 or
3) characters into an integer.

Ian


Reply | Threaded
Open this post in threaded view
|

Re: Dolphin Smalltalk questions

Ted Bracht-2
In reply to this post by Costas Menico
Hi Costas,

"Costas Menico" <[hidden email]> wrote in message
news:[hidden email]...

> I would appreciate some assistance with the following questions:
>
> 1) Why does an instance of FileStream keep working even after I send a
> close to the instance?
>
> E.g.
>
> x:=FileStream readWrite: 'c:\temp.txt'.
> x nextPutAll: 'Hello file'.
> x close.
> x nextPutAll: 'Hello file again'.
> x size.

The file on the file system is closed, but the filestream still exists. You
can check that by looking at the file temp.txt. When the file is open, you
have a pointer from your file stream to that file, and any changes in the
file stream are written to the file at the time you say #close or #flush.
When you close the file (with FileStream>>close), you remove that
connection, but the filestream still exists, so you can still make changes
to that. You have to reconnect the filestream and the file to update the
file again.

>
> 2) What is the best way to add non-printable ASCII characters to a
> string.  For example I would like to add CR or LF or ESC.
>
> For example the following works but it looks convoluted.
>
> x:='My line has a tab at the end' , (9 asCharacter asString).

Character has a number of class methods; tab, cr, space and so on. So if you
want to add something like that to your stream, you can do it like this:
y := 'Hello'
z := 'there'
a := WriteStream on: String new.
a nextPutAll: y.
a nextPut: Character space.
a nextPutAll: z.
a contents

Hope this helps.

>
> Other languages have shortcuts for some of these characters like \x09
> in C or #09 in Pascal.
>
> Costas Menico
>
>
>

Ted
>


Reply | Threaded
Open this post in threaded view
|

Re: Dolphin Smalltalk questions

Stefan Matthias Aust
In reply to this post by Costas Menico
On Wed, 15 Nov 2000 03:18:53 GMT, [hidden email] (Costas Menico)
wrote:

>2) What is the best way to add non-printable ASCII characters to a
>string.  For example I would like to add CR or LF or ESC.

You might want to add a method similar to

withEscapes
        | s i j c |
        s := self writeStream.
        j := 1.
        [(i := self indexOf: $\ startingAt: j) > 0] whileTrue: [
                s nextPutAll: (self copyFrom: j to: i - 1).
                c := self at: i + 1.
                c = $n ifTrue: [s nextPut: Character cr].
                c = $t ifTrue: [s nextPut: Character tab].
                c = $\ ifTrue: [s nextPut: c].
                j := i + 2].
        s nextPutAll: (self copyFrom: j to: self size).
        ^ s contents

to class String.  Now you can use

        x := '1\n2\n' withEscapes

to assign a string with two cr-terminated lines.  The method can
certainly be improved, I just hacked it together in a few minutes.

bye
--
Stefan Matthias Aust \/ Truth Until Paradox