-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandline.cpp
More file actions
154 lines (141 loc) · 3.57 KB
/
Copy pathcommandline.cpp
File metadata and controls
154 lines (141 loc) · 3.57 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* commandline.cpp
* Written By Ethan House
*
* Contains bulk of the program logic.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "commandline.h"
// Global Variables
static int thr_id;
pthread_t p_thread;
void start_gstreamer(){
// Creates thread for audio playback
thr_id = pthread_create(&p_thread,NULL,thread_init,NULL);
}
void *thread_init(void *){
// Gstreamer init function.
init_gstreamer();
}
void terminate(int status){
// status(0) : Exit Cleanly
// status(1) : incompatible flags
// status(2) : File or Stream doesn't exist
// status(3) : Memory Allocation Error
switch(status){
case 0:
break;
case 1:
fprintf(stderr,"Incompatible Flags, exiting\n");
break;
case 2:
fprintf(stderr,"File or Stream doesn't exist, exiting\n");
break;
case 3:
fprintf(stderr,"Memory Allocation Error, exiting\n");
break;
default:
fprintf(stderr,"I Just don't know what went wrong, exiting\n");
break;
}
exit(status);
}
void argument_parser(int argc, char *argv[]){
// variables
bool input_lock(false); // value to make surer -s and -f aren't used together
int return_status; // int value that varies depending on function return
char *file_location = NULL;
char *stream_location = NULL;
char *cwd=NULL;
char *destination;
int array_size1;
int array_size2;
size_t size;
// function calls
start_gstreamer(); // Starts gstreamer thread for audio playback
// argument checker
for(int x = 0;x < argc;x++){
if(!strcmp(argv[x],"-v" ) || !strcmp(argv[x],"--version")){
print_version();
terminate(0);
}
if(!strcmp(argv[x],"-h" ) || !strcmp(argv[x],"--help" )){
print_helpDoc(NULL);
terminate(0);
}
if(!strcmp(argv[x],"-s" ) || !strcmp(argv[x],"--stream")){
if(input_lock == true)
terminate(1); // If a input flag is already declared, exit gracefully
input_lock = true;
stream_location = argv[x+1];
}
if(!strcmp(argv[x],"-f") || !strcmp(argv[x],"--file")){
if(input_lock == true)
terminate(1); // If a input flag is already declared, exit gracefully
input_lock=true;
file_location = argv[x+1];
}
}
// At this point we check to see if stream or file are actually there.
// If files do not exist then we return the proper error codes and terminate
// cleanly.
if(file_location != NULL){
cwd=getcwd(cwd,size);
if(file_location[0] == '.' && file_location[1] == '/'){
// Not Implemented
}
else if(file_location[0] == '/'){
strcpy(destination,file_location);
if (check_file(destination)) {
// All Good
}else {
// Unable to locate file
printf("Unable to locate file: %s\n",destination);
terminate(2);
}
}
else{
array_size1 = strlen(file_location);
array_size2 = strlen(cwd);
destination = new char[array_size1+array_size2];
if(destination == 0){
terminate(3); // terminate with memory allocation error
}
strcpy(destination,cwd);
strcat(destination,"/");
strcat(destination,file_location);
if(check_file(destination)){
// All Good
}else{
// Unable to locate file
printf("Unable to locate file: %s\n",destination);
terminate(2);
}
}
}
else if(stream_location != NULL){
// Not implemented
}else{
// Not implemented
terminate(2);
}
}
bool check_file(char *file_loc){
// if file exists return true
// We did not use boost which would have been easier but is another dep
FILE * pfile;
pfile = fopen(file_loc,"r");
if (pfile==NULL){
return false;
}else{
fclose(pfile);
return true;
}
}
bool check_stream(char *stream_loc){
// Not Implemented
return true;
}
/* End of File */