Aggiunta stricmp() per UNIX

git-svn-id: svn://10.65.10.50/trunk@34 c028cbd2-c16b-5b4b-a496-9718f37d4682
This commit is contained in:
villa 1994-08-19 07:10:19 +00:00
parent ea150a8931
commit 184c4cc092
2 changed files with 271 additions and 254 deletions

View File

@ -1,225 +1,237 @@
#include <ctype.h> #include <ctype.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <strings.h> #include <strings.h>
#define __UTILITY_CPP #define __UTILITY_CPP
#include <utility.h> #include <utility.h>
#if XVT_OS == XVT_OS_SCOUNIX #if XVT_OS == XVT_OS_SCOUNIX
#include <unistd.h> #include <unistd.h>
int remove(const char* path) int remove(const char* path)
{ return unlink(path); } { return unlink(path); }
#else #else
#include <io.h> #include <io.h>
#define F_OK 0 #define F_OK 0
#endif #endif
bool fcopy(const char* orig, const char* dest) bool fcopy(const char* orig, const char* dest)
{ {
#if XVT_OS == XVT_OS_SCOUNIX #if XVT_OS == XVT_OS_SCOUNIX
const char* const rflag = "r"; const char* const rflag = "r";
const char* const wflag = "w"; const char* const wflag = "w";
#else #else
const char* const rflag = "rb"; const char* const rflag = "rb";
const char* const wflag = "wb"; const char* const wflag = "wb";
#endif #endif
FILE* i = fopen(orig, rflag); FILE* i = fopen(orig, rflag);
if (!i) return error_box("Impossibile leggere il file %s", orig); if (!i) return error_box("Impossibile leggere il file %s", orig);
FILE* o = fopen(dest, wflag); FILE* o = fopen(dest, wflag);
CHECKS(o, "Impossibile scrivere il file ", dest); CHECKS(o, "Impossibile scrivere il file ", dest);
const word size = 16*1024; const word size = 16*1024;
TString buffer(size); TString buffer(size);
while (TRUE) while (TRUE)
{ {
const word letti = fread((char*)(const char*)buffer, 1, size, i); const word letti = fread((char*)(const char*)buffer, 1, size, i);
fwrite((char*)(const char*)buffer, 1, letti, o); fwrite((char*)(const char*)buffer, 1, letti, o);
if (letti < size) break; if (letti < size) break;
} }
fclose(o); fclose(o);
fclose(i); fclose(i);
return TRUE; return TRUE;
} }
bool fexist(const char* file) bool fexist(const char* file)
{ {
return access(file, F_OK) == 0; return access(file, F_OK) == 0;
} }
// Best function of the year // Best function of the year
// Certified 99% // Certified 99%
char* format(const char* fmt, ...) char* format(const char* fmt, ...)
{ {
va_list pars; va_list pars;
va_start(pars, fmt); va_start(pars, fmt);
const int tot = vsprintf(__tmp_string, fmt, pars); const int tot = vsprintf(__tmp_string, fmt, pars);
va_end(pars); va_end(pars);
CHECK(tot >= 0 && tot < sizeof(__tmp_string)-1, "Ue'! Ma quanto scrivi?"); CHECK(tot >= 0 && tot < sizeof(__tmp_string)-1, "Ue'! Ma quanto scrivi?");
return(__tmp_string); return(__tmp_string);
} }
const char* cmd2name(const char* argv0, const char* argv1) const char* cmd2name(const char* argv0, const char* argv1)
{ {
TFilename app(argv0); app.ext(""); app = app.name(); TFilename app(argv0); app.ext(""); app = app.name();
if (argv1) app << ' ' << argv1; if (argv1) app << ' ' << argv1;
app.lower(); app.lower();
const int par = app.find(" -"); const int par = app.find(" -");
if (par > 0) if (par > 0)
{ {
int num = atoi(app.mid(par+2)) + 1; int num = atoi(app.mid(par+2)) + 1;
char c = (num > 9) ? ('a'+num-10) : ('0'+num); char c = (num > 9) ? ('a'+num-10) : ('0'+num);
app.cut(par); app.cut(par);
app << c << "00"; app << c << "00";
} }
return strcpy(__tmp_string, app); return strcpy(__tmp_string, app);
} }
/////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////
// Conversione in cifre romane // Conversione in cifre romane
/////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////
HIDDEN const char * cifre_romane = "IVXLCDM@"; HIDDEN const char * cifre_romane = "IVXLCDM@";
HIDDEN const int valori_cifre [] = { 1, 5, 10, 50, 100, 500, 1000, -1 }; HIDDEN const int valori_cifre [] = { 1, 5, 10, 50, 100, 500, 1000, -1 };
HIDDEN int ctoi(char c) HIDDEN int ctoi(char c)
{ {
if (c == '\0') return 0; if (c == '\0') return 0;
c = toupper(c); c = toupper(c);
for (int i = 0; cifre_romane[i]; i++) for (int i = 0; cifre_romane[i]; i++)
if (cifre_romane[i] == c) return valori_cifre[i]; if (cifre_romane[i] == c) return valori_cifre[i];
return -1; return -1;
} }
int rtoi(const char * val) int rtoi(const char * val)
{ {
if (val == NULL) return 0; if (val == NULL) return 0;
int tot = 0; int tot = 0;
int value = ctoi (val[0]); int value = ctoi (val[0]);
for (int i = 1; value > 0; i++) for (int i = 1; value > 0; i++)
{ {
const int next_val = ctoi(val[i]); const int next_val = ctoi(val[i]);
if (value < next_val) tot -= value; if (value < next_val) tot -= value;
else tot += value; else tot += value;
value = next_val; value = next_val;
} }
return (value == 0) ? tot : -1; return (value == 0) ? tot : -1;
} }
const char* itor(int num) const char* itor(int num)
{ {
HIDDEN char roman_string[16]; HIDDEN char roman_string[16];
int cifra = 0; int cifra = 0;
for (int pos = 7; pos--;) for (int pos = 7; pos--;)
{ {
int val = valori_cifre[pos]; int val = valori_cifre[pos];
int quanti = num / val; int quanti = num / val;
if (quanti < 4) if (quanti < 4)
{ {
if ((pos & 1) && quanti == 1 && (num/valori_cifre[pos-1]) == 9) if ((pos & 1) && quanti == 1 && (num/valori_cifre[pos-1]) == 9)
{ {
roman_string[cifra++] = cifre_romane[pos-1]; roman_string[cifra++] = cifre_romane[pos-1];
roman_string[cifra++] = cifre_romane[pos+1]; roman_string[cifra++] = cifre_romane[pos+1];
val = valori_cifre[pos-1]; val = valori_cifre[pos-1];
quanti = 9; quanti = 9;
} }
else for (int i = 0; i < quanti; i++) else for (int i = 0; i < quanti; i++)
roman_string[cifra++] = cifre_romane[pos]; roman_string[cifra++] = cifre_romane[pos];
} }
else else
{ {
roman_string[cifra++] = cifre_romane[pos]; roman_string[cifra++] = cifre_romane[pos];
roman_string[cifra++] = cifre_romane[pos+1]; roman_string[cifra++] = cifre_romane[pos+1];
} }
num -= quanti * val; num -= quanti * val;
} }
roman_string[cifra] = '\0'; roman_string[cifra] = '\0';
return roman_string; return roman_string;
} }
const char *esc(const char* s) const char *esc(const char* s)
{ {
const char *s1 = s; const char *s1 = s;
char *s2 = __tmp_string; char *s2 = __tmp_string;
int base; int base;
while (*s1) while (*s1)
{ {
if (*s1 == '\\') if (*s1 == '\\')
{ {
s1++; s1++;
switch (tolower(*s1)) switch (tolower(*s1))
{ {
case 'b' : *s2++ = '\b'; break; case 'b' : *s2++ = '\b'; break;
case 'e' : *s2++ = '\033'; break; case 'e' : *s2++ = '\033'; break;
case 'f' : *s2++ = '\f'; break; case 'f' : *s2++ = '\f'; break;
case 'n' : *s2++ = '\n'; break; case 'n' : *s2++ = '\n'; break;
case 'r' : *s2++ = '\r'; break; case 'r' : *s2++ = '\r'; break;
case 't' : *s2++ = '\t'; break; case 't' : *s2++ = '\t'; break;
default : default :
{ {
if (isdigit(*s1)) if (isdigit(*s1))
{ {
if (*s1 == '0') if (*s1 == '0')
{ {
s1++; s1++;
if (tolower(*s1) == 'x') if (tolower(*s1) == 'x')
{ {
s1++; s1++;
base = 16; base = 16;
} }
else base = 8; else base = 8;
} }
else base = 10; else base = 10;
*s2 = 0; *s2 = 0;
char c = tolower(*s1); char c = tolower(*s1);
while (isdigit(c) || (base == 16 && c >= 'a' && c <= 'f')) while (isdigit(c) || (base == 16 && c >= 'a' && c <= 'f'))
{ {
*s2 *= base; *s2 *= base;
if (isdigit(*s1)) *s2 += (*s1 - 48); if (isdigit(*s1)) *s2 += (*s1 - 48);
else *s2 += (*s1 - 'a' + 10) & 0x0F; else *s2 += (*s1 - 'a' + 10) & 0x0F;
s1++; s1++;
c = tolower(*s1); c = tolower(*s1);
} }
s2++; s1--; s2++; s1--;
} }
else *s2++ = *s1; else *s2++ = *s1;
} }
} }
} }
else else
if (*s1 == '^') if (*s1 == '^')
{ {
s1++; s1++;
*s2++ = (tolower(*s1) - 'a' + 1); *s2++ = (tolower(*s1) - 'a' + 1);
} }
else *s2++ = *s1 ; else *s2++ = *s1 ;
s1++; s1++;
} }
*s2 = '\0'; *s2 = '\0';
return(__tmp_string); return(__tmp_string);
} }
#if XVT_OS == XVT_OS_SCOUNIX
int stricmp(const char* s1, const char* s2)
{
while (toupper(*s1) == toupper(*s2))
if (*s1++ == '\0' && *s2++ == '\0')
return 0;
return *s1 - *s2;
}
#endif

View File

@ -1,29 +1,34 @@
#ifndef __UTILITY_H #ifndef __UTILITY_H
#define __UTILITY_H #define __UTILITY_H
#ifndef __STDTYPES_H #ifndef __STDTYPES_H
#include <stdtypes.h> #include <stdtypes.h>
#endif #endif
/* @FPUB */ /* @FPUB */
char* format (const char* fmt, ...); char* format (const char* fmt, ...);
const char* cmd2name(const char* argv0, const char* argv1 = ""); const char* cmd2name(const char* argv0, const char* argv1 = "");
int rtoi(const char * roman); int rtoi(const char * roman);
const char* itor(int i); const char* itor(int i);
bool fcopy(const char* orig, const char* dest); bool fcopy(const char* orig, const char* dest);
bool fexist(const char* file); bool fexist(const char* file);
const char* esc(const char*);
#if XVT_OS == XVT_OS_SCOUNIX
#define ODD(x) (x & 1) int stricmp(const char*, const char*);
#define EVEN(x) !(x & 1) #endif
#ifdef __UTILITY_CPP const char* esc(const char*);
char __tmp_string[1024];
#else #define ODD(x) (x & 1)
extern char __tmp_string[1024]; #define EVEN(x) !(x & 1)
#endif
#ifdef __UTILITY_CPP
char __tmp_string[1024];
/* @END */ #else
#endif // __UTILITY_H extern char __tmp_string[1024];
#endif
/* @END */
#endif // __UTILITY_H