TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Peter2 on October 25, 2013, 09:50:20 AM

Title: Converting LIST to STR and backwards?
Post by: Peter2 on October 25, 2013, 09:50:20 AM
I use some LISTs which I have to save into a text-file (INI), and when I read them from INI and want to use them as LISTs..

Any ideas? Standard commands which I missed? Special routines here or in DosLIB?

Thanks and regards
Title: Re: Converting LIST to STR and backwards?
Post by: Lee Mac on October 25, 2013, 10:01:45 AM
Maybe this thread (http://bit.ly/1aJyxF5) will help?
Title: Re: Converting LIST to STR and backwards?
Post by: Marc'Antonio Alessi on October 27, 2013, 02:32:55 PM
Why not write directly in Lisp lists?
See attached.

Code: [Select]
; Function: ALE_UtlCfg_WriteData
;
; Version 1.00 - 21/06/2006
;
; Example: (ALE_UtlCfg_WriteData '#CfgData #CfgFile)
;
(defun ALE_UtlCfg_WriteData (DatNam LspOut / FilPnt TmpVal SysTim)
  (if #CfgFlag
    (progn
      (vl-file-delete (strcat LspOut ".bak"))
      (cond
        ( (vl-file-rename LspOut (strcat LspOut ".bak")) )
        ( T
          (alert
            (strcat
;|e|;         "Impossible to rename CfgData.lsp!\n\n"
;|e|;         "This is normal for first installation.\n"
            )
          )
          (vl-file-rename LspOut (strcat LspOut ".bak"))
        )
      )
      (setq FilPnt (open LspOut "w") #CfgFlag nil)
      (write-line (strcat "(setq " (vl-symbol-name DatNam) " '(") FilPnt)
      (foreach ForElm (vl-symbol-value DatNam)
        (princ "(" FilPnt) (prin1 (car ForElm) FilPnt) (princ "\n" FilPnt)
        (foreach ForEl2 (cdr ForElm)
          (if (= 'REAL (type (setq TmpVal (cdr ForEl2))))
           (progn
             (princ "  (" FilPnt) (prin1 (car ForEl2) FilPnt) (princ " . " FilPnt)
             (princ (rtos (cdr ForEl2) 2) FilPnt) (princ ")\n" FilPnt)
           )
           (progn (princ "  " FilPnt) (prin1 ForEl2 FilPnt) (princ "\n" FilPnt))
          )
        )
        (write-line ")" FilPnt)
      )
      (write-line "))" FilPnt)
      (close FilPnt)
      (while (not (setq SysTim (vl-file-systime LspOut))) (ale_fuzzy 10))
      SysTim
    )
  )
)
Title: Re: Converting LIST to STR and backwards?
Post by: Peter2 on October 27, 2013, 03:27:21 PM
Hi Lee, hi Marc'Antonio

thanks for your codes. Both are very powerful ideas which I will save for (maybe) later use.

But it seems that my question was not precise enough; the basic consideration was:

a) I have a text-based information which is connected with a "value"
b) The first part has a defined length, the second part can be concatenated with different parts and have a different length
c) This info has to be stored in a text-file
d) After reading from the text file the info has to be parsed again (in its different parts)

Because all info is text it could be a problem to define the "delimiter" between the "parts" - , : - _ / ; are not allowed as delimiter.
So the best thing would be to save it in a list: no problems with length, no problems with delimiters, ..

pseudo-Example of the INI-file:

Code: [Select]
[Definitions]
drilling=((modus . select) (Content ("this is an [important] text") ([LOGINNAME]) ("nice weather today, really {hot}") ({attdef.blockname}))

You see, the "Content" has a user-defined, free combination of standard text and different "dynamic values" which must be managed by the software ...
But the problem is: How to store this stuff in a text file and read it as list?

In the meantime I modified my code and try with delimiters

Code: [Select]
[Drilling]
modus=select
value= {SPEISUNG.RCS_OUTPUT_v01} {MELDUNG_NR..RCS_OUTPUT_v01}

The idea is that delimiter " " (Alt+0160) and "" (Alt+0182) are strange enough that nobody would use them in a text.

But my code is not proved and not done now - if someone has a better idea than my delimiters or a solution for my list I would be happy to see it
Title: Re: Converting LIST to STR and backwards?
Post by: snownut2 on October 27, 2013, 04:55:07 PM
Peter,

Could you possibly save the list in a CSV file.  Its not per say a text file, but can be viewed with notepad, excel, or any other text viewer.

That way you can separate your values with commas.....

Bruce
Title: Re: Converting LIST to STR and backwards?
Post by: Peter2 on October 27, 2013, 05:24:48 PM
Hi Bruce

beside the "logical" disadvantages of CSV related to INI there would be a problem to save "hello, my name is John" ..
Title: Re: Converting LIST to STR and backwards?
Post by: Lee Mac on October 27, 2013, 06:03:17 PM
How about using an arbitrary delimiter character (such as '=') in your INI file and mark any occurrences of the delimiter character within the actual data as literals using a backslash prefix that your parsing function could then test for.

This is how a CSV file differentiates cell delimiter characters (commas / semi-colons) from such characters found within the cell content, only using quotation marks as literal indicators.
Title: Re: Converting LIST to STR and backwards?
Post by: Peter2 on October 27, 2013, 06:14:42 PM
How about using an arbitrary delimiter character (such as '=') in your INI file and mark any occurrences of the delimiter character within the actual data as literals using a backslash prefix that your parsing function could then test for....
Hi Lee
if I understand you correctly this is similar to my delimiters mentioned above?
Title: Re: Converting LIST to STR and backwards?
Post by: Lee Mac on October 27, 2013, 06:34:42 PM
if I understand you correctly this is similar to my delimiters mentioned above?

As I understand it, you are suggesting the use of obscure delimiter characters, with the assumption that such characters will not appear in the content; I am suggesting using any delimiter character and marking those which appear within the content as literals.

For example, if '=' is the delimiter:
Code: [Select]
key=some\=textWould be read as:
Code: [Select]
(key . "some=text")
Then, for literal backslashes:
Code: [Select]
key=some\\textWould be read as:
Code: [Select]
(key . "some\text")(of course, this would appear as "some\\text", since the backslash is also an escape character in AutoLISP).