-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandCopyMp3Latest.cpp
229 lines (201 loc) · 7.42 KB
/
randCopyMp3Latest.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <bits/stdc++.h>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <filesystem>
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <vector>
#include <unistd.h>
using namespace std;
namespace fs = std::filesystem;
#define BUF_SIZE 4096 /* use a buffer size of 4096 bytes */
#define OUTPUT_MODE 0700 /*protection bits for output file */
class mp3FileList{
protected:
long numFiles;
std::string strMp3Path, srcFile, srcFile2, dstFile;
std::vector<std::string> mp3Files;
std::string dstDir;
int *ptrRandList, numRandNr;
string cStrMp3Path, cDstDir;
void tokenizeFNameStr(std::string const &str, const char* delim,
std::vector<std::string> &out)
{
char *token = strtok(const_cast<char*>(str.c_str()), delim);
while (token != nullptr)
{
out.push_back(std::string(token));
token = strtok(nullptr, delim);
}
}
void addFilesMp3Dir(){
std::string path;
path=strMp3Path;
// This structure would distinguish a file from a
// directory
struct stat sb;
// Looping until all the items of the directory are
// exhausted
for (const auto& entry : fs::directory_iterator(path)) {
// Converting the path to const char * in the
// subsequent lines
std::filesystem::path outfilename = entry.path();
std::string outfilename_str = outfilename.string();
const char* path = outfilename_str.c_str();
// Testing whether the path points to a
// non-directory or not If it does, displays path
if (stat(path, &sb) == 0 && !(sb.st_mode & S_IFDIR))
mp3Files.push_back(path);
}
}
public:
mp3FileList(std::string strMp3Path, long numFiles, std::string dstDir, int* ptrRandList, int numRandNr){
mp3FileList::strMp3Path = strMp3Path;
mp3FileList::numFiles=numFiles;
mp3FileList::dstDir = dstDir;
mp3FileList::ptrRandList=ptrRandList;
mp3FileList::numRandNr=numRandNr;
//excute functions to make class ready for use
addFilesMp3Dir();
}
~mp3FileList(){
}
void copyRandFiles(){
int srcFileDesc, dstFileDesc, readByteCount, writeByteCount;
int i, indOutp;
std::string filePath;
std::vector<std::string> outStr;
const char* delim = "/";
char buffer[BUF_SIZE];
outStr.clear();
for(i=0; i<numRandNr; i++){
srcFile=mp3Files[ptrRandList[i]];
srcFile2=mp3Files[ptrRandList[i]];
tokenizeFNameStr(srcFile2, delim, outStr);
dstFile=dstDir + "/" + outStr[3];
outStr.clear();
srcFileDesc = open(srcFile.c_str(), O_RDONLY);
dstFileDesc = creat(dstFile.c_str(), OUTPUT_MODE);
while(1){
readByteCount = read(srcFileDesc, buffer, BUF_SIZE);
if(readByteCount<=0)
break;
writeByteCount = write(dstFileDesc, buffer, readByteCount);
if(writeByteCount<=0)
exit(4);
}
//close the files
close(srcFileDesc);
close(dstFileDesc);
}
}
};
class randNumList{
protected:
int *pekIntVect, numItemsRanList;
long numFiles;
std::string strMp3Path;
void cntMp3sDir(){
numFiles=0;
std::string path;
path=strMp3Path;
// This structure would distinguish a file from a
// directory
struct stat sb;
// Looping until all the items of the directory are
// exhausted
for (const auto& entry : fs::directory_iterator(path)) {
// Converting the path to const char * in the
// subsequent lines
std::filesystem::path outfilename = entry.path();
std::string outfilename_str = outfilename.string();
const char* path = outfilename_str.c_str();
// Testing whether the path points to a
// non-directory or not If it does, displays path
if (stat(path, &sb) == 0 && !(sb.st_mode & S_IFDIR))
numFiles++;
}
}
void genRandNums(){
int largNumRange, numFound, curRanNum, curLstInd, i, k;
largNumRange=numFiles;
//init randnom number generator with seed.
srand(time(0));
pekIntVect[0]=1+(rand()%largNumRange);
numFound=0;
do{
curRanNum=(int)rand()%largNumRange+1;
if(pekIntVect[0]==curRanNum){
numFound=1;
}
}while(numFound==1);
pekIntVect[1]=curRanNum;
curLstInd=1;
i=0;
while(1){
do{
numFound=0;
curRanNum=1+ (rand()%largNumRange);
for(int k=0; k<=curLstInd; k++){
if(pekIntVect[k]==curRanNum)
numFound=1;
}
}while(numFound==1);
pekIntVect[curLstInd+1]=curRanNum;
curLstInd++;
i++;
//numsToGen=Totalt numbers to generate minus the two already generated by the code above this loop
if(i==(numItemsRanList-2))
break;
}
}
public:
randNumList(std::string strMp3Path, float procFilesCopy){
randNumList::strMp3Path=strMp3Path;
cntMp3sDir();
randNumList::numItemsRanList = (int) ( (procFilesCopy/100) * numFiles );
pekIntVect = new int[numItemsRanList];
genRandNums();
}
~randNumList(){
delete[] pekIntVect;
}
void createList(int aNumItems){
numItemsRanList=aNumItems;
pekIntVect = new int[numItemsRanList];
}
int *retPtrArrInt(void){
return pekIntVect;
}
void prnŃumLst(){
int q;
for(q=0; q<numItemsRanList; q++)
cout << " " << pekIntVect[q] << " ";
}
int retNumItemsRanList(){
return numItemsRanList;
}
long retNumFiles(){
return numFiles;
}
};
int main(void){
int numItemsRanList, *ptrArr2, q, i;
long numFiles;
float procFilesCopy;
std::string strMp3Dir, dstDir;
cout << "In what directory is the library of mp3files stored?";
cin >> strMp3Dir;
cout << "In what directory do you wish the random compilation of mp3 files to be created?";
cin >> dstDir;
cout << "How many percent of the entire list do you wish that the random library of mp3 should contain?";
cin >> procFilesCopy;
randNumList ranNumList(strMp3Dir, procFilesCopy );
mp3FileList mp3List(strMp3Dir, ranNumList.retNumFiles(), dstDir, ranNumList.retPtrArrInt(), ranNumList.retNumItemsRanList() );
mp3List.copyRandFiles();
return 0;
}