home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / libnix-0.8-src.lha / libnix-0.8 / sources / nix / locale / setlocale.c < prev   
Encoding:
C/C++ Source or Header  |  1995-01-17  |  5.2 KB  |  197 lines

  1. #include <libraries/locale.h>
  2. #include <locale.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <dos/dos.h>
  6. #ifdef __GNUC__ /* This is very sad :-( */
  7. #undef BITSPERBYTE
  8. #undef MAXINT
  9. #undef MININT
  10. #endif
  11. #include <limits.h>
  12. #ifdef __GNUC__
  13. #include <inline/exec.h>
  14. #include <inline/locale.h>
  15. #endif
  16. #include <strsup.h>
  17. #include <stabs.h>
  18.  
  19. extern struct LocaleBase *LocaleBase;
  20. extern char __localename[];
  21. extern struct Locale *__localevec[];
  22.  
  23. /* for LC_CTYPE */
  24. extern const unsigned char __ctype[];
  25. extern const unsigned char *_ctype_;
  26. static unsigned char *ctype;
  27.  
  28. /* for LC_NUMERIC */
  29. extern unsigned char *__decimalpoint;
  30.  
  31. /* for LC_TIME */
  32. extern long __gmtoffset;
  33. extern int  __dstflag;
  34.  
  35. extern struct lconv __lconv;
  36.  
  37. char *setlocale(int category,const char *name)
  38. { static char *string=NULL;
  39.   size_t i,a,b;
  40.   struct Locale *vec[5];
  41.   unsigned char *s1,*s2,c;
  42.   
  43.   if(string!=NULL) /* Free string if possible */
  44.   { free(string);
  45.     string=NULL; }  
  46.  
  47.   if(category<0||category>5)
  48.     return NULL;
  49.  
  50.   if(category==LC_ALL)
  51.   { a=0;
  52.     b=4; }
  53.   else
  54.     a=b=category-1;
  55.  
  56.   if(name==NULL) /* return name of current locale */
  57.   { size_t s=0;
  58.     for(i=a;i<=b;i++)
  59.       if(__localevec[i]!=NULL)
  60.         s+=strlen(__localevec[i]->loc_LocaleName);
  61.       else
  62.         s++; /* "C" locale */
  63.  
  64.     if((string=malloc(s+b-a+1))==NULL)
  65.       return NULL;
  66.  
  67.     string[0]='\0';
  68.     for(i=a;i<=b;i++)
  69.     { if(__localevec[i]!=NULL)
  70.         strcat(string,__localevec[i]->loc_LocaleName);
  71.       else
  72.         strcat(string,"C");
  73.       if(i!=b)
  74.         strcat(string,"\n");
  75.     }
  76.     return string;
  77.   }
  78.  
  79.   if((string=malloc(strlen_plus_one(name)))==NULL) /* gets freed next time */
  80.     return NULL;
  81.   strcpy(string,name);
  82.  
  83.   s1=s2=string;
  84.   for(i=a;i<=b;i++)
  85.   { while(*s2!='\0'&&*s2!='\n')
  86.       s2++;
  87.     c=*s2;
  88.     *s2='\0';
  89.     if(LocaleBase==NULL||(s1[0]=='C'&&s1[1]=='\0')) /* This is the only place */
  90.       vec[i]=NULL;                         /* LocaleBase gets tested for NULL */
  91.     else
  92.       if((vec[i]=OpenLocale(s1[0]=='\0'?NULL:s1))==NULL)
  93.       { for(;i-->a;)
  94.           CloseLocale(vec[i]);
  95.         return NULL; }
  96.     *s2=c;
  97.     if(c=='\0')
  98.       s2=string;
  99.     s1=s2;
  100.   }
  101.  
  102.   for(i=a;i<=b;i++)
  103.   { if(__localevec[i]!=NULL)
  104.       CloseLocale(__localevec[i]);
  105.     __localevec[i]=vec[i];
  106.   }
  107.  
  108.   if(__localevec[LC_CTYPE-1]!=NULL)
  109.   { struct Locale *locale=__localevec[LC_CTYPE-1];
  110.     ctype[0]=0;
  111.     for(i=0;i<256;i++)
  112.       ctype[i+1]=((IsPrint(locale,i)&&!IsGraph(locale,i))?128:0)|
  113.                  (IsXDigit(locale,i)?64:0)|(IsCntrl(locale,i)?32:0)|
  114.                  (IsPunct(locale,i)?16:0)|(IsSpace(locale,i)?8:0)|
  115.                  (IsDigit(locale,i)?4:0)|(IsLower(locale,i)?2:0)|
  116.                  (IsUpper(locale,i)?1:0);
  117.     _ctype_=ctype;
  118.   }else
  119.     _ctype_=__ctype;
  120.  
  121.   if(__localevec[LC_MONETARY-1]!=NULL)
  122.   { struct Locale *locale=__localevec[LC_MONETARY-1];
  123.     __lconv.int_curr_symbol  =locale->loc_MonIntCS;
  124.     __lconv.currency_symbol  =locale->loc_MonCS;
  125.     __lconv.mon_decimal_point=locale->loc_MonDecimalPoint;
  126.     __lconv.mon_thousands_sep=locale->loc_MonGroupSeparator;
  127.     __lconv.mon_grouping     =locale->loc_MonFracGrouping;
  128.     __lconv.positive_sign    =locale->loc_MonPositiveSign;
  129.     __lconv.negative_sign    =locale->loc_MonNegativeSign;
  130.     __lconv.int_frac_digits  =locale->loc_MonIntFracDigits;
  131.     __lconv.frac_digits      =locale->loc_MonFracDigits;
  132.     __lconv.p_cs_precedes    =locale->loc_MonPositiveCSPos;
  133.     __lconv.p_sep_by_space   =locale->loc_MonPositiveSpaceSep;
  134.     __lconv.p_sign_posn      =locale->loc_MonPositiveSignPos;
  135.     __lconv.n_cs_precedes    =locale->loc_MonNegativeCSPos;
  136.     __lconv.n_sep_by_space   =locale->loc_MonNegativeSpaceSep;
  137.     __lconv.n_sign_posn      =locale->loc_MonNegativeSignPos;
  138.   }else
  139.   { __lconv.int_curr_symbol  ="";
  140.     __lconv.currency_symbol  ="";
  141.     __lconv.mon_decimal_point="";
  142.     __lconv.mon_thousands_sep="";
  143.     __lconv.mon_grouping     ="";
  144.     __lconv.positive_sign    ="";
  145.     __lconv.negative_sign    ="";
  146.     __lconv.int_frac_digits  =CHAR_MAX;
  147.     __lconv.frac_digits      =CHAR_MAX;
  148.     __lconv.p_cs_precedes    =CHAR_MAX;
  149.     __lconv.p_sep_by_space   =CHAR_MAX;
  150.     __lconv.p_sign_posn      =CHAR_MAX;
  151.     __lconv.n_cs_precedes    =CHAR_MAX;
  152.     __lconv.n_sep_by_space   =CHAR_MAX;
  153.     __lconv.n_sign_posn      =CHAR_MAX;
  154.   }
  155.  
  156.   if(__localevec[LC_NUMERIC-1]!=NULL)
  157.   { struct Locale *locale=__localevec[LC_NUMERIC-1];
  158.     __lconv.decimal_point=locale->loc_DecimalPoint;
  159.     __lconv.thousands_sep=locale->loc_GroupSeparator;
  160.   }else
  161.   { __lconv.decimal_point=".";
  162.     __lconv.thousands_sep=""; 
  163.   }
  164.   __decimalpoint=__lconv.decimal_point;
  165.  
  166.   if(__localevec[LC_TIME-1]!=NULL)
  167.     __gmtoffset=__localevec[LC_TIME-1]->loc_GMTOffset;
  168.   else
  169.     __gmtoffset=0;
  170.     
  171.   return (char *)name;
  172. }
  173.  
  174. void __initlocale(void)
  175. { if ((ctype=malloc(257))!=NULL)
  176.   {
  177.     LocaleBase=(struct LocaleBase *)OpenLibrary(__localename,0); /* Don't give up if this failes */
  178.  
  179.     if (setlocale(LC_ALL,"")!=NULL) /* Set the default locale */
  180.       return;
  181.   }
  182.   exit(20);
  183. }
  184.  
  185. void __exitlocale(void)
  186. { int i;
  187.   for(i=0;i<5;i++)
  188.     if(__localevec[i]!=NULL)
  189.       CloseLocale(__localevec[i]);
  190.   
  191.   if(LocaleBase!=NULL)
  192.     CloseLibrary((struct Library *)LocaleBase);
  193. }
  194.  
  195. ADD2INIT(__initlocale,-10);
  196. ADD2EXIT(__exitlocale,-10);
  197.