save to string instead of file

Started by HPW, February 14, 2004, 10:47:06 AM

Previous topic - Next topic

HPW

I don't know if this is possible now in an other way.



I want a function like 'save', but I do not want to write to a temp-file and then read it back. The counter-part is ready with 'eval-string'.



It could be an option to 'save' or a new command like:



(stream varname [symbol])



I have in mind an other sort of storage and would like to send the resulting stream to a DLL, from where it could get back.
Hans-Peter

Lutz

#1
could you give a small example what this function would do?



Lutz

HPW

#2
When you open the newLISP-TK browser you write a file editbox.lsp in c:temp. I want the complete content of this file in a symbol as a long string including all CRLF. So simple replace the file-target by a string-buffer in the command 'save'. This string contain the lisp-source which I can send to my DLL.
Hans-Peter

Lutz

#3
If you don't care for formatting or pretty printing the following function will work:



 (define (save-string sym)

  (append "(set '" (string sym " ") (string (eval sym))))



for example:



(define (double x) (+ x x))



(save-string 'double) => "(set 'double (lambda (x) (+ x x))"



The returned string could be sent to the newlisp.dll for evaluation, where it would define the same function.



Lutz

ps: parenthesis missing! see 2 posts down for correction by HPW

HPW

#4
>I don't know if this is possible now in an other way.



Yes, that is the kind of other way I search.

For this time I don't care for formatting.

So I will go this way.

Thanks for the code.
Hans-Peter

HPW

#5
Oops, missing paranthesis:



(define (save-string sym)
(append "(set '" (string sym " ") (string (eval sym))")"))
Hans-Peter

HPW

#6
And it still does not do the same as 'save'.



with save:

(set 'CADT_WOINT_VARARTTAB '(
  ("KorpFa" 1 1 "Korpusfarbe")
  ("Progr" 1000 1 "Programm")))


with save-string:

(set 'atest [text](set 'CADT_WOINT_VARARTTAB (("KorpFa" 1 1 "Korpusfarbe")("Progr" 1000 1"Programm")))[/text])


When eval-string there is a quote missing.
Hans-Peter

eddier

#7
You need a single quote before  (("KorpFa"  as  '(("KorpFa"



Eddie

Lutz

#8
yes, I guess the 'save-string' function would have to be more complex and distinghish the type stored in a symbol:



- For lambda expressions and numbers leave like it is

- for lists which are not lambda put a single quote

- for strings put quotes around it



(define (save-string sym)
  (set 'contents (eval sym))
  (if (or (lambda? contents) (integer? contents) (float? contents))
      (append "(set '" (string sym " ") (string (eval sym)) ")")
      (list? contents)
      (append "(set '" (string sym " ") (string "'" (eval sym)) ")")
      (string? contents)
      (append "(set '" (string sym " ") (string """ (eval sym)) ")")))


You may still run into problems with nested [text] [/text] tags. Perhaps you should save to a temp-file like the tk-frontend is doing it and then let the dll 'load' it.



Lutz

HPW

#9
>Perhaps you should save to a temp-file like the tk-frontend is doing it and then let the dll 'load' it.



That option is possible of cource, but I wanted to avoid the step and stream it directly to the DLL. Would it be possible to make the 'save' command to optional work with an text-buffer instead of a file?



Anyway, thanks again for the improved code.
Hans-Peter

Lutz

#10
Yes, I looked into it. But it would be something after version 8.0. I will try then to make something like a 'string device' which also could be used in other circumstances, wherever a file is used. Internally a string stream device already exists but is not used/available to all functions dealing with files.



For upcoming version 8.0 in early summer I am concentrating on fixing bugs, more speed improvements, cutting old deprecated features and only doing small improvements, which have only a local affect on one function.



I want to introduce newLISP to a broader audience, have it rock solid and well documented. With the help of many of you in this forum newLISP has evolved to a more richly featured scripting language and LISP and it is time to do something to promote it to a wider audience.



Lutz

nigelbrown

#11
Seeing the discussion is around eval of strings I have a suggestion and seek feedback -

With the aim of creating dedicated utilities using newLISP could a compile option be created in which a string, probably not too big for a simple but helpful utility, could be compiled into a newLisp.exe such that running the exe began execution with eval'ing that string (rather than looking for an init.lsp). This would have a few advantages over the external init file approach viz

- the exe could be moved around without having to remember the init file

- multiple of these exe could reside in the same directory without init files interfering with each other

- the extra step/bother of having a batch/bash file to run newLISP with the desired startup lisp file is removed

- it is more secure (the internal string could even be largely encrypted although an external one could also) than running the risk of someone fiddling with an external lsip file



For me moving the exe around without having to remember the init file is the main advantage.

I see the overhead of multiple exe sizes as not an issue in the day of 200G drives and the small footprint of newLISP.



This option would be very much in the spirit of newLISP as having as one of its many strengths the nature of being great 'little' language to do utilities in. I say little compared to Common Lisp systems!



An thoughts on an exe the inits from an internal string?



Nigel

Lutz

#12
Such a thing already exists! look for the file "link.lsp" in the newLISP source distribution. This file lets you link (and encrypt) lisp source with the newLISP executable, which then gets automatically executed when the executable is started. It works for both, Win32 and Linux.



Lutz

nigelbrown

#13
Well done Lutz! I'll take it for a spin.



It's such an important example it must be mentioned in the manual! and not just languish in the examples directory - but I'm sure that was on the to-do list.



Thanks

Nigel

HPW

#14
Lutz,



>But it would be something after version 8.0. I will try then to make something like a 'string device' which also could be used in other circumstances, wherever a file is used.



Sounds very good. Until that time I will use the other options.





Nigel,



Besides the option with link.lsp for smaller solutions, you can also use freewrap, starpacks/TclDevKit to make larger self contained apps. There you can pack large number of files in virtual files systems inside the app. You can find discussion about this (freewrap) here on the forum.
Hans-Peter