📄 util.c
字号:
if( (len == 0) ||
(len > 255) ||
badchar ) return(FALSE);
return(TRUE);
}
BOOLEAN _illegal_alias_char(char ch) /*__fn__*/
{
int n;
for(n=0; _bad_alias_chars[n]!=0; n++)
{
if(ch == _bad_alias_chars[n]) return(TRUE);
}
return(FALSE);
}
BOOLEAN _illegal_lfn_char(char ch) /*__fn__*/
{
int n;
for(n=0; _bad_lfn_chars[n]!=0; n++)
{
if(ch == _bad_lfn_chars[n]) return(TRUE);
}
return(FALSE);
}
RTFS_FILE(malias.c, pc_malias)
#ifndef __PCDISK__ /* This allows us to build the lib with subroutines split */
#include <pcdisk.h>
#endif
/*****************************************************************************
PC_MALIAS - Create a unique alias for input_file
Description
Fills fname and fext with a valid short file name alias that is unique
in the destination directory (dest). Not to be confused with pc_galias,
which finds the currently used short file name alias for an existing
file.
Returns
TRUE if a unique alias could be found, FALSE otherwise
*****************************************************************************/
BOOLEAN pc_malias(char *fname, char *fext, byte *input_file, DROBJ *dest) /*__fn__*/
{
int n,i,s;
DROBJ *pobj;
BOOLEAN aliasunique;
char filename[9],fileext[4],alias[13];
if(!pc_valid_lfn((char *)input_file)) return(FALSE);
if(pc_valid_sfn((char *)input_file)) /* No change necessary! */
{
pc_fileparse(fname,fext,(char *)input_file);
pc_str2upper(fname,fname);
pc_str2upper(fext,fext);
return(TRUE);
}
while(*input_file=='.' || *input_file==' ') input_file++;
/* find extension start */
for(n=0,i=0; input_file[n]!=0; n++) /* i holds the position right */
{ /* after the last period */
if(input_file[n]=='.') i=n+1;
}
if(i>0 && input_file[i]!=0){
/* copy extension to fileext[] */
for(n=i,s=0; input_file[n]!='\0' && s<3; n++)
{
if(input_file[n]!=' ')
{
if(_illegal_alias_char(input_file[n]))
{
fileext[s++] = '_';
}
else
fileext[s++] = input_file[n];
}
}
fileext[s]=0;} else { i=512; fileext[0]=0; } /* null terminate */
/* copy file name to filename[], filtering out spaces, periods and
replacing characters illegal in alias names with '_' */
for(n=0,s=0; n<i && input_file[n]!=0 && s<6; n++)
{
if(input_file[n]!=' ' && input_file[n]!='.')
{
if(_illegal_alias_char(input_file[n]) || input_file[n]>127)
{
filename[s++] = '_';
}
else
filename[s++] = input_file[n];
}
}
for(;s<8;s++) /* pad with spaces */
{
filename[s] = ' ';
}
filename[8]=0; /* null terminate filename[] */
pc_str2upper(filename,filename);
pc_str2upper(fileext,fileext);
i-=i; /* i=0; i is the alias index */
do
{
i++;
/* append (TEXT[])i to filename[] */
for(n=7,s=i; s>0 && n>0; s/=10,n--)
{
filename[n] = (char)(((char)s%10)+'0');
}
if(n==0 && s>0)
return(FALSE);
else
filename[n]='~';
/* copy filename[] to alias[], filtering out spaces */
for(n=0,s=0; s<8; s++)
{
if(filename[s]!=' ')
alias[n++]=filename[s];
}
if(fileext[0] != 0)
{
alias[n++]='.'; /* insert separating period */
/* copy fileext[] to alias[] */
for(s=0; fileext[s]!=0; s++,n++)
{
alias[n]=fileext[s];
}
}
alias[n]=0; /* null terminate alias[] */
pobj = pc_get_inode(0,dest,(byte*)alias,0,FALSE);
if(pobj)
{
aliasunique = FALSE;
pc_freeobj(pobj);
}
else
aliasunique = TRUE;
}
while(!aliasunique);
pc_fileparse(fname,fext,alias);
return(TRUE);
}
RTFS_FILE(cksum.c, pc_cksum)
#ifndef __PCDISK__ /* This allows us to build the lib with subroutines split */
#include <pcdisk.h>
#endif
/*****************************************************************************
PC_CKSUM - Compute checksum on string
Description
Computes a single UTINY checksum based on the first 11 characters in
the input string (test). This value is used to link LFNINODE's on the
disk directory table.
Returns
The value of the checksum
*****************************************************************************/
byte pc_cksum(byte *test) /*__fn__*/
{
byte sum,i;
for (sum = i = 0; i < 11; i++)
{
sum = (byte)((((sum & 0x01)<<7)|((sum & 0xFE)>>1)) + test[i]);
}
return(sum);
}
#endif /* VFAT */
/*****************************************************************************
PC_CNVRT - Convert intel byte order to native byte order.
Summary
#include <pcdisk.h>
dword to_DWORD (from) Convert intel style 32 bit to native 32 bit
byte *from;
word to_WORD (from) Convert intel style 16 bit to native 16 bit
byte *from;
void fr_WORD (to,from) Convert native 16 bit to 16 bit intel
byte *to;
word from;
void fr_DWORD (to,from) Convert native 32 bit to 32 bit intel
byte *to;
dword from;
Description
This code is known to work on 68K and 808x machines. It has been left
as generic as possible. You may wish to hardwire it for your CPU/Code
generator to shave off a few bytes and microseconds, be careful though
the addresses are not guaranteed to be word aligned in fact to_WORD AND
fr_WORD's arguments are definately NOT word alligned when working on odd
number indeces in 12 bit fats. (see pc_faxx and pc_pfaxx().
Note: Optimize at your own peril, and after everything else is debugged.
Bit shift operators are used to convert intel ordered storage
to native. The host byte ordering should not matter.
Returns
Example:
See other sources.
*****************************************************************************
*/
/* Convert a 32 bit intel item to a portable 32 bit */
dword to_DWORD ( byte *from) /*__fn__*/
{
dword res;
#if (KS_LITTLE_ENDIAN && KS_LITTLE_ODD_PTR_OK)
res = ((dword) *((dword *)from));
#else
dword t;
t = ((dword) *(from + 3)) & 0xff;
res = (t << 24);
t = ((dword) *(from + 2)) & 0xff;
res |= (t << 16);
t = ((dword) *(from + 1)) & 0xff;
res |= (t << 8);
t = ((dword) *from) & 0xff;
res |= t;
#endif
return(res);
}
/* Convert a 16 bit intel item to a portable 16 bit */
word to_WORD ( byte *from) /*__fn__*/
{
word nres;
#if (KS_LITTLE_ENDIAN && KS_LITTLE_ODD_PTR_OK)
nres = ((word) *((word *)from));
#else
word t;
t = (word) (((word) *(from + 1)) & 0xff);
nres = (word) (t << 8);
t = (word) (((word) *from) & 0xff);
nres |= t;
#endif
return(nres);
}
#if (defined(ERTFS_SA))
#ifndef tc_strcpy
int tc_strcpy(PFCHAR targ, PFCCHAR src) /*__fn__*/
{
int loop_cnt=0;
do
{
targ[loop_cnt] = src[loop_cnt];
} while(src[loop_cnt++]);
return loop_cnt;
}
#endif
// compares 2 strings; returns 0 if they match
#ifndef tc_strcmp
int tc_strcmp(PFCCHAR s1, PFCCHAR s2) /*__fn__*/
{
int index=0;
while (s1[index] == s2[index] && s1[index] && s2[index])
{
index++;
}
if (!s1[index] && !s2[index])
return 0;
if (s1[index] < s2[index])
return -1;
else
return 1;
}
#endif
#ifndef tc_strlen
int tc_strlen(PFCCHAR string) /*__fn__*/
{
int len=0;
while (string[len] != 0)
len++;
return len;
}
#endif
// ********************************************************************
// compare function that takes far pointers regardless of model
#ifndef tc_comparen
BOOLEAN tc_comparen(PFBYTE a, PFBYTE b, int n) /*__fn__*/
{ while(n--) {if (*a++ != *b++) return(FALSE);} return(TRUE); }
#endif
#ifndef tc_memset
void tc_memset(PFBYTE p, byte b, int n) /*__fn__*/
{ while(n--) {*p++=b;} }
#endif
#ifndef tc_memchr
PFVOID tc_memchr(PFCBYTE str, int chr, int n) /*__fn__*/
{
int i;
for (i = 0; i < n; i++)
if (*str++ == (byte)chr)
{
str -= 1;
return ((PFVOID) str);
}
return 0;
}
#endif
#ifndef tc_strcat
PFCHAR tc_strcat(PFCHAR targ, PFCCHAR src) /*__fn__*/
{
int ret_val;
ret_val = tc_strlen(targ);
tc_strcpy(targ + ret_val, src);
return targ;
}
#endif
#ifndef tc_isdigit
int tc_isdigit(char ch)
{
return (ch >= '0' && ch <= '9');
}
#endif
#ifndef tc_isprint
int tc_isprint(char ch)
{
return (ch >= ' ' && ch < 127);
}
#endif
#ifndef tc_iscntrl
int tc_iscntrl(char ch)
{
return (ch > 0 && ch < 32) || (ch == 127);
}
#endif
#ifndef tc_atoi
int tc_atoi(PFCHAR s)
{
return((int)tc_atol(s));
}
#endif
#ifndef tc_atol
long tc_atol(PFCHAR s)
{
long n;
BOOLEAN neg;
// skip over tabs and spaces
while ( (*s == ' ') || (*s == '\t') )
s++;
n = 0;
neg = FALSE;
if (*s == '-')
{
neg = TRUE;
s++;
}
while (*s && (*s >= '0' && *s <= '9'))
{
n = n * 10;
n += (long) (*s - '0');
s++;
}
if (neg)
n = 0 - n;
return(n);
}
#endif
// ********************************************************************
// KEEP COMPILER HAPPY ROUTINES
// ********************************************************************
#if (defined(__BORLANDC__))
#pragma warn -aus /* turn off warning "identifier is assigned a value that is never used" */
#endif
#if (!NACT_OS)
// Used to keep the compiler happy
void ARGSUSED_PVOID(PFVOID p) /*__fn__*/
{
p = p;
}
// Used to keep the compiler happy
void ARGSUSED_INT(int i) /*__fn__*/
{
i = i;
}
#endif
#if (defined(__BORLANDC__))
#pragma warn .aus /* turn warning back on */
#endif
#endif // ERTFS_SA
/* Convert a portable 16 bit to a 16 bit intel item */
void fr_WORD ( byte *to, word from) /*__fn__*/
{
#if (KS_LITTLE_ENDIAN && KS_LITTLE_ODD_PTR_OK)
*((word *)to) = from;
#else
*to = (byte) (from & 0x00ff);
*(to + 1) = (byte) ((from >> 8) & 0x00ff);
#endif
}
/* Convert a portable 32 bit to a 32 bit intel item */
void fr_DWORD ( byte *to, dword from) /*__fn__*/
{
#if (KS_LITTLE_ENDIAN && KS_LITTLE_ODD_PTR_OK)
*((dword *)to) = from;
#else
*to = (byte) (from & 0xff);
*(to + 1) = (byte) ((from >> 8) & 0xff);
*(to + 2) = (byte) ((from >> 16) & 0xff);
*(to + 3) = (byte) ((from >> 24) & 0xff);
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -