📄 qsteproper.cpp
字号:
#include <fstream>#include <string>#if WIN32#else#include <unistd.h>#endif#include "Platform.h"#include "properfile.h"#include "filepath.h"#include "Scintilla.h"#include "qsteproper.h"#include "qsteeditor.h"using namespace std;static bool isFileExist(std::string file){ if(file.empty()) return false; ifstream f; f.open(file.c_str()); if(f.is_open() == false) return false; f.close(); return true; }static bool isFileCanWrite(std::string file){ if(file.empty()) return false; ifstream f; f.open(file.c_str(),ios_base::out); if(f.is_open() == false) return false; f.close(); return true;}QSteProper::QSteProper(){ initialized = false;}QSteProper::QSteProper(const char *filename,const char *workdir){ initialized = false; std::string file = workdir; file += filename; if(!isFileExist(file)) return; for(int index = 0;index < importMax;index++){ importFiles[index] = 0; } Clear(); FilePath filebase(file.c_str()); Read(filebase,filebase.Directory(),importFiles,importMax); initialized = true; m_filename = filename; m_workdir = workdir;}QSteProper::~QSteProper(){}bool QSteProper::writeIn(const char *key,const char *val,bool in){ if(!initialized) return false; if(!isFileCanWrite(m_filename)) return false; ifstream infile; ofstream outfile; std::string filename = key; filename += ".tmpfile"; outfile.open(filename.c_str(),ios_base::out | ios_base::trunc); if(outfile.is_open() == false) return false; infile.open(m_filename.c_str()); if(infile.is_open() == false){ outfile.close(); return false; } char buf[2048]; std::string data = key; data += "="; data += val; while(1){ infile.getline(buf,2048); if((strncmp(buf,key,strlen(key)) == 0) && (buf[strlen(key)] == '=' || buf[strlen(key)] == ' ')){ outfile << data.c_str(); in = false; }else{ outfile << buf; } if(!infile.eof()){ outfile << "\n"; }else{ break; } } if(in){ outfile << "\n"; outfile << data.c_str(); } outfile.close(); infile.close(); unlink(m_filename.c_str()); rename(filename.c_str(),m_filename.c_str()); return true;}bool QSteProper::addIn(const char *key,const char *val,bool in){ if(!initialized) return false; if(!isFileCanWrite(m_filename)) return false; ifstream infile; ofstream outfile; std::string filename = key; filename += ".tmpfile"; outfile.open(filename.c_str(),ios_base::out | ios_base::trunc); if(outfile.is_open() == false) return false; infile.open(m_filename.c_str()); if(infile.is_open() == false){ outfile.close(); return false; } char buf[2048]; std::string data; while(1){ infile.getline(buf,2048); if((strncmp(buf,key,strlen(key)) == 0) && (buf[strlen(key)] == '=' || buf[strlen(key)] == ' ')){ data = buf; int pos = data.find_first_of('='); data.insert(pos + 1,std::string(val) + ' '); outfile << data.c_str(); in = false; }else{ outfile << buf; } if(!infile.eof()){ outfile << "\n"; }else{ break; } } if(in){ outfile << "\n"; data = key; data += '='; data += val; outfile << data.c_str(); } outfile.close(); infile.close(); unlink(m_filename.c_str()); rename(filename.c_str(),m_filename.c_str()); return true;}bool QSteProper::saveToFile(const char *filename){ if(filename == NULL) return false; return printToFile(filename);}bool QSteProper::loadFromFile(const char *filename,const char *workdir){ std::string file = workdir; file += filename; if(!isFileExist(file.c_str())){ return initialized = false; } for(int index = 0;index < importMax;index++){ importFiles[index] = 0; } Clear(); FilePath filebase(file.c_str()); Read(filebase,filebase.Directory(),importFiles,importMax); m_filename = filename; m_workdir = workdir; return initialized = true;}QSteLexProper::QSteLexProper(const char *filename):QSteProper(filename){ m_showdegree = 0;}QSteLexProper::~QSteLexProper(){}SString QSteLexProper::getKeyWordsList(){}const char *QSteLexProper::getLanguageType(){ return m_type.c_str();}void QSteLexProper::setLanguageType(const char *type){ m_type = type;}SString QSteLexProper::getKeyWord(int index){ if(index < 0) return ""; SString lang = getLanguageType(); lang.insert(0,"."); if(index == 0){ SString key = "keywords."; return GetNewExpand(key.c_str(),lang.c_str()); }else{ SString key(index + 1); key += '.'; key.insert(0,"keywords"); return GetNewExpand(key.c_str(),lang.c_str()); }}void QSteLexProper::setSuperPs(QSteProper *sps){ if(sps == NULL) return ; superPS = static_cast<PropSet*>(sps);}const char *QSteLexProper::getLexPostfix(){ //for (int root = 0; root < hashRoots; root++) { //for (Property *p = props[root]; p; p = p->next) { //if(isprefix(p->key,"file.patterns")){ //printf("key = %s\n",p->key); //printf("val= %s\n",p->val); //} //} //} if(!isInitialized()) return NULL; else{ std::string pattern = "file.patterns."; pattern += getLanguageType(); for (int root = 0; root < hashRoots; root++) { for (Property *p = props[root]; p; p = p->next) { if(isprefix(p->key,pattern.c_str())){ return p->val; } } } } return NULL;}QSteAbbrevProper::QSteAbbrevProper(const char *filename):QSteProper(filename){ m_showdegree = 0;}QSteAbbrevProper::~QSteAbbrevProper(){}SString QSteAbbrevProper::getAbbrev(const char *abb){ if(abb == NULL) return SString(""); return Get(abb);}bool QSteAbbrevProper::addAbbrev(const char *abb,const char *str){ return false;}QSteGlobalProper::QSteGlobalProper(const char *filename,const char *workdir):QSteProper(filename,workdir){ for(int i = 0;i < importMax;i++){ lexPointer[i] = NULL; } initialize();}QSteGlobalProper::~QSteGlobalProper(){}class QSteEditor;void QSteGlobalProper::addAutoCompleteWords(SString acword,SString *wordlist,QSteEditor *curr){ if(wordlist == NULL || curr == NULL) return; std::list<QSteEditor*>::iterator i_index; TextToFind ft = {{0,0},0,{0,0}}; const int flags = SCFIND_WORDSTART | (curr->m_acIgnoreCase? 0 : SCFIND_MATCHCASE); for(i_index = m_steeditorlist.begin();i_index != m_steeditorlist.end();i_index++){ if(*i_index == curr || (*i_index)->getLexerProper() != curr->getLexerProper()) continue; int posCurrentWord = -1; unsigned int minWordLength = 0; unsigned int nwords = 0; int doclen = (*i_index)->getTextLength(); ft.lpstrText = const_cast<char*>(acword.c_str()); ft.chrg.cpMin = 0; ft.chrg.cpMax = doclen; ft.chrgText.cpMin = 0; ft.chrgText.cpMax = 0; int posFind = (*i_index)->sendMsgToSci(SCI_FINDTEXT, flags, reinterpret_cast<char *>(&ft)); while (posFind >= 0 && posFind < doclen) { // search all the document int wordEnd = posFind + acword.length(); if (posFind != posCurrentWord) { while((curr)->wordCharacters.contains((*i_index)->getCharacter(wordEnd))){ wordEnd++; } size_t wordLength = wordEnd - posFind; if (wordLength > acword.length()) { SString word = (*i_index)->getRange(posFind, wordEnd); word.insert(0, " "); word.append(" "); if (!wordlist->contains(word.c_str())) { // add a new entry *wordlist += word.c_str() + 1; if (minWordLength < wordLength) minWordLength = wordLength; nwords++; } } } ft.chrg.cpMin = wordEnd; posFind = (*i_index)->sendMsgToSci(SCI_FINDTEXT, flags, reinterpret_cast<char *>(&ft)); } }}void QSteGlobalProper::initialize(){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -