Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Tue Dec 12, 2017 1:22 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Trying to get the full ASCII character set
PostPosted: Sun Feb 17, 2008 8:28 am 
Offline
User avatar

Joined: Mon Nov 28, 2005 2:46 am
Posts: 81
Location: Sydney
Hi all,
I'm building a plug-in that does some analysis of a piece of text. I won't go into the finer details, but for the sake of this demonstration, imagine I was trying to replace the asc() function. I want to be able to return the ASCII value of the first character in a string. Here is my function in C.

static int CharValueMethod(REALstring s2)
{   
   const char *s1 = REALCString(s2);
   unsigned char s1char;
   s1char = (unsigned char) s1[0];
   return s1char;
}
So I've passed the string to the function, I take the first character of the string, then I return its value. The problem I have is that it only seems to return the value with standard characters. If I try this with a character such as 'â' (an 'a' with a circumflex) or é (an 'e' with a grave accent), I get the same value of -30, regardless of the character I send to the function.

I figure this must be a really simple one to solve, but my C knowledge is sketchy at best.

Thanks in advance,
brayne


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Feb 17, 2008 11:14 am 
Offline

Joined: Fri Sep 30, 2005 10:01 am
Posts: 283
Location: Germany, Munich
This is not a C language problem but one with handling UTF encoded strings in general.

There are two ways to solve this:

1. You use the Unicode or TextConverter form the Carbon lib to get a character from the string. This is not easy.

2. You convert the string into a UTF-16 string, and then get the first 16 bit value. For some remote scripts (Chinese or something, not sure what exactly), this will still not work as you'd need to use UTF-32, but for Latin based scripts it'll be enough.

Thomas

_________________
User of RB since first version. Provider of many free and outdated plugins.
Code for sharing: http://www.tempel.org/RB/Resources
Arbed, a unique tool for editing projects: http://www.tempel.org/Arbed
Zip compression classes: http://www.tempel.org/RB/ZipPackage


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Feb 17, 2008 2:56 pm 
Offline
User avatar

Joined: Mon Nov 28, 2005 2:46 am
Posts: 81
Location: Sydney
Thanks for that Thomas,

I only expect Latin based text with this, so UTF-16 sounds like a possibility.

Could you possibly provide a small code example of how I would convert to a UTF-16 string, and then pass it to my C function?

Thanks,
brayne


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Feb 17, 2008 3:45 pm 
Offline
User avatar

Joined: Wed Sep 28, 2005 8:39 am
Posts: 9341
Location: St Augusta, MN
The way I'd do it is to have the plugin handle it -- so the caller can pass in whatever they want, and the plugin will convert as needed.

int Foobar( REALstring s )
{
// If no string, then fail
if (!s) return -1;

// Convert the string to UTF-16
REALstring converted = REALConvertString( s, kREALTextEncodingUTF16 );

// Get the string's contents as utf-16 characters
size_t numBytes = 0;
wchar_t *contents = REALGetStringContents( converted, &numBytes );

if (contents) {
size_t numChars = numBytes / 2;
// Do whatever you need to do here
}

// Unlock the converted string so we don't leak
REALUnlockString( converted )
}

Sorry about the formatting -- the forums aren't too great at grokking C++ code. This is untested, but should point you in the right direction.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Feb 17, 2008 4:58 pm 
Offline
User avatar

Joined: Mon Nov 28, 2005 2:46 am
Posts: 81
Location: Sydney
Thanks for that Aaron,

I don't seem to be having any luck with REALGetStringContents. When I compile with Xcode, it's not treating it as a function. It says:
REALGetStringContents was not declared in this scope


I can't find any reference to this function in the REALbasic Plugin reference, so I'm at a loss as to what I might be doing wrong.

Any ideas?

Thanks,
brayne


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Feb 17, 2008 7:38 pm 
Offline
User avatar

Joined: Wed Sep 28, 2005 8:39 am
Posts: 9341
Location: St Augusta, MN
That function exists only within the newer plugin SDKs (2007r4 or r5 and up), so that'd be the problem. You could get around it in older SDKs by calling REALstring->CString() and REALstring->Length() on the REALstring object.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Feb 20, 2008 6:06 am 
Offline
User avatar

Joined: Mon Nov 28, 2005 2:46 am
Posts: 81
Location: Sydney
Hi again Aaron,

I've managed to get some time on this again, and I'm afraid I need a little more help. I could be wrong, but I think I'm getting really close. As mentioned before, I'm trying to return an integer value that corresponds to a particular character. It's part of a much bigger project I'm working on, and I can't just use asc().

When I use this code, it just returns a "3" for every character I send. However, if I send two characters, it gives me the right value for the second character, as long as the character is one of the standard ASCII. If I send a UTF character down, I get a value, but only if it's the third character in the string I send. I hope that makes sense!


static int CharValueMethod(REALstring s2)
{   
   if (!s2) return 0;
   REALstring s3 = REALConvertString( s2, kCFStringEncodingUTF16 );
   size_t StringLength = s3->Length();
   wchar_t *content = (wchar_t *)malloc(StringLength);
   memcpy(content, s3->CString(), StringLength);
   REALUnlockString( s3 );
   int s1char = content[0];
   free(content);
   return s1char;   
}


I hope this is an easy one to resolve.

Thanks again,
brayne


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Feb 20, 2008 7:42 am 
Offline

Joined: Fri Sep 30, 2005 10:01 am
Posts: 283
Location: Germany, Munich
Not tested, but this should be a simplified version of your code (the malloc and memcpy are moot):

static int CharValueMethod(REALstring s2)
{   
   if (!s2) return 0;
   REALstring s3 = REALConvertString( s2, kCFStringEncodingUTF16 );
   int s1char = 0;
   if (s3 && s3->Length() >= 2) {
      short *content = (short *) s3->CString();
      s1char = content[0];
   }
   REALUnlockString( s3 );
   return s1char;   
}


I do not see, however, why you should get wrong results.

_________________
User of RB since first version. Provider of many free and outdated plugins.
Code for sharing: http://www.tempel.org/RB/Resources
Arbed, a unique tool for editing projects: http://www.tempel.org/Arbed
Zip compression classes: http://www.tempel.org/RB/ZipPackage


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Feb 20, 2008 4:19 pm 
Offline
User avatar

Joined: Mon Nov 28, 2005 2:46 am
Posts: 81
Location: Sydney
Thanks Aaron,

You're a champion, as always.

brayne


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Feb 20, 2008 5:25 pm 
Offline

Joined: Fri Sep 30, 2005 10:01 am
Posts: 283
Location: Germany, Munich
brayne wrote:
Thanks Aaron

Errm...

_________________
User of RB since first version. Provider of many free and outdated plugins.
Code for sharing: http://www.tempel.org/RB/Resources
Arbed, a unique tool for editing projects: http://www.tempel.org/Arbed
Zip compression classes: http://www.tempel.org/RB/ZipPackage


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Feb 20, 2008 5:35 pm 
Offline
User avatar

Joined: Mon Nov 28, 2005 2:46 am
Posts: 81
Location: Sydney
Sorry tempel,
I was too busy looking at your solution to see who the author was.

A million apologies.

You're a champion too.

brayne.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Feb 20, 2008 5:44 pm 
Offline

Joined: Fri Sep 30, 2005 10:01 am
Posts: 283
Location: Germany, Munich
brayne wrote:
You're a champion too.

brayne.

Does it mean that it works now?

_________________
User of RB since first version. Provider of many free and outdated plugins.
Code for sharing: http://www.tempel.org/RB/Resources
Arbed, a unique tool for editing projects: http://www.tempel.org/Arbed
Zip compression classes: http://www.tempel.org/RB/ZipPackage


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Feb 20, 2008 5:53 pm 
Offline
User avatar

Joined: Mon Nov 28, 2005 2:46 am
Posts: 81
Location: Sydney
tempel wrote:
Does it mean that it works now?

It does. I've also learned a whole lot of stuff during this exercise.

Thanks,
brayne


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group