sparrowFile

SparrowFile is for file handling.  Most basic tasks (opening, reading a amount of bytes, closing) are already done by SDL. sparrowFile extends this to checking whether a file exists, reading until specific signs, searching files, creating folders, etc.  Use only /! not \ ...  For more information about file functions see http://www.libsdl.org/cgi/docwiki.cgi/SDL_API and scroll down to “Files (RWops)”.  Keep in mind: spFilePointer and SDL_RWops* are EXACTLY the same.  ;-)

Summary
sparrowFileSparrowFile is for file handling.
Enumerations
spFileErrorEnumeration for errors while working with files
spFileTypeEnumeration for file types
spFileSortTypeThis enumeration is needed for sorting files when searching in a folder
Types
spFilePointerSame lik SDL_RWops*, but looks better ;)
spFileListLinked list type for searching results, which contains found files.
Functions
spFileExistsTests, whether the file “filename” exists ;-)
spReadOneLineReads one line from a SDL_RWops file.
spWriteOneLineWrites one line from a SDL_RWops file.
spReadUntilReads signs from the file “file” until the buffer is full (buffer_len) or the sign “end_sign” is reached.
spCreateDirectoryChainCreates a directory chain (like /home/user/.config/pinball/settings) if it not exists already.
spRemoveFileRemoves/deletes a file
spRemoveDirectoryRemoves an EMPTY directory
spRenameFileRenames a file
spRenameDirectoryRenames a directory, works exactly like spRenameFile.
spFileGetDirectorySearches for files Puts a double linked list of found files in directory to pointer, found files are directly extracted from the system and may very well not be ordered.
spFileDeleteListDeletes a list created by spFileGetDirectory.
spFileSortListSorts the file list like you want.
spConfigGetPathReturns the path to config files, which may be everywhere on different targets, e.g.
Types
spConfigEntryList type for config entries.
spConfigConfig type
Functions
spConfigReadReads a config and saves it to a spConfig struct
spConfigWriteWrites the config to the file.
spConfigFreeFrees the struct if you don’t need it anymore
spConfigGetStringReturns the string value to a specific key.
spConfigGetStringWithCommentReturns the string value to a specific key.
spConfigGetBoolReturns the bool value to a specific key.
spConfigGetBoolWithCommentReturns the bool value to a specific key.
spConfigSetBoolSets the bool value to a specific key.
spConfigSetBoolWithCommentSets the bool value to a specific key.
spConfigGetIntReturns the int value to a specific key.
spConfigGetIntWithCommentReturns the int value to a specific key.
spConfigSetIntSets the int value to a specific key.
spConfigSetIntWithCommentSets the int value to a specific key.
spConfigGetFloatReturns the float value to a specific key.
spConfigGetFloatWithCommentReturns the float value to a specific key.
spConfigSetFloatSets the float value to a specific key.
spConfigSetFloatWithCommentSets the float value to a specific key.

Enumerations

spFileError

Enumeration for errors while working with files

SP_FILE_EVERYTHING_OKno error, everything ok
SP_FILE_ACCESS_ERRORerror while accessing the file
SP_FILE_NOT_FOUND_ERRORthe file coudln’t be found
SP_FILE_ALREADY_EXISTS_ERRORthe file already exists
SP_FILE_INVALID_PARAMETER_ERRORsome invalid parameter like a file as parameter if a path is needed
SP_FILE_UNKNOWN_ERRORunknown error

spFileType

Enumeration for file types

SP_FILE_FILEa normal file
SP_FILE_DIRECTORYa directory
SP_FILE_LINKa link to a file or a directory

spFileSortType

This enumeration is needed for sorting files when searching in a folder

SP_FILE_SORT_BY_NAMESorting by name
SP_FILE_SORT_BY_TYPESorting by type
SP_FILE_SORT_BY_TYPE_AND_NAMESort by both
SP_FILE_SORT_BACKWARDSCombine this with the above to sort backwards

Types

spFilePointer

Same lik SDL_RWops*, but looks better ;)

spFileList

Linked list type for searching results, which contains found files.

Variables

name (char*)filename
type (spFileType)filetype, see spFileType
last_modtime of the last modification in seconds since 1.1.1970 00:00:00
last_acctime of the last access in seconds since 1.1.1970 00:00:00
prev (spFileList*)previous found
next (spFileList*)next found
count (int)only valid for the first element, describes the number of found files

Functions

spFileExists

PREFIX int spFileExists(const char *filename)

Tests, whether the file “filename” exists ;-)

Parameters

filenamethe filename to check

Returns

int1 if the file exists, 0 if not

spReadOneLine

PREFIX int spReadOneLine(spFilePointer file ,
char *buffer,
int buffer_len)

Reads one line from a SDL_RWops file.

Parameters

filethe file to read
bufferwhere the file content is written to
buffer_lenthe length of buffer (+ zero byte!).

Returns

intIf the end of file is reached, 1 is returned, else 0

spWriteOneLine

PREFIX int spWriteOneLine(spFilePointer file ,
char *buffer)

Writes one line from a SDL_RWops file.

Parameters

filethe file to read
buffercontent to be written

Returns

int1 at error, 0 else

spReadUntil

PREFIX int spReadUntil(spFilePointer file ,
char *buffer,
int buffer_len,
char end_sign,
char ignore_windows_return)

Reads signs from the file “file” until the buffer is full (buffer_len) or the sign “end_sign” is reached.  The sign before “end_sign” is the last sign of the string!  If you read more signs from the file, the sign AFTER “end_sign” is the next you will read.  It is useful for parsing simple XML files.

Parameters

filethe file to be read
bufferthe buffer for reading signs
buffer_lenthe length of the buffer
end_signthe sign to which it will be read
ignore_windows_returnWindows uses 2 signs for line breaks (VERY smart...), so if you set ignore_windows_return to 1, \r will be ignored.  Necessary for Windows text files, but for binary files it doesn’t make sense at all.

Returns

intLike spReadOneLine it returns 1 if eof is reached, else 0.

spCreateDirectoryChain

PREFIX spFileError spCreateDirectoryChain(const char *directories)

Creates a directory chain (like /home/user/.config/pinball/settings) if it not exists already.  That means if you create e.g. rainbow/dash, the folder rainbow will be created if it doesn’t exist.

Parameters

directoriesthe path of the directory to create

Returns

spFileErrorSP_FILE_EVERYTHING_OK if no error, else see spFileError

spRemoveFile

PREFIX spFileError spRemoveFile(const char *filename)

Removes/deletes a file

Parameters

filenamethe name of the file

Returns

spFileErrorSP_FILE_EVERYTHING_OK if no error, else see spFileError

See Also

spRemoveDirectory

spRemoveDirectory

PREFIX spFileError spRemoveDirectory(const char *dirname)

Removes an EMPTY directory

Parameters

dirnamethe name of the directory

Returns

spFileErrorSP_FILE_EVERYTHING_OK if no error, else see spFileError

See Also

spRemoveFile

spRenameFile

PREFIX spFileError spRenameFile(const char* filename ,
const char *newname)

Renames a file

Parameters

filenamethe old filename
newnamethe new filename

Returns

spFileErrorSP_FILE_EVERYTHING_OK if no error, else see spFileError

See Also

spRenameDirectory

spRenameDirectory

Renames a directory, works exactly like spRenameFile.  In fact it is just a redefinement.

See Also

spRenameFile

spFileGetDirectory

PREFIX spFileError spFileGetDirectory(spFileListPointer *pointer,
char *directory,
int recursive,
int no_hidden_files)

Searches for files Puts a double linked list of found files in directory to pointer, found files are directly extracted from the system and may very well not be ordered.  Call spFileSortList on the list afterwards.  Finally call spFileDeleteList to dispose the list.  Returned filepaths include the passed directory string

Parameters

pointerpointer to an existing <spFileListPointer> (not a struct), the result will be placed in here
directorypath of the directory to be searched.  Use forward slashes (even on Windows).  Use “.” for working directory, “..” for parent dir.  DON’T include a trailing slash
recursiveif 0 only directory will be searched, if 1 the subdirectories, too.  Be carefull with infinite directory loops!
no_hidden_filesif 1, hidden files like .git are ignored.

Returns

spFileErrorSP_FILE_EVERYTHING_OK if no error, else see spFileError

spFileDeleteList

PREFIX void spFileDeleteList(spFileListPointer list)

Deletes a list created by spFileGetDirectory.

Parameters

listthe list to be deleted

spFileSortList

PREFIX void spFileSortList(spFileListPointer *list,
spFileSortType sortBy)

Sorts the file list like you want.

Parameters

listthe list to be sorted
sortBythe sorting pattern.  The possible sort pattern are (self-explanatory): SP_FILE_SORT_BY_NAME, SP_FILE_SORT_BY_TYPE, SP_FILE_SORT_BY_TYPE_AND_NAME.  Add SP_FILE_SORT_BACKWARDS to sort backwards

spConfigGetPath

PREFIX char* spConfigGetPath(char *buffer,
char *subfolder,
char *file)

Returns the path to config files, which may be everywhere on different targets, e.g. in appdata for the pandora, home-folder for the gcw, etc.

Parameters

bufferbuffer to fill with the path
subfoldername of the program used to create subfolders e.g. in ~/.config.  Not used by every target.  In fact only by x86 linux and gcw.
filename of the config file to give the path to

Returns

char*filled pointer to buffer

Types

spConfigEntry

List type for config entries.

Variables

key (char[64])name of the config setting, e.g.  “resolution”
value (char[512])value of the config setting, e.g.  “800x480”
next (pointer)next element in list

spConfig

Config type

Variables

filename (char*)full path of the config
firstEntry (pointer)pointer to the first entry in the config file

Functions

spConfigRead

PREFIX spConfigPointer spConfigRead(char *filename,
char *subfolder)

Reads a config and saves it to a spConfig struct

Parameters

filenamefilename of the config.  It will be saved to a specific folder on every target
subfolderneeded to “calculate” this specific folder.  Should just be your program name

Returns

spConfigPointerPointer to a spConfig Struct

spConfigWrite

PREFIX void spConfigWrite(spConfigPointer config)

Writes the config to the file.

Parameters

configconfig, that shall be written

spConfigFree

PREFIX void spConfigFree(spConfigPointer config)

Frees the struct if you don’t need it anymore

Parameters

configconfig, that shall be freed

spConfigGetString

PREFIX char* spConfigGetString(spConfigPointer config,
char *key,
char *default_value)

Returns the string value to a specific key.  If the key is not found, it will be added with the default_value.  There is no Setter function, because you can edit the returned pointer directly.

Parameters

configconfig to read from
keykey of the item
default_valuevalue to add/return if the key is not available yet

Returns

char*the value

spConfigGetStringWithComment

PREFIX char* spConfigGetStringWithComment(spConfigPointer config,
char *key,
char *default_value,
char *comment)

Returns the string value to a specific key.  If the key is not found, it will be added with the default_value.  There is no Setter function, because you can edit the returned pointer directly.  If the key is not available in the config yet, the comment will be added before the setting

Parameters

configconfig to read from
keykey of the item
default_valuevalue to add/return if the key is not available yet
commenta comment, which will be set before the key-value-pair in the config if it doesn’t exist yet

Returns

char*the value

spConfigGetBool

PREFIX int spConfigGetBool(spConfigPointer config,
char *key,
int default_value)

Returns the bool value to a specific key.  If the key is not found, it will be added with the default_value.

Parameters

configconfig to read from
keykey of the item
default_valuevalue to add/return if the key is not available yet

Returns

intthe value.  1 means true, 0 false

spConfigGetBoolWithComment

PREFIX int spConfigGetBoolWithComment(spConfigPointer config,
char *key,
int default_value,
char *comment)

Returns the bool value to a specific key.  If the key is not found, it will be added with the default_value.  If the key is not available in the config yet, the comment will be added before the setting

Parameters

configconfig to read from
keykey of the item
default_valuevalue to add/return if the key is not available yet
commenta comment, which will be set before the key-value-pair in the config if it doesn’t exist yet

Returns

intthe value.  1 means true, 0 false

spConfigSetBool

PREFIX void spConfigSetBool(spConfigPointer config,
char *key,
int value)

Sets the bool value to a specific key.  If the key is not found, it will be added.

Parameters

configconfig to write to
keykey of the item
valuevalue to set

spConfigSetBoolWithComment

PREFIX void spConfigSetBoolWithComment(spConfigPointer config,
char *key,
int value,
char *comment)

Sets the bool value to a specific key.  If the key is not found, it will be added.  If the key is not available in the config yet, the comment will be added before the setting

Parameters

configconfig to write to
keykey of the item
valuevalue to set
commenta comment, which will be set before the key-value-pair in the config if it doesn’t exist yet

spConfigGetInt

PREFIX int spConfigGetInt(spConfigPointer config,
char *key,
int default_value)

Returns the int value to a specific key.  If the key is not found, it will be added with the default_value.

Parameters

configconfig to read from
keykey of the item
default_valuevalue to add/return if the key is not available yet

Returns

intthe value

spConfigGetIntWithComment

PREFIX int spConfigGetIntWithComment(spConfigPointer config,
char *key,
int default_value,
char *comment)

Returns the int value to a specific key.  If the key is not found, it will be added with the default_value.  If the key is not available in the config yet, the comment will be added before the setting

Parameters

configconfig to read from
keykey of the item
default_valuevalue to add/return if the key is not available yet
commenta comment, which will be set before the key-value-pair in the config if it doesn’t exist yet

Returns

intthe value

spConfigSetInt

PREFIX void spConfigSetInt(spConfigPointer config,
char *key,
int value)

Sets the int value to a specific key.  If the key is not found, it will be added.

Parameters

configconfig to write to
keykey of the item
valuevalue to set

spConfigSetIntWithComment

PREFIX void spConfigSetIntWithComment(spConfigPointer config,
char *key,
int value,
char *comment)

Sets the int value to a specific key.  If the key is not found, it will be added.  If the key is not available in the config yet, the comment will be added before the setting

Parameters

configconfig to write to
keykey of the item
valuevalue to set
commenta comment, which will be set before the key-value-pair in the config if it doesn’t exist yet

spConfigGetFloat

PREFIX float spConfigGetFloat(spConfigPointer config,
char *key,
float default_value)

Returns the float value to a specific key.  If the key is not found, it will be added with the default_value.

Parameters

configconfig to read from
keykey of the item
default_valuevalue to add/return if the key is not available yet

Returns

floatthe value

spConfigGetFloatWithComment

PREFIX float spConfigGetFloatWithComment(spConfigPointer config,
char *key,
float default_value,
char *comment)

Returns the float value to a specific key.  If the key is not found, it will be added with the default_value.  If the key is not available in the config yet, the comment will be added before the setting

Parameters

configconfig to read from
keykey of the item
default_valuevalue to add/return if the key is not available yet
commenta comment, which will be set before the key-value-pair in the config if it doesn’t exist yet

Returns

floatthe value

spConfigSetFloat

PREFIX void spConfigSetFloat(spConfigPointer config,
char *key,
float value)

Sets the float value to a specific key.  If the key is not found, it will be added.

Parameters

configconfig to write to
keykey of the item
valuevalue to set

spConfigSetFloatWithComment

PREFIX void spConfigSetFloatWithComment(spConfigPointer config,
char *key,
float value,
char *comment)

Sets the float value to a specific key.  If the key is not found, it will be added.  If the key is not available in the config yet, the comment will be added before the setting

Parameters

configconfig to write to
keykey of the item
valuevalue to set
commenta comment, which will be set before the key-value-pair in the config if it doesn’t exist yet
PREFIX int spFileExists(const char *filename)
Tests, whether the file “filename” exists ;-)
PREFIX int spReadOneLine(spFilePointer file ,
char *buffer,
int buffer_len)
Reads one line from a SDL_RWops file.
PREFIX int spWriteOneLine(spFilePointer file ,
char *buffer)
Writes one line from a SDL_RWops file.
PREFIX int spReadUntil(spFilePointer file ,
char *buffer,
int buffer_len,
char end_sign,
char ignore_windows_return)
Reads signs from the file “file” until the buffer is full (buffer_len) or the sign “end_sign” is reached.
PREFIX spFileError spCreateDirectoryChain(const char *directories)
Creates a directory chain (like /home/user/.config/pinball/settings) if it not exists already.
PREFIX spFileError spRemoveFile(const char *filename)
Removes/deletes a file
PREFIX spFileError spRemoveDirectory(const char *dirname)
Removes an EMPTY directory
PREFIX spFileError spRenameFile(const char* filename ,
const char *newname)
Renames a file
PREFIX spFileError spFileGetDirectory(spFileListPointer *pointer,
char *directory,
int recursive,
int no_hidden_files)
Searches for files Puts a double linked list of found files in directory to pointer, found files are directly extracted from the system and may very well not be ordered.
PREFIX void spFileDeleteList(spFileListPointer list)
Deletes a list created by spFileGetDirectory.
PREFIX void spFileSortList(spFileListPointer *list,
spFileSortType sortBy)
Sorts the file list like you want.
PREFIX char* spConfigGetPath(char *buffer,
char *subfolder,
char *file)
Returns the path to config files, which may be everywhere on different targets, e.g.
PREFIX spConfigPointer spConfigRead(char *filename,
char *subfolder)
Reads a config and saves it to a spConfig struct
Config type
PREFIX void spConfigWrite(spConfigPointer config)
Writes the config to the file.
PREFIX void spConfigFree(spConfigPointer config)
Frees the struct if you don’t need it anymore
PREFIX char* spConfigGetString(spConfigPointer config,
char *key,
char *default_value)
Returns the string value to a specific key.
PREFIX char* spConfigGetStringWithComment(spConfigPointer config,
char *key,
char *default_value,
char *comment)
Returns the string value to a specific key.
PREFIX int spConfigGetBool(spConfigPointer config,
char *key,
int default_value)
Returns the bool value to a specific key.
PREFIX int spConfigGetBoolWithComment(spConfigPointer config,
char *key,
int default_value,
char *comment)
Returns the bool value to a specific key.
PREFIX void spConfigSetBool(spConfigPointer config,
char *key,
int value)
Sets the bool value to a specific key.
PREFIX void spConfigSetBoolWithComment(spConfigPointer config,
char *key,
int value,
char *comment)
Sets the bool value to a specific key.
PREFIX int spConfigGetInt(spConfigPointer config,
char *key,
int default_value)
Returns the int value to a specific key.
PREFIX int spConfigGetIntWithComment(spConfigPointer config,
char *key,
int default_value,
char *comment)
Returns the int value to a specific key.
PREFIX void spConfigSetInt(spConfigPointer config,
char *key,
int value)
Sets the int value to a specific key.
PREFIX void spConfigSetIntWithComment(spConfigPointer config,
char *key,
int value,
char *comment)
Sets the int value to a specific key.
PREFIX float spConfigGetFloat(spConfigPointer config,
char *key,
float default_value)
Returns the float value to a specific key.
PREFIX float spConfigGetFloatWithComment(spConfigPointer config,
char *key,
float default_value,
char *comment)
Returns the float value to a specific key.
PREFIX void spConfigSetFloat(spConfigPointer config,
char *key,
float value)
Sets the float value to a specific key.
PREFIX void spConfigSetFloatWithComment(spConfigPointer config,
char *key,
float value,
char *comment)
Sets the float value to a specific key.
Enumeration for file types
no error, everything ok
Enumeration for errors while working with files
Renames a directory, works exactly like spRenameFile.
Sorting by name
Sorting by type
Sort by both
Combine this with the above to sort backwards
Close