-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsndcat_main.cpp
120 lines (117 loc) · 3.75 KB
/
sndcat_main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include "ComS229Util.h"
#include "sndcatUtil.h"
using namespace sndCatUtil;
/*NOTE on how I am handling specific cases.
1. If the Channels do not match, this is an error.
2. If the Samplerate is different, this is an error
3. If there is a conflict in BitRes, this is an error.
4. Sample size will be changed accordingly.*/
int main(int argc, char** argv) {
ComS229 addingTo; //This is the first element. It will be the master
ComS229 beingAdded; //This is the second element. It will be deleted after each read.
bool output = false;
bool hasOFile = false;
string OFileName = "";
string input;
if (argc > 1) {
if (argc == 2) {
//This means there's only one argument.
//The only possibility is they want the help screen.
char *CInput = new char[50];
CInput = argv[1];
if (CInput[0] == '-') {
if (CInput[1] == 'h') {
cout << "Enter the names of the files you would like to concatenate separated by spaces." << endl;
cout << "./sndcat [-o yourfilenamehere.cs229] testing1.cs229 testing2.cs229" << endl;
cout << "Or enter -h to see the help screen" << endl ;
}
else {
cout << "Sorry, this switch was not recognized. Please try again." << endl;
}
}
else {
cout << "Sorry, the input was not recognized. Please try again." << endl;
}
}
if (argc > 2) {
//This means there's atleast two files.
try {
vector<string> vec;
for (int i = 1; i < argc; i++) {
string input(argv[i]); //reading in user input.
vec.push_back(input);
}
if ((signed int)vec.size() == 1) {
//This means there's only one argument.
//The only possibility is they're specifying
//the output file or wanting the help screen.
string input = vec[0];
if (input[0] == '-') {
if (input[1] == 'h') {
cout << "Enter the names of the files you would like to concatenate separated by spaces." << endl;
cout << "Or enter '-o yourfilenamehere.cs229' to specify an output file." << endl;
}
else {
cout << "Sorry, this switch was not recognized. Please try again." << endl;
}
}
}
if ((signed int)vec.size() > 1) {
//This means there's atleast two files.
//Call the method to cat 2 strings and loop it.
for (int j = 0; j < (signed int) vec.size(); j++) {
if (vec[j].find("-o") != string::npos) //Saving the outputfile name.
{
j++;
OFileName = vec[j];
hasOFile = true;
}
else if (addingTo.getDataLength() == 0) { //This is reading the first file specified
addingTo.readFile(vec[j]);
}
else if (addingTo.getDataLength() != 0) { //This is reading every other file specified
if (beingAdded.readFile(vec[j])) {
addingTo = sndCatUtil::concatenate(addingTo, beingAdded); //This will concatenate the two files
if (addingTo.getBitRes() == 0) {
return 0;
}
beingAdded.clean(); //removing all data from beingAdded since it will be rewritten if there's more than two files
output = true;
}
}
else {
//cerr
}
}
if (hasOFile) {
addingTo.setFileName(OFileName); //This sets the output name
sndCatUtil::output(addingTo); //this outputs to that file
}
else if (output) {
sndCatUtil::stdOutput(addingTo); //This will print to stdout
}
else {
//error, you aren't supposed to output yet.
cerr << "There was not output file specified and there was no file read";
}
}
}
catch (std::ios_base::failure e)
{
//Handle io exception here
cerr << "IO EXCEPTION";
}
}
}
//delete created elements
else {
cerr << "No files specified.";
}
return 0;
}