/* Dieses Programm demonstriert die Benutzung der
 * Funktion FormatString()
 * Verwendeter C-Compiler: SAS-C 6.5x
 */
#include <exec/types.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/locale.h>
#include <proto/utility.h>
#include <stdio.h>

struct Library *LocaleBase=NULL;
struct Locale *locale;

/* Diese Routine wird vom Betriebssystem angesprungen.
 * Wir leiten den Aufruf schnurstracks an unsere eigene
 * Routine weiter */
ULONG __saveds __asm hookEntry(
             register __a0 struct Hook *h, 
             register __a2 void *obj, 
             register __a1 void *msg)
{
  return((*h->h_SubEntry) (h,obj,msg));
}
/* Füllt die Hook-Struktur aus */
void InitHook(struct Hook *hook, 
              ULONG (*func)(), 
              void *data)
{
  if( hook ) {
    hook->h_Entry = (ULONG (*)()) hookEntry;
    hook->h_SubEntry = func;
    hook->h_Data = data;
  }
}
/* Erhält zeichenweise Informationen von FormatString()
 * der Locale-Library und gibt sie aus */
ULONG __saveds __asm My_FormatString(
  register __a0 struct Hook *h, 
  register __a2 void *obj, 
  register __a1 void *msg) /* Hier steht das Zeichen */
{
  if( msg != 0 ) printf("%c",msg);
  else           printf("\n");
  return 1;
}
/* Diese Funktion wurde implementiert, um die Daten 
 * per Stack übergeben zu können
 */
void CallFormatString(struct Hook *h, char *format, 
                      ULONG tag1, ...)
{
   FormatString(locale,format,&tag1,h);
}
void main(ULONG argc, char **argv)
{
  struct Hook formstring;
  if( argc ) {
    /* Nur vom CLI zu starten */
    LocaleBase=OpenLibrary("locale.library",38);
    if( LocaleBase ) {
      locale=OpenLocale(NULL);
      if( locale ) {
        InitHook(&formstring, My_FormatString, NULL);
        /* Normale Reihenfolge */
        CallFormatString(&formstring, 
                  "Stunde: %ld, Minute: %ld", 10, 20);
        /* Umgekehrte Reihenfolge (die Daten im
         * Datenstrom behalten aber ihre ursprüngliche
         * Postition */
        CallFormatString(&formstring, 
              "Minute: %2$ld, Stunde: %1$ld", 10, 20);
        CloseLocale( locale );
      }
      CloseLibrary(LocaleBase);
    } else printf("Locale-Library nicht vorhanden\n");
  }
}
