filesystem.hpp #6

  • //
  • guest/
  • ShadauxCat/
  • Sprawl/
  • Mainline/
  • filesystem/
  • filesystem.hpp
  • View
  • Commits
  • Open Download .zip Download (3 KB)
#pragma once

#include "../string/String.hpp"
#include <stdio.h>
#include "../collections/Vector.hpp"

namespace sprawl
{
	namespace filesystem
	{
		char const* LineSeparator();
		char const* NullDevice();

		void Chdir(String const& path);
		String GetCwd();

		enum class RelativeTo
		{
			Beginning,
			CurrentPosition,
			End,
		};

		class File
		{
		public:
			File(FILE* file, sprawl::String const& mode)
				: m_file(file)
				, m_mode(mode)
				, m_refCount(file == nullptr ? nullptr : new std::atomic<int>(1))
			{
				//
			}

			File()
				: m_file(nullptr)
				, m_mode()
				, m_refCount(nullptr)
			{
				//
			}

			File(File const& other);

			void operator=(File const& other);

			~File();

			operator bool()
			{
				return Valid();
			}

			bool operator!()
			{
				return !Valid();
			}

			bool Valid()
			{
				return m_file != nullptr;
			}

			void Close();
			void Sync();
			void Flush();
			int FileNo();
			FILE* NativeHandle() { return m_file; }
			bool IsATTY();

			String Read(int numBytes = -1);
			String ReadLine(int numBytes = -1);

			void Seek(int offset, RelativeTo relativeTo);
			int Tell();

			void Truncate(int size = -1);

			void Write(String const& str);

			bool IsClosed();
			String const& Mode();

			int FileSize();
		private:
			FILE* m_file;
			String m_mode;
			std::atomic<int>* m_refCount;
		};

		File Open(String const& path, char const* const mode);

		struct PermissionType
		{
			typedef int Type;
			enum : Type
			{
				OwnerRead = 0400,
				OwnerWrite = 0200,
				OwnerExecute = 0100,
				GroupRead = 040,
				GroupWrite = 020,
				GroupExecute = 010,
				AllRead = 04,
				AllWrite = 02,
				AllExecute = 01,
			};
		};

		bool MkDir(String const& path, PermissionType::Type mode = 0777);
		bool MakeDirs(String const& path, PermissionType::Type mode = 0777);
		bool Remove(String const& path);
		bool RemoveDirs(String const& path);
		bool Rename(String const& path, String const& newName);
		bool Renames(String const& path, String const& newName);
		bool RmDir(String const& path);
		bool RmTree(String const& path);

		struct AccessType
		{
			typedef int Type;
			enum : Type
			{
				Exists = 0,
				Read = 1 << 1,
				Write = 1 << 2,
				Execute = 1 << 3,
			};
		};

		bool Access(String const& path, AccessType::Type type);

		void ChMod(String const& path, PermissionType::Type type);

		void PutEnv(String const& name, String const& value);
		sprawl::String GetEnv(String const& name, String const& defaultValue = "");
		void UnsetEnv(String const& name);

		collections::Vector<sprawl::String> ListDir(String const& directory);

		bool MakeSymlink(String const& target, String const& link);

		int GetPid();
	}
}
# Change User Description Committed
#6 16131 ShadauxCat - Exposed FILE* object in sprawl::filesystem::File
- Added ability to specify flush behavior for custom handlers via std::function (interesting note - apparently with optimization enabled, calls to std::function can execute faster than virtual function calls)
- Threads that destruct with no Join() after exiting with an uncaught exception will terminate with an error message rather than swallowing the exception and letting it disappear

#review-16132
#5 14822 ShadauxCat Last batch of filesystem code for now, added MakeSymlink and GetPid, removed other todo functions for the time being.
Also fixed some bugs:

-Linux IsLink() implementation not using lstat = broken
-File::IsClosed() would crash if file were created via default constructor or null handle
-Remove() and RmDir() on Windows were inconsistent with Linux - in Linux all symlinks are removed with Remove() even if they point to directories. Forced windows to work the same way.
-Asked RmTree to please not descend into symbolic links to directories, but just to remove them, thanks.
-Removed starting \\?\ from result of RealPath() on Windows
-Fixed IsFile() on Windows just not working - assuming anything that's not a directory is a file now.
-Fixed StringBuilder only printing the first character if you passed it type char* instead of type char const*

#review-14823
#4 14816 ShadauxCat Filled in some more filesystem functions, added appropriate unit tests.
Only a few remain.

#review-14817
#3 14781 ShadauxCat -Finished path library
-Added some more functionality to filesystem library
-Added a few more unit tests, still more needed for path

#review-14782
#2 14761 ShadauxCat First drop of code for sprawl::filesystem and sprawl::path.
Library will continue to grow.

Also fixed a warning on linux.

#review-14762
#1 12508 ShadauxCat -Added threading library.
Currently only functional for Linux; Windows will fail to link. (I will fix this soon.)
-Fixed missing move and copy constructors in List and ForwardList
-Fixed broken move constructor in HashMap
-Fixed missing const get() in HashMap
-Fixed broken operator-> in ListIterator
-Added sprawl::noncopyable
-Added sketch headers for filesystem library
-Made StringLiteral hashable, added special hashes for pointers and integers in murmur3
-Fixed compiler warning in async_network
-Updated memory allocators to use new threading library for mutexes
-Added accessibility to sprawl::StringLiteral to be able toa ccess its pointer and length and perform pointer comparisons

#review-12504