home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************/
- /* */
- /* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
- /* ------------------------------- ------------------ */
- /* */
- /* Book: ACM Devices Amiga C Club */
- /* Chapter: Narrator Device Tulevagen 22 */
- /* File: Example2.c 181 41 LIDINGO */
- /* Author: Anders Bjerin SWEDEN */
- /* Date: 92-04-26 */
- /* Version: 1.00 */
- /* */
- /* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
- /* */
- /* Registered members may use this program freely in their */
- /* own commercial/noncommercial programs/articles. */
- /* */
- /***********************************************************/
-
- /* This example demonstrates how you can use a while */
- /* loop to translate parts of a string until the whole */
- /* string has been translated. */
-
-
-
- /* Declares the datatypes like STRPTR etc: */
- #include <exec/types.h>
-
-
-
- /* Size of the phonetic string buffer. (This buffer */
- /* is very small and the phonetic string will most */
- /* certainly not fit in it.) */
- #define PHONETIC_BUFFER_SIZE 30
-
-
-
- /* Pointer to the translator library: */
- struct Library *TranslatorBase;
-
-
-
- /* Declare our functions: */
- void main();
- void clean_up( STRPTR text );
-
-
-
- void main()
- {
- /* If all characters could be translated and stored */
- /* in the phonetic string this variable will be set */
- /* to zero. On the other hand, if some words could */
- /* not be translated since they did not fit in the */
- /* phonetic string this variable will contain a */
- /* negative value on how many characters actually */
- /* were translated. */
- int char_translated;
-
- /* This variable contsins the current position */
- /* in the string which is translated: */
- int current_position;
-
- /* The original string: */
- char *original_string =
- "Dear honourable C programmer, do you like the new version of this manual? ";
-
- /* The phonetic string: */
- char phonetic_string[ PHONETIC_BUFFER_SIZE ];
-
-
-
- /* Open the translator library: */
- TranslatorBase = (struct Library *)
- OpenLibrary( "translator.library", 0 );
-
- /* Have we successfully opened the library? */
- if( !TranslatorBase )
- clean_up( "Could not open the translator library!" );
-
-
-
- /* Show the user the original string: */
- printf( "Original string:\n %s\n", original_string );
- printf( "Phonetic string:\n" );
-
-
-
- /* Start with the first character in the original string: */
- current_position = 0;
-
- /* Translate (parts of) our string into phonetics: */
- char_translated =
- Translate( original_string,
- strlen( original_string ),
- phonetic_string,
- PHONETIC_BUFFER_SIZE );
-
- /* Print the translated part: */
- printf( " %s\n", phonetic_string );
-
-
-
- /* As long as Translate() does not return zero we stay */
- /* in this while loop and continues to translate the */
- /* original string into phonetics: */
- while( char_translated )
- {
- /* Increase the current position in the original string: */
- /* (Remember that "char_translated" variable is negative, */
- /* and we must therefore use the "-=" operator and not */
- /* the "+=" to increase the currrent position.[-- = +]) */
- current_position -= char_translated;
-
- /* Translate the following part our string into phonetics: */
- /* (Note that when we put brackets after a string we we */
- /* get the character at the specified position, but since */
- /* we want the address of that position we also have to */
- /* put the pointer "&" sign infront of the string.) */
- char_translated =
- Translate( &original_string[ current_position],
- strlen( &original_string[ current_position] ),
- phonetic_string,
- PHONETIC_BUFFER_SIZE );
-
- /* Print the translated part: */
- printf( " %s\n", phonetic_string );
- }
-
- /* The complete string has now been translated: */
- printf( "All words have now been translated!\n" );
-
-
-
- /* Clean up and quit: */
- clean_up( "The End!" );
- }
-
-
-
- /* Close and return everything that has been */
- /* opened and allocated before we quit: */
-
- void clean_up( STRPTR text )
- {
- /* Close the translator library: */
- if( TranslatorBase )
- CloseLibrary( TranslatorBase );
-
- /* Remember that you must ALWAYS close the libraries you have */
- /* opened. This is especially important when you are using the */
- /* translator library since it is rather large and is loaded */
- /* into memory when opened. If you forget to close this library */
- /* you will waste a lot of memory which only can be recovered */
- /* when the user turns off the computer. */
-
- /* Print the last message: */
- printf( "%s\n", text );
-
- /* Quit: */
- exit( 0 );
- }
-
-