// -*- mode: C++; tab-width: 4; -*- // vi:ts=4 noexpandtab autoindent /* * Matthew Birky * 10/22/2014 */ #include <iostream> #include <fstream> #include <cstring> #include <stdlib.h> #include <string> #include <sstream> using namespace std; const char *gProgname = "<unknown program>"; /* * convert a string to any compatible value */ template<typename T> T ValOf(string s) { stringstream os; T val; os << s; os >> val; return val; } /* * Thank you mwittenberg for the code * return the filename (strip off the preceding path, if any) */ const char * GetProgname(const char *argp) { const char *p = strrchr(argp, '/'); return p ? p+1 : argp; } /* * Show usage synopsis */ void usage() { cout << gProgname << " [file name] [-c val] [-d dir] [-e [extension name]] [-h] [-l] [-s val[k|m|g]]" << endl; } /* * Show optional error message (if desired) * before showing usage synopsis */ void help(const char ch = '\0') { if(ch) cout << gProgname << ": " << "Unknown option '" << ch << "'" << endl; usage(); exit(1); } /* * Show arbitrary messag (if desired) * before showing usage synopsis */ void help(const char *cp) { if(cp) cout << gProgname << ": " << cp << endl; usage(); exit(1); } void SanatizeFileDirectory(string & fileDirectory) { // There may be an issues if the string is less than 3 characters if(fileDirectory.compare(fileDirectory.length() -3, 3, "...") == 0) { fileDirectory.erase(fileDirectory.end() -3, fileDirectory.end()); } // The file directory must end with a / or it will be part of the name if(fileDirectory.compare(fileDirectory.length() -1, 1, "/")) { fileDirectory.push_back('/'); } } int main(int argc, const char * argv[]) { unsigned int fileCount = 1; unsigned long long fileSize = 10; //bytes const char * fileName = "test"; string fileDirectory; string fileExtension; int const fillArraySize = 20; char nolineFillArray[fillArraySize] = {'0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5','6','7','8','9'}; char lineFillarray[fillArraySize] = {'0','\n','1','\n','2','\n','3','\n','4','\n','5','\n','6','\n','7','\n','8','\n','9','\n'}; char * fillArray = nolineFillArray; gProgname = GetProgname(*argv); while(--argc > 0) { const char *arg = *++argv; if(*arg == '-') { while(*++arg) { switch(*arg) { case 'c': if( (argc > 1) && isdigit(*argv[1]) ) { argc--; fileCount = ValOf<unsigned int>(*++argv); } else { help("-c requires a number"); } break; case 'd': if(argc > 1) { argc--; fileDirectory = *++argv; SanatizeFileDirectory(fileDirectory); } break; case 'e': if(argc > 1) { argc--; fileExtension = *++argv; } else { fileExtension = ".txt"; } break; case 'h': help(); break; case 'l': fillArray = lineFillarray; break; case 's': if( (argc > 1) && isdigit(*argv[1]) ) { argc--; string arg_string = *++argv; fileSize = ValOf<unsigned int>(arg_string); unsigned int arg_length = arg_string.length(); switch(arg_string[arg_length - 1]) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': break; case 'k': case 'K': fileSize *= 1024; break; case 'm': case 'M': fileSize *= 1024 * 1024; break; case 'g': case 'G': fileSize *= 1024 * 1024 * 1024; break; default: help(*arg); break; } } else { help("-s requires a number"); } break; default: help(*arg); break; } } } else { fileName = arg; } } // Making an output array of 50 times the fill array size // This array is then used to reduce the number of write calls needed int const outputArraySize = fillArraySize * 50; char outputArray[outputArraySize] = {0}; for(int numMemCpys(0); numMemCpys < (outputArraySize / fillArraySize); ++numMemCpys) { memcpy(outputArray + (numMemCpys * fillArraySize), fillArray, fillArraySize); } // Create number of files as called for for(int fileNum(0); fileNum < fileCount; fileNum++) { stringstream outputFileName; outputFileName << fileDirectory << fileName; if(fileCount != 1) { outputFileName << fileNum; } /* * Tack on the extension only if the user provided an extension, * but first interpose a period unless the user-providided extension * begins with a period. */ if( fileExtension.length() ) { if( fileExtension[0]!= '.' ) outputFileName << "."; outputFileName << fileExtension; } std::ofstream outputfile; outputfile.open(outputFileName.str().c_str()); unsigned long long outputSize=fileSize; while(outputSize > outputArraySize) { outputSize -= outputArraySize; outputfile.write(outputArray, outputArraySize); } outputfile.write(outputArray, outputSize); outputfile.close(); } return 0; }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#5 | 21816 | mbirky |
Adding in the directory argument. This allows the tool to better integrate with P4v as a custom tool. Fixed a small cleanup issue with the sanity test script. |
||
#4 | 18692 | mbirky |
Fixing a variable mismatch issue. An unsigened int was being set to an unsigned long long. The variable runeth over. |
||
#3 | 18333 | mbirky |
"Adding the file back after accidentaly deleting it. #review-18299 should now have the whole thing added" |
||
#2 | 18332 | mbirky | "Adding in the changes I had made as part of #review-18299 to the new location" | ||
#1 | 18325 | mbirky | Changing the folder locations to fix issues with workshop naming convention. | ||
//guest/mbirky/testfile_maker/main/main.cpp | |||||
#1 | 18324 | mbirky |
Changing the folders to have a main. Should fix some issues intorduced by adding a branch in the workshop |
||
//guest/mbirky/testfile_maker/main.cpp | |||||
#13 | 12429 | mbirky |
Adding the feature that if only one file is made there is no trailing 0. @mwittenberg |
||
#12 | 11689 | mbirky |
Adding in the ability to specify the size measured in k, m, or g. @mwittenberg |
||
#11 | 11479 | mbirky | Adding in the option for writing each character on a new line @mittenberg | ||
#10 | 11100 | mbirky |
Adding the help option with help functions for when bad arguments are passed in. @mwittenberg |
||
#9 | 10990 | mbirky |
Changing how the options are parsed. This method suggested by @mwittenberg offers a few advantages such as being able to pass -xyz for options instead of having to separate them out into -x -y -z. |
||
#8 | 10956 | mbirky |
Adding the option of setting the extension of the files. @mwittenberg |
||
#7 | 10954 | mbirky | Adding in the capturing of the program name. | ||
#6 | 10845 | mbirky | Change variable names to be more consistent. | ||
#5 | 10830 | mbirky |
Added the -n flag for naming the file. Edited the Makefile so that it no longer does a remove on test* files. And edited the README to document the new naming feature. |
||
#4 | 10828 | mbirky |
Added in the ValOf(string) function and replaced the atoi function with it. @mwittenberg |
||
#3 | 10809 | mbirky |
Added the -n flag for naming the file. Edited the Makefile so that it no longer does a remove on test* files. And edited the README to document the new naming feature. |
||
#2 | 10807 | mbirky | Fix build issues on Ubuntu. | ||
#1 | 10731 | mbirky |
Initial commit for testfile maker. Includes two options -c and -s. The -c option allows for the number of files created to be set. The -s option allows for the size of the file in bytes to be set. |