//Sample code file: var/ndk/webBuildengine/tmp/viewable_samples/7999585e-fdab-4e7b-b86c-67d55e862138/EuroKeep.c

//Warning: This code has been marked up for HTML

/***************************************************************************
$name: EuroKeep.c 
$version: 1.0 
$date_modified: 012703 
$description: Demonstrates a reasonably interesting of the condition variable.
$owner: NKS Product Manager 
Copyright (c) 2003 Novell, Inc. All Rights Reserved.

THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
USE AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO THE LICENSE AGREEMENT
ACCOMPANYING THE SOFTWARE DEVELOPMENT KIT (SDK) THAT CONTAINS THIS WORK.
PURSUANT TO THE SDK LICENSE AGREEMENT, NOVELL HEREBY GRANTS TO DEVELOPER A
ROYALTY-FREE, NON-EXCLUSIVE LICENSE TO INCLUDE NOVELL'S SAMPLE CODE IN ITS
PRODUCT. NOVELL GRANTS DEVELOPER WORLDWIDE DISTRIBUTION RIGHTS TO MARKET,
DISTRIBUTE, OR SELL NOVELL'S SAMPLE CODE AS A COMPONENT OF DEVELOPER'S
PRODUCTS. NOVELL SHALL HAVE NO OBLIGATIONS TO DEVELOPER OR DEVELOPER'S
CUSTOMERS WITH RESPECT TO THIS CODE.
****************************************************************************/

/*--------------------------------------------------------------------------
Shown here is how to use the "x" functions in UniLib to filter unmappable
characters. In this example, we are looking for a Euro character in Unicode
and, to preserve it, we are remapping it to 0xFF for the local code page,
ostensibly 437, where it does not exist.
--------------------------------------------------------------------------*/
#include <stdio.h>
#include <screen.h>
#include <unilib.h>


int   UniToLocEuroMapFunc( char **, size_t, const unicode_t **, void * );

int UniToLocEuroMapFunc
(
   char               **dest,
   size_t            remaining,
   const unicode_t   **src,
   void               *userParm
)
{
#pragma unused(remaining)
/*
** If uninx2loc(), like uni2loc(), cannot map a character, it will call us.
** The only character we pretend to know how to map here is the Euro. Any
** other will cause us to fail the translation as unmappable. If we wrote
** other custom mapping functions with different duties, we might disambiguate
** between them using a 'userParm' value. Here, we do it trivially (because
** we don't really need this) for the purposes of illustration and our sign is
** the value 0x99.
*/
   if (**src != 0x20AC || userParm != (void *) 0x99)
      return -1;

   **dest = 0xFF;      // here's what it will be in 437

   (*dest)++;         // update destination and source


/*
** Since we only mapped one source character, we do not bump the source because
** uninx2loc() counts on bumping it by one character. If we were doing some
** context-sensitive matching and wanted to eat n source characters, we would
** bump it by n-1. Yeah, this stinks, but I can't change it now or a lot of
** people would break.
   (*src)++;
*/

   return 0;
}

int main( int argc, char *argv[] )
{
   int            err;
   char            output_string[80];
   size_t         outLen;
   unicode_t      input_string[80], right_bracket[2];
   UniRuleTable_t   table;
#pragma unused(argc, argv)

   setscreenmode(0);

/*
** Manufacture the input string articificially for this example. The source
** character itself (for Euro) must be expressed in reverse order (this is
** Intel) from the way one would normally think about it, thus 0xAC20.
*/
   right_bracket[0] = ']';
   right_bracket[1] = 0x00;
   asc2uni(input_string, "This is the Euro character: [");
   unicat(input_string, (unicode_t *) "\xAC\x20");
   unicat(input_string, right_bracket);

   outLen = sizeof(output_string);

   // now, pretend this is a Unicode string we are translating into 437...

   if (!(err = UniGetTable(437, &table)))
   {
      err = uninx2loc(table, output_string, &outLen, input_string,
                     unilen(input_string), UniToLocEuroMapFunc, (void *) 0x99,
                     UNI_MAP_BY_FUNC);

      printf("%s\n", (err) ? "Oops! Why didn't this work?" : output_string);

      err = UniDisposeTable(table);
   }
   else
   {
      printf("Error opening translation table for codepage 437...\n");
   }

   return err;
}