2dx下生成我的图片名字声明

//
//  main.cpp
//  AutoRes
//
//  Created by dai on 15/2/7.
//  Copyright (c) 2015年 dai. All rights reserved.
//

#include <iostream>
#include <vector>
#include <string>
#include <dirent.h>
#include "Res.h"
#include <fstream>
#include <sys/stat.h>
#include <algorithm>
#include <cassert>
using namespace std;


bool endWith(const string& src,const string& postFix,bool ignoreCase = true);

bool isTargetFile(const string& filename){
    vector<string> postfix;
    postfix.push_back("png");
    postfix.push_back("jpg");
    postfix.push_back("jpeg");
    
    for (int i = 0; i < postfix.size(); ++i) {
        if (endWith(filename,postfix[i])) {
            return true;
        }
    }
    return false;
}

string getFileNameWithoutPostFix(const string& src){
    auto pos = src.rfind('.');
    return src.substr(0,pos);
}

// 根据文件夹自动处理资源,生成相应的声明文件
// 根据输入的路径进行递归的处理

using Stat = struct stat;

void processDir(const string& fullpath,ofstream& out,int depth = 1){
    
    DIR* dir = opendir(fullpath.c_str());
    if (dir) {
        dirent* dp = nullptr;
        Stat statbuf;
        while ((dp = readdir(dir)) != nullptr) {
            // 跳过.  .. 和隐藏文件
            if (dp->d_name[0] == '.') {
                continue;
            }
            
            //判断是否是目录
            string tmpPath = fullpath + "/" + dp->d_name;
            lstat(tmpPath.c_str(),&statbuf);
            
            if (S_ISDIR(statbuf.st_mode)) {
                
                int newDepth = depth + 1;
                
                // 目录
                processDir(tmpPath,out,newDepth);
            }else{
                // for debug
              //  cout << dp->d_name<< endl;
                
                if (isTargetFile(dp->d_name)) {
                    string nameWithoutPostFix = getFileNameWithoutPostFix(dp->d_name);
                    out << "const static string IMG_" << nameWithoutPostFix << " = " <<  "\"" << dp->d_name << "\";" << endl;
                }
            }
        }
    }
    closedir(dir);
}

// 添加头文件
void addHead(ofstream& out);

// 添加尾
void addTail(ofstream& out);

bool isDir(const string& path){
    bool isDirectory = false;
    DIR* dir = opendir(path.c_str());
    if (dir) {
        dirent* dp = readdir(dir);
        if (dp) {
            Stat statbuf;
            lstat(path.c_str(),&statbuf);
            if (S_ISDIR(statbuf.st_mode)) {
                isDirectory =  true;
            }
        }
    }
    closedir(dir);
    return isDirectory;
}

int main(int argc, const char * argv[]) {
    
    if (argc < 3) {
        cout << "copy ~/Desktop/images ~/Desktop" << endl;
        return 0;
    }
    
//    cout << argv[1] << endl;
//    cout << argv[2] << endl;
    
    string srcPath = argv[1];
    string dstFile = argv[2];
    
    // 1.要求 srcPath dstFile 都是一个有效目录
    if (!isDir(srcPath)) {
        cout << "输入有效的源文件夹路径" << endl;
        return 1;
    }
    
    if (!isDir(dstFile)) {
        cout << "输入有效的目的文件夹路径" << endl;
        return 1;
    }
    
    //检查输入里面是否有 '/'
    if (srcPath[srcPath.size() - 1] == '/') {
        srcPath = srcPath.substr(0,srcPath.size()-1);
    }
    
    if (dstFile[dstFile.size() - 1] == '/') {
        dstFile += "Res.h";
    }else{
        dstFile += "/Res.h";
    }
    
    ofstream out(dstFile);
    addHead(out);
    processDir(srcPath,out,1);
    addTail(out);
    out.close();
    
    return 0;
}


bool endWith(const string& src,const string& postFix,bool ignoreCase){
    auto pos = src.rfind('.');
    if (pos != src.size()) {
        string result = src.substr(pos + 1);
        
        if (ignoreCase) {
            transform(result.begin(),result.end(),result.begin(),::tolower);
            string pstFix(postFix);
            transform(postFix.begin(),postFix.end(),pstFix.begin(), ::tolower);
            if (result == pstFix) {
                return true;
            }
        }else{
            if (result == postFix) {
                return true;
            }
        }
    }
    return false;
}

void addHead(ofstream& out){
    
//#ifndef __AutoRes__Res__
//#define __AutoRes__Res__

    out << "#ifndef __AutoRes__Res__"<<endl;
    out << "#define __AutoRes__Res__"<<endl;
    out << "#include <string>" << endl;
    out << "using namespace std;" << endl;
    out << endl;
    out << endl;
}

void addTail(ofstream& out){
    
    out << endl;
    out << endl;
    out << "#endif" << endl;
    out << endl;
}

主要就是递归,注意 DIR dirent 的使用


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值