renamed ff_open to ff_find, added "savepos" thing to a few funcs

master
Ondřej Hruška 10 years ago
parent 50b933231c
commit 91f2963c1f
  1. 30
      fat16.c
  2. 6
      fat16.h

@ -761,13 +761,20 @@ void ff_root(const FAT16* fat, FFILE* file)
/** /**
* Find a file with given "display name" in this directory. * Find a file with given "display name" in this directory.
* If file is found, "dir" will contain it's handle. * If file is found, "dir" will contain it's handle.
* Either way, "dir" gets modified and you may need to rewind it afterwards.
*/ */
bool ff_open(FFILE* dir, const char* name) bool ff_find(FFILE* file, const char* name)
{ {
// save orig pos
FSAVEPOS orig = ff_savepos(file);
char fname[11]; char fname[11];
ff_rawname(name, fname); ff_rawname(name, fname);
return dir_find_file_raw(dir, fname); bool ret = dir_find_file_raw(file, fname);
if (!ret)
ff_reopen(file, &orig);
return ret;
} }
@ -819,6 +826,8 @@ bool find_empty_file_slot(FFILE* file)
bool ff_newfile(FFILE* file, const char* name) bool ff_newfile(FFILE* file, const char* name)
{ {
const FSAVEPOS orig = ff_savepos(file);
// Convert filename to zero padded raw string // Convert filename to zero padded raw string
char fname[11]; char fname[11];
ff_rawname(name, fname); ff_rawname(name, fname);
@ -827,11 +836,17 @@ bool ff_newfile(FFILE* file, const char* name)
bool exists = dir_find_file_raw(file, fname); bool exists = dir_find_file_raw(file, fname);
ff_first(file); // rewind dir ff_first(file); // rewind dir
if (exists) if (exists)
{
ff_reopen(file, &orig);
return false; // file already exists in the dir. return false; // file already exists in the dir.
}
if (!find_empty_file_slot(file)) if (!find_empty_file_slot(file))
{
ff_reopen(file, &orig);
return false; // error finding a slot return false; // error finding a slot
}
// Write into the new slot // Write into the new slot
const uint16_t newclu = alloc_cluster(file->fat); const uint16_t newclu = alloc_cluster(file->fat);
@ -847,6 +862,8 @@ bool ff_newfile(FFILE* file, const char* name)
*/ */
bool ff_mkdir(FFILE* file, const char* name) bool ff_mkdir(FFILE* file, const char* name)
{ {
const FSAVEPOS orig = ff_savepos(file);
// Convert filename to zero padded raw string // Convert filename to zero padded raw string
char fname[11]; char fname[11];
ff_rawname(name, fname); ff_rawname(name, fname);
@ -855,11 +872,16 @@ bool ff_mkdir(FFILE* file, const char* name)
bool exists = dir_find_file_raw(file, fname); bool exists = dir_find_file_raw(file, fname);
ff_first(file); // rewind dir ff_first(file); // rewind dir
if (exists) if (exists)
{
ff_reopen(file, &orig);
return false; // file already exusts in the dir. return false; // file already exusts in the dir.
}
if (!find_empty_file_slot(file)) if (!find_empty_file_slot(file))
{
ff_reopen(file, &orig);
return false; // error finding a slot return false; // error finding a slot
}
// Write into the new slot // Write into the new slot
const uint16_t newclu = alloc_cluster(file->fat); const uint16_t newclu = alloc_cluster(file->fat);

@ -249,10 +249,10 @@ void ff_first(FFILE* file);
/** /**
* Find a file with given "display name" in this directory, and open it. * Find a file with given "display name" in this directory, and open it.
* If file is found, "dir" will contain it's handle. * If file is found, "file" will contain it's handle.
* If file is NOT found, the handle points to the last entry of the directory. * Otherwise, the handle is unchanged.
*/ */
bool ff_open(FFILE* dir, const char* name); bool ff_find(FFILE* file, const char* name);
// -------- FILE INSPECTION ----------- // -------- FILE INSPECTION -----------

Loading…
Cancel
Save