C/C++做文件夹中的索引

main.cpp
================================.
#if defined(_MSC_VER)
#pragma warning (disable: 4503) // see http://www.experts-exchange.com/Programming/Languages/CPP/Q_21426912.html
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <io.h>
#include <windows.h>

#include <iostream>
#include <fstream>
#include <sstream>
#include <strstream>

#include <algorithm>

#include <string>
#include <vector>
#include <map>

#include "find.h"

bool jREINDEX;
bool jINDEXFILE;
bool jSearch;
bool jUseINDEXFILE;

void _write_spacers(int iFullWidth=80)
{
while(iFullWidth){
std::cout << "-";
iFullWidth--;
}
}

void _JustifyCenter(const std::string& sTitle)
{
int iLen,iNum;
if(!(sTitle.empty())){
iLen=sTitle.size();
if((iLen%2)==0){
iNum=((80-iLen)/2);
while(iNum){
printf(" ");
iNum--;
}
std::cout << sTitle << std::endl;
}else{
iNum=((80-(iLen-1))/2);
while(iNum){
std::cout << " ";
iNum--;
}
std::cout<< sTitle << std::endl;
}
}else{
std::cout << "String is empty!" << std::endl;
}
}

int main()
{
jREINDEX=false;
jINDEXFILE=true;
jSearch=true;
jUseINDEXFILE=true;

std::string sFileName;
std::string sCom;

wph::FindFolder xFind;
while(1){
std::cout << "输入你要查询的文件名称: ";
std::cin >> sFileName;

if(!jREINDEX){
if(xFind.m_mIndex.empty()){
jSearch=true;
}else{
jSearch=false;
}
}else{
jSearch=true;
}

if(jSearch){
if(!(_access(".\\index.ffindex", 0)) && jINDEXFILE && !jREINDEX){
xFind._parse();
}else{
xFind.m_mIndex.clear();
xFind._traverse("D:");
if(_access(".\\index.ffindex", 0) && jUseINDEXFILE){
xFind._save();
}
}
}

_JustifyCenter("查询结果");

_write_spacers();

xFind._list(sFileName);

_write_spacers();

std::cout << "搜索用时: " << xFind.m_xTimeSec << "\t";
std::cout << "搜索到文件夹数量: " << xFind.m_iSize << "\t";
std::cout << "遍历文件夹数量: " << xFind.m_mIndex.size() << std::endl;
xFind.m_iSize=0;
xFind.m_xTimeSec=1;

_write_spacers();
}
return 0;
}


find.cpp
==============================
#if defined(_MSC_VER)
#pragma warning (disable: 4786) // see http://lists.boost.org/MailArchives/boost-users/msg02350.php
#pragma warning (disable: 4503) // see http://www.experts-exchange.com/Programming/Languages/CPP/Q_21426912.html
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <io.h>
#include <windows.h>

#include <iostream>
#include <fstream>
#include <sstream>
#include <strstream>

#include <algorithm>

#include <string>
#include <vector>
#include <map>

#include "find.h"

void wph::FindFolder::_look_folder(std::string& str, std::vector<std::string>& vFileInfo)
{
long Handle;
struct _finddata_t xFileInfo;

str+="\\";
std::string sFilepath=str;

//bool j=_checkFolderExist(str);
bool j=true;

if(j){
sFilepath+="*.*";
if(!((Handle=_findfirst(sFilepath.c_str(), &xFileInfo))==-1L)){
vFileInfo.push_back(xFileInfo.name);
//std::cout << xFileInfo.name << std::endl;
while(_findnext(Handle, &xFileInfo)==0){
/*if(xFileInfo.name=="." || xFileInfo.name==".."){
continue;
}*/

vFileInfo.push_back(xFileInfo.name);
//std::cout << xFileInfo.name << "\t" << xFileInfo.size << std::endl;
}
}else{
return;
}
_findclose(Handle);
}
}

void wph::FindFolder::_traverse(std::string sFileName)
{
//std::string sFolderName;
std::vector<std::string> vFileName;
std::vector<std::string> vFolder;

_look_folder(sFileName, vFileName);

time_t xStart;
time_t xEnd;

xStart=time(&xStart);
//m_mIndex.clear();

for(std::vector<std::string>::iterator it=vFileName.begin(); it!=vFileName.end(); it++){
std::string sName=*it;
if(sName=="." || sName==".."){
continue;
}

//std::string* sNamePoint = new std::string(sName);
std::string sBuff=sFileName+sName;
DWORD dw=GetFileAttributes(sBuff.c_str());
if(dw & FILE_ATTRIBUTE_DIRECTORY){
//std::cout << sBuff << std::endl;
_traverse(sBuff);
}else if(m_mIndex.find(sName)==m_mIndex.end()){
//sFolderName = new std::string(sName);
vFolder.push_back(sFileName);
m_mIndex[sName]=vFolder;
vFolder.clear();
}else if(m_mIndex.find(sName)!=m_mIndex.end()){
//sFolderName = new std::string(sName);
vFolder=m_mIndex[sName];
vFolder.push_back(sFileName);
m_mIndex[sName]=vFolder;
vFolder.clear();
}
}
xEnd=time(&xEnd);
time_t xTime=xEnd-xStart;
struct tm* p=localtime(&xTime);
m_xTimeSec=p->tm_sec;
m_xTimeMin=p->tm_min;
}

void wph::FindFolder::_list(std::string sKey)
{
std::map<std::string, std::vector<std::string> >::iterator it=m_mIndex.find(sKey);
std::vector<std::string> vFolder=it->second;

for(std::vector<std::string>::iterator iter=vFolder.begin(); iter!=vFolder.end(); iter++){
//std::string* pStr=*iter;
std::string sBuff=*iter;
std::cout << sBuff << std::endl;
m_iSize++;
}
}

void wph::FindFolder::_save()
{
std::ofstream xFile;
xFile.open(".\\index.ffindex");

for(std::map<std::string, std::vector<std::string> >::iterator it=m_mIndex.begin(); it!=m_mIndex.end(); it++){
std::string sName=it->first;
std::vector<std::string> vFolder=it->second;

xFile << sName << "\t";

for(std::vector<std::string>::iterator iter=vFolder.begin(); iter!=vFolder.end(); iter++){
std::string str=*iter;

xFile << str;
xFile << " ";
}
xFile << std::endl;
}

xFile.close();
}

void wph::FindFolder::_parse()
{
std::ifstream xFile;
time_t xStart;
time_t xEnd;

xStart=time(&xStart);

xFile.open(".\\index.ffindex");

std::ostringstream os;
os << xFile.rdbuf();
std::string sContent=os.str();

xFile.close();

bool xOkToFile=true;

std::string sFileName;
std::string sFolderName;

std::vector<std::string> vFolderName;

for(std::string::size_type x=0; x<sContent.size(); x++){

if(sContent.at(x)=='\t'){
xOkToFile=false;
continue;
}else if(sContent.at(x)=='\n'){
xOkToFile=true;
m_mIndex[sFileName]=vFolderName;
sFileName="";
vFolderName.clear();
continue;
}else if(sContent.at(x)=='\r'){
continue;
}

if(xOkToFile){
sFileName+=sContent.at(x);
}else{
if(sContent.at(x)==' '){
vFolderName.push_back(sFolderName);
sFolderName="";
}else{
sFolderName+=sContent.at(x);
}
}
}
xEnd=time(&xEnd);
time_t xTime=xEnd-xStart;
struct tm* p=localtime(&xTime);
m_xTimeSec=p->tm_sec;
m_xTimeMin=p->tm_min;
}

void wph::FindFolder::_clear()
{
m_mIndex.clear();
}

find.h
===============================
#if defined(_MSC_VER)
#pragma warning (disable: 4786) // see http://lists.boost.org/MailArchives/boost-users/msg02350.php
#pragma warning (disable: 4503) // see http://www.experts-exchange.com/Programming/Languages/CPP/Q_21426912.html
#endif

#if !defined(FIND_H)
#define FIND_H

#include <string>
#include <vector>
#include <map>

namespace wph{

class FindFolder{

public:

int m_xTimeSec;
int m_xTimeMin;

long int m_iSize;

std::map<std::string, std::vector<std::string> > m_mIndex;

public:

void _look_folder(std::string& str, std::vector<std::string>& vFileInfo);
void _traverse(std::string sFileName);
void _list(std::string sKey);
void _save();
void _parse();
void _clear();

public:
FindFolder()
: m_xTimeSec(0)
, m_xTimeMin(0)
, m_iSize(0)
{
return;
}

~FindFolder()
{
m_mIndex.clear();
return;
}

FindFolder(std::string sFileName)
{
_traverse(sFileName);
return;
}
};

}

#endif
=================================================================================
提醒各位:第一次查找文件时会有些慢,等几秒就好了,不要以为是程序有问题哦。

你可能感兴趣的:(c/c++)