awtk/tools/str_gen/str_gen.cc

87 lines
2.3 KiB
C++
Raw Normal View History

2018-05-04 11:47:09 +08:00
/**
* File: str_gen.cc
2018-05-15 09:31:58 +08:00
* Author: AWTK Develop Team
2018-05-04 11:47:09 +08:00
* Brief: str_gen
*
2020-01-01 11:27:36 +08:00
* Copyright (c) 2018 - 2020 Guangzhou ZHIYUAN Electronics Co.,Ltd.
2018-05-04 11:47:09 +08:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* License file for more details.
*
*/
/**
* History:
* ================================================================
* 2018-05-03 Li XianJing <xianjimli@hotmail.com> created
*
*/
#include "str_gen.h"
#include "tkc/wstr.h"
#include "tkc/buffer.h"
2020-07-08 16:39:01 +08:00
#include "tkc/mem.h"
2018-08-24 07:45:37 +08:00
#include "base/locale_info.h"
2018-05-04 11:47:09 +08:00
void StrGen::Add(const string& language, const Sentence& sentence) {
StrTable::iterator iter = this->str_table.find(language);
if (iter == this->str_table.end()) {
this->str_table.insert(pair<string, Sentences>(language, Sentences()));
}
iter = this->str_table.find(language);
iter->second.Add(sentence);
return;
}
2020-07-08 16:39:01 +08:00
int32_t StrGen::Output(const string& language, wbuffer_t* wbuffer) {
2018-05-04 11:47:09 +08:00
StrTable::iterator iter = this->str_table.find(language);
2020-07-08 16:39:01 +08:00
return_value_if_fail(iter != this->str_table.end(), 0);
2018-05-04 11:47:09 +08:00
Sentences& sentences = iter->second;
sentences.Sort();
2020-07-08 16:39:01 +08:00
return this->OutputSentences(sentences, wbuffer);
2018-05-04 11:47:09 +08:00
}
vector<string> StrGen::GetLanguages() {
vector<string> languages;
for (StrTable::iterator i = this->str_table.begin(); i != this->str_table.end(); i++) {
languages.push_back(i->first);
}
return languages;
}
2020-07-08 16:39:01 +08:00
int32_t StrGen::OutputSentences(const Sentences& sentences, wbuffer_t* wbuffer) {
2018-05-04 11:47:09 +08:00
size_t header_size = 0;
size_t nr = sentences.sentences.size();
2020-07-08 16:39:01 +08:00
return_value_if_fail(nr >= 1, 0);
header_size = nr * sizeof(str_pair_t) + sizeof(str_table_t);
str_table_t* table = (str_table_t*)TKMEM_ALLOC(header_size);
2020-07-09 17:32:41 +08:00
memset(table, 0, header_size);
2020-07-08 16:39:01 +08:00
wbuffer_write_binary(wbuffer, table, header_size);
2018-05-04 11:47:09 +08:00
table->nr = nr;
table->version = 0;
2020-07-08 16:39:01 +08:00
for (size_t i = 0; i < nr; i++) {
2018-05-04 11:47:09 +08:00
const Sentence& iter = sentences.sentences[i];
2020-07-08 16:39:01 +08:00
table->strs[i].key = wbuffer->cursor;
wbuffer_write_string(wbuffer, iter.key.c_str());
2018-05-04 11:47:09 +08:00
2020-07-08 16:39:01 +08:00
table->strs[i].value = wbuffer->cursor;
wbuffer_write_string(wbuffer, iter.value.c_str());
2018-05-04 11:47:09 +08:00
}
2020-07-08 16:39:01 +08:00
int32_t size = wbuffer->cursor;
wbuffer->cursor = 0;
wbuffer_write_binary(wbuffer, table, header_size);
TKMEM_FREE(table);
return size;
2018-05-04 11:47:09 +08:00
}