📄 fsutils.c
字号:
/****************************************************************
File Name: FSUTILS.C *
*****************************************************************
Last Modified Date: 2001/04/17
Compiler : GNU Cross-compiler/SDS
Platform : X86 protection mode, MIPS, Dragonball
Usage :
Utility functions for file system, including my
replacement functions for ANSI-C functions.
****************************************************************/
#include <sys/syscall.h>
//#include <ansi_c/stdio.h>
/**** modified by chilong 8/23/2001 ****/
#include <stdio.h>
/**** modified by chilong 8/23/2001 ****/
#include <kernel/general.h>
//#include "../rDebug/include/rdebug.h"
#include "../include/FileSys.h"
extern void printString(char *string);
/*************************************************************
Function: myStrLen
Description:
get the length of a string
Input:
src - the source string with a mix of single-byte and
double-byte chars
Output:
**************************************************************/
int myStrLen(unsigned char *src)
{
int length = 0;
while (*src != '\0')
{
length++;
src++;
}
return length;
}
/**************************************************************
Function: myMemSet
Description:
set every byte in the destination buffer to a value
Input:
dest - the destination buffer
value - the target value
length - the length of the buffer in bytes
**************************************************************/
void *myMemSet(void *dest, char value, unsigned long length)
{
char *dp = (char *)dest;
while (length !=0)
{
*dp++ = value;
length--;
}
return(dest);
}
/**************************************************************
Function: myMemCpy
Description:
copy the content of one buffer to another buffer
Input:
out - the source
in - the destination
length - amount of data to be copied in bytes
**************************************************************/
void *myMemCpy(void *out, const void *in, unsigned long length)
{
char *dst = (char *)out;
char *src = (char *)in;
void *save = (char *)out;
while (length--)
{
*dst++ = *src++;
}
return save;
}
/**************************************************************
Function: myStrCpy
Description:
copy the content of a string to another buffer
Input:
to - the source string
from - the destination string
**************************************************************/
char *myStrCpy(char *to, const char *from)
{
char *p = to;
while (*p++ = *from++)
;
return(to);
}
/*************************************************************
Function: endian_little2local_short
Description:
Convert a short integer from little endian to the
endian used by the current CPU
Input:
little - the value in little endian
local - the short integer in the endian used by the local CPU
**************************************************************/
void endian_little2local_short(unsigned char *little, unsigned short *local)
{
*local = (unsigned short)little[1];
*local = (*local << 8) | little[0];
return;
}
/*************************************************************
Function: endian_local2little_short
Description:
Convert a short integer from the endian used by the
current CPU to little endian
Input:
local - the short integer in the endian used by the local CPU
little - the resulting value in little endian
**************************************************************/
void endian_local2little_short(unsigned short *local, unsigned char *little)
{
little[1] = (*local >> 8);
little[0] = *local & 0x00FF;
return;
}
/*************************************************************
Function: endian_little2local_long
Description:
Convert a long integer from little endian to the
endian used by the current CPU
Input:
little - the value in little endian
local - the long integer in the endian used by the local CPU
**************************************************************/
void endian_little2local_long(unsigned char *little, unsigned long *local)
{
*local = (unsigned short)little[3];
*local = (*local << 8) | little[2];
*local = (*local << 8) | little[1];
*local = (*local << 8) | little[0];
return;
}
/*************************************************************
Function: endian_local2little_long
Description:
Convert a long integer from the endian used by the
current CPU to little endian
Input:
local - the long integer in the endian used by the local CPU
little - the resulting value in little endian
**************************************************************/
void endian_local2little_long(unsigned long *local, unsigned char *little)
{
little[3] = (unsigned char)((*local >> 24) & 0x000000FF);
little[2] = (unsigned char)((*local >> 16) & 0x000000FF);
little[1] = (unsigned char)((*local >> 8) & 0x000000FF);
little[0] = (unsigned char)(*local & 0x000000FF);
return;
}
/*************************************************************
Function: toUpperCase
Description:
Converts all lowercase char in a string to uppercase.
Input:
string - target string
Output:
string - in uppercase
Note:
The original string with lowercase char is destroyed
after this function.
**************************************************************/
void toUpperCase(unsigned char *string)
{
while (*string != '\0')
{
if (*string < 0x80)
{
if ((*string >= 'a') && (*string <= 'z'))
*string -= (char)('a' - 'A');
string++;
}
else
{
string++;
if (*string == '\0')
return;
else
string++;
}
}
}
/*************************************************************
Function: iCompareChar
Description:
Case-insensitive comparison of 2 characters
Input:
char1, char2 - the two chars to be compared
Output:
1 match
0 unmatch
**************************************************************/
#if 0//Kevin
int iCompareChar(unsigned char char1, unsigned char char2)
{
if (char1 >= 'a' && char1 <= 'z')
char1 -= ('a' - 'A');
if (char2 >= 'a' && char2 <= 'z')
char2 -= ('a' - 'A');
if (char1 == char2)
return 1;
else
return 0;
}
#endif
/*************************************************************
Function: timeConvert
Description:
convert file's date and time to time in seconds
Input:
fDate - file date
fTime - file time
timeInSec - the resulting time in seconds
Output:
NONE
**************************************************************/
void timeConvert(unsigned short fDate, unsigned short fTime, unsigned long *timeInSec)
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
unsigned int days = 0;
// 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
// h h h h h m m m m m m s s s s s
// 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
// y y y y y y y m m m m d d d d d
tm_sec = (fTime & 0x001F) * 2;
tm_min = (fTime & 0x07E0) >> 5;
tm_hour = (fTime & 0xf800) >> 11;
tm_mday = (fDate & 0x001F);
/* because the available value of month is 0~6 */
tm_mon = ((fDate & 0x01E0) >> 5);
/* in C, year base is 1900 , but in TICKERS base is 1990 and in fat12 year base is 1980 */
tm_year = ((fDate & 0xFE00) >> 9) + 90;
*timeInSec = tm_sec + tm_min * 60 + tm_hour * 3600;
days = (tm_year - 70) / 4;
if (tm_year >= 100)
days--;
days += ((tm_year - 70) * 365);
tm_year += 1900;
days += ((tm_mon * 3057 - 3007) / 100);
if (tm_mon > 2)
{
if (tm_year % 400 == 0 || (tm_year % 4 == 0 && tm_year % 100 != 0))
days--;
else
days -= 2;
}
days += tm_mday;
*timeInSec += days * 3600 * 24;
return;
}
/*************************************************************
Function: myPrintInt
Description:
Similar to printInt() but without going to the next line
Input:
number - the number to be printed
type - type of printing
**************************************************************/
void myPrintInt(unsigned long number, int type)
{
char string[20];
intToString((unsigned int)number, string, type);
printString(string);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -