This commit is contained in:
wyy 2013-06-23 23:58:25 +08:00
parent 84c5a2db67
commit 12ac1c9a6b
6 changed files with 155 additions and 0 deletions

2
.gitignore vendored
View File

@ -6,3 +6,5 @@ tags
*.d
*.ut
log
main
cppcommon/cmlib.a

46
Makefile Normal file
View File

@ -0,0 +1,46 @@
CC = g++
CCOPT = -Wall -c
LINK = g++
LINKOPT =
PACKA = ar
PARCAOPT = rc
DOLINK = $(LINK) $(LINKOPT) -o $@ $^
SOURCES := $(wildcard *.cpp)
OBJS := $(patsubst %.cpp,%.o,$(SOURCES))
CMDIR = ./cppcommon/
CMLIB = $(CMDIR)cmlib.a
# remove the objs after compilation
.INTERMEDIATE:
.PHONY: clean
# This is a suffix rule
#.c.o:
%.o: %.cpp
$(CC) $(CCOPT) $<
# Main Targets
all: main
main: $(OBJS) $(CMLIB)
$(DOLINK)
$(CMLIB): $(CMDIR)
cd $(CMDIR) && $(MAKE)
#unit test
Trie.ut: Trie.cpp Trie.h $(CMLIB)
g++ -o $@ $< -DTRIE_UT $(CMLIB)
clean:
rm -f *.o *.ut $(CMLIB) main
sinclude $(SOURCES:.cpp=.d)
%.d:%.cpp
@set -e; rm -f $@; \
$(CC) -MM $< > $@.$$$$; \
sed 's,\($*\).o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$

49
Trie.cpp Normal file
View File

@ -0,0 +1,49 @@
#include "Trie.h"
namespace CppJieba
{
Trie::Trie()
{
}
Trie::~Trie()
{
}
bool Trie::init(const char* const filepath)
{
ifstream ifile(filepath);
string line;
vector<string> vecBuf;
while(getline(ifile, line))
{
vecBuf.clear();
splitStr(line, vecBuf, " ");
PRINT_VECTOR(vecBuf);
getchar();
uint16_t strbuf[1024];
size_t unilen = utf8ToUnicode(line.c_str(), line.size(), strbuf);
for(int i = 0; i < unilen; i++)
{
// printf("%x\n", strbuf[i]);
}
char utf8str[512]={0};
unicodeToUtf8(strbuf, unilen, utf8str);
//cout<<strlen(utf8str);
cout<<utf8str<<endl;
}
}
}
#ifdef TRIE_UT
using namespace CppJieba;
int main()
{
Trie trie;
trie.init();
return 0;
}
#endif

23
Trie.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef TRIE_H
#define TRIE_H
#include <iostream>
#include <fstream>
#include "cppcommon/str_functs.h"
#include "cppcommon/vec_functs.h"
#include "globals.h"
namespace CppJieba
{
using namespace CPPCOMMON;
using namespace std;
class Trie
{
public:
Trie();
~Trie();
public:
bool init(const char* const filepath = DICT_FILE_PATH);
};
}
#endif

6
globals.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef GLOBALS_H
#define GLOBALS_H
const char * const DICT_FILE_PATH = "dict.txt";
#endif

29
main.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <iostream>
#include <iconv.h>
#include <stdint.h>
#include <fstream>
#include <memory.h>
#include "cppcommon/str_functs.h"
using namespace std;
using namespace CPPCOMMON;
int main()
{
ifstream ifile("dict.txt");
string line;
while(getline(ifile, line))
{
uint16_t strbuf[1024];
size_t unilen = utf8ToUnicode(line.c_str(), line.size(), strbuf);
for(int i = 0; i < unilen; i++)
{
// printf("%x\n", strbuf[i]);
}
char utf8str[512]={0};
unicodeToUtf8(strbuf, unilen, utf8str);
//cout<<strlen(utf8str);
cout<<utf8str<<endl;
}
return 0;
}