Writing uncommon ASCII characters programmatically
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
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 :Ö