How can I use fprintf to write text from a TEdit control to a file in C++Builder 10.1 - c++builder

I'm pretty sure this worked in an earlier version of C++Builder but now I need to figure out the easiest way to get this to work in C++Builder 10.1.
fprintf(out, "%s\n", Edit1->Text);
I have no idea why, but right now it seems to be writing only the first character of the text in Edit1 to the file.
Also if anyone has a link to a good source for best practices for dealing with strings C++Builder 10.1, I would be grateful.

After some more reading it seems the solution is pretty easy.
fwprintf(out, L"%s\n", Edit1->Text);
What I find most odd is, as near as I can tell, the Text property doesn't have a type. How are you supposed to know work with it when it has no type?

I'm pretty sure this worked in an earlier version of C++Builder
Maybe, but only in pre-CB2009 versions, where the Text was an AnsiString (since CB2009, it is now a UnicodeString), and only as a fluke of undefined behavior, since you can't pass an (Ansi|Unicode)String object to fprintf() to begin with, it expects a pointer to a C-style character string instead. The sole data member of an AnsiString is a char*, and the sole data member of a UnicodeString is a wchar_t*/char16_t* on Windows/Posix (respectively).
now I need to figure out the easiest way to get this to work in C++Builder 10.1.
You have a few choices:
cast the Text to an AnsiString and then use its c_str() method to get a char* pointer:
fprintf(out, "%s\n", AnsiString(Edit1->Text).c_str());
use fprintf() with the %Ls placeholder instead:
fprintf(out, "%ls\n", Edit1->Text.c_str());
use fwprintf() instead:
fwprintf(out, L"%s\n", Edit1->Text.c_str());
use a more modern file I/O library, like the standard C++ std::ofstream, or the RTL's TStreaWriter, etc.
I have no idea why, but right now it seems to be writing only the first character of the text in Edit1 to the file.
Because you are telling fprintf() (via %s) to expect an 8bit null-terminated char* string, but you are passing it a 16bit wchar_t* string instead. ASCII characters have 0x00 bytes in them in 16bit strings.
Also if anyone has a link to a good source for best practices for dealing with strings C++Builder 10.1, I would be grateful.
Have you read Embarcadero's Migration and Upgrade documentation yet? It has a section on migrating ANSI code to Unicode.

Related

Convert from UTF-8 to Shift-JIS, in Pascal

I need the Pascal (Delphi or Free Pascal) function to convert UTF-8 text (e.g. in string) to Shift-JIS (also e.g. in string).
For Shift-JIS => UTF-8 I found the code, which is here. Now I need the backward converter.
I want to add this code to Lazarus library LConvEncoding, so it must be without using 'iconv' or Win32 API, it must be pure Pascal. It may be code in another language if it's easy to rewrite it in Pascal.
I found the answer, this code can be made from the Unicode official data:
ftp://ftp.unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/SHIFTJIS.TXT .
That file tells how to get UTF-16 codes, and next I must convert UTF-16 -> UTF-8, using UTF8Encode().

Delphi xe8 error reading textfile created from Delphi 7 version and vice versa

I try to convert a code from Delphi 7 to delphi xe8 and I cannot find a solution to the following case.
Our old application creates a txt file which first row is like that
±HEADER―ID°N1799―USER_ID°N1―PATH_NAME―R_DATABASE°TC:\DATA―R_SERVER°TTEST_SRV―R_COMPUTER°TMYPC―
Char(―) is chr(175).
We tried to read the already created file from our new application with Delphi xe8 like that:
StrData := TStringList.Create;
StrData.LoadFromFile(sFile);
StrData.Text returns the desired text but chr(175) is replaced with chr(8213).
In order to go on I did the followings:
StrData.LoadFromFile(sFile,TEncoding.ANSI);
StrData.Text := StringReplace(StrData.Text,Chr(8213),Chr(175),[rfReplaceAll]);
What I cannot solve is the opposite case.
I have to create that file from Delphi xe8 so as it would be exactly the same with the one produced from the old delphi 7 application.
At the beginning I used the same code we had:
StrData.SavetoFile(sFile); //returns text but chr(175) is replaced with (?)
Also i tried all encodings with no results.
StrData.SavetoFile(sFile,Ansi);//returns text but chr(175) is replaced with (?) etc.
The same results also when converts the code to TStreamFile or textfile.
base64 encode files
Old one - Correct one (StrData.SavetoFile(sFile)) //Delphi 7
wrFIRUFERVLigJVJRMKwTjE4NjbigJVSSUTCsE4zNjHigJVDU0lURV9JRMKwTjHigJVSU0lURV9JRMKwTjEwMeKAlVNTSVRFX0lEwrBOMeKAlVRSTl9EQVRFwrBENDIzODIuNjA2NzkzOTgxNeKAlVVTRVJfSUTCsE4x4oCVUEFUSF9OQU1FwrBUXFxkZWxwaGkyMDEycjJcQkVORUZJVF9URVNUXFBBX09GRklDRV9WU0xcVFJBTlNGRVJcRVhQT1JU4oCVRklMR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Ul9QQVRIwrBUwq9TSVpFX1BDS8KwTsKvU0laRV9EQVRBwrBOwq9TSVpFX0FUQ0jCsE7Cr1NJWkVfRE9DU8KwTsKvREFURV9JTsKwRMKvUl9EQVRBQkFTRcKwVMKvUl9TRVJWRVLCsFTCr1JfQ09NUFVURVLCsFTCrw==
Any ideas?
The file saved by your Delphi 7 program is UTF-8 encoded. I decoded the base64 that you supplied and look at it in a hex editor. It looks like this:
The first two bytes are C2 B1. That is the UTF-8 encoding for ±. You can check that here: https://mothereff.in/utf-8.
Use LoadFromFile(..., TEncoding.UTF8) to load the file, and SaveToFile(..., TEncoding.UTF8) to save it. That's all you need to do. Note that when you save this way then a BOM will be included in the file. If that is not desired then it possible to omit the BOM, as has been covered here before.
Do note that you must remove the call to StringReplace. That modifies the text and serves no useful purpose. You absolutely do not wish to replace U+2015 ― with U+00AF ¯.
Based on the comments to this answer it seems that you have some Delphi 7 code that produced UTF-8 encoded text which behaves incorrectly when executed by Delphi XE8. That's not surprising due to the change from ANSI to UTF-16. You will need to revisit this code and adapt it appropriately. It's impossible for us to say more given the fact the only you have this code.
It feels very much as though you are trying things almost at random and hoping for a quick fix. That is not productive. You will only make progress with a clear understanding of Unicode, and your program. You will need to step back, slow down, and fill in the gaps in your knowledge.

How to programmatically set application name in Japanese?

Currently I am trying to set application name using
net.rim.blackberry.api.homescreen.HomeScreen.setName("これはある");
but it throws exception: IllegalArgumentException.
Can anyone provide the solution?
I am using Blackberry JDE 5.0.
This is probably a string encoding problem. Try
new String(new String("これはある").getBytes("UTF-16BE"), "UTF-16BE");
It's not pretty but I think that will work.
Here's a link to the Blackberry string spec: http://www.blackberry.com/developers/docs/5.0.0api/java/lang/String.html
By default it's ISO-8859-1 which does not include Japanese characters.
The problem you are facing is how to get a string represented in your source code into your application with the same characters. For latin characters, this is pretty straightforward, as we can just put the characters in quotes, and get a string, like "Hello world"
When you go to non-latin, like Japanese, it gets harder. You can still directly write Japanese in your source code, but you need to make sure your editor and your compiler agree on an encoding so that the characters can be interpreted correctly. The Java-SE compiler takes an argument "-encoding" which allows you to specify the encoding of your java source files.
Unfortunately, rapc, the BlackBerry compiler, does not offer an option to specify encoding, even though it is invoking javac itself. So rapc uses the platform default, which is utf-8 on Linux and OSX and iso-8859-1 on Windows.
The way around this problem is to use a feature of the Java language for parsing strings - unicode escaping. By entering the six character sequence "\u3053" in a string, the java compiler will parse that number as hexidecimal and use the corresponding unicode code point, solving problems with source file encoding.
So "Hello world" and "\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064" will result in the same strings appearing in your class files.
Because of this, Svetlin's answer from the comments is the right approach here:
net.rim.blackberry.api.homescreen.HomeScreen.setName("\u3053\u308C\u306F\u3042\u‌​308B");

Strange string behaviour with dll call

I have a Delphi 2007 Program, which calls a Delphi 2010 DLL. The Program is big and not yet ported to 2010, so there is no way i can change this right now.
I use SimpleShareMem Unit to pass strings but also tried ShareMem with borlndmm.dll.
For one function i now pass a string from the Delphi 2007 programm to the dll (Therefore AnsiString). When debugging the dll, this AnsiString looks normal and can be viewed in the watch list. But if i do a simple think like
AnAnsiString := PassedAnsiString;
the variable AnAnsistring gets the value '???????'#0#0#0'???A', but PassedAnsiString is still readable in watchlist. Also a string append like
AnAnsiString := PassedAnsiString + NotPassedAnsiString;
uses '???????'#0#0#0'???A' as value.
Where is the problem? Is it a 2007 <-> 2010 issue? How to fix it?
Thanks for help.
This is probably because an encoding field that was added to ansistring in D2009. IOW the record (TAnsiRec) at negative offset of the ansistring pointer is different and shifted between unicode and non unicode Delphi's (ansistring changed too in unicode versions!)
I don't think there is a decent solution for this except dropping back to p(ansi)char level

How does acrobat encode annotations added as sticky notes to pdfs?

We have been reading and writing Sticky Notes/Annotations/Comments to pdfs via an activex control in our application for a number of years. We have recently upgraded to Delphi2009 with Unicode Support. The following is causing problems.
When we call
CAcroPDAnnot.GetContents
The results seem to be rather strange and we lose our Unicode Chars. It is not like saving as an ansi string which would usually result in returning ????? instead we get a string such as
‚És‚­“ú‚É•—Ž×‚ð‚Ђ¢‚½‚ç
For a string of Japanese characters.
However if I save the comments in the pdf to a datafile via the menu in the pdf itself it is written to file as something like
0kˆL0Oeå0k˜¨ª0’0r0D0_0‰
The latter can be export and reimported into an acrobat pdf and will recreate the correct unicode characters. However once I call CAcroPDAnnot.GetContents in my code it is coming back as something else.
Is CAcroPDAnnot.GetContents broken?
Is there an encoding scheme I should be aware of?
Is there an alternative I might be able to do?
Thanks
‚És‚­“ú‚É•—Ž×‚ð‚Ђ¢‚½‚ç
That's the string:
に行く日に風邪をひいたら
in CP-932 aka Shift-JIS encoding, an awful but lamentably still-popular encoding in Japan.
You're currently interpreting it in as CP-1252 (Windows Western European). If your PDF-reading component won't convert it for you automatically, you'll need to find a way to detect what encoding the document is in and convert it manually.
I don't know what Delphi provides for reading encodings, but have you got the encodings for Shift-JIS installed in Windows, from the Control Panel -> Regional Options -> "Install files for East Asian languages" option? If not, that might explain why it'd be failing to convert automatically, perhaps.
You're not exactly giving us a lot of information to work with.
I take it you're talking about the "Acrobat.CAcroPDAnnot" class' method GetContents here. Which version of Acrobat are you using? Have you perhaps switched versions (or run an update) around the time you started programming with Delphi 2009?
Then: how did you instantiate the object? If using a *_TLB.pas file generated from the DLL, are you certain it still matches it? (Try re-generating it, if uncertain).
Third: how are you calling the method? What type of variable are you assigning the result to?
What might also help, is if you could provide a sample of an annotation (preferably including non-ASCII chars); and for that annotation:
what it should look like (and what it does look like inside Reader)
what it returns when using a pre-2009 version of Delphi*
what it returns when using Delphi 2009*
(* preferably the HEX byte codes of the (ansi/wide)strings; but output from the Ctrl-F7 inspector should do)
Then maybe someone could provide a more meaningful answer.
Ok, one of the main differences between Delphi 2009 and the earlier versions is that the default string type is an unicode string. That means that if you use the same ActiveX component as in previous versions, you are passing unicode strings to ascii strings and that is usually not a good idea.
There are a couple of solutions for this problem:
Try if you can upgrade your activeX component so that it supports full unicode strings.
Use AnsiString and not string to communicate with the activeX component. In this case, you can still use the old interface, but you are still bound to the same limitations.
Use an other control that creates pdf. There is a lot to find, but be prepared to change a big chunk of your software. (Some controls are XML based and use encoding. )

Resources