Question
Norman W. Freeman · Oct 1, 2020

Is there a built-in method to escape special characters in a string, in a similar way to what is done by Portal with global data ?

If a global node contains special characters, (eg : a line returns), it will be displayed like this in Portal ("System > Globals > View Global Data" panel) :

^A(1) = "this is"_$c(13,10)_"a test"

I would like to export global data to a txt file using a similar format.

I already wrote the main code (that loops on all nodes and dump them to file), the problem is how to handle special characters.
For the moment I replace them manually one by one. It works, but it's far from perfect :

set text = ... //get global data
set text = $replace(text,"""","""""") //double the quotes
set text = $replace(text,$c(13),"""_$c(13)_""") 
set text = $replace(text,$c(10),"""_$c(10)_""")
...

Is there a built-in function in cache that handle special characters in a string by splitting it into smaller strings (with concatenation in between), something similar what Portal does ? 
Or even better : a built-in function that can export a global to a txt file directly, in a format similar to Portal.
 

10
0 5 86 4

Replies

Hi Norman,

Not sure if this helps, but write commands generally interpret $char whereas zwrite won't. I'm not sure what you're using to output to a file but perhaps you could try using zwrite.

I'm not sure what the portal uses, maybe somebody else has a cleaner solution.

Further elaborating on the suggestion of @Vic Sun 

set file="c:\whateverfilename.txt"   ;according to file system and access rights
if $data(^%SYS)    ; or whatever global
open file:("WNS")
use file zwrite @$ZR  ; send all output to file
close file

you can even verify it from terminal command line and the result looks like this:

^%SYS("tercap","vt320","bin","hon")=$c(3,155)_"1m"
^%SYS("tercap","vt320","bin","ich")=$c(0,0,155,129,0,0)_"@ÿ"
^%SYS("tercap","vt320","bin","il")=$c(0,0,155,129,0,0)_"Lÿ"
^%SYS("tercap","vt320","bin","il1")=$c(2,155)_"L"
^%SYS("tercap","vt320","bin","ind")=$c(2,27)_"D"
^%SYS("tercap","vt320","bin","init")=$c(18,155)_"!p"_$c(155)_"62""p"_$c(27)_"F"_$c(27)_")0"_$c(155)_"?7h"                                                      ^%SYS("tercap","vt320","bin","poff")=$c(3,155)_"4i"
^%SYS("tercap","vt320","bin","pon")=$c(3,155)_"5i"
^%SYS("tercap","vt320","bin","ri")=$c(2,27)_"M"
^%SYS("tercap","vt320","bin","ron")=$c(3,155)_"7m"
^%SYS("tercap","vt320","bin","uon")=$c(3,155)_"4m"
^%SYS("tercap","vt320","cols")=80
^%SYS("tercap","vt320","flags")=16
^%SYS("tercap","vt320","lins")=24
^%SYS("tercap","vt320","names")="vt320|VT320|DEC-vt320"
^%SYS("tercap","vt320","src","aoff")="$c(155),""0m"""
^%SYS("tercap","vt320","src","bell")="$c(7)"
^%SYS("tercap","vt320","src","bon")="$c(155),""5m"""
^%SYS("tercap","vt320","src","bormap")="1,108,1,108,1,107,1,107,1,109,1,109,1,106,1,106,1,113,1,113,1,120,1,120,1,113,1,113,1,120,1,120,1,108,1,107,1,109,1,106,1,113,1,113,1,120,1,120,1,97,1,97,1,97,1,97,0,42,1,126,1,97"
^%SYS("tercap","vt320","src","clr")="$c(155),""H"",$c(155),""J"""
^%SYS("tercap","vt320","src","coff")="$c(155),""?25l"""

Wonderful. I works like a charm. Thank you. 

Hi

Just to pad out that example:

set gbl="^%SYS",x="",file="c:\temp\myfile.txt"

open file:("WNS"):0

else  w !,"Unable to open file" quit

for  set x=$o(@gbl@(x)) q:x=""  zw @$ZR  w !

close file

for multiple globals:

set file="c:\temp\text.txt"

open file:"("WNS"):0

for gbl="^ABC,^XYZ,^PQTY,^%Nigel"  set x="" for  set x=$o(@gbl@(x)) w !!:x=""  q:x=""  use file zw @$ZR

close file

Nigel

Hi Norman,
Quote() method of %Global might be what you are lookig for:

 USER>write ##class(%Global).Quote($lb("abc", """", $c(13, 10)))
$lb("abc","""",$c(13,10))