#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#include <locale.h>
#include <libraries/locale.h>
#include <proto/locale.h>
#include <proto/exec.h>
#include <constructor.h>

#define ADDS(st)  tmp=strftime(s,maxsize-size,(st),timeptr);break;

#define ADDN(a,b) tmp=strfnumb(s,maxsize-size,(a),(b));break;

#define STOR(c)   if(++size<=maxsize)*s++=(c);

#define STR(a) \
(__localevec[LC_TIME-1]==NULL?strings[(a)-1]:GetLocaleStr(__localevec[LC_TIME-1],(a)))

#define strlen_plus_one(s) strlen(s)+1



/***************** Locale stuff ******************/

/* This is the vector of Locales for the different locale types.
 * Since the default locale is not NULL, NULL means "C" locale.
 * This is the default if locale.library could not be opened
 * or setlocale is never referenced.
 */

static struct Locale *__localevec[5]=
{ NULL,NULL,NULL,NULL,NULL };

/* If you need __localevec you need LocaleBase and vice versa.
 */
struct Library *LocaleBase=NULL;

static unsigned char *__decimalpoint=".";
static struct lconv __lconv_strftime;
static long __gmtoffset;


/* All calendar strings */
static const unsigned char *strings[]=
{ "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday",
  "Sun","Mon","Tue","Wed","Thu","Fri","Sat",
  "January","February","March","April","May","June",
  "July","August","September","October","November","December",
  "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",
  "","","AM","PM" };

static size_t strfnumb(char *s,size_t maxsize,signed int places,size_t value)
{ size_t size=0;
  if(places>1)
    size=strfnumb(s,maxsize,places-1,value/10);
  else if(value>=10)
    size=strfnumb(s,maxsize,places+1,value/10);
  else
    while ((places++<-1) && (++size<=maxsize)) s[size-1]=' ';
  if(++size<=maxsize)
    s[size-1]=(value%10+'0');
  return size;
}

size_t strftime(char *s,size_t maxsize,const char *format,const struct tm *timeptr)
{ size_t size=0,tmp;
  while(*format)
  { if(*format=='%')
    { tmp=0;
      switch(*++format)
      { case 'a':
          ADDS(STR(ABDAY_1+timeptr->tm_wday));
        case 'b':
        case 'h':
          ADDS(STR(ABMON_1+timeptr->tm_mon));
        case 'c':
          ADDS("%m/%d/%y");
        case 'd':
          ADDN(2,timeptr->tm_mday);
        case 'e':
          ADDN(-2,timeptr->tm_mday);
        case 'j':
          ADDN(3,timeptr->tm_yday+1);
        case 'k':
          ADDN(-2,timeptr->tm_hour);
        case 'l':
          ADDN(-2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
        case 'm':
          ADDN(2,timeptr->tm_mon+1);
        case 'p':
          ADDS(STR(AM_STR+(timeptr->tm_hour>=12)));
        case 'r':
          ADDS("%I:%M:%S %p");
        case 'w':
          ADDN(1,timeptr->tm_wday);
        case 'x':
          ADDS("%m/%d/%y %H:%M:%S");
        case 'y':
          ADDN(2,timeptr->tm_year%100);
        case 'A':
          ADDS(STR(DAY_1+timeptr->tm_wday));
        case 'B':
          ADDS(STR(MON_1+timeptr->tm_mon));
        case 'C':
          ADDS("%a %b %e %H:%M:%S %Y");
        case 'D':
          ADDS("%m/%d/%y");
        case 'H':
          ADDN(2,timeptr->tm_hour);
        case 'I':
          ADDN(2,timeptr->tm_hour%12+(timeptr->tm_hour%12==0)*12);
        case 'M':
          ADDN(2,timeptr->tm_min);
        case 'R':
          ADDS("%H:%M");
        case 'S':
          ADDN(2,timeptr->tm_sec);
        case 'T':
        case 'X':
          ADDS("%H:%M:%S");
        case 'U':
          ADDN(2,(timeptr->tm_yday+7-timeptr->tm_wday)/7);
        case 'W':
          ADDN(2,(timeptr->tm_yday+7-(6+timeptr->tm_wday)%7)/7);
        case 'Y':
          ADDN(4,timeptr->tm_year+1900);
        case 't':
          STOR('\t');
          break;
        case 'n':
          STOR('\n');
          break;
        case '%':
          STOR('%');
          break;
        case '\0':
          format--;
          break;
      }
      size+=tmp;
      s+=tmp;
    }
    else
      STOR(*format);
    format++;
  }
  STOR('\0');
  if(size>maxsize)
  { s-=size;
    if(maxsize) /* Don't know if this is necessary, therefore it's here ;-) */
      s[maxsize-1]='\0';
    size=1;
  }
  return size-1;
}



/****************** Setlocale **********************/

char *setlocale(int category,const char *name)
{ static char *string=NULL;
  size_t i,a,b;
  struct Locale *vec[5];
  unsigned char *s1,*s2,c;
  
  if(string!=NULL) /* Free string if possible */
  { free(string);
    string=NULL; }  

  if(category<0||category>5)
    return NULL;

  if(category==LC_ALL)
  { a=0;
    b=4; }
  else
    a=b=category-1;

  if(name==NULL) /* return name of current locale */
  { size_t s=0;
    for(i=a;i<=b;i++)
      if(__localevec[i]!=NULL)
        s+=strlen(__localevec[i]->loc_LocaleName);
      else
        s++; /* "C" locale */

    if((string=malloc(s+b-a+1))==NULL)
      return NULL;

    string[0]='\0';
    for(i=a;i<=b;i++)
    { if(__localevec[i]!=NULL)
        strcat(string,__localevec[i]->loc_LocaleName);
      else
        strcat(string,"C");
      if(i!=b)
        strcat(string,"\n");
    }
    return string;
  }

  if((string=malloc(strlen_plus_one(name)))==NULL) /* gets freed next time */
    return NULL;
  strcpy(string,name);

  s1=s2=string;
  for(i=a;i<=b;i++)
  { while(*s2!='\0'&&*s2!='\n')
      s2++;
    c=*s2;
    *s2='\0';
    if(LocaleBase==NULL||(s1[0]=='C'&&s1[1]=='\0')) /* This is the only place */
      vec[i]=NULL;                         /* LocaleBase gets tested for NULL */
    else
      if((vec[i]=OpenLocale(s1[0]=='\0'?NULL:s1))==NULL)
      { for(;i-->a;)
          CloseLocale(vec[i]);
        return NULL; }
    *s2=c;
    if(c=='\0')
      s2=string;
    s1=s2;
  }

  for(i=a;i<=b;i++)
  { if(__localevec[i]!=NULL)
      CloseLocale(__localevec[i]);
    __localevec[i]=vec[i];
  }

  if(__localevec[LC_MONETARY-1]!=NULL)
  { struct Locale *locale=__localevec[LC_MONETARY-1];
    __lconv_strftime.int_curr_symbol  =locale->loc_MonIntCS;
    __lconv_strftime.currency_symbol  =locale->loc_MonCS;
    __lconv_strftime.mon_decimal_point=locale->loc_MonDecimalPoint;
    __lconv_strftime.mon_thousands_sep=locale->loc_MonGroupSeparator;
    __lconv_strftime.mon_grouping     =locale->loc_MonFracGrouping;
    __lconv_strftime.positive_sign    =locale->loc_MonPositiveSign;
    __lconv_strftime.negative_sign    =locale->loc_MonNegativeSign;
    __lconv_strftime.int_frac_digits  =locale->loc_MonIntFracDigits;
    __lconv_strftime.frac_digits      =locale->loc_MonFracDigits;
    __lconv_strftime.p_cs_precedes    =locale->loc_MonPositiveCSPos;
    __lconv_strftime.p_sep_by_space   =locale->loc_MonPositiveSpaceSep;
    __lconv_strftime.p_sign_posn      =locale->loc_MonPositiveSignPos;
    __lconv_strftime.n_cs_precedes    =locale->loc_MonNegativeCSPos;
    __lconv_strftime.n_sep_by_space   =locale->loc_MonNegativeSpaceSep;
    __lconv_strftime.n_sign_posn      =locale->loc_MonNegativeSignPos;
  }else
  { __lconv_strftime.int_curr_symbol  ="";
    __lconv_strftime.currency_symbol  ="";
    __lconv_strftime.mon_decimal_point="";
    __lconv_strftime.mon_thousands_sep="";
    __lconv_strftime.mon_grouping     ="";
    __lconv_strftime.positive_sign    ="";
    __lconv_strftime.negative_sign    ="";
    __lconv_strftime.int_frac_digits  =CHAR_MAX;
    __lconv_strftime.frac_digits      =CHAR_MAX;
    __lconv_strftime.p_cs_precedes    =CHAR_MAX;
    __lconv_strftime.p_sep_by_space   =CHAR_MAX;
    __lconv_strftime.p_sign_posn      =CHAR_MAX;
    __lconv_strftime.n_cs_precedes    =CHAR_MAX;
    __lconv_strftime.n_sep_by_space   =CHAR_MAX;
    __lconv_strftime.n_sign_posn      =CHAR_MAX;
  }

  if(__localevec[LC_NUMERIC-1]!=NULL)
  { struct Locale *locale=__localevec[LC_NUMERIC-1];
    __lconv_strftime.decimal_point=locale->loc_DecimalPoint;
    __lconv_strftime.thousands_sep=locale->loc_GroupSeparator;
  }else
  { __lconv_strftime.decimal_point=".";
    __lconv_strftime.thousands_sep=""; 
  }
  __decimalpoint=__lconv_strftime.decimal_point;

  if(__localevec[LC_TIME-1]!=NULL)
    __gmtoffset=__localevec[LC_TIME-1]->loc_GMTOffset;
  else
    __gmtoffset=0;
    
  return (char *)name;
}


CONSTRUCTOR_P(initstrftime,200)
{ 
    LocaleBase=(struct LocaleBase *)OpenLibrary("locale.library",37); /* Don't give up if this failes */

    if (setlocale(LC_ALL,"")!=NULL) /* Set the default locale */
      return 0;

    exit(20);
}

DESTRUCTOR_P(closestrftime,200)
{ int i;
  for(i=0;i<5;i++)
    if(__localevec[i]!=NULL)
      CloseLocale(__localevec[i]);
  
  if(LocaleBase!=NULL)
    CloseLibrary((struct Library *)LocaleBase);
}

// ADD2INIT(__initlocale,-10);
// ADD2EXIT(__exitlocale,-10);


