📄 filesys.h
字号:
/*
* Copyright (c) 2004, Dennis Kuschel.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/**
* @file filesys.h
* @author Dennis Kuschel
* @brief Simple file system for embedded devices
*
* This software is from http://mycpu.mikrocontroller.net.
* Please send questions and bug reports to dennis_k@freenet.de.
*/
#ifndef _FILESYS_H
#define _FILESYS_H
#define HAVE_EMBEDDED_FILESYSTEM 1
#include "sys.h"
/** @defgroup fsys Embedded File System *//** @{ */
#ifndef FS_MAXFILES
/**
* Set the maximum number of simultanousely opened files.
* Default is 10.
*/
#define FS_MAXFILES 10
#endif
#ifndef FS_STANDARD_API
/**
* Set this define to 1 to enable standard I/O function
* names for open/close/read/write etc.
*/
#define FS_STANDARD_API 0
#endif
#ifndef FS_SUBDIRECTORIES
/**
* Enable support for 'virtual' subdiretories when set to 1.
* The filesystem API will be extendet by some subdirectory functions.
*/
#define FS_SUBDIRECTORIES 1
#endif
#ifndef FS_MAXFNAMELEN
/**
* Maximum length of a filename
*/
#define FS_MAXFNAMELEN 256
#endif
/* Flags used with fsys_open */
#define FSO_RDWR 0x000 /**< open for reading and writing */
#define FSO_RDONLY 0x001 /**< open for reading only */
#define FSO_WRONLY 0x002 /**< open for writing only */
#define FSO_CREAT 0x004 /**< create new file and open it */
#define FSO_TRUNC 0x008 /**< open and truncate file to zero length */
#define FSO_SEQUENTIAL 0x000 /**< file access is primarily sequential */
#define FSO_RANDOM 0x010 /**< file access is primarily random */
#define FSO_BINARY 0x000 /**< file mode is binary (untranslated) */
#define FSO_TEXT 0x020 /**< file mode is text (translated) */
#define FSO_EXCL 0x040 /**< open only if file doesn't already exist */
#define FSO_APPEND 0x080 /**< writes are done at end of file */
/* Flags used with fsys_stat */
#define FSS_IFMT 0x00170000 /**< file type mask */
#define FSS_IFDIR 0x00040000 /**< directory */
#define FSS_IFCHR 0x00020000 /**< character special */
#define FSS_IFIFO 0x00010000 /**< pipe */
#define FSS_IFREG 0x00100000 /**< regular */
#define FSS_IREAD 0x00000400 /**< read permission, owner */
#define FSS_IWRITE 0x00000200 /**< write permission, owner */
#define FSS_IEXEC 0x00000100 /**< execute/search permission, owner */
/* Attributes used with by the structure struct fsys_finddata */
#define FSA_NORMAL 0x00 /**< normal file - No read/write restrictions */
#define FSA_RDONLY 0x01 /**< read only file */
#define FSA_HIDDEN 0x02 /**< hidden file */
#define FSA_SYSTEM 0x04 /**< system file */
#define FSA_SUBDIR 0x08 /**< subdirectory */
#define FSA_ARCH 0x10 /**< archive file */
/* Flags used with fsys_seek */
#define FSSEEK_SET 0 /**< seek starts at beginning of file */
#define FSSEEK_CUR 1 /**< seek starts from current position */
#define FSSEEK_END 2 /**< seek starts from end of file */
/* Define macros to match the standard I/O API */
#if FS_STANDARD_API
#define O_RDWR FSO_RDWR
#define O_RDONLY FSO_RDONLY
#define O_WRONLY FSO_WRONLY
#define O_CREAT FSO_CREAT
#define O_TRUNC FSO_TRUNC
#define O_SEQUENTIAL FSO_SEQUENTIAL
#define O_RANDOM FSO_RANDOM
#define O_BINARY FSO_BINARY
#define O_TEXT FSO_TEXT
#define O_EXCL FSO_EXCL
#define O_APPEND FSO_APPEND
#define S_IFMT FSS_IFMT
#define S_IFDIR FSS_IFDIR
#define S_IFCHR FSS_IFCHR
#define S_IFIFO FSS_IFIFO
#define S_IFREG FSS_IFREG
#define S_IREAD FSS_IREAD
#define S_IWRITE FSS_IWRITE
#define S_IEXEC FSS_IEXEC
#define A_NORMAL FSA_NORMAL
#define A_RDONLY FSA_RDONLY
#define A_HIDDEN FSA_HIDDEN
#define A_SYSTEM FSA_SYSTEM
#define A_SUBDIR FSA_SUBDIR
#define A_ARCH FSA_ARCH
#define open(a,b) fsys_open(a,b)
#define close(a) fsys_close(a)
#define read(a,b,c) fsys_read(a,b,c)
#define write(a,b,c) fsys_write(a,b,c)
#define eof(a) fsys_eof(a)
#define seek(a,b,c) fsys_seek(a,b,c)
#define tell(a) fsys_tell(a)
#define stat fsys_stat
#define findfirst(a,b) fsys_findfirst(a,b)
#define findnext(a,b) fsys_findnext(a,b)
#define findclose(a) fsys_findclose(a)
#define rename(a,b) fsys_rename(a,b)
#define remove(a) fsys_remove(a)
#endif
/* file time */
struct fsys_time
{
u16_t year; /**< year (0 - 65535) */
u8_t month; /**< month of year (1 - 12) */
u8_t day; /**< day of month (1 - 31) */
u8_t hour; /**< hour (0 - 23) */
u8_t min; /**< minute (0 - 59) */
u8_t sec; /**< second (0 - 59) */
u8_t spare; /* not used */
};
/* file status */
struct fsys_stat
{
u32_t st_mode; /**< file attributes */
sint_t st_size; /**< file size in bytes */
struct fsys_time st_time; /**< file creation time */
};
/* file find data */
struct fsys_finddata
{
u32_t attrib; /**< bitfield of attributes FSA_xxx */
struct fsys_time time; /**< file creation time */
u32_t size; /**< file size in bytes */
char name[(FS_MAXFNAMELEN+3)&~3]; /**< file name */
};
/**
* Open a file for read or write access.
* This function is equivalent to the one from the runtime library.
* @param filename Name of the file that shall be opened.
* No wildcards are allowed.
* @param mode Bitfield of file attributes:
* - FSO_RDWR Open an existing file for reading
* and writing.
* - FSO_RDONLY Open an existing file for read
* access only.
* - FSO_WRONLY Open an existing file for write
* access only.
* - FSO_CREAT Open a file for reading or writing.
* If the file does not exist, it will
* be created. You may also use the flags
* FSO_WRONLY, FSO_EXCL and FSO_TRUNC
* together with this flag.
* - FSO_EXCL If this flag is set together with
* FSO_CREAT, a new file will be created
* and opend for writing, but only when
* no file with this name just exist.
* Thus, this flag prevents from
* overwriting an existing file.
* - FSO_TRUNC Open an existing file for reading
* or writing. The file size will be
* set to 0. This flag may be used
* together with FSO_CREAT to create
* a fresh new file; an existing file
* will be overwritten.
* - FSO_APPEND Append new data always to the end
* of the file. The file pointer is
* automatically set to the end of the
* file before a write is performed.
* @return A file handle on success, or -1 when an error has happened.
* @sa fsys_close
*/
sint_t fsys_open(const char *filename, const sint_t mode);
/**
* Create and open a temporary file.
* Sometimes it is useful to create a temporary file to store
* some data for a short time. This function automatically
* generates an unique file name, creates an empty file and
* returns the handle to the file. Note that this function is
* useful in conjunction with ::fsys_replace to provide dynamic
* html pages for a http demon.
* @param filename Pointer to a user-provided buffer. The name of
* the temporary file will be copied into this buffer.
* @param mode Mode flags (see ::fsys_open). Only the flags
* FSO_WRONLY and FSO_APPEND do make sense, the flag
* FSO_CREAT is implied by this function.
* @return A file handle on success, or -1 when an error has happened.
* @sa fsys_replace, fsys_open, fsys_close
*/
sint_t fsys_opent(char *filename, const sint_t mode);
/**
* Close a file again.
* This function is equivalent to the one from the runtime library.
* @param file Handle to the file to close.
* @sa fsys_open, fsys_read, fsys_write
*/
void fsys_close(const sint_t file);
/**
* Write a block of data to a file.
* This function is equivalent to the one from the runtime library.
* @param file Handle to the file to write to.
* @param buf Pointer to a user provided buffer that contains
* the data to write.
* @param count Number of bytes to write.
* @return The number of bytes that have successfully been written,
* or -1 when an error has happened.
* @sa fsys_open, fsys_close, fsys_read
*/
sint_t fsys_write(const sint_t file, const char *buf, const sint_t count);
/**
* Read a block of data from a file.
* This function is equivalent to the one from the runtime library.
* @param file Handle to the file to read from.
* @param buf Pointer to a user provided buffer that shall be
* filled with the read data.
* @param count Maximum number of bytes that shall be read.
* @return The number of bytes that have successfully been read,
* or -1 when an error has happened. Zero is returned when
* no more data is available (-> End Of File).
* @sa fsys_open, fsys_close, fsys_write
*/
sint_t fsys_read(const sint_t file, const char *buf, const sint_t count);
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -