# Ifndef snilrubbishfilefactory_h _
# Define snilrubbishfilefactory_h _
# Include <qstring. h>
# Include <qtimer. h>
# Include <qfile. h>
# Include <qdir. h>
# Include <qimage. h>
# Include <sys/select. h>
# Include </usr/include/unistd. h>
# Include <fstream>
# Include <iostream>
Using STD: ifstream;
Using STD: ofstream;
Using STD: fstream;
Using STD: ofstream;
Using STD: ios_base;
Using STD: cout;
Using STD: CIN;
Using STD: Endl;
# Include <unistd. h>
Void flushmemorytodevice (){
Sync ();
}
/* Date: 2007/01/10
* Version0.001 added the spam file generation function.
* Version0.002 added the function of copying a specified image file to generate garbage-copyimage
* The sync function is added to version0.003. Every time a spam is generated, sync-sync is called once.
*/
Void millisleep (int ms ){
If (MS> 0 ){
Struct timeval TV;
TV. TV _sec = 0;
TV. TV _usec = Ms * 1000;
Select (0, 0, 0, 0, & TV );
}
}
Class snilrubbishfilefactory {
Public:
Snilrubbishfilefactory (qstring rubbishdir ):
Rubbishsum _ (0), processedargc _ (1), copyfromimage _ (false), flushmemtodev (false ){
Rubbishdir _ = rubbishdir;
}
Bool processargv (INT argc, char * argv []) {
Bool exitprogream = true;
Bool gotomakerubbish = false;
Bool processdone = true;
While (processdone ){
Qstring argvstring;
// Qwarning ("processedargc _ --> % d", processedargc _);
// Qwarning ("argc ---> % d", argc );
If (processedargc _ <argc ){
// If any command line parameter is not processed
Argvstring = qstring (argv [processedargc _]);
Processedargc _ ++;
} Else {
Return gotomakerubbish;
}
If (argvstring = qstring ("-changedir ")){
If (argc> processedargc _){
Qstring dir = qstring (argv [processedargc _]);
Qdir D (DIR );
If (D. exists ()){
Rubbishdir _ = dir;
Qwarning ("Change dir --> % s", rubbishdir _. Latin1 ());
} Else {
Qwarning ("No This dir --> % s", dir. Latin1 ());
Qwarning ("use default-> % s", rubbishdir _. Latin1 ());
}
} Else {
Qwarning ("No dir specify! ");
Qwarning ("use default-> % s", rubbishdir _. Latin1 ());
}
Qwarning ("##################");
Qwarning ("-changedir done ");
Qwarning ("##################/N ");
Processedargc _ ++;
Continue;
}
If (argvstring = qstring ("-sync ")){
Flushmemtodev = true;
Qwarning ("##################");
Qwarning ("-sync done ");
Qwarning ("##################/N ");
Continue;
}
If (argvstring = qstring ("-copyimage ")){
If (argc> processedargc _){
Copyimagename = qstring (argv [processedargc _]);
If (! Qfile: exists (copyimagename )){
Qwarning ("No this file --> % s", copyimagename. Latin1 ());
Return gotomakerubbish;
}
Copyimage _ = qimage (copyimagename );
Copyfromimage _ = true;
} Else {
Qwarning ("no file specify! ");
Return gotomakerubbish;
}
Qwarning ("##################");
Qwarning ("-copyimage done ");
Qwarning ("##################/N ");
Processedargc _ ++;
Continue;
}
If (argvstring = qstring ("clean ")){
Qwarning ("clean [% s] Rubbish! ", Rubbishdir _. Latin1 ());
Qdir rubbishdir (rubbishdir _);
Int removenum = 0;
For (unsigned int I = 0; I <rubbishdir. Count (); I ++ ){
If (rubbishdir [I]! = "." & Rubbishdir [I]! = ".."){
Rubbishdir. Remove (rubbishdir [I]);
Removenum ++;
}
}
Qwarning ("remove rubbish num-> % d", removenum );
Qwarning ("##################");
Qwarning ("clean done ");
Qwarning ("##################/N ");
Return exitprogream;
}
If (argvstring = qstring ("-- version ")){
Qwarning ("Snail il rubbishmaker version 0.003! ");
Qwarning ("0.001 ---> make empty rubbish only! ");
Qwarning ("0.002 ---> make specify image rubbish! ");
Qwarning ("0.003 ---> Add System Call sync ()! ");
Return exitprogream;
}
If (argvstring = qstring ("-- Help ")){
Qwarning ("Rubbish [-Sync] [[-copyimage] imageabspath] [[-check]? KB] [[-changedir] dirpath] [clean] [-- help] [-- version] -- make rubish game !! ");
Return exitprogream;
}
If (argvstring = qstring ("-check ")){
If (argc> processedargc _){
Qstring goodrubbishvalue = qstring (argv [processedargc _]);
Int goodrubbishval = goodrubbishvalue. toint ();
Qwarning ("good rubbish size? --> % S ", goodrubbishvalue. Latin1 ());
Qdir rubbishdir (rubbishdir _);
// If (argc> 2 ){
// Qstring ruubishsizestring (argv [2]);
// Int ruubishsize = ruubishsizestring. toint ();
// Cout <"---------------------------------------------------" <Endl;
// Qwarning ("good rubbish is % d kb !!! ", Ruubishsize );
// Cout <"---------------------------------------------------" <Endl;
//}
Bool hasabad = false;
For (unsigned int I = 0; I <rubbishdir. Count (); I ++ ){
If (rubbishdir. Count () <= 2 ){
Qwarning ("no rubbish! ");
Return exitprogream;
}
If (rubbishdir [I]! = "." & Rubbishdir [I]! = ".."){
Qfile F (rubbishdir. filepath (rubbishdir [I]);
If (float) F. Size ()/1024! = (Float) goodrubbishval ){
Hasabad = true;
Qwarning ("% s-> % fkb", rubbishdir [I]. Latin1 (), (float) F. Size ()/1024 );
}
}
}
If (! Hasabad ){
Qwarning ("all rubbish is good !!!!!!!!!!! ");
}
} Else {
Qwarning ("No goodrubbish size exist! ");
}
Qwarning ("##################");
Qwarning ("-check done ");
Qwarning ("##################/N ");
Return exitprogream;
}
}
Return gotomakerubbish;
}
Void collectrubbishinfo (){
Qwarning ("/n !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!! ");
Qwarning ("make rubbish -- snil 2008/01/08 ");
Qwarning ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!! /N ");
Int rubbishnum;
Cout <"-------------------------------------------------" <Endl;
Cout <"[input rubbish num:]" <Endl ;;
Cin> rubbishnum;
If (rubbishnum <= 0 ){
Rubbishnum = 10;
}
Setrubbishnum (rubbishnum );
Cout <"Rubbish num:" <rubbishnum <Endl;
Cout <"-------------------------------------------------" <Endl;
If (! Copyfromimage _){
Int rubbishsize;
Cout <"-------------------------------------------------" <Endl;
Cout <"[input rubbishsize (Kb):]";
Cin> rubbishsize;
If (rubbishsize <= 0 ){
Rubbishsize = 10;
}
Setrubbishsize (rubbishsize );
Cout <"Rubbish size (Kb):" <rubbishsize <Endl;
Cout <"-------------------------------------------------" <Endl;
}
Int speed;
Cout <"-------------------------------------------------" <Endl;
Cout <"[input createrubbishspeed (MS):]";
Cin> speed;
If (speed <= 0 ){
Speed = 1;
}
Setrubbishcreatespeed (speed );
Cout <"Rubbish size (Kb):" <speed <Endl;
Cout <"-------------------------------------------------" <Endl;
}
Void startmakerubbish (){
Qstring rubbishname;
While (++ rubbishsum _ <= rubbishnum _){
Rubbishname = qstring (rubbishdir _). append ("/"). append ("Rubbish"). append (qstring: Number (rubbishsum _));
If (copyfromimage _){
Rubbishname. append (". jpg ");
Makerubbishfromimage (rubbishname );
} Else {
Makedefaultrubbish (rubbishname );
Qwarning ("make rubbish --> % s", rubbishname. Latin1 ());
}
If (flushmemtodev ){
Flushmemorytodevice ();
}
Millisleep (makerubbishspeed _);
}
Qwarning ("------------------ enjoy U rubbish! -------------------");
}
PRIVATE:
Int rubbishnum _;
Int rubbishkbsize _;
Int makerubbishspeed _;
Int rubbishsum _;
Qstring rubbishdir _;
Int processedargc _;
Qimage copyimage _;
Qstring copyimagename;
Bool copyfromimage _;
Bool flushmemtodev;
Void setrubbishnum (INT num ){
Rubbishnum _ = num;
}
Void setrubbishsize (INT numk ){
Rubbishkbsize _ = numk;
}
Void setrubbishcreatespeed (INT Sec ){
Makerubbishspeed _ = sec;
}
Void makedefaultrubbish (qstring name ){
Ofstream to (name. Latin1 (), ios_base: Binary );
If (! To ){
Qwarning ("cannot open the file: % s", name. Latin1 ());
Return;
}
Static char Buf [1024];
Int sum = 0;
Do {
To. Write (BUF, 1024 );
Sum ++;
} While (sum <rubbishkbsize _);
To. Close ();
}
Void makerubbishfromimage (qstring name ){
Static const char * format = qimage: imageformat (copyimagename );
Copyimage _. Save (name, format );
Qwarning ("make image rubbish --> % s", name. Latin1 ());
}
};
Int main (INT argc, char * argv []) {
# Ifndef arm_version
Qstring dir = "/home/snail il/desktop/rubbish ";
# Else
Qstring dir = "/MMC/rubbish ";
# Endif // arm_version
Snilrubbishfilefactory Factory (DIR );
If (factory. processargv (argc, argv )){
Return 0;
}
Factory. collectrubbishinfo ();
Factory. startmakerubbish ();
}
# Endif/* snilrubbishfilefactory_h _*/