LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-12-2010, 03:37 AM   #1
hs_linux
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Rep: Reputation: 0
wcslen / _tcslen not returning correct value


I have having some trouble working with wide characters, which is,

Sample code:
TCHAR fname[512];
_tcslcpy(fname, L"/root/Desktop/all/TheGrandTunnels.fgc", 512);
printf("%d", _tcslen(fname));

The output that I expect is 37 but it gives 19 (ie just half of 37).

Has it something to do with the locale setting?

I looked at makefiles and found the following,
WLOCALE = -fexec-charset=8859_1 -finput-charset=8859_1
AM_CXXFLAGS = $(WLOCALE)

Then I printed the return value of setlocale(LC_ALL, "") to know the current locale and it showed LC_CTYPE=en_US.UTF-8 and other subcategories were mainly "C". I then tried to set the current locale to 8859_1 (hoping that this might solve the problem) by calling,
char* curlocale = setlocale(LC_ALL, "en_US.ISO-8859-1");

But even after this, the value of wcslen is 19.

Please suggest.
 
Old 02-12-2010, 04:21 AM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

I'm not sure exactly what you're trying to do, what your platform is, what compiler you're using, or how your runtime environment is configured...

... But it sure sounds like you want to dump the binary value of "fname" after your "_tcslcpy()".

I'd start there.

IMHO .. PSM
 
Old 02-12-2010, 05:15 AM   #3
hs_linux
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Original Poster
Rep: Reputation: 0
I am using CentOS 4.7 and gcc as compiler. My application is a Java app which calls native C++ code through JNI. The code that I have mentioned above is in the C++ shared library. I am not setting up any environment variables for runtime use.

The code above pertains to a filename which I have hardcoded for time being (to be sure about where problems are originating). When this filename is converted to ansi string, the latter contains only half the string and therefore the file can't be opened. So, I put a _tcslen at source to see if the things are arriving correctly from the source. But unfortunately not. _tcslen returns the same length as is that of ansi string which is half of actual.

thanks.
 
Old 02-12-2010, 10:57 AM   #4
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
'Sounds like a nightmare. JNI can be a *very* wily beast to try taming.

ANYWAY - my original suggestion still stands:
Quote:
you want to dump the binary value of "fname" after your "_tcslcpy()"
You need to see if "fname" is exactly 39 characters immediately after your "_tcslcpy()" (I'm reasonably sure it probably is).

You need to determine if "fname" is contiguous 8-bit bytes, or 16-bit wide characters. I'm not sure why you're using Unicode "L" constants, and I hope you don't think that changing the runtime locale (e.g. "$LANG") will at all affect a compile time constant (your string).

And once you completely understand what's happening at "_tcslcpy()", then you need to methodically trace the string from that point forward.

Good luck .. PSM

PS:
If you're using JNI, I'd strongly encourage you to use straight C: *not* C++.
 
Old 02-16-2010, 08:57 AM   #5
hs_linux
LQ Newbie
 
Registered: Dec 2009
Posts: 14

Original Poster
Rep: Reputation: 0
Hi paulsm4,
Sorry for getting back late.
The problem was coming b'cos on linux the wchar_t is of 4 bytes, but in the library a compiler switch made it of 2 bytes.
So the compile time constant was 2 bytes per character but the api _tcslen assumes 4 bytes which is why length returned was half of actual.
And yes, I did dump the binary value of fname when the problem started occurring and it was always coming out correct.

thanks for showing interest in my post. regards.
 
  


Reply

Tags
unicode


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rows in lists on my Debian are correct/black/correct/black... eikhorsholm Linux - Desktop 1 10-01-2009 06:24 PM
Nagios plugin check_snmp not returning correct information DrStrangepork Linux - Software 2 09-25-2009 11:43 AM
Returning a variable? onthefritz Programming 5 03-29-2008 10:35 PM
ld returning 1 angelo_maci Linux - Software 1 04-10-2006 02:41 PM
Returning an object Mohsen Programming 12 08-07-2003 03:41 AM


All times are GMT -5. The time now is 03:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration