コード例 #1
0
ファイル: filesystem.cpp プロジェクト: 3660628/mrpt
/*---------------------------------------------------------------
					deleteFilesInDirectory
---------------------------------------------------------------*/
bool mrpt::system::deleteFilesInDirectory(const string &path, bool deleteDirectoryAsWell)
{
	if (!directoryExists(path)) return false;

	CDirectoryExplorer::TFileInfoList	lstFiles;
	CDirectoryExplorer::explore(path,FILE_ATTRIB_ARCHIVE | FILE_ATTRIB_DIRECTORY,lstFiles);

	for (CDirectoryExplorer::TFileInfoList::iterator i=lstFiles.begin();i!=lstFiles.end();++i)
	{
		if (i->isDir)
		{
			if (i->name!="." && i->name!="..")
			{
				if (!mrpt::system::deleteFilesInDirectory(i->wholePath, true))
					return false;
			}
		}
		else
		{
			if (!mrpt::system::deleteFile(i->wholePath))
				return false;
		}
	}

	// Finally, delete the directory itselt.
	if (deleteDirectoryAsWell)
			return 0==_rmdir(path.c_str());
	else	return true;
}
コード例 #2
0
ファイル: test.cpp プロジェクト: 3660628/mrpt
// ------------------------------------------------------
//				TestDirExplorer
// ------------------------------------------------------
void TestDirExplorer()
{
	CDirectoryExplorer::TFileInfoList	lst;

	string		path( mrpt::system::getcwd() );
	printf("Exploring path: %s\n",path.c_str());

	CDirectoryExplorer::explore(
		path,
		FILE_ATTRIB_ARCHIVE | FILE_ATTRIB_DIRECTORY,
		lst );

	printf("Found %i files:\n",(unsigned int)lst.size());

	for (CDirectoryExplorer::TFileInfoList::iterator it=lst.begin();it!=lst.end();++it)
	{
		printf("name: %s\n",it->name.c_str());
		printf("wholePath: %s\n",it->wholePath.c_str());
		printf("isDir: %c\n", it->isDir ? 'Y':'N' );
		printf("size: %lu bytes\n", (unsigned long)it->fileSize );
		printf("-----------------------\n");
	}

}
コード例 #3
0
// ------------------------------------------------------
//                        MAIN
// ------------------------------------------------------
int main(int argc, char** argv)
{
    try
    {
        if (argc<2)
        {
            printf("Usage: %s source1.cpp [source2.cpp ...]\n",argv[0]);
            return -1;
        }

        for (int i=1; i<argc; i++)
        {
            CDirectoryExplorer::TFileInfoList lst;
            CDirectoryExplorer::explore(argv[i],FILE_ATTRIB_ARCHIVE,lst);

            if (lst.empty())
            {
                cerr << "Error: Not found " << argv[i] << endl;
                continue;
            }

            for (CDirectoryExplorer::TFileInfoList::const_iterator it=lst.begin(); it!=lst.end(); it++)
            {
                FILE *f = fopen(it->wholePath.c_str(),"rb");
                if (!f)
                {
                    cerr << "Error: Cannot open" << it->wholePath << endl;
                    continue;
                }
                unsigned char buf[10];
                fread(buf,1,3,f);

                // Get file size:
                fseek(f,0,SEEK_END);
                const long N = ftell(f);
                fclose(f);

                if (buf[0]==0xEF && buf[1]==0xBB && buf[2]==0xBF)
                {
                    cout  << "Skipping (already has BOM): " << it->wholePath << endl;
                    continue;
                }

                // Add BOM: =======================================
                // Read the whole file:
                unsigned char *fil_buf = new unsigned char[N+3];
                fil_buf[0] = 0xEF;
                fil_buf[1] = 0xBB;
                fil_buf[2] = 0xBF;

                f = fopen(it->wholePath.c_str(),"rb");
                fread(fil_buf+3,1,N,f);
                fclose(f);

                // Write new version:
                f = fopen(it->wholePath.c_str(),"wb");
                fwrite(fil_buf,1,N+3,f);
                fclose(f);

                delete[] fil_buf;

                cout << "Added BOM: " << it->wholePath << endl;
            }
        }


        return 0;
    } catch (exception &e)
    {
        cerr << "EXCEPCTION: " << e.what() << endl;
        return -1;
    }
    catch (...)
    {
        cerr << "Untyped excepcion!!";
        return -1;
    }
}