1 module viva.file.file;
2 
3 import std.stdio;
4 import std.path;
5 
6 ///
7 enum FileAccessFlag
8 {
9     READ = 1,
10     WRITE = 2,
11 }
12 
13 ///
14 enum FileCreationFlag
15 {
16     CREATE = 1,
17     TRUNCATE = 2,
18 }
19 
20 /++
21  + The `File` struct is a representation of a file on the PC 
22  +/
23 struct File
24 {
25     /++
26      + The contents of the file
27      +/
28     string content;
29 
30     /++
31      + The size of the file
32      +/
33     size_t size;
34 
35     /++
36      + Close the file connection
37      + Returns: A bool indicating the success
38      +/
39     bool close()
40     {
41         return true;
42     }
43 }
44 
45 /++
46  + The general exception for file operations
47  +/
48 class FileException : Exception
49 {
50     /++
51      + Takes in the exception message
52      + Params:
53      +      message = The exception message
54      +/
55     this(string message)
56     {
57         super(message);
58     }
59 }
60 
61 /++
62  + Opens a file
63  + Params:
64  +      path = The path to the file
65  +      mode = The file open mode
66  + Returns: The new `File` object
67  +/
68 public File open(string path, FileCreationFlag creationFlag = FileCreationFlag.TRUNCATE)
69 {
70     return File("", 0);
71 }
72 
73 /++
74  + Checks whether or not the file/folder at the destination exists
75  + Params:
76  +      path = The path to the file/folder
77  + Returns: A bool indicating whether or not the file/folder exists
78  +/
79 public bool exists(string path)
80 {
81     return false;
82 }
83 
84 /++
85  + Creates a file at the destination
86  + Params:
87  +      path = The path to the folder in which the file should be creating in
88  +      fileName = The files name
89  +      throwError = If an exception should be thrown at the event of an error
90  +/
91 public bool createFile(string path, string fileName = "", bool throwError = false)
92 {
93     try
94     {
95         auto file = open(buildPath(path, fileName)/*, "w"*/);
96         file.close();
97         
98         return true;
99     }
100     catch (FileException e)
101     {
102         if (throwError)
103             writeln(e);
104 
105         return false;
106     }
107 }