scide: avoid recursive calls to Main::instance
[supercollider.git] / common / SC_TextUtils.cpp
blob105abf296a4ea1807549e6dd935c7343276cb7cc
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
5 Copyright (c) 2012 Tim Blechmann. All rights reserved.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <cstring>
24 #define OPENCURLY '{'
25 #define CLOSCURLY '}'
27 int rtf2txt(char* txt)
29 int rdpos=0, wrpos=0;
30 char c;
31 if (strncmp(txt,"{\\rtf",5)!=0) return 0; // OK, not an RTF file
32 text:
33 switch (txt[wrpos]=txt[rdpos++])
35 case 0:
36 /*{
37 char fname[32];
38 sprintf(fname, "rtf2txt_out%d.txt", bugctr++);
39 FILE *fp = fopen(fname, "w");
40 fwrite(txt,wrpos,1,fp);
41 fclose(fp);
42 }*/
43 return wrpos;
44 case OPENCURLY:
45 case CLOSCURLY:
46 case '\n': goto text;
47 case '\\':
48 if (strncmp(txt+rdpos,"fonttbl",7)==0
49 || strncmp(txt+rdpos,"filetbl",7)==0
50 || strncmp(txt+rdpos,"colortbl",8)==0
51 || strncmp(txt+rdpos,"stylesheet",10)==0
54 int level = 1;
55 while(level && (c=txt[rdpos++]) != 0) {
56 if (c == OPENCURLY) level++;
57 else if (c == CLOSCURLY) level--;
59 } else if (strncmp(txt+rdpos,"\'a0",3)==0 || (strncmp(txt+rdpos,"\'A0",3)==0))
61 txt[wrpos++] = ' '; rdpos = rdpos + 3;
62 } else {
63 if (txt[rdpos]==CLOSCURLY || txt[rdpos]==OPENCURLY
64 || txt[rdpos]=='\\' || txt[rdpos]=='\t'|| txt[rdpos]=='\n')
65 { txt[wrpos++] = txt[rdpos++]; goto text; }
66 if (strncmp(txt+rdpos,"tab",3)==0) { txt[wrpos++] = '\t'; }
67 if (strncmp(txt+rdpos,"par",3)==0) { txt[wrpos++] = '\n'; }
69 while((c=txt[rdpos++]) && c!=' ' && c!='\\');
70 if (c=='\\') rdpos--;
72 goto text;
73 default :
74 wrpos++;
75 goto text;
79 // strips HTML down to plaintext tags in a fairly simple-minded way
80 int html2txt(char* txt)
82 int rdpos=-1, wrpos=0, bodypos=-1;
83 bool intag = false;
85 // First check if we can find a BODY tag to start at
86 while(bodypos == -1 && txt[++rdpos] != 0){
87 if(strncmp(txt+rdpos, "<body", 5) == 0) // FIXME: should be case-insensitive, ideally
88 bodypos = rdpos;
90 if(bodypos != -1)
91 rdpos = bodypos;
92 else
93 rdpos = 0;
95 // Now we start from our start, and add the non-tag text to the result
96 while(txt[rdpos] != 0){
97 if(intag){
98 if(txt[rdpos++] == '>')
99 intag = false;
100 }else{
101 if(txt[rdpos] == '<'){
102 intag = true;
103 ++rdpos;
104 }else{
106 if(strncmp(txt+rdpos, "&amp;", 5)==0){
107 txt[wrpos++] = '&';
108 rdpos += 5;
109 }else if(strncmp(txt+rdpos, "&nbsp;", 6)==0){
110 txt[wrpos++] = ' ';
111 rdpos += 6;
112 }else if(strncmp(txt+rdpos, "&lt;", 4)==0){
113 txt[wrpos++] = '<';
114 rdpos += 4;
115 }else if(strncmp(txt+rdpos, "&gt;", 4)==0){
116 txt[wrpos++] = '>';
117 rdpos += 4;
118 }else{
120 txt[wrpos++] = txt[rdpos++];
125 txt[wrpos] = 0;
126 return wrpos;