Skip to content

Commit

Permalink
Add 'partition_tokens_/' from commit '5223460d9e9cd0af1e37d55fbc4dd11…
Browse files Browse the repository at this point in the history
…dd9ad010b'

git-subtree-dir: partition_tokens_
git-subtree-mainline: b890227
git-subtree-split: 5223460
  • Loading branch information
rshipp committed Nov 3, 2019
2 parents b890227 + 5223460 commit 566deae
Show file tree
Hide file tree
Showing 9 changed files with 620 additions and 0 deletions.
4 changes: 4 additions & 0 deletions partition_tokens_/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
partition_tokens
*.o
*.a
*.test
3 changes: 3 additions & 0 deletions partition_tokens_/.gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "googletest"]
path = googletest
url = [email protected]:google/googletest.git
34 changes: 34 additions & 0 deletions partition_tokens_/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Project 1: `partition_tokens`

Read the Piazza post here:

https://piazza.com/class/ii5r7hikwh0vu?cid=61

## How to use it

To run this program from the command line, type:
```
make run
```

The program will wait for input. Type whatever you want, followed by a new
line, and then press CTRL-D to send the end-of-file (EOF) character to stop
reading. The program will then try to partition the input and print the
results.

Alternatively, you can save your command to a file so you don't have to keep
typing the same thing again and again while you work. Assuming the file is
called 'input.txt', first run 'make' and then do:
```
./partition_tokens < input.txt
```

There is also a suite of unit tests that you can use to help you implement
your partition_tokens function. To run them, type:
```
make test
```

You'll see pass / fail results for all the tests. Work on fixing them one at a
time, and before you know it, you'll have an implementation in which you can
be extremely confident. =)
50 changes: 50 additions & 0 deletions partition_tokens_/command.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Contains the implementation of functions declared in command.h.
*/

#include "command.h"
#include <algorithm>
#include <iostream>
#include <iterator>

using namespace std;


bool partition_tokens(vector<string> tokens, vector<command_t>& commands) {
// TODO: implement me

// This is optional, but hopefully it helps. Read the Piazza post. =)

return true;
}


// Feel free to ignore everything below here. It's just code to allow you to
// cout a command in a nice, pretty format. =)


const char* input_types[] = {
"READ_FROM_STDIN",
"READ_FROM_FILE",
"READ_FROM_PIPE"
};


const char* output_types[] = {
"WRITE_TO_STDOUT",
"WRITE_TO_PIPE",
"WRITE_TO_FILE",
"APPEND_TO_FILE"
};


ostream& operator <<(ostream& out, const command_t& cmd) {
copy(cmd.argv.begin(), cmd.argv.end(), ostream_iterator<string>(out, " "));

out << "\n input: " << input_types[cmd.input_type]
<< "\n output: " << output_types[cmd.output_type]
<< "\n infile: " << cmd.infile
<< "\n outfile: " << cmd.outfile;

return out;
}
86 changes: 86 additions & 0 deletions partition_tokens_/command.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Contains the definition of the command_t struct.
*/

#pragma once
#include <vector>
#include <string>
#include <ostream>


/**
* Enum representing the possible types of input sources.
*/
enum InputType {
READ_FROM_STDIN,
READ_FROM_FILE,
READ_FROM_PIPE
};


/**
* Enum representing the possible types of output destinations.
*/
enum OutputType {
WRITE_TO_STDOUT,
WRITE_TO_PIPE,
WRITE_TO_FILE,
APPEND_TO_FILE
};


/**
* Simple representation of a command to execute. Includes the command's
* arguments as well as information about its input and output types.
*/
struct command_t {
/**
* The array of arguments representing the command to execute.
*/
std::vector<std::string> argv;

/**
* Where this command should get its input.
*/
InputType input_type;

/**
* Where this command should write its output.
*/
OutputType output_type;

/**
* The file from which this command should read its input. May be empty.
*/
std::string infile;

/**
* The file to which this command should write its output. May be empty.
*/
std::string outfile;

/**
* Constructor. Defaults input_type and output_type to READ_FROM_STDIN and
* WRITE_TO_STDOUT, respectively.
*/
command_t() : input_type(READ_FROM_STDIN), output_type(WRITE_TO_STDOUT) {}
};


/**
* Partitions the given vector of tokens into one or more commands based on
* the position of pipes or file redirects.
*
* @param tokens The tokens to partition
* @param commands The vector to fill with the partitioned commands
* @return true if successfully partitioned; false otherwise
*/
bool partition_tokens(
std::vector<std::string> tokens,
std::vector<command_t>& commands);


/**
* Enable the output operator (<<) to output commands in a readable format.
*/
std::ostream& operator <<(std::ostream& out, const command_t& command);
Loading

0 comments on commit 566deae

Please sign in to comment.