字典合并小工具 @ OS X 10.10 Yosemite

// DicMerge.cpp

// 程序用途:将当前目录下收集到的字典合并,同时去掉重复项(注:仅去掉同一个字典文件内的重复项,如果多个不同字典文件内有重复的项目,则是会合并到一块的,而要解决这样的重复项,很简单,再单独合并一次结果文件自身即可)。

#include <dirent.h>

#include <iostream>
#include <fstream>

#include <string>

#include <set>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

static bool FileGetLine(const char* szFileName, vector<string>& v)
{
    bool fRet = false;
    
    const int MAX_LINE_LEN = 512;
    static char szLineData[MAX_LINE_LEN];
    
    ifstream infile;
    infile.open(szFileName);
    if ( !infile.fail() )
    {
        while (true)
        {
            infile.getline(szLineData, MAX_LINE_LEN);
            if ( infile.eof() )
            {
                fRet = true;
                break;
            }
            else
            {
                if ( '\r' == szLineData[strlen(szLineData)-1] )
                {
                    szLineData[strlen(szLineData)-1] = '\0';
                }
                v.push_back(szLineData);
            }
        }
        
        infile.close();
    }
    
    return fRet;
}

static int DicFromFile(const char* szFileName, vector<string>& vDic)
{
    vector<string> v;
    FileGetLine(szFileName, v);
    
    set<string> s;
    for ( vector<string>::iterator it_vs = v.begin(); it_vs != v.end(); ++it_vs )
    {
        s.insert(*it_vs);
    }
    
    for ( set<string>::iterator it_ss = s.begin(); it_ss != s.end(); ++it_ss )
    {
        vDic.push_back(*it_ss);
    }
    
    return 0;
}

void SearchAndMergeDicFile(const char* szDir, vector<string>& vDic)
{
    DIR* d = NULL;
    struct dirent* de = NULL;
    
    d = opendir(szDir);
    if ( d != NULL )
    {
        while ( (de = readdir(d)) != NULL )
        {
            if ( DT_REG == de->d_type )
            {
                char* ext = strrchr(de->d_name, '.');
                if ( ext != NULL )
                {
                    if ( 0 == strcmp(ext, ".dic") )
                    {
                        printf("[%s]\n", de->d_name);
                        DicFromFile(de->d_name, vDic);
                    }
                }
            }
        }
        
        closedir(d);
    }
}

void SaveDicToFile(vector<string>& vDic, const char* szFileName)
{
    FILE* fp = fopen(szFileName, "wb");
    if ( fp )
    {
        for ( vector<string>::iterator it_vs = vDic.begin(); it_vs != vDic.end(); ++it_vs )
        {
            fwrite((*it_vs).c_str(), 1, (*it_vs).length(), fp);
            fwrite("\r\n", 1, 2, fp);
        }
        
        fclose(fp);
    }
}

int main(void)
{
    cout<<"--- DicMerge on Mac v1.0 (20150928) ---"<<endl;
    vector<string> vDic;
    SearchAndMergeDicFile("./", vDic);
    sort(vDic.begin(), vDic.end());
    SaveDicToFile(vDic, "./results.dic");
    cout<<"Done[save to results.dic]"<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值