DirectorySave: move code to ParseLine()

This commit is contained in:
Max Kellermann 2014-02-24 23:14:14 +01:00
parent 55cd5a9a78
commit 8963cd1fab

View File

@ -70,6 +70,18 @@ directory_save(FILE *fp, const Directory &directory)
fprintf(fp, DIRECTORY_END "%s\n", directory.GetPath()); fprintf(fp, DIRECTORY_END "%s\n", directory.GetPath());
} }
static bool
ParseLine(Directory &directory, const char *line)
{
if (StringStartsWith(line, DIRECTORY_MTIME)) {
directory.mtime =
ParseUint64(line + sizeof(DIRECTORY_MTIME) - 1);
} else
return false;
return true;
}
static Directory * static Directory *
directory_load_subdir(TextFile &file, Directory &parent, const char *name, directory_load_subdir(TextFile &file, Directory &parent, const char *name,
Error &error) Error &error)
@ -84,6 +96,7 @@ directory_load_subdir(TextFile &file, Directory &parent, const char *name,
Directory *directory = parent.CreateChild(name); Directory *directory = parent.CreateChild(name);
while (true) {
const char *line = file.ReadLine(); const char *line = file.ReadLine();
if (line == nullptr) { if (line == nullptr) {
error.Set(directory_domain, "Unexpected end of file"); error.Set(directory_domain, "Unexpected end of file");
@ -91,24 +104,17 @@ directory_load_subdir(TextFile &file, Directory &parent, const char *name,
return nullptr; return nullptr;
} }
if (StringStartsWith(line, DIRECTORY_MTIME)) { if (StringStartsWith(line, DIRECTORY_BEGIN))
directory->mtime = break;
ParseUint64(line + sizeof(DIRECTORY_MTIME) - 1);
line = file.ReadLine(); if (!ParseLine(*directory, line)) {
if (line == nullptr) { error.Format(directory_domain,
error.Set(directory_domain, "Unexpected end of file"); "Malformed line: %s", line);
directory->Delete(); directory->Delete();
return nullptr; return nullptr;
} }
} }
if (!StringStartsWith(line, DIRECTORY_BEGIN)) {
error.Format(directory_domain, "Malformed line: %s", line);
directory->Delete();
return nullptr;
}
success = directory_load(file, *directory, error); success = directory_load(file, *directory, error);
if (!success) { if (!success) {
directory->Delete(); directory->Delete();