Files

43 lines
1.9 KiB
C

#ifndef GW_STARTUP_LOCALE_TEXT_H
#define GW_STARTUP_LOCALE_TEXT_H
#include <stddef.h>
#include <string.h>
static int gw_locale_space(char c) { return c==' ' || c=='\t' || c=='\r'; }
static int gw_locale_equal(const char *s,size_t n,const char *word) {
size_t i;if(strlen(word)!=n)return 0;
for(i=0;i<n;++i){char c=s[i];if(c>='A'&&c<='Z')c=(char)(c+'a'-'A');if(c!=word[i])return 0;}
return 1;
}
/* Preserve unrelated lines byte-for-byte; remove every locale assignment,
* including mixed-case duplicates, before appending the two canonical CVars. */
static int gw_locale_text(const char *in,size_t length,char *out,size_t capacity,size_t *written) {
static const char settings[]="SET textLocale \"enUS\"\r\nSET audioLocale \"enUS\"\r\n";
size_t pos=0,used=0;
if(memchr(in,0,length))return 0;
if(length>=3 && (unsigned char)in[0]==0xef && (unsigned char)in[1]==0xbb && (unsigned char)in[2]==0xbf){
if(capacity<3)return 0;
memcpy(out,in,3);used=pos=3;
}
while(pos<length){
size_t end=pos,a,b;int locale=0;
while(end<length && in[end]!='\n')++end;
if(end<length)++end;
a=pos;while(a<end&&gw_locale_space(in[a]))++a;b=a;
while(b<end&&!gw_locale_space(in[b])&&in[b]!='\n')++b;
if(gw_locale_equal(in+a,b-a,"set")){
a=b;while(a<end&&gw_locale_space(in[a]))++a;b=a;
while(b<end&&!gw_locale_space(in[b])&&in[b]!='\n')++b;
locale=gw_locale_equal(in+a,b-a,"textlocale")||gw_locale_equal(in+a,b-a,"audiolocale");
}
if(!locale){if(end-pos>capacity-used)return 0;memcpy(out+used,in+pos,end-pos);used+=end-pos;}
pos=end;
}
if(used && out[used-1]!='\n' && !(used==3&&(unsigned char)out[0]==0xef)){
if(capacity-used<2)return 0;
out[used++]='\r';out[used++]='\n';
}
if(capacity-used<sizeof(settings)-1)return 0;
memcpy(out+used,settings,sizeof(settings)-1);*written=used+sizeof(settings)-1;return 1;
}
#endif