phim xxx

indian girls

Archive for December 10th, 2006

Syntax highlighter

Today my thoughts about making syntax highlighting for my blog came back to me. As my blog is based on bash scripts, I needed an application that would do the actual highlighting. I know there exists a plugin for nanoblogger that enables syntax highlighting, but it’s fair from being mature. So I sat down and implemented
my own generic syntax highlighter. It’s not complete yet, but it already does some highlighting and it only took about an hour to implement. Qt was very helpful, as usual :)

Testing the highlighter should include trying to parse the code of the highlighter itself and so I did. You can see the result below.

  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include
  7. #include
  8. class�QHighlighter{
  9. public:
  10. ��QHighlighter(QIODevice�*indevice);
  11. ��void�readConfig(QIODevice�*configdevice);
  12. ��QString�parse();
  13. protected:
  14. ��QString�isKeyword(const�QString�&);
  15. ��QString�parseNumber();
  16. ��QString�parseIdentifier();
  17. ��QString�parseString();
  18. ��QString�parseComment();
  19. ��QString�parsePreproc();
  20. ��QString�parseSLComment();
  21. ��void�makeLines(bool�count=false);
  22. ��QIODevice�*_indevice;
  23. ��bool�_perror;
  24. ��QString�output;
  25. ��QMap<QString,�QStringList>�keywords;
  26. ��
  27. };
  28. void�QHighlighter::readConfig(QIODevice�*configdevice){
  29. ����QDomDocument�doc;
  30. ����if(!doc.setContent(configdevice)){
  31. ��������qDebug(“CONFIG ERROR”);
  32. ��������return;
  33. ����}
  34. ����QDomElement�root�=�doc.documentElement();
  35. ����for(QDomElement�keywselem�=�root.firstChildElement(“keywords”);�
  36. ��������!keywselem.isNull();�
  37. ��������keywselem�=�keywselem.nextSiblingElement(“keywords”))
  38. ����{
  39. ��������QStringList�kw;
  40. ��������for(QDomElement�key�=�keywselem.firstChildElement(“keyword”);�
  41. ������������!key.isNull();
  42. ������������key�=�key.nextSiblingElement(“keyword”))
  43. ��������{
  44. ������������kw�<<�key.text().trimmed();
  45. ��������}
  46. ��������QDomElement�keystyle�=�keywselem.firstChildElement(“style”);
  47. ��������QString�kstyle�=�keystyle.text().replace(“$”,�“%”).replace(“[“,�“<").replace(“]–>,�“>”);
  48. ��������keywords[kstyle]�=�kw;
  49. ����}
  50. }
  51. QString�QHighlighter::isKeyword(const�QString�&s){
  52. ��foreach(QString�k,�keywords.keys()){
  53. ����const�QStringList�&slist�=�keywords[k];
  54. ����foreach(QString�el,�slist){
  55. ������if(QRegExp(el).exactMatch(s))�return�k.arg(s);
  56. ����}
  57. �}
  58. �return�s;
  59. }
  60. QHighlighter::QHighlighter(QIODevice�*indevice){
  61. ��_indevice�=�indevice;
  62. ��_perror�=�false;
  63. }
  64. void�QHighlighter::makeLines(bool�countLines){
  65. ��if(!countLines)�output�=�output.replace(“\n”,�
    );
  66. ��else�{
  67. ����QString�n�=�
      “;

    1. ����foreach(QString�line,�output.split(“\n”)){
    2. ������n+=QString(
    3. %1
    4. \n”).arg(line);

    5. ����}
    6. ����n+=

    ;

  68. ����output�=�n;
  69. ��}
  70. }
  71. QString�QHighlighter::parse(){
  72. ��QString�current;
  73. ��while(!_indevice->atEnd()){
  74. ����char�c�=�_indevice->peek(1).at(0);
  75. ����QChar�ch(c);
  76. ����if(ch.isNumber())�{
  77. ������current�=�parseNumber();
  78. ������output�+=QString(%1).arg(current);
  79. ����}�else�if(ch.isLetter()�||�ch==‘_’)�{
  80. ������current�=�parseIdentifier();
  81. ������output�+=�isKeyword(current);
  82. ����}�else�if(c==‘”‘||�c==‘\”�)�{
  83. ������current�=�parseString().replace(“<“,�“<“);
  84. ������output�+=QString(%1).arg(current);
  85. ����}�else�if(c==‘/’�&&�_indevice->peek(2)==“/*”)�{
  86. ������current�=�parseComment().replace(“<“,�“<“);
  87. ������output�+=QString(%1).arg(current);
  88. ����}�else�if(c==‘/’�&&�_indevice->peek(2)==“//”){
  89. ������current�=�parseSLComment().replace(“<“,�“<“);
  90. ������output�+=�QString(%1).arg(current);
  91. ����}�else�if(c==‘#’){
  92. ������current�=�parsePreproc().replace(“<“,�“<“);
  93. ������output�+=�QString(%1).arg(current);
  94. ����}�else�if(c==‘<‘){
  95. ����_indevice->getChar(&c);
  96. ������output�+=�“<“;
  97. ����}�else�{
  98. ������_indevice->getChar(&c);
  99. ������if(c!=‘ ‘)
  100. ������output�+=�c;
  101. ������else�output�+=“�”;
  102. ����}
  103. ��}
  104. ��makeLines(true);
  105. ��return�output;
  106. }
  107. QString�QHighlighter::parsePreproc(){
  108. ��return�_indevice->readLine();
  109. }
  110. QString�QHighlighter::parseSLComment(){
  111. ��return�_indevice->readLine();
  112. }
  113. QString�QHighlighter::parseNumber(){
  114. ��QString�result;
  115. ��char�c;
  116. ��while(!_indevice->atEnd()){
  117. ����if(!_indevice->getChar(&c)){
  118. ������_perror�=�true;
  119. ������return�result;
  120. ����}
  121. ����QChar�ch(c);
  122. ����if(ch.isNumber()�||�c==‘.’){
  123. ������result+=c;
  124. ����}�else�{
  125. ������_indevice->ungetChar(c);
  126. ������return�result;
  127. ����}
  128. ��}
  129. ��return�result;
  130. }
  131. QString�QHighlighter::parseComment(){
  132. ��QString�result;
  133. ��char�c=‘\0′;
  134. ��char�prev=‘\0′;
  135. ��while(!_indevice->atEnd()){
  136. ����if(!_indevice->getChar(&c)){
  137. ������_perror�=�true;
  138. ������return�result;
  139. ����}
  140. ����// match */
  141. ����if(c==‘/’�&&�prev==‘*’){
  142. ��������result�+=c;
  143. ��������return�result;
  144. ����}
  145. ����prev�=�c;
  146. ����result+=c;
  147. ��}
  148. ���_perror�=�true;
  149. ��return�result;
  150. }
  151. QString�QHighlighter::parseIdentifier(){
  152. ��QString�result;
  153. ��char�c;
  154. ��while(!_indevice->atEnd()){
  155. ����if(!_indevice->getChar(&c)){
  156. ������_perror�=�true;
  157. ������return�result;
  158. ����}
  159. ����QChar�ch(c);
  160. ����if(ch.isLetter()�||�ch.isDigit()�||�c==‘_’�||�c==‘:’){
  161. ������result+=c;
  162. ����}�else�{
  163. ������_indevice->ungetChar(c);
  164. ������return�result;
  165. ����}
  166. ��}
  167. ��return�result;
  168. }
  169. QString�QHighlighter::parseString(){
  170. ��QString�result;
  171. ��char�c;
  172. ��char�sep;
  173. ��_indevice->getChar(&sep);
  174. ��result+=sep;
  175. ��while(!_indevice->atEnd()){
  176. ����if(!_indevice->getChar(&c)){
  177. ������_perror�=�true;
  178. ������return�result;
  179. ����}
  180. ����if(c==‘\\’){
  181. ������if(!_indevice->getChar(&c)){
  182. ��������_perror�=�true;
  183. ��������return�result;
  184. ������}
  185. ������result+=‘\\’;
  186. ������result+=c;
  187. ����}�else�if(c!=sep)�
  188. ������result+=c;
  189. �����else�{
  190. ������// _indevice->ungetChar(c);
  191. ��������result+=c;
  192. �������return�result;
  193. �����}
  194. ��}
  195. ��_perror�=�true;
  196. ��return�result;
  197. }
  198. int�main(int�argc,�char�**argv){
  199. ��QFile�infile(argv[1]);
  200. ��if(!infile.open(QFile::ReadOnly))�return�2;
  201. ��QHighlighter�h(&infile);
  202. ��QFile�config(“h.config”);
  203. ��if(!config.open(QFile::ReadOnly))�return�2;
  204. ��h.readConfig(&config);
  205. ��QString�result�=�h.parse();
  206. ��QFile�outfile(“hh.html”);
  207. ��outfile.open(QFile::WriteOnly);
  208. // outfile.write(”
    ");

  209. ��outfile.write(result.toLocal8Bit());
  210. // outfile.write(“”);

  211. ��return�0;
  212. }

  213. ]]>

xnxx indian