Mercurial > dwindows
comparison compat.c @ 72:ab77a22a2a36
Added localization APIs as well as some miscellaneous bug fixes.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Fri, 15 Feb 2002 09:30:50 +0000 |
parents | 2be5174bdb5d |
children | 2f339dd13496 |
comparison
equal
deleted
inserted
replaced
71:291c30a92b9b | 72:ab77a22a2a36 |
---|---|
728 | 728 |
729 int fsseek(FILE *stream, long offset, int whence) | 729 int fsseek(FILE *stream, long offset, int whence) |
730 { | 730 { |
731 return fseek(stream, offset, whence); | 731 return fseek(stream, offset, whence); |
732 } | 732 } |
733 | |
734 static int locale_number = -1, locale_count = 0; | |
735 static char **locale_text = NULL; | |
736 | |
737 void _free_locale(void) | |
738 { | |
739 if(locale_text) | |
740 { | |
741 int z; | |
742 | |
743 for(z=0;z<locale_count;z++) | |
744 { | |
745 if(locale_text[z]) | |
746 free(locale_text[z]); | |
747 } | |
748 free(locale_text); | |
749 locale_text = NULL; | |
750 } | |
751 } | |
752 | |
753 void _stripcrlf(char *buf) | |
754 { | |
755 int z, len = strlen(buf); | |
756 | |
757 for(z=0;z<len;z++) | |
758 { | |
759 if(buf[z] == '\r' || buf[z] == '\n') | |
760 { | |
761 buf[z] = 0; | |
762 return; | |
763 } | |
764 } | |
765 } | |
766 | |
767 /* Initialize the locale engine | |
768 * Returns: TRUE on success, FALSE on failure. | |
769 */ | |
770 int locale_init(char *filename, int my_locale) | |
771 { | |
772 FILE *fp = fopen(filename, FOPEN_READ_TEXT); | |
773 static char text[1025]; | |
774 int count = 0; | |
775 | |
776 _free_locale(); | |
777 | |
778 if(fp) | |
779 { | |
780 | |
781 fgets(text, 1024, fp); | |
782 if(strncasecmp(text, "MESSAGES=", 9) == 0 && (count = atoi(&text[9])) > 0) | |
783 { | |
784 int current = -1; | |
785 | |
786 locale_text = calloc(count, sizeof(char *)); | |
787 | |
788 while(!feof(fp)) | |
789 { | |
790 fgets(text, 1024, fp); | |
791 _stripcrlf(text); | |
792 | |
793 if(strncasecmp(text, "LOCALE=", 7) == 0) | |
794 { | |
795 if(current > -1) | |
796 { | |
797 fclose(fp); | |
798 locale_count = count; | |
799 locale_number = my_locale; | |
800 return 1; | |
801 } | |
802 if(atoi(&text[7]) == my_locale) | |
803 current = 0; | |
804 } | |
805 else if(current > -1 && current < count) | |
806 { | |
807 /* Use defaults on blank lines */ | |
808 if(text[0]) | |
809 locale_text[current] = strdup(text); | |
810 current++; | |
811 } | |
812 } | |
813 } | |
814 fclose(fp); | |
815 } | |
816 if(locale_text && count) | |
817 { | |
818 locale_count = count; | |
819 locale_number = my_locale; | |
820 return 1; | |
821 } | |
822 return 0; | |
823 } | |
824 | |
825 /* Retrieve a localized string if available */ | |
826 char *locale_string(char *default_text, int message) | |
827 { | |
828 if(locale_number > -1 && message < locale_count && message > -1 && locale_text[message]) | |
829 return locale_text[message]; | |
830 return default_text; | |
831 } | |
832 |