Skip to content

Commit 4a12a9a

Browse files
committed
Slight modification to how file reader works
Now it also pushes the length of the file read before the content of the file
1 parent 71d7d3b commit 4a12a9a

File tree

6 files changed

+12
-11
lines changed

6 files changed

+12
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ Supports all eight directions. Finishes the program execution.
270270

271271
### **File Reader (r)**
272272
Supports northeast, north, northwest, west and southwest directions. Continuously pops the value on top of the stack and constructs a filename from those values until either it hits a zero or the stack becomes empty. (If it hits a zero, it also pops that)
273-
Then reads the file with that filename in binary mode and pushes its content on top of the stack in reverse order, meaning the first character of the file will be the value on top of the stack. If it fails, it pushes zero to the stack.
273+
Then, it reads the file with that filename in binary mode, pushes its content on top of the stack in reverse order and the length of the string. If it fails to open the file, it only pushes a zero to the stack.
274274

275275
### **File Openers**
276276
Similar to File Reader, File Openers read a filename from the stack in the same way. After opening the file, they push a positive integer as unique identifier that can be used to communicate with file.

examples/file_appending.ec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"sad_face.ec"
2-
? Appends 'A' to file with given filename ?
2+
? Appends 'A' to file with given filename. Press Ctrl-D after entering the filename. ?
33

44
+->--+
55
| &

examples/file_reading.ec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"sad_face.ec"
2-
? Enter the filename that you want to read from ?
2+
? Enter the filename that you want to read from and press Ctrl-D ?
33

44
+->--+
55
| &
66
| # +☹
77
| # | +---P
88
| +-+-+ ] | *
9-
| [ ] +----## | )
9+
| [ ] +-E--## | )
1010
+--+ +--R | [ | |
1111
*-r | +-}+
1212
| n

examples/file_writing.ec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"sad_face.ec"
2-
? Writes 'A' to file with given filename ?
2+
? Writes 'A' to file with given filename. Press Ctrl-D after entering the filename. ?
33

44
+->--+
55
| &

include/utility/ArgParser.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ SOFTWARE.
2525
#pragma once
2626

2727
#define ELECTRA_VERSION_MAJOR 2
28-
#define ELECTRA_VERSION_MINOR 2
29-
#define ELECTRA_VERSION_PATCH 1
28+
#define ELECTRA_VERSION_MINOR 3
29+
#define ELECTRA_VERSION_PATCH 0
3030

3131
#include <vector>
32+
#include <string>
3233
#include <unordered_map>
3334

3435
// Simple argument parser

src/components/FileReader.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ bool FileReader::work(Current::Ptr& current, std::vector<Current::Ptr>& currentV
5555
std::ifstream ifs(filename, std::ios::binary);
5656
if(ifs.fail())
5757
{
58-
defaultLogger.log(LogType::WARNING, "(FileReader) Unable to open \"{}\". Pushing 0 to stack.", filename);
58+
defaultLogger.log(LogType::WARNING, "(FileReader) Unable to open \"{}\".", filename);
5959
current->stackPtr->push(0);
6060
return Cable::work(current, currentVector);
6161
}
@@ -65,11 +65,11 @@ bool FileReader::work(Current::Ptr& current, std::vector<Current::Ptr>& currentV
6565
ifs.close();
6666
std::string fileContent = ss.str();
6767

68-
while(!fileContent.empty())
68+
for(auto it = fileContent.rbegin(); it != fileContent.rend(); ++it)
6969
{
70-
current->stackPtr->push(static_cast<var_t>( fileContent[fileContent.length() - 1] ));
71-
fileContent.pop_back();
70+
current->stackPtr->push(static_cast<var_t>(*it));
7271
}
72+
current->stackPtr->push(static_cast<var_t>( fileContent.length() ));
7373
defaultLogger.log(LogType::INFO, "(FileReader) Read \"{}\".", filename);
7474

7575
return Cable::work(current, currentVector);

0 commit comments

Comments
 (0)