|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Geektimus |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 | + * this software and associated documentation files (the "Software"), to deal in |
| 6 | + * the Software without restriction, including without limitation the rights to |
| 7 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 8 | + * the Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | + * subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 16 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 17 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 18 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +package com.codingmaniacs.scala.exercises.fs.commands |
| 23 | + |
| 24 | +import com.codingmaniacs.scala.exercises.fs.State |
| 25 | +import com.codingmaniacs.scala.exercises.fs.directories.Directory |
| 26 | +import com.codingmaniacs.scala.exercises.fs.files.File |
| 27 | + |
| 28 | +import scala.annotation.tailrec |
| 29 | + |
| 30 | +class Echo(items: Array[String]) extends Command { |
| 31 | + |
| 32 | + def createContent(items: Array[String], topIndex: Int): String = { |
| 33 | + |
| 34 | + @tailrec |
| 35 | + def createContentRec(currIdx: Int, acc: String): String = |
| 36 | + currIdx match { |
| 37 | + case i if i >= topIndex => acc |
| 38 | + case i => createContentRec(i + 1, acc + items(currIdx)) |
| 39 | + } |
| 40 | + |
| 41 | + createContentRec(0, "") |
| 42 | + } |
| 43 | + |
| 44 | + def getRootAfterEcho( |
| 45 | + currentDir: Directory, |
| 46 | + path: List[String], |
| 47 | + contents: String, |
| 48 | + append: Boolean |
| 49 | + ): Directory = |
| 50 | + if (path.isEmpty) { |
| 51 | + currentDir |
| 52 | + } else if (path.tail.isEmpty) { |
| 53 | + val dirEntry = currentDir.findEntry(path.head) |
| 54 | + if (dirEntry == null) { |
| 55 | + currentDir.addEntry(new File(currentDir.path, path.head, contents)) |
| 56 | + } else if (dirEntry.isDirectory) { |
| 57 | + currentDir |
| 58 | + } else { |
| 59 | + if (append) { |
| 60 | + currentDir.replaceEntry(path.head, dirEntry.asFile.appendContents(contents)) |
| 61 | + } else { |
| 62 | + currentDir.replaceEntry(path.head, dirEntry.asFile.setContents(contents)) |
| 63 | + } |
| 64 | + } |
| 65 | + } else { |
| 66 | + val nextDir = currentDir.findEntry(path.head).asDirectory |
| 67 | + val newNextDir = getRootAfterEcho(nextDir, path.tail, contents, append) |
| 68 | + |
| 69 | + if (newNextDir == nextDir) { |
| 70 | + currentDir |
| 71 | + } else { |
| 72 | + currentDir.replaceEntry(path.head, newNextDir) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + def doEcho(state: State, contents: String, filename: String, append: Boolean): State = |
| 77 | + if (filename.contains(Directory.SEPARATOR)) { |
| 78 | + state.setMessage("Echo: Filename must not contain separators") |
| 79 | + } else { |
| 80 | + val newRoot: Directory = getRootAfterEcho( |
| 81 | + state.root, |
| 82 | + state.workingDir.getAllFoldersInPath :+ filename, |
| 83 | + contents, |
| 84 | + append |
| 85 | + ) |
| 86 | + if (newRoot == state.root) { |
| 87 | + state.setMessage(s"$filename: no such file") |
| 88 | + } else { |
| 89 | + State(newRoot, newRoot.findDescendant(state.workingDir.getAllFoldersInPath)) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + override def apply(state: State): State = |
| 94 | + if (items.isEmpty) { |
| 95 | + state |
| 96 | + } else if (items.size == 1) { |
| 97 | + state.setMessage(items(0)) |
| 98 | + } else { |
| 99 | + val op = items(items.length - 2) |
| 100 | + val filename = items(items.length - 1) |
| 101 | + val contents = createContent(items, items.length - 2) |
| 102 | + if (">>".equals(op)) { |
| 103 | + doEcho(state, contents, filename, append = true) |
| 104 | + } else if (">".equals(op)) { |
| 105 | + doEcho(state, contents, filename, append = false) |
| 106 | + } else { |
| 107 | + state.setMessage(createContent(items, items.length)) |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments