Subversion Repositories SvarDOS

Compare Revisions

Ignore whitespace Rev 1276 → Rev 1277

/svarlang.lib/trunk/auto_nls.c
0,0 → 1,67
/* This file is part of the svarlang project and is published under the terms
* of the MIT license.
*
* Copyright (C) 2021-2023 Mateusz Viste
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
 
#include <stdlib.h>
#include <string.h>
 
#include "svarlang.h"
 
 
int svarlang_autoload_nlspath(const char *progname) {
const char *lang;
const char *nlspath;
char buff[128];
unsigned short i;
 
/* read and validate LANG */
lang = getenv("LANG");
if ((lang == NULL) || (lang[0] == 0) || (lang[1] == 0)) return(-1);
 
/* read and validate NLSPATH */
nlspath = getenv("NLSPATH");
if (nlspath == NULL) return(-2);
 
/* look into every path in NLSPATH */
while (*nlspath != 0) {
 
/* skip any leading ';' separators */
while (*nlspath == ';') nlspath++;
 
if (*nlspath == 0) return(-3);
 
/* copy nlspath to buff and remember len */
for (i = 0; (nlspath[i] != 0) && (nlspath[i] != ';'); i++) buff[i] = nlspath[i];
nlspath += i;
 
/* add a trailing backslash if there is none (non-empty paths empty) */
if ((i > 0) && (buff[i - 1] != '\\')) buff[i++] = '\\';
 
strcpy(buff + i, progname);
strcat(buff + i, ".lng");
 
if (svarlang_load(buff, lang) == 0) return(0);
}
/* failed to load anything */
return(-4);
}
/svarlang.lib/trunk/autoload.c
22,17 → 22,9
* DEALINGS IN THE SOFTWARE.
*/
 
#include <stdlib.h>
 
#include "svarlang.h"
 
int svarlang_autoload(const char *progname) {
const char *s;
char langid[3];
s = getenv("LANG");
if ((s == NULL) || (*s == 0)) return(-1);
langid[0] = s[0];
langid[1] = s[1];
langid[2] = 0;
return(svarlang_load(progname, langid, getenv("NLSPATH")));
if (svarlang_autoload_nlspath(progname) == 0) return(0);
return(-1);
}
/svarlang.lib/trunk/makefile
7,33 → 7,37
 
CFLAGS=-0 -wx -we -ox
 
svarlngs.lib: autoload.c svarlang.c version.c
svarlngs.lib: autoload.c auto_nls.c svarlang.c version.c
wcc $(CFLAGS) -ms autoload.c
wcc $(CFLAGS) -ms auto_nls.c
wcc $(CFLAGS) -ms svarlang.c
wcc $(CFLAGS) -ms version.c
if exist svarlngs.lib del svarlngs.lib
wlib -n svarlngs.lib +autoload.obj +svarlang.obj +version.obj
wlib -n svarlngs.lib +autoload.obj +auto_nls.obj +svarlang.obj +version.obj
 
svarlngc.lib: autoload.c svarlang.c version.c
svarlngc.lib: autoload.c auto_nls.c svarlang.c version.c
wcc $(CFLAGS) -mc autoload.c
wcc $(CFLAGS) -mc auto_nls.c
wcc $(CFLAGS) -mc svarlang.c
wcc $(CFLAGS) -mc version.c
if exist svarlngc.lib del svarlngc.lib
wlib -n svarlngc.lib +autoload.obj +svarlang.obj +version.obj
wlib -n svarlngc.lib +autoload.obj +auto_nls.obj +svarlang.obj +version.obj
 
svarlngm.lib: autoload.c svarlang.c version.c
svarlngm.lib: autoload.c auto_nls.c svarlang.c version.c
wcc $(CFLAGS) -mm autoload.c
wcc $(CFLAGS) -mm auto_nls.c
wcc $(CFLAGS) -mm svarlang.c
wcc $(CFLAGS) -mm version.c
if exist svarlngm.lib del svarlngm.lib
wlib -n svarlngm.lib +autoload.obj +svarlang.obj +version.obj
wlib -n svarlngm.lib +autoload.obj +auto_nls.obj +svarlang.obj +version.obj
 
svarlngl.lib: autoload.c svarlang.c version.c
svarlngl.lib: autoload.c auto_nls.c svarlang.c version.c
wcc $(CFLAGS) -ml autoload.c
wcc $(CFLAGS) -ml auto_nls.c
wcc $(CFLAGS) -ml svarlang.c
wcc $(CFLAGS) -ml version.c
if exist svarlngl.lib del svarlngl.lib
wlib -n svarlngl.lib +autoload.obj +svarlang.obj +version.obj
wlib -n svarlngl.lib +autoload.obj +auto_nls.obj +svarlang.obj +version.obj
 
 
tlumacz.exe: tlumacz.c
/svarlang.lib/trunk/svarlang.c
135,42 → 135,20
}
 
 
int svarlang_load(const char *progname, const char *lang, const char *nlspath) {
int svarlang_load(const char *fname, const char *lang) {
unsigned short langid;
unsigned short fd;
char buff[128];
char hdr[4];
unsigned short buff16[2];
unsigned short i;
 
if (lang == NULL) return(-1);
if (nlspath == NULL) nlspath = ""; /* nlspath can be NULL, treat is as empty */
 
langid = *((unsigned short *)lang);
langid &= 0xDFDF; /* make sure lang is upcase */
 
TRYNEXTPATH:
fd = FOPEN(fname);
if (fd == 0xffff) return(-1);
 
/* skip any leading ';' separators */
while (*nlspath == ';') nlspath++;
 
/* copy nlspath to buff and remember len */
for (i = 0; (nlspath[i] != 0) && (nlspath[i] != ';'); i++) buff[i] = nlspath[i];
nlspath += i;
 
/* add a trailing backslash if there is none (non-empty paths empty) */
if ((i > 0) && (buff[i - 1] != '\\')) buff[i++] = '\\';
 
strcpy(buff + i, progname);
strcat(buff + i, ".lng");
 
fd = FOPEN(buff);
if (fd == 0xffff) { /* failed to open file - either abort or try next path */
if (*nlspath == 0) return(-2);
goto TRYNEXTPATH;
}
 
/* read hdr, should be "SvL\33" */
if ((FREAD(fd, buff, 4) != 4) || (memcmp(buff, "SvL\33", 4) != 0)) {
if ((FREAD(fd, hdr, 4) != 4) || (memcmp(hdr, "SvL\33", 4) != 0)) {
FCLOSE(fd);
return(-3);
}
/svarlang.lib/trunk/svarlang.h
26,35 → 26,34
#define SVARLANG_H
 
/* library version */
#define SVARLANGVER "20230630"
#define SVARLANGVER "20230709"
 
/* returns a pointer to a string with the SvarLANG's library version,
* independently of the SVARLANGVER string above. */
const char *svarlang_getver(void);
 
/* loads translations for program progname, language lang, in paths.
/* loads lang translations from file fname.
*
* only the two first letters of the lang strings are meaningful and they are
* case insensitive.
*
* paths can be either a directory path (like "C:\DATA") or a list of paths
* separated by a semicolon (example: "C:\DATA;.\LANGS;."). It may also be
* NULL, in which case only the current directory will be searched.
* a typical call would be: svarlang_load("myprog.lng", "PL");
*
* a typical call would be this: svarlang_load("myprog", "PL", NULL);
*
* this function returns 0 on success, non-zero otherwise. It is still possible
* to call svarlang_strid() after a load failure, the previously loaded
* language will be used then, or the default language if no loading has been
* done yet. */
int svarlang_load(const char *progname, const char *lang, const char *paths);
int svarlang_load(const char *fname, const char *lang);
 
 
/* same as svarlang_load(), but relies on getenv() to pull LANG and NLSPATH.
/* this relies on getenv() to pull LANG and NLSPATH variables and looks
* for a translation file named "%NLSPATH%\progname.lng".
* this call should be used only by "CORE" SvarDOS programs. */
int svarlang_autoload_nlspath(const char *progname);
 
/* alias to svarlang_autoload_nlspath() */
int svarlang_autoload(const char *progname);
 
 
/* Returns a pointer to the string "id". Does not require svalang_load() to be
* executed, but then it will only return the reference language strings.
* a string id is the concatenation of the CATS-style identifiers, for example
/svarlang.lib/trunk/svarlang.txt
37,7 → 37,7
Then, DEFLANG.C must be compiled and linked to your program along with
SVARLNGx.LIB. From there you will be able to use SvarLANG calls, typically:
 
svarlang_load("myprogram", "pl", "."); /* load .\myprogram.lng */
svarlang_load("myprogram.lng", "pl"); /* load PL lang from myprogram.lng */
puts(svarlang_str(2, 0)); /* display the string with id 2.0 */
 
Read svarlang.h for more information about available functions.