#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <map>
#include <iterator>
#include <string>
#include <fstream>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <strings.h>
#include <memory.h>
#include <stdlib.h>

using namespace std;

typedef map<string, bool> FILE_LIST;

class CFileList
{
public:
    CFileList();
    ~CFileList();

public:
    bool GetFileList(FILE_LIST& list, string strDir); //디렉토리 및 파일목록을 구한다
    void ShowFileList(FILE_LIST& list);//리스트를 출력한다
};

CFileList::CFileList()
{
}

CFileList::~CFileList()
{
}

bool CFileList::GetFileList(FILE_LIST& list, string strDir)
{
    struct stat statinfo;
    memset(&statinfo, 0, sizeof(statinfo));
    lstat(strDir.c_str(), &statinfo);
    if(!S_ISDIR(statinfo.st_mode))
    {
        cout<<strDir + " is not directory"<<endl;
        return false;
    }

    DIR *dir;
    struct dirent *entry;

     if ((dir = opendir(strDir.c_str())) == NULL)
     {
         cout<<strDir + " open error"<<endl;
        return false;
     }

    while ((entry = readdir(dir)) != NULL)
    {
        memset(&statinfo, 0, sizeof(statinfo));
        string strFilePath = strDir + "/" + entry->d_name;
        while(strFilePath.find("//") != string::npos)
                strFilePath.replace(strFilePath.find("//"), 2, "/");

        lstat(strFilePath.c_str(), &statinfo);

        if(S_ISDIR(statinfo.st_mode)) // 디렉토리 이면
        {
            if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
                continue;

            list.insert(pair<string, bool>(strFilePath, true)); //디렉토리 경로를 넣음

 
            string strSubDir = strDir + "/" + entry->d_name;
            GetFileList(list, strSubDir); //디렉토리 안으로 들어감
        }
        else
        {
            //파일이면
            list.insert(pair<string, bool>(strFilePath, false)); //파일경로를 넣음
        }
    }

    //읽은 디렉토리 닫기
     closedir(dir);

    return true;
}

void CFileList::ShowFileList(FILE_LIST& list)
{
    //  특정 폴더 위치
    char path[] = "/media/HP01049471071/2015Y/Image/"; 
   
    FILE_LIST::iterator itr;
    cout << "[ShowFileList] : " << endl;
    for(itr = list.begin(); itr != list.end(); itr++)
    {
        if ( itr->second == true )
        {  
            //  exclude directory
            cout<<"[DIRECTORY] " + itr->first<<endl;
        }
        else
        {
            //  string to char
            char *fileName = new char[(itr->first).length()+1];
            strcpy(fileName,(itr->first).c_str()); 
           
            //  Make a command sentence
            char cmd[1024];
            sprintf(cmd, "mv %s %s", fileName, path );
            sprintf(cmd, "cp %s %s", fileName, path );

            //  move / copy
            system(cmd);

            //  release
            free(fileName);
        }
    }
}

int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        cout<<"usage: filelist [directory path]"<<endl;
        return 1;
    }

    CFileList FileList;

    string strSrcDir = argv[1];

    FILE_LIST file_list;

    FileList.GetFileList(file_list, strSrcDir);
    FileList.ShowFileList(file_list);

    return 0;
}

+ Recent posts