String manipulation help

String manipulation help

schmidtjBEEVY
Advocate Advocate
463 Views
5 Replies
Message 1 of 6

String manipulation help

schmidtjBEEVY
Advocate
Advocate

Hello All,

 

I am trying to write a lisp function that looks at a file address and changes the address from a 'drive:user\username\folder1\subfolder2' format to 'drive:user/username/folder1/subfolder2' format.

 

My end goal is to have a set of functions that locate a properties file, reads the information to confirm proper formatting, then  prompt the user for new information to write to the properties file. I have everything mostly written and working so far, and am starting to integrate them together. The snag I've run into is the findfile function outputs the location in a string in a 'drive:user\username\folder1\subfolder2' format. To open a file I need the string formatted like 'drive:user/username/folder1/subfolder2' 

 

I wrote a function to go thru a string and identify instances of '\', but it appears to never exit my while loop. I am assuming that strings in lisp end in a nul character like in other languages, I am using nil for my exit comparison in this case. I am hoping someone can look at what I've got and tell me what I'm doing wrong, or if I am fundamentally not understanding how strings work in LSP.

 

Thanks in advanced,

JD

 

Reference I am using for findfile: https://help.autodesk.com/view/OARX/2018/CHS/?guid=GUID-D671F67D-F92B-41FF-B9FA-A48EF52CF607

Note: it says in this reference that the string returned is suitable for the open function, but that is not the case as of writing this post.

0 Likes
Accepted solutions (1)
464 Views
5 Replies
Replies (5)
Message 2 of 6

Kent1Cooper
Consultant
Consultant

A couple of quickie things I notice:  I think you want this [in two places, and with substr spelled right in both places]:

(substr fname 1 count)

to be, instead:

(substr fname count 1)

 

And this:

(if (/= '\'

to be, instead:

(if (/= "\\"

Kent Cooper, AIA
Message 3 of 6

Kent1Cooper
Consultant
Consultant

Also, I don't see any reason to do this:

 

(if (/= '\' (sbustr fname 1 count))
(setq beepee "meh");; THEN expression - Do [functionally] nothing if true
;; ELSE expression
;;;;;;;;;;;;;;; First Folder ;;;;;;;;;;;;;;;;
(progn

 

when you can test for it to be equal rather than not-equal, and make the procedure the THEN expression, and just forget any ELSE expression:

 

(if (= "\\" (substr fname count 1))
;; THEN expression
;;;;;;;;;;;;;;; First Folder ;;;;;;;;;;;;;;;;
(progn

Kent Cooper, AIA
Message 4 of 6

Kent1Cooper
Consultant
Consultant

... and another thing ...

 

Look into the use of the (cond) function instead of a series of nested (if) functions with the successive ones being the 'else' expressions of the prior ones.  If I understand what's going on [and I haven't dug very deep], one advantage is that it will save you a bunch of (progn) "wrappers" around your 'then' expressions, because when a (cond) function's test expression returns something other than nil, it goes ahead and does what's in the rest of the condition, without the need for that "wrapper" because it's not a 'then'-vs.-'else' situation requiring a single function for each [which is what (progn) does for you in (if) functions].

Kent Cooper, AIA
Message 5 of 6

schmidtjBEEVY
Advocate
Advocate

Thanks for all the pointers, Kent. I was looking for a function similar to case statements, and hadn't noticed cond previously. I will implement these changes tomorrow, and see if it works.

 

Best,

JD

0 Likes
Message 6 of 6

ВeekeeCZ
Consultant
Consultant
Accepted solution

First of all, I don't really understand the purpose. But I don't have an acade, whatever that is.

 

Yes, findfile returns path with "\\", but open can work with it. The slash-forward is a substitute to make your work easier (you don't need to think of the first \ which is an escape-char - read THIS explanation) but it's not a necessity. Not sure if there is any command that requires the usage of "/" and does not support "\".

 

Anyway, if you really need it for some reason, we have a function for this:

 

(vl-string-translate "\\" "/" nfile)