home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / datafiles / text / c_manual / devices / narratordevice / example1.c < prev    next >
C/C++ Source or Header  |  1995-02-27  |  5KB  |  141 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Devices                 Amiga C Club       */
  7. /* Chapter: Narrator Device             Tulevagen 22       */
  8. /* File:    Example1.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-26                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This very simple example demonstrates how to     */
  21. /* open the translator library, translate a string, */
  22. /* and finally close the library before the program */
  23. /* terminates.                                      */
  24.  
  25.  
  26.  
  27. /* Declares the datatypes like STRPTR etc: */
  28. #include <exec/types.h>
  29.  
  30.  
  31.  
  32. /* Size of the phonetic string buffer. (Remember */
  33. /* that a phonetic string needs more space than  */
  34. /* the original string.)                         */
  35. #define PHONETIC_BUFFER_SIZE 100
  36.  
  37.  
  38.  
  39. /* Pointer to the translator library: */
  40. struct Library *TranslatorBase;
  41.  
  42.  
  43.  
  44. /* Declare our functions: */
  45. void main();
  46. void clean_up( STRPTR text );
  47.  
  48.  
  49.  
  50. void main()
  51. {
  52.   /* If all characters could be translated and stored */
  53.   /* in the phonetic string this variable will be set */
  54.   /* to zero. On the other hand, if some words could  */
  55.   /* not be translated since they did not fit in the  */
  56.   /* phonetic string this variable will contain a     */
  57.   /* negative value on how many characters actually   */
  58.   /* were translated.                                 */
  59.   int char_translated;
  60.   
  61.   /* The original string: */
  62.   char *original_string = "Hello world";
  63.   
  64.   /* The phonetic string: */
  65.   char phonetic_string[ PHONETIC_BUFFER_SIZE ];
  66.  
  67.  
  68.  
  69.  
  70.   /* Open the translator library: */
  71.   TranslatorBase = (struct Library *)
  72.     OpenLibrary( "translator.library", 0 );
  73.  
  74.   /* Have we successfully opened the library? */
  75.   if( !TranslatorBase )
  76.     clean_up( "Could not open the translator library!" );
  77.  
  78.  
  79.  
  80.   /* The translator library has now been opened, so    */
  81.   /* we may now start to use the Translate() function. */
  82.   
  83.  
  84.  
  85.   /* Translate our string into phonetics: (The Translate() */
  86.   /* functipon can sadly only translate English text, but  */
  87.   /* with small modifications of the phonetic string it    */
  88.   /* can be used with most languages.)                     */
  89.   char_translated =
  90.     Translate( original_string, strlen( original_string ),
  91.                phonetic_string, PHONETIC_BUFFER_SIZE );
  92.  
  93.   /* If all characters could successfully be translated */
  94.   /* and stored in the phonetic string Translate()      */ 
  95.   /* returns zero, else a negativa value is returned    */
  96.   /* which tells us how many characters were actually   */
  97.   /* translated: (Note that we put a minus sign infront */
  98.   /* of the variable to make it positive.)              */
  99.   if( char_translated )
  100.     printf( "Translated only %d characters!\n", -char_translated );
  101.   else
  102.     printf( "All characters successfully translated!\n" );
  103.  
  104.  
  105.  
  106.   /* Show the user what we got: */
  107.   printf( "Original string: %s\n", original_string );
  108.   printf( "Phonetic string: %s\n", phonetic_string );
  109.  
  110.  
  111.  
  112.   /* Clean up and quit: */
  113.   clean_up( "The End!" );
  114. }
  115.  
  116.  
  117.  
  118. /* Close and return everything that has been */
  119. /* opened and allocated before we quit:      */
  120.  
  121. void clean_up( STRPTR text )
  122. {
  123.   /* Close the translator library: */
  124.   if( TranslatorBase )
  125.     CloseLibrary( TranslatorBase );
  126.  
  127.   /* Remember that you must ALWAYS close the libraries you have   */
  128.   /* opened. This is especially important when you are using the  */
  129.   /* translator library since it is rather large and is loaded    */
  130.   /* into memory when opened. If you forget to close this library */
  131.   /* you will waste a lot of memory which only can be recovered   */
  132.   /* when the user turns off the computer.                        */ 
  133.  
  134.   /* Print the last message: */
  135.   printf( "%s\n", text );
  136.  
  137.   /* Quit: */
  138.   exit( 0 );
  139. }
  140.  
  141.