Re: Writing uncommon ASCII characters programmatically

"Gabriel Scherer [email protected] [ocaml_beginners]" <[email protected]>
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <CAPFanBHoxBkqU=bKsTxUbEQb3Jqn8id+oamfzNyC=RnS8ydoqw@mail.gmail.com>
\ddd will output precisely the byte you ask for. How it will be interpreted
by the reader depends on the encoding it has been configured for. "It is
misread in the .sql file" doesn't mean much, it all depends on which
software reads that file and how it is configured -- whether it expects
UTF-8 input, or ISO-latin1, or just plain ASCII.

On Tue, Jan 27, 2015 at 10:24 AM, Doyle Jonathan [email protected]
[ocaml_beginners] <[email protected]> wrote:

>
>
> In my project where I use Ocaml to manipulate MySQL files, I need to wtite
> some text into files programmatically, and I am experiencing some trouble
> when the text has some accentuated Latin characters :
>
> 1) When I type accentuated character directly in the Ocaml source
> interpreter, it is misread in the receiving .sql file (this is example s1
> in the code below)
>
> 2) According to the Ocaml manual, I could use \ddd where ddd is the ASCII
> decimal code for the character. When I try that, it is misread  n the
> receiving .sql file again, but in a different way ( this is example s2 in
> the code below).
>
> Below I copied some functions from my personal library, then explained
> miscelleneaous attempts to write correctly an accentuated Latin character.
>
> Code from my personal library :
>
> type complete_filename=CFN of string;;
> type vague_filename=string;;
> let make_filename_complete (s:vague_filename)=
>   let home=Sys.getenv("HOME") in
>   if s="" then CFN(home) else
>   let c=String.get s 0 in
>   if c='/' then CFN(s) else
>   if c='~' then CFN(home^(String.sub s 1 (String.length(s)-1))) else
>   CFN((Sys.getcwd ())^"/"^s);;
> let open_out_locally x=try open_out(x) with
> _->failwith("File "^x^" cannot be opened out");;
> let erase_file_and_fill_it_with_contents_of_buffer (vx:vague_filename) b=
>    let x=(fun (CFN(u))->u)(make_filename_complete(vx)) in
>   let john=open_out_locally(x) in
>   (Buffer.output_buffer(john)(b);close_out john);;
> let erase_file_and_fill_it_with_string s (x:vague_filename)=
>    let n=String.length(s) in
>    let b=Buffer.create(n) in
>    let _=Buffer.add_string b s in
>    erase_file_and_fill_it_with_contents_of_buffer x b;;
>
>
> And here are my failed attempts :
>
> let s1="This is the French e grave character : è"  ;;
> erase_file_and_fill_it_with_string s1 "example.sql";;
>
> It produces the following output in example.sql :
> This is the French e grave character : è
>
> let s2="This is the French e grave character :\133"  ;;
> erase_file_and_fill_it_with_string s2 "example.sql";;
>
> It produces the following output in example.sql :
> This is the French e grave character :Ö
>
>
>
>
>
> 
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.