1

I have written a program which would categorize files based on their extensions.As of now,I am grouping them by if-else conditions[code below].I am planning to use a separate class and then in that class keep all the document types.Any more simple implementations are welcome.

if(it->path().extension() == ".txt"||it->path().extension() == ".doc"||it->path().extension() == ".docx"||it->path().extension() == ".pdf") 
    {   
        f[0].filePath=it->path();
        f[0].fileName=it->path().filename();
        f[0].fileExt=it->path().extension();

        query="Insert into Filemanager(Filepath,Filename,Category)values('" + f[0].filePath.string() + "','" + f[0].fileName.string() + "','Documents')";       
    }
    else if(it->path().extension() == ".mp3"||it->path().extension() == ".wma") 
    {      
        f[1].filePath=it->path();
        f[1].fileName=it->path().filename();
        f[1].fileExt=it->path().extension();            
        query="Insert into Filemanager(Filepath,Filename,Category)values('" + f[0].filePath.string() + "','" + f[0].fileName.string() + "','Music')";       
    }
    else if(it->path().extension() == ".mp4") 
    {          
        f[2].filePath=it->path();
        f[2].fileName=it->path().filename();
        f[2].fileExt=it->path().extension();            
        query="Insert into Filemanager(Filepath,Filename,Category)values('" + f[0].filePath.string() + "','" + f[0].fileName.string() + "','Videos')";      
    }
    else if(it->path().extension() == ".jpg"||it->path().extension() == ".jpeg"||it->path().extension() == ".gif") 
    {          
        f[3].filePath=it->path();
        f[3].fileName=it->path().filename();
        f[3].fileExt=it->path().extension();            
        query="Insert into Filemanager(Filepath,Filename,Category)values('" + f[0].filePath.string() + "','" + f[0].fileName.string() + "','Pictures')";        
    }
    else 
    {     
        f[4].filePath=it->path();
        f[4].fileName=it->path().filename();
        f[4].fileExt=it->path().extension();            
        query="Insert into Filemanager(Filepath,Filename,Category)values('" + f[0].filePath.string() + "','" + f[0].fileName.string() + "','Others')";      
    }

I want to avoid if-else conditions.Thanks in advance.

Final Reworked code:

std::map<string,string> docTypes = boost::assign::map_list_of (".txt", "Documents") (".pdf", "Documents") (".pdf", "Documents") (".rtf", "Documents")
                                                                                        (".jpg", "Pictures") (".jpeg", "Pictures") (".gif", "Pictures")
                                                                                        (".mp3", "Audio") (".mp4", "Audio")
                                                                                        (".wma", "Video") (".flv", "Video");        
f[0].filePath=it->path();
    f[0].fileName=it->path().filename();
    std::map<std::string,std::string>::iterator docIterator=docTypes.find(it->path().extension().string());
    if( docIterator != docTypes.end() ) 
    {
         category = docIterator->second;
    }
    else
    {
        category="Others";
    }
    query="Insert into Filemanager(Filepath,Filename,Category)values('" + f[0].filePath.string() + "','" + f[0].fileName.string() + "','" + category + "')";                

4 Answers4

4

Try something like this: (I didn't compile it so it may have some errors)

std::map<std::string,int> extMap;

// put your extensions here...
extMap.insert( std::pair<std::string,int>(".txt", 0) );
extMap.insert( std::pair<std::string,int>(".doc", 0) );

extMap.insert( std::pair<std::string,int>(".mp3", 1) );
...

// find an extension and its index
std::map<std::string,int>::iterator it2 = extMap.find( it->path().extension() );

if( it2 != extMap.end() ) {
    int index = it2->second;

    f[index].filePath = ...
}
kolenda
  • 2,741
  • 2
  • 19
  • 30
1

u have too much repeated code, u can make a method that takes an extension and return the folder it is supposed to be in, then store this file in the returned folder

Ahmed Matar
  • 241
  • 1
  • 6
1

This is not related to avoid the if-then conditions, but given that your are going to rework your code...

not knowing if you're working in a "secure environment", I would use parameterized queries rather than constructing the query in a string. These are much better for handling SQL Injection Attacks.

SQL injection is being one of the mostly exploited issues in application security and has found a place in OWASP Top 10 since 2004. There are many blog posts, papers available on SELECT query injection exploiting WHERE or HAVING clauses but there are also SQL injection in INSERT query.

Community
  • 1
  • 1
manlio
  • 18,345
  • 14
  • 76
  • 126
0

I've had to use auto as I've got no idea what your types are. There's also no error checking (i.e. if an extension isn't matched).

auto path = it->path();
auto extension = path.extension();

int index = -1;
auto queryFolder = "";

    if (extension == ".txt" || extension == ".doc" || extension == ".docx" || extension == ".pdf") 
    {   
        index = 0;
        queryFolder = "Documents";
    }
    else if (extension == ".mp3" || extension == ".wma") 
    {      
        index = 1;
        queryFolder = "Music";
    }
    else if (extension == ".mp4") 
    {          
        index = 2;
        queryFolder = "Videos";
    }
    else if (extension == ".jpg" || extension == ".jpeg" || extension == ".gif") 
    {          
        index = 3;
        queryFolder = "Pictures";        
    }
    else 
    {     
        index = 4;
        queryFolder = "Others";
    }

    f[index].filePath = path;
    f[index].fileName = path.filename();
    f[index].fileExt = extension;     

    auto query = "Insert into Filemanager(Filepath,Filename,Category)values('" + f[index].filePath.string() + "','" + f[index].fileName.string() + "','";
    query += queryFolder;
    query += "')";
Mark Ingram
  • 71,849
  • 51
  • 176
  • 230