1、nmp_font_base.h
#ifndef __NMP_FONT_BASE__
#define __NMP_FONT_BASE__
#include<string>
#include <iostream>
#include<map>
#include<list>
using namespace std;
class NmpFontBase{
public:
NmpFontBase();
virtual ~NmpFontBase();
void init(string path);
list<string> getFonts(string font);
private:
int readFontFile();
string m_fontPath;
multimap<string, string>m_fontMapPool;
};
#endif //__NMP_FONT_BASE__
2、nmp_font_base.cpp
#include <stdio.h>//fopen
#include <string.h> //strrchr
#include <regex.h> //regcomp
//#include <string>
#include <fstream>//ifstream
#include "nmp_font_base.h"
NmpFontBase::NmpFontBase(){
}
NmpFontBase::~ NmpFontBase(){
}
void NmpFontBase::init(string path){
m_fontPath = path;
readFontFile();
}
list<string> NmpFontBase::getFonts(string font){
cout<<"begin get from list ------>"<<endl;
//cout<<m_fontMapPool.size()<<endl;
//cout<<m_fontMapPool.count(font)<<endl;
int size = m_fontMapPool.count(font);
multimap<string, string>::iterator it;
it = m_fontMapPool.find(font);
multimap<string, string>::size_type st;
list<string>fontList;
for (st = 0; st != size; st++, it++){
//cout << it->first << " " << it->second << "\n";
fontList.push_back(it->second);
}
cout<<"<---------end get from list"<<endl;
return fontList;
}
int NmpFontBase::readFontFile(){
#if 0
FILE *fp;
fp=fopen(m_fontPath.c_str(),"r");
if(fp==NULL){
cout<<"read font error"<<endl;
return -1;
}
char buf[32];
int i=0;
cout<<"begin read form file ------>0"<<endl;
regex_t re0;
regcomp(&re0, "[a-z]+", REG_EXTENDED);
regmatch_t pm0[1];
while(fgets(buf, sizeof(buf), fp) !=NULL){
char* c = strrchr(buf, '\n');
if (c) {
*c = 0;
} else {
cout<<"buffer did not end in newline, may have been truncated"<<endl;
}
regexec(&re0, buf, 1, pm0, 0);
// cout<<pm[0].rm_so <<","<<pm[0].rm_eo <<endl;
}
regfree(&re0);
cout<<"0<---------end read form file"<<endl;
fclose(fp);
#endif
ifstream fin(m_fontPath.c_str());
string s;
regex_t re;
regcomp(&re, "[a-z]+", REG_EXTENDED);
regmatch_t pm[1];
cout<<"begin read form file ------>"<<endl;
while(getline(fin,s)){
regexec(&re, s.c_str(), 1, pm, 0);
m_fontMapPool.insert(pair<string,string>(s.substr(pm[0].rm_so,pm[0].rm_eo-pm[0].rm_so),s.substr(0,pm[0].rm_so)));
}
regfree(&re);
cout<<"<---------end read form file"<<endl;
}
Makefile: