/*                                                                        */
/* HSCCapital                                                             */
/*                                                                        */
/* Written by Michal Durys - misha@femina.com.pl                          */
/* To compile under non AmigaOS compatibile machine undefine USE_LOCALE.  */
/*                                                                        */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define USE_LOCALE

#ifdef USE_LOCALE
#include <proto/exec.h>
#include <proto/locale.h>

struct Library *LocaleBase;
#endif

enum
{
  CHAR_NONE,
  CHAR_UPPER,
  CHAR_LOWER
};

// prototypes
void print_capitals(unsigned char *, int, int);

// version string
static unsigned char version[]="$VER: HSCCapital 1.0 " __AMIGADATE__;

main(int argc, char *argv[])
{
  // check # of arguments
  if (argc<4) return(20);
  // do our job
  print_capitals(argv[1], atoi(argv[2]), atoi(argv[3]));
}

void print_capitals(unsigned char *str_in, int font_big, int font_small)
{
  unsigned char str_out[256], last_char=CHAR_NONE, current_char;
  unsigned char tag_font_big[32], tag_font_small[32], tag_font_end[32];
  int i=0;

#ifdef USE_LOCALE
  struct Locale *locale;

  // init locale
  LocaleBase=(struct Library *)OpenLibrary("locale.library", 0);
  locale=OpenLocale(NULL);
#endif

  // init strings
  strcpy(str_out, (char *)"");
  sprintf(tag_font_big, "<font size=%d>", font_big);
  sprintf(tag_font_small, "<font size=%d>", font_small);
  strcpy(tag_font_end, "</font>");

  for (i=0; i<strlen(str_in); i++)
  {
    current_char=str_in[i];
#ifdef USE_LOCALE
    if ((IsUpper(locale, current_char)) || (IsDigit(locale, current_char)))
#else
    if ((isupper(current_char)) || (isdigit(current_char)))
#endif
    {
      if (last_char==CHAR_LOWER) strcat(str_out, tag_font_end);
      if (!(last_char==CHAR_UPPER)) strcat(str_out, tag_font_big);
      strncat(str_out, &current_char, 1);
      last_char=CHAR_UPPER;
    }
    else
    {
      if (last_char==CHAR_UPPER) strcat(str_out, tag_font_end);
      if (!(last_char==CHAR_LOWER)) strcat(str_out, tag_font_small);
      // convert current char to uppercase
#ifdef USE_LOCALE
      current_char=ConvToUpper(locale, current_char);
#else
      current_char=toupper(current_char);
#endif
      strncat(str_out, &current_char, 1);
      last_char=CHAR_LOWER;
    }
  }
  // close font tag
  strcat(str_out, tag_font_end);

  // print html line
  printf("%s", str_out);

#ifdef USE_LOCALE
  // close locale
  CloseLocale(locale);
  CloseLibrary(LocaleBase);
#endif
}

