Author Topic: lisp don't work!!!why? (non-ANSI characters)  (Read 4961 times)

0 Members and 1 Guest are viewing this topic.

userzhl

  • Mosquito
  • Posts: 6
lisp don't work!!!why? (non-ANSI characters)
« on: May 10, 2012, 09:15:29 PM »
(setq file "D:\\窗帘_F9AAFA6A-5E9B-4C46-8283-F10E8F70936A.atc")
(setq new "c:\\《工具》\\《工具》")
(setq fn (open file "r"))
(setq data (read-line fn))
(close fn)
(SETQ DD (x$sub new "D:\\《工具》" DATA))
(setq Fn2 (open file "w"))
(write-line DD Fn2)
(close Fn2)
;lisp don't work!!!why?
« Last Edit: May 28, 2012, 08:53:01 AM by CAB »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: lisp don't work!!!why?
« Reply #1 on: May 10, 2012, 09:56:14 PM »
What is this function ?
x$sub


ps:
Poll is not needed to post question :)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

userzhl

  • Mosquito
  • Posts: 6
Re: lisp don't work!!!why?
« Reply #2 on: May 11, 2012, 02:07:13 AM »
(defun x$sub (n$ o$ str / n)
  (setq n 0)
  (while (setq n (vl-string-search o$ str n))
   (setq str (vl-string-subst n$ o$ str n))
   (setq n (+ n (strlen n$)))
  )str
)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: lisp don't work!!!why?
« Reply #3 on: May 11, 2012, 04:06:57 AM »
The application appears to work fine on my system, with the exception of the asian characters. I cannot test that because they do not copy correctly to my computer.

I saved your files, created a test lisp and successfully changed "<GeneralProperties>" to "<SpecialProperties>" without issue.

I did make one change to your code and that was to eliminate the starting point for the string search and string substitute. Because you are editing every occurence in the file, you can safely omit that parameter.

Code - Auto/Visual Lisp: [Select]
  1. (defun x$sub (n$ o$ str / n)
  2.   (while (vl-string-search o$ str)
  3.    (setq str (vl-string-subst n$ o$ str))
  4.   )str
  5. )


Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Lee Mac

  • Seagull
  • Posts: 12923
  • London, England
Re: lisp don't work!!!why?
« Reply #4 on: May 11, 2012, 05:59:42 AM »
I did make one change to your code and that was to eliminate the starting point for the string search and string substitute. Because you are editing every occurence in the file, you can safely omit that parameter.

I would be inclined to retain the position argument to account for the case in which the replacement string contains the search string pattern, e.g. replacing 'is' with 'this'. For such an example, the ommission of the incrementing position argument would cause the code to loop infinitely.

userzhl

  • Mosquito
  • Posts: 6
Re: lisp don't work!!!why?
« Reply #5 on: May 11, 2012, 08:43:23 AM »
Keith™
Lee Mac
still don't work?

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: lisp don't work!!!why?
« Reply #6 on: May 11, 2012, 12:28:18 PM »
I think it may be the asian characters. Can you verify that vl-string-subst and vl-string-search accurately finds the asian characters?
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

userzhl

  • Mosquito
  • Posts: 6
Re: lisp don't work!!!why?
« Reply #7 on: May 11, 2012, 09:23:38 PM »
;and why this is all right?
(defun x$sub (n$ o$ str / n)
  (setq n 0)
  (while (setq n (vl-string-search o$ str n))
   (setq str (vl-string-subst n$ o$ str n))
   (setq n (+ n (strlen n$)))
  )str
)
(setq file "D:\\窗帘_F9AAFA6A-5E9B-4C46-8283-F10E8F70936A.atc")
(setq new "c:\\")
(setq fn (open file "r"))
(setq data (read-line fn))
(close fn)
(SETQ DD (x$sub new "D:\\" DATA))
(setq Fn2 (open file "w"))
(write-line DD Fn2)
(close Fn2)

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: lisp don't work!!!why?
« Reply #8 on: May 14, 2012, 08:30:26 AM »
As Keith has hinted at ... it's due to the non-ANSI characters. Remember the vl-* functions were made originally in the mid-late 90's (Vital Lisp): long before ACad became UniCode compliant. So it's no wonder it has some strange quirks when using unicode characters on these functions.

Try to rewrite your x$sub function to only use AutoLisp function calls instead of Visual Lisp. I've found that the direct ARX functions like substr/wcmatch/strcat/etc. seem to work with strings which confuse vl-string-* functions.

Edit, perhaps this (though it's not very efficient):
Code - Auto/Visual Lisp: [Select]
  1. (defun str-subst-all (new old string / search return)
  2.   (setq search (strcat old "*") return "")
  3.   (while (not (eq string ""))
  4.     (if (wcmatch string search)
  5.       (setq string (substr string (1+ (strlen old))) return (strcat return new))
  6.       (setq return (strcat return (substr string 1 1)) string (substr string 2))))
  7.   return)
« Last Edit: May 14, 2012, 08:39:59 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

userzhl

  • Mosquito
  • Posts: 6
Re: lisp don't work!!!why?
« Reply #9 on: May 27, 2012, 10:47:08 PM »
还是没解决 :-(

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: lisp don't work!!!why?
« Reply #10 on: May 28, 2012, 03:30:11 AM »
还是没解决 :( <Still not resolved>
OK, I've created a file named test.txt on my C drive in a folder with this path:
Code: [Select]
C:\\TEMP\\测试\\test.txtIf I type the folling into the command prompt:
Code: [Select]
Command: (setq fn (findfile "C:\\TEMP\\测试\\test.txt"))
"C:\\TEMP\\测试\\test.txt"
As you can see it works fine for unicode letters in the path.

What might have an issue is the lisp write-line as it saves to file in ANSI format. Perhaps look into something Lee's done before: http://www.theswamp.org/index.php?topic=40709.0

Edit: It seems the open function causes an issue already:
Code: [Select]
Command: (setq fn (findfile "C:\\TEMP\\测试\\test.txt"))
"C:\\TEMP\\测试\\test.txt"

Command: (setq f (open fn "r"))
nil
So I'm guessing you might have to use a FileSystem object to obtain the 8.3 names for the path and then send that to open instead of the unicodes.
« Last Edit: May 28, 2012, 03:33:18 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: lisp don't work!!!why?
« Reply #11 on: May 28, 2012, 05:23:10 AM »
It seems even the FileSystem object gives issues:
Code: [Select]
Command: (setq fs (vlax-get-or-create-object "Scripting.FileSystemObject"))
#<VLA-OBJECT IFileSystem3 00000000006d66d0>

Command: (setq fn (findfile "C:\\TEMP\\测试\\test.txt"))
"C:\\TEMP\\测试\\test.txt"

Command: (setq f (vlax-invoke fs 'GetFile fn))
; error: Exception occurred
Perhaps you need to use DosLib.
Code: [Select]
Command: (setq fn (findfile "C:\\TEMP\\测试\\test.txt"))
"C:\\TEMP\\测试\\test.txt"

Command: (setq sfn (dos_shortpath fn))
"C:\\TEMP\\D147~1\\test.txt\\"

Command: (setq sfn (vl-string-right-trim "\\" sfn))
"C:\\TEMP\\D147~1\\test.txt"

Command: (setq f (open sfn "r"))
#<file "C:\\TEMP\\D147~1\\test.txt">

Command: (read-line f)
"C:\\TEMP\\还是没解决"

Command: (close f)
nil
Notice though that read-line reads the UTF8 file as if it's an ANSI encoded file. So stuff inside that file needs to be converted properly - see the link in my previous post for working with unicode files properly.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

userzhl

  • Mosquito
  • Posts: 6
Re: lisp don't work!!!why? (non-ANSI characters)
« Reply #12 on: May 28, 2012, 10:51:33 AM »
thank you irneb!
read the file is OK!
(setq file "d:\\窗帘_F9AAFA6A-5E9B-4C46-8283-F10E8F70936A.atc")
(setq data (vk_ReadTextStream file "utf-8"))

But,how to replace data?