tstring ChatCtrl::rtfEscape(tstring str) { tstring::size_type i = 0; while((i = str.find_first_of(_T("{}\\\n"), i)) != tstring::npos) { switch(str[i]) { // no need to process \r handled elsewhere case '\n': str.replace(i, 1, _T("\\line\n")); i+=6; break; default: str.insert(i, _T("\\")); i+=2; } } return str; }
int32 CBrainMemory::RetrieveText(int64 MeaningID,tstring& Text,bool Nest){ int64 CurrentRoomValue,RoomType; int64 CurrentID = MeaningID; //首先得到意义空间的信息 if(!GetRoomInfo(MeaningID,CurrentRoomValue,RoomType))return 0; if(!Nest){ //首次外部调用时检查MeaningID代表记忆是否为可读的文字信息,嵌套调用则忽略 if(UnReadable(CurrentRoomValue))return 0; } //向上漫游,找到父空间空间的ID和逻辑明文,得记忆的形ID CppSQLite3Query Result = LBrainQuery("*",CurrentRoomValue,LB_CHILD_ID,CurrentID); if(Result.eof())return 0; CurrentID = Result.getInt64Field(0); if(CurrentID == ROOT_SPACE)return 0; CurrentRoomValue = Result.getInt64Field(1); //总是其他空间的空间识别ID //如果是字符,则可以确定所取文本应该是token int ch = IDToChar(CurrentRoomValue); if(isascii(ch)){ TCHAR buf[100]; //暂存token,一个单词99字符应该足够了 int p = 0; buf[p++] = ch; //继续向上漫游,应该全部都是字符 for(;;){ //根据本空间的ID和逻辑明文,找到父空间空间的ID和逻辑明文, CppSQLite3Query Result = LBrainQuery("*",CurrentRoomValue,LB_CHILD_ID,CurrentID); if(Result.eof())return 0; CurrentID = Result.getInt64Field(0); //如果得到的空间ID为根空间,则表示找到顶了。 if(CurrentID == ROOT_SPACE){ buf[p]='\0'; _tcsrev(buf); Text = buf; //如果是形容词则加上引号 if(RoomType == MEMORY_REFERENCE){ Text.insert(Text.begin(),1,_T('\"')); Text+=_T('\"'); } return 1; //表示得到一个token } CurrentRoomValue = Result.getInt64Field(1); if(p<100) buf[p++] = IDToChar(CurrentRoomValue); } } //不是字符,则需要嵌套处理,然后根据返回值添加标点符号 int32 n = 0; vector<tstring> StrList; for(;;){ tstring s; n = RetrieveText(CurrentRoomValue,s,false); StrList.push_back(s); //继续向上漫游,根据本空间的ID和逻辑明文,找到父空间空间的ID和逻辑明文, CppSQLite3Query Result = LBrainQuery("*",CurrentRoomValue,LB_CHILD_ID,CurrentID); if(Result.eof())return 0; CurrentID = Result.getInt64Field(0); //如果得到的空间ID为根空间,则表示找到顶了。 if(CurrentID == ROOT_SPACE)break; CurrentRoomValue = Result.getInt64Field(1); } TCHAR flag=_T(' '); if(n==1){ //子句,token之间应该有空格 flag = _T(' '); } else if(n==2){ //句子,子句之间有逗号 flag = _T(','); } else if(n ==3){ //段落,句子之间有句号 flag = _T('.'); } else if(n== 4){//文本,段落之间要分行 flag = _T('\n'); } assert(n<5); vector<tstring>::reverse_iterator It = StrList.rbegin(); while(It != StrList.rend()){ Text += *It; if(Text.size()){ TCHAR& ch = Text[Text.size()-1]; if(!ispunct(ch)){ Text += flag; }else if(Text.size()>2) { ch = Text[Text.size()-2]; if (isspace(ch)) { Text.erase(Text.size()-2,1); } } } It++; } if(RoomType == MEMORY_REFERENCE) { Text.insert(Text.begin(),1,_T('\"')); Text+='\"'; assert(n==1 || n==0); //引用被看作是一个token,应该只出现在子句里 return 1; } return ++n; }