haiku-dumber/Utils.cpp

52 lines
1.3 KiB
C++

/*
* Copyright 2024, My Name <my@email.address>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "Utils.h"
#include <File.h>
#include <Path.h>
#include <Directory.h>
#include <FindDirectory.h>
#include <Message.h>
#include <String.h>
status_t SaveMessageToFile(const BMessage& message, const char* fileName) {
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) {
return B_ERROR;
}
path.Append(fileName);
BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE);
if (file.InitCheck() != B_OK) {
return file.InitCheck();
}
return message.Flatten(&file);
}
BMessage LoadMessageFromFile(const char* fileName) {
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) {
return BMessage(B_ERROR); // Return a message indicating an error
}
path.Append(fileName);
BFile file(path.Path(), B_READ_ONLY);
if (file.InitCheck() != B_OK) {
return BMessage(file.InitCheck()); // Return a message with the error status
}
BMessage message;
status_t status = message.Unflatten(&file);
if (status != B_OK) {
return BMessage(status); // Return a message indicating unflattening error
}
return message;
}