/project01/project01/src/project01.cpp

https://gitlab.com/cmarch314/PythonProjects · C++ · 286 lines · 228 code · 43 blank · 15 comment · 58 complexity · 5fa72feb6a6274f293a39d5f282f1e89 MD5 · raw file

  1. //============================================================================
  2. // Name : project01.cpp
  3. // Author : Gregory Jeckell
  4. // Version :
  5. // Copyright :
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8. #include "FileSystem53.cpp"
  9. using namespace std;
  10. void displayCommands();
  11. void create(string name);
  12. void destroy(string name);
  13. void open(string name);
  14. void close(int index);
  15. void read(int index, int count);
  16. void write(int index, char chartowrite, int count);
  17. void seek(int index, int pos);
  18. void directory();
  19. void init(string disk);
  20. void load(string disk);
  21. void save();
  22. void callFileSystem(string[6]);
  23. char convertStringToChar(string str);
  24. FileSystem53* fileSystem;
  25. int main()
  26. {
  27. const int MAX_ARGS = 6;
  28. string line;
  29. displayCommands();
  30. cout << "Please enter a command from this list!" << endl;
  31. while(true){
  32. cout << "$ ";
  33. getline(cin, line);
  34. istringstream iss(line);
  35. string argv[MAX_ARGS];
  36. //load command and args into array
  37. for (int i = 0; i < MAX_ARGS; i++)
  38. iss >> argv[i];
  39. //call appropriate filesystem command with given arguments
  40. callFileSystem(argv);
  41. }//end while
  42. return 0;
  43. }//end main
  44. void displayCommands(){
  45. cout << "cr <name> : Create a file" << endl;
  46. cout << "de <name> : Destroy a file" << endl;
  47. cout << "op <name> : Open a file" << endl;
  48. cout << "cl <index> : Close a file" << endl;
  49. cout << "rd <index> : Read a file" << endl;
  50. cout << "wr <index> <char> <count> : Write to an open file" << endl;
  51. cout << "sk <index> <pos> : Seek to new position" << endl;
  52. cout << "dr : Display file directory" << endl;
  53. cout << "in <disk_cont> : Initialize disk" << endl;
  54. cout << "ld <disk cont> : Restore disk" << endl;
  55. cout << "sv : Save disk" << endl;
  56. }
  57. void create(string name) {
  58. if (!fileSystem) {
  59. cout << "File system does not exist yet" << endl;
  60. return;
  61. }
  62. int msg = fileSystem->create(name);
  63. if (msg == fileSystem->FLAG_ERROR_FULL)
  64. cout << "error: disk is full" << endl;
  65. else if (msg == fileSystem->FLAG_ERROR_FILEEXISTS)
  66. cout << "error: file already exists" << endl;
  67. else
  68. cout << "file " << name << " created" << endl;
  69. }
  70. void destroy(string name) {
  71. if (!fileSystem) {
  72. cout << "File system does not exist yet" << endl;
  73. return;
  74. }
  75. if (fileSystem->deleteFile(name) == fileSystem->FLAG_ERROR_NOTFOUND){
  76. cout << "error: file not found" << endl;
  77. }
  78. else
  79. cout << "file " << name << " destroyed" << endl;
  80. }
  81. void open(string name) {
  82. if (!fileSystem) {
  83. cout << "File system does not exist yet" << endl;
  84. return;
  85. }
  86. int msg = fileSystem->open(name);
  87. if (msg == fileSystem->FLAG_ERROR_NOTFOUND){
  88. cout << "error: file not found" << endl;
  89. }
  90. else if (msg == fileSystem->FLAG_ERROR_FULL){
  91. cout << "error: disk is full" << endl;
  92. }
  93. else
  94. cout << "file " << name << " opened, index=" << msg << endl; //return index
  95. }
  96. void close(int index) {
  97. if (!fileSystem) {
  98. cout << "File system does not exist yet" << endl;
  99. return;
  100. }
  101. if (index == 0)
  102. cout << "error: cannot close directory file" << endl;
  103. else
  104. if (index > fileSystem->MAX_OPEN_FILE ||
  105. fileSystem->close(index) == fileSystem->FLAG_ERROR_NOTFOUND){
  106. cout << "error: invalid OFT index" << endl;
  107. } else {
  108. cout << "file " << index << " closed" << endl;
  109. }
  110. }
  111. void read(int index, int count) {
  112. if (!fileSystem) {
  113. cout << "File system does not exist yet" << endl;
  114. return;
  115. }
  116. int readReturn = fileSystem->read(index, count);
  117. if (readReturn == fileSystem->FLAG_ERROR_NOTFOUND){
  118. cout << "error: file not found" << endl;
  119. }
  120. else
  121. cout << endl << " bytes read: " << readReturn << endl; //successful read
  122. }
  123. void write(int index, char chartowrite, int count) {
  124. if (!fileSystem) {
  125. cout << "File system does not exist yet" << endl;
  126. return;
  127. }
  128. int msg = fileSystem->write(index, chartowrite, count);
  129. if (msg == fileSystem->FLAG_ERROR_NOTFOUND){
  130. cout << "error: no open file found" << endl;
  131. }
  132. else
  133. if (msg == fileSystem->FLAG_ERROR_FULL){
  134. cout << "error: file is full" << endl;
  135. }
  136. else
  137. cout << count << " bytes written" << endl; //successful write
  138. }
  139. void seek(int index, int pos) {
  140. if (!fileSystem) {
  141. cout << "File system does not exist yet" << endl;
  142. return;
  143. }
  144. if (fileSystem->lseek(index, pos) == fileSystem->FLAG_ERROR_NOTFOUND){
  145. cout << "error: no open file found" << endl;
  146. }
  147. else
  148. cout << "current position is " << pos << endl;
  149. }
  150. void directory() {
  151. if (!fileSystem) {
  152. cout << "File system does not exist yet" << endl;
  153. return;
  154. }
  155. cout << "shell.directory" << endl;
  156. fileSystem->directory();
  157. }
  158. void init(string disk) {
  159. cout << "This system currently supports only a 64x64 disk" << endl;
  160. delete fileSystem;
  161. fileSystem = new FileSystem53(64, 64, disk);
  162. cout << "disk initialized" << endl;
  163. }
  164. void load(string disk) {
  165. if (fileSystem)
  166. delete fileSystem;
  167. fileSystem = fileSystem->restore(disk);
  168. }
  169. void save() { //parameter for save was unused!!
  170. fileSystem->save();
  171. cout << "disk saved" << endl;
  172. }
  173. void callFileSystem(string argv[6]){
  174. string command = argv[0];
  175. if (command == "cr"){
  176. string name = argv[1];
  177. create(name);
  178. }
  179. else
  180. if (command == "de"){
  181. string name = argv[1];
  182. destroy(name);
  183. }
  184. else
  185. if (command == "op"){
  186. //string name = argv[1];
  187. open(argv[1]);
  188. }
  189. else
  190. if (command == "cl"){
  191. int index = atoi(argv[1].c_str());
  192. close(index);
  193. }
  194. else
  195. if (command == "rd"){
  196. int index = atoi(argv[1].c_str());
  197. int count = atoi(argv[2].c_str());
  198. read(index, count);
  199. }
  200. else
  201. if (command == "wr"){
  202. int index = atoi(argv[1].c_str());
  203. int count = atoi(argv[3].c_str());
  204. write(index, convertStringToChar(argv[2]), count);
  205. }
  206. else
  207. if (command == "sk"){
  208. int index = atoi(argv[1].c_str());
  209. int pos = atoi(argv[2].c_str());
  210. seek(index, pos);
  211. }
  212. else
  213. if (command == "dr"){
  214. directory();
  215. }
  216. else
  217. if (command == "in"){
  218. init(argv[1]);
  219. }
  220. else
  221. if (command == "sv"){
  222. save();
  223. }
  224. else
  225. if (command == "ld") {
  226. load(argv[1]);
  227. }
  228. else{
  229. cout << "error: not a valid command" << endl;
  230. displayCommands();
  231. }
  232. }
  233. char convertStringToChar(string str){
  234. int lengthOfString=str.length();
  235. char characters[lengthOfString];
  236. str.copy( characters, lengthOfString );
  237. return characters[0];
  238. }
  239. /*
  240. //test contents of array
  241. for (int i = 0; i < MAX_ARGS; i++)
  242. cout << "argv[" << i << "] " << argv[i] << " "; cout << endl;
  243. */