home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2002 October / VPR0210A.ISO / OPENOFFICE / f_0302 / xrot.c < prev   
C/C++ Source or Header  |  2002-07-04  |  6KB  |  237 lines

  1. /*------------------------------------------------------------------------
  2.  
  3. $Workfile:   ROT13.CL  $
  4.  
  5. $Header: /cvs/sc/sc/addin/rot13/rot13.cl,v 1.1.1.1 2000/09/18 16:44:46 hr Exp $
  6.  
  7. Description:    StarCalc ROT13 AddIn Example
  8.  
  9. (c) Copyright 1998 - 2000, Sun Microsystems, Inc.
  10.  
  11. ------------------------------------------------------------------------*/
  12.  
  13. static char rot13_Id[]="@(#) StarCalc Rot13 AddIn (c) 1998-2000 Sun Microsystems, Inc.";
  14.  
  15. #include <string.h>
  16. #include <stdio.h>
  17.  
  18. #include <xlang.h>
  19. #include <addin.h>
  20. #include <rot13.hrc>
  21.  
  22. /**
  23.  * the current language the Addin is using
  24.  */
  25. static USHORT _nLanguage=LANGUAGE_ENGLISH;
  26.  
  27.  
  28. /**
  29.  * Get text resource for current language.
  30.  * Remember that 8-bit characters are shown in
  31.  * system dependend code pages!
  32.  * To get correct results you will have to distuinguish
  33.  * for example between UNIX and WIN and OS2 target systems.
  34.  */
  35. static char* getText( int nResource )
  36. {
  37.     switch( nResource ) {
  38.         case ROT13_DESC:
  39.             switch( _nLanguage ) {
  40.                 case LANGUAGE_GERMAN:
  41.                 return( "ROT13 Algorithmus, jedes alphabetische Zeichen des Textes wird um 13 im Alphabet rotiert." );
  42.                 case LANGUAGE_ENGLISH:
  43.                 default:
  44.                 return( "ROT13 Algorithm, each alphabetical character of the text is rotated by 13 in the alphabet");
  45.             }
  46.             break;
  47.         case ROT13_PAR1_NAME:
  48.             switch( _nLanguage ) {
  49.                 case LANGUAGE_GERMAN:
  50.                 return( "Text" );
  51.                 case LANGUAGE_ENGLISH:
  52.                 default:
  53.                 return( "Text");
  54.             }
  55.             break;
  56.         case ROT13_PAR1_DESC:
  57.             switch( _nLanguage ) {
  58.                 case LANGUAGE_GERMAN:
  59.                 return( "Der Text der rotiert werden soll" );
  60.                 case LANGUAGE_ENGLISH:
  61.                 default:
  62.                 return( "The text that is to be rotated");
  63.             }
  64.             break;
  65.         default:
  66.             break;
  67.     }
  68.     return("");
  69. }
  70.  
  71.  
  72. /**
  73.  * Get neutral language for specific language.
  74.  * This simplifies the getText switch cases and allows to handle
  75.  * previously unknown language derivates due to foreign installations.
  76.  * If you want to distinguish between some dialects change this function
  77.  * to return the desired nLang before doing the bit masking stuff.
  78.  * See xlang.h for defined LANGUAGE_*
  79.  */
  80. static USHORT GetNeutralLanguage( USHORT nLang )
  81. {
  82.     USHORT nPrimLang;
  83.  
  84.     /* ignore LANGUAGE_USER* */
  85.     if ( (nLang & 0x03FF) >= 0x0200 )
  86.         return nLang;
  87.  
  88.     nLang &= 0x03FF;
  89.  
  90.     nPrimLang = nLang | 0x0400;
  91.  
  92.     switch ( nPrimLang )
  93.     {
  94.         case LANGUAGE_CHINESE_TRADITIONAL:
  95.             nLang = LANGUAGE_CHINESE;
  96.             break;
  97.         case LANGUAGE_ENGLISH_US:
  98.             nLang = LANGUAGE_ENGLISH;
  99.             break;
  100.         case LANGUAGE_NORWEGIAN_BOKMAL:
  101.             nLang = LANGUAGE_NORWEGIAN;
  102.             break;
  103.         case LANGUAGE_PORTUGUESE_BRAZILIAN:
  104.             nLang = LANGUAGE_PORTUGUESE;
  105.             break;
  106.  
  107.         default:
  108.             nLang = nPrimLang;
  109.             break;
  110.     }
  111.  
  112.     return nLang;
  113. }
  114.  
  115.  
  116. /**
  117.  * StarCalc calls this function to set a new current Language for the Addin
  118.  *
  119.  * @param *nLanguage
  120.  *
  121.  */
  122. void CALLTYPE SetLanguage( USHORT* nLanguage )
  123. {
  124.     _nLanguage = GetNeutralLanguage( *nLanguage );
  125. }
  126.  
  127.  
  128. /**
  129.  * Tell StarCalc how many new functions this Addin provides.
  130.  *
  131.  * @param *nCount - returns the number of functions which are exported to StarCalc
  132.  *
  133.  */
  134. void CALLTYPE GetFunctionCount( USHORT *nCount )
  135. {
  136.     *nCount = 1;
  137. }
  138.  
  139. /**
  140.  * Provides neccessary data for each new function to StarCalc
  141.  *
  142.  * @param *nNo Input: Function number between 0 and nCount - 1
  143.  * @param *pFuncName Output: Functionname which should be called in the AddIn-DLL
  144.  * @param *nParamCount Output: Number of Parameter. Must be greater than 0, because there's always a return-Value. Maximum is 16.
  145.  * @param *peType Output: Pointer to arrray with exactly 16 variables of typ Paramtype. nParamCount Entries are set to the type of the corresponding Parameters.
  146.  * @param *pInternalName Output: Functionname as seen by the Spreadsheet user
  147.  *
  148.  * @see #GetFunctionCount, #GetParameterDescription
  149.  *
  150.  */
  151. void CALLTYPE GetFunctionData( USHORT *    nNo,
  152.                    char *      pFuncName,
  153.                    USHORT *    nParamCount,
  154.                    ParamType * peType,
  155.                    char *      pInternalName )
  156. {
  157.  
  158.     switch( *nNo ) {
  159.     case 0:
  160.         /* the function name is the same in all languages */
  161.         SO_StringCopy( pInternalName, "Rot13" );
  162.         SO_StringCopy( pFuncName,     "Rot13" );
  163.         peType[0] = PTR_STRING;
  164.         peType[1] = PTR_STRING;
  165.         *nParamCount=2;
  166.         break;
  167.      default:
  168.         *nParamCount    = 0;
  169.         *pFuncName     = 0;
  170.         *pInternalName = 0;
  171.         break;
  172.     }
  173. }
  174.  
  175. /**
  176.  * Provides descriptions for each new function to StarCalc
  177.  * which are shown is the autopilot
  178.  *
  179.  * @param *nNo Input Parameter, Function number between 0 and nCount - 1
  180.  * @param *nParam Parameter Number
  181.  * @param *pName Output: Name of the parameter
  182.  * @param *pDesc Output: Description of the parameter
  183.  *
  184.  * @see #GetFunctionCount, #GetParameterDescription
  185.  */
  186. void CALLTYPE GetParameterDescription( USHORT* nNo, USHORT* nParam,
  187. char* pName, char* pDesc )
  188. {
  189.     *pName = 0;
  190.     *pDesc = 0;
  191.  
  192.  
  193.     switch ( *nNo ) {
  194.     case 0:
  195.         switch ( *nParam ) {
  196.         case 0:
  197.             SO_StringCopy(pDesc,getText(ROT13_DESC));
  198.             break;
  199.         case 1:
  200.             SO_StringCopy(pName,getText(ROT13_PAR1_NAME));
  201.             SO_StringCopy(pDesc,getText(ROT13_PAR1_DESC));
  202.         }
  203.     }
  204. }
  205.  
  206. /**
  207.  * ROT13 Algorithm, each alphabetical character of the text is rotated by 13 in the alphabet
  208.  *
  209.  * @param *ret
  210.  * @param *src
  211.  *
  212.  * ER: well, my personal favorite algorithm is
  213.  * main(_){while(_=~getchar())putchar(~_-1/(~(_|32)/13*2-11)*13);}
  214.  * but for clarification we do it somehow different here ;-)
  215.  */
  216. void CALLTYPE Rot13(char *ret, char *src)
  217. {
  218.     if ( ! ret ) return;
  219.     if ( ! src ) *ret='\0';
  220.  
  221.     for(;src && *src; src++ , ret++) {
  222.     *ret=*src;
  223.     if (*ret >= 'A' && *ret <= 'Z') {
  224.         if ( (*ret +=13) > 'Z' ) *ret-=26;
  225.     } else if (*ret >= 'a' && *ret < 'n') {
  226.         *ret +=13;
  227.     } else if (*ret >= 'n' && *ret <= 'z') {
  228.         *ret -=13;
  229.     }
  230.     }
  231.     *ret=*src;
  232. }
  233.  
  234.  
  235.  
  236.  
  237.