Skip to content

Commit

Permalink
Merge pull request #1706 from alamt22/cpp-temp-file-support
Browse files Browse the repository at this point in the history
File system api c++: added support for creating temp. files and directories
  • Loading branch information
heinezen authored Nov 18, 2024
2 parents 2170ff5 + a144c8c commit 19557aa
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions copying.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ _the openage authors_ are:
| Nikhil Ghosh | NikhilGhosh75 | nghosh606 à gmail dawt com |
| Edvin Lindholm | EdvinLndh | edvinlndh à gmail dawt com |
| Jeremiah Morgan | jere8184 | jeremiahmorgan dawt bham à outlook dawt com |
| Tobias Alam | alamt22 | tobiasal à umich dawt edu |

If you're a first-time committer, add yourself to the above list. This is not
just for legal reasons, but also to keep an overview of all those nicknames.
Expand Down
10 changes: 10 additions & 0 deletions libopenage/util/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "util/filelike/python.h"
#include "util/path.h"
#include "util/strings.h"
#include "util/fslike/directory.h"


namespace openage::util {
Expand Down Expand Up @@ -121,5 +122,14 @@ std::ostream &operator<<(std::ostream &stream, const File &file) {
return stream;
}

static File get_temp_file() {
fslike::Directory temp_dir = fslike::Directory::get_temp_directory();
std::string file_name = std::tmpnam(nullptr);
std::ostringstream dir_path;
temp_dir.repr(dir_path);
mode_t mode = 0777;
File file_wrapper = File(dir_path.str() + file_name, mode);
return file_wrapper;
}

} // namespace openage::util
3 changes: 2 additions & 1 deletion libopenage/util/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,10 @@ class OAAPI File {
void flush();
ssize_t size();
std::vector<std::string> get_lines();

std::shared_ptr<filelike::FileLike> get_fileobj() const;

static File get_temp_file();

protected:
std::shared_ptr<filelike::FileLike> filelike;

Expand Down
8 changes: 8 additions & 0 deletions libopenage/util/fslike/directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <dirent.h>
#include <fcntl.h>
#include <iostream>
#include <filesystem>
#include <sys/stat.h>
#include <sys/types.h>
#include <utility>
Expand Down Expand Up @@ -292,4 +293,11 @@ std::ostream &Directory::repr(std::ostream &stream) {
return stream;
}

static Directory get_temp_directory() {
std::string temp_dir_path = std::filesystem::temp_directory_path() / std::tmpnam(nullptr);
bool create = true;
Directory directory = Directory(temp_dir_path, create);
return directory;
}

} // namespace openage::util::fslike
3 changes: 2 additions & 1 deletion libopenage/util/fslike/directory.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Directory : public FSLike {

std::ostream &repr(std::ostream &) override;

static Directory get_temp_directory();

protected:
/**
* resolve the path to an actually usable one.
Expand All @@ -59,7 +61,6 @@ class Directory : public FSLike {

std::string basepath;
};

} // namespace fslike
} // namespace util
} // namespace openage
2 changes: 1 addition & 1 deletion libopenage/util/path.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2023 the openage authors. See copying.md for legal info.
// Copyright 2015-2024 the openage authors. See copying.md for legal info.

#include "path.h"

Expand Down
2 changes: 1 addition & 1 deletion libopenage/util/path.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2023 the openage authors. See copying.md for legal info.
// Copyright 2015-2024 the openage authors. See copying.md for legal info.

#pragma once

Expand Down

0 comments on commit 19557aa

Please sign in to comment.