Permalink
Browse files

common: move text utils to separate source file in common

Signed-off-by: Tim Blechmann <tim@klingt.org>
  • Loading branch information...
1 parent 1cd62ff commit dd13593c03154b004695873f85b36aabb148fa5d @timblechmann timblechmann committed Aug 19, 2012
Showing with 169 additions and 107 deletions.
  1. +128 −0 common/SC_TextUtils.cpp
  2. +30 −0 common/SC_TextUtils.hpp
  3. +2 −0 editors/sc-ide/CMakeLists.txt
  4. +4 −0 lang/CMakeLists.txt
  5. +2 −107 lang/LangSource/PyrLexer.cpp
  6. +3 −0 platform/mac/CMakeLists.txt
View
128 common/SC_TextUtils.cpp
@@ -0,0 +1,128 @@
+/*
+ SuperCollider real time audio synthesis system
+ Copyright (c) 2002 James McCartney. All rights reserved.
+ http://www.audiosynth.com
+ Copyright (c) 2012 Tim Blechmann. All rights reserved.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ 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
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#include <cstring>
+
+#define OPENCURLY '{'
+#define CLOSCURLY '}'
+
+int rtf2txt(char* txt)
+{
+ int rdpos=0, wrpos=0;
+ char c;
+ if (strncmp(txt,"{\\rtf",5)!=0) return 0; // OK, not an RTF file
+text:
+ switch (txt[wrpos]=txt[rdpos++])
+ {
+ case 0:
+ /*{
+ char fname[32];
+ sprintf(fname, "rtf2txt_out%d.txt", bugctr++);
+ FILE *fp = fopen(fname, "w");
+ fwrite(txt,wrpos,1,fp);
+ fclose(fp);
+ }*/
+ return wrpos;
+ case OPENCURLY:
+ case CLOSCURLY:
+ case '\n': goto text;
+ case '\\':
+ if (strncmp(txt+rdpos,"fonttbl",7)==0
+ || strncmp(txt+rdpos,"filetbl",7)==0
+ || strncmp(txt+rdpos,"colortbl",8)==0
+ || strncmp(txt+rdpos,"stylesheet",10)==0
+ )
+ {
+ int level = 1;
+ while(level && (c=txt[rdpos++]) != 0) {
+ if (c == OPENCURLY) level++;
+ else if (c == CLOSCURLY) level--;
+ }
+ } else if (strncmp(txt+rdpos,"\'a0",3)==0 || (strncmp(txt+rdpos,"\'A0",3)==0))
+ {
+ txt[wrpos++] = ' '; rdpos = rdpos + 3;
+ } else {
+ if (txt[rdpos]==CLOSCURLY || txt[rdpos]==OPENCURLY
+ || txt[rdpos]=='\\' || txt[rdpos]=='\t'|| txt[rdpos]=='\n')
+ { txt[wrpos++] = txt[rdpos++]; goto text; }
+ if (strncmp(txt+rdpos,"tab",3)==0) { txt[wrpos++] = '\t'; }
+ if (strncmp(txt+rdpos,"par",3)==0) { txt[wrpos++] = '\n'; }
+
+ while((c=txt[rdpos++]) && c!=' ' && c!='\\');
+ if (c=='\\') rdpos--;
+ }
+ goto text;
+ default :
+ wrpos++;
+ goto text;
+ }
+}
+
+// strips HTML down to plaintext tags in a fairly simple-minded way
+int html2txt(char* txt)
+{
+ int rdpos=-1, wrpos=0, bodypos=-1;
+ bool intag = false;
+
+ // First check if we can find a BODY tag to start at
+ while(bodypos == -1 && txt[++rdpos] != 0){
+ if(strncmp(txt+rdpos, "<body", 5) == 0) // FIXME: should be case-insensitive, ideally
+ bodypos = rdpos;
+ }
+ if(bodypos != -1)
+ rdpos = bodypos;
+ else
+ rdpos = 0;
+
+ // Now we start from our start, and add the non-tag text to the result
+ while(txt[rdpos] != 0){
+ if(intag){
+ if(txt[rdpos++] == '>')
+ intag = false;
+ }else{
+ if(txt[rdpos] == '<'){
+ intag = true;
+ ++rdpos;
+ }else{
+ /*
+ if(strncmp(txt+rdpos, "&amp;", 5)==0){
+ txt[wrpos++] = '&';
+ rdpos += 5;
+ }else if(strncmp(txt+rdpos, "&nbsp;", 6)==0){
+ txt[wrpos++] = ' ';
+ rdpos += 6;
+ }else if(strncmp(txt+rdpos, "&lt;", 4)==0){
+ txt[wrpos++] = '<';
+ rdpos += 4;
+ }else if(strncmp(txt+rdpos, "&gt;", 4)==0){
+ txt[wrpos++] = '>';
+ rdpos += 4;
+ }else{
+ */
+ txt[wrpos++] = txt[rdpos++];
+ //}
+ }
+ }
+ }
+ txt[wrpos] = 0;
+ return wrpos;
+}
+
View
30 common/SC_TextUtils.hpp
@@ -0,0 +1,30 @@
+/*
+ SuperCollider real time audio synthesis system
+ Copyright (c) 2002 James McCartney. All rights reserved.
+ http://www.audiosynth.com
+ Copyright (c) 2012 Tim Blechmann. All rights reserved.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ 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
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#ifndef SC_TEXTUTILS_HPP
+#define SC_TEXTUTILS_HPP
+
+// strips out all the RichTextFile crap
+int rtf2txt(char* txt);
+int html2txt(char* txt);
+
+
+#endif // SC_TEXTUTILS_HPP
View
2 editors/sc-ide/CMakeLists.txt
@@ -74,6 +74,8 @@ set ( ide_src
widgets/settings/shortcuts_page.cpp
widgets/util/text_format_list_widget.cpp
widgets/util/popup_widget.cpp
+
+ ${CMAKE_SOURCE_DIR}/common/SC_TextUtils.cpp
)
set( ide_forms
View
4 lang/CMakeLists.txt
@@ -2,6 +2,8 @@ include_directories(${CMAKE_SOURCE_DIR}/include/common
${CMAKE_SOURCE_DIR}/include/lang
${CMAKE_SOURCE_DIR}/include/plugin_interface
${CMAKE_SOURCE_DIR}/include/server
+ ${CMAKE_SOURCE_DIR}/common
+
${CMAKE_SOURCE_DIR}/external_libraries/boost-lockfree
${CMAKE_SOURCE_DIR}/external_libraries/threadpool
${CMAKE_SOURCE_DIR}/external_libraries/yaml-cpp-0.3.0/include
@@ -75,6 +77,8 @@ set(sclang_sources
${CMAKE_SOURCE_DIR}/common/SC_StandAloneInfo_Darwin.cpp
${CMAKE_SOURCE_DIR}/common/SC_StringBuffer.cpp
${CMAKE_SOURCE_DIR}/common/SC_StringParser.cpp
+ ${CMAKE_SOURCE_DIR}/common/SC_TextUtils.cpp
+
${CMAKE_SOURCE_DIR}/common/scsynthsend.cpp
${CMAKE_SOURCE_DIR}/common/sc_popen.cpp
)
View
109 lang/LangSource/PyrLexer.cpp
@@ -67,7 +67,7 @@
#include "SC_LibraryConfig.h"
#include "SC_DirUtils.h"
-
+#include "SC_TextUtils.hpp"
int yyparse();
int processaccidental1(char *s);
@@ -124,8 +124,6 @@ std::set<std::string> compiledDirectories;
#define CLOSCURLY '}'
#define CLOSPAREN ')'
-//int rtf2txt(char* txt);
-
int sc_strtoi(const char *str, int n, int base)
{
int z = 0;
@@ -173,7 +171,7 @@ extern void asRelativePath(char *inPath, char *outPath)
strcpy(outPath, inPath);
return;
}
- strcpy(outPath, inPath + len);
+ strcpy(outPath, inPath + len);
}
@@ -218,109 +216,6 @@ static bool getFileText(char* filename, char **text, int *length)
int bugctr = 0;
-// strips out all the RichTextFile crap
-int rtf2txt(char* txt)
-{
- int rdpos=0, wrpos=0;
- char c;
- if (strncmp(txt,"{\\rtf",5)!=0) return 0; // OK, not an RTF file
-text:
- switch (txt[wrpos]=txt[rdpos++])
- {
- case 0:
- /*{
- char fname[32];
- sprintf(fname, "rtf2txt_out%d.txt", bugctr++);
- FILE *fp = fopen(fname, "w");
- fwrite(txt,wrpos,1,fp);
- fclose(fp);
- }*/
- return wrpos;
- case OPENCURLY:
- case CLOSCURLY:
- case '\n': goto text;
- case '\\':
- if (strncmp(txt+rdpos,"fonttbl",7)==0
- || strncmp(txt+rdpos,"filetbl",7)==0
- || strncmp(txt+rdpos,"colortbl",8)==0
- || strncmp(txt+rdpos,"stylesheet",10)==0
- )
- {
- int level = 1;
- while(level && (c=txt[rdpos++]) != 0) {
- if (c == OPENCURLY) level++;
- else if (c == CLOSCURLY) level--;
- }
- } else if (strncmp(txt+rdpos,"\'a0",3)==0 || (strncmp(txt+rdpos,"\'A0",3)==0))
- {
- txt[wrpos++] = ' '; rdpos = rdpos + 3;
- } else {
- if (txt[rdpos]==CLOSCURLY || txt[rdpos]==OPENCURLY
- || txt[rdpos]=='\\' || txt[rdpos]=='\t'|| txt[rdpos]=='\n')
- { txt[wrpos++] = txt[rdpos++]; goto text; }
- if (strncmp(txt+rdpos,"tab",3)==0) { txt[wrpos++] = '\t'; }
- if (strncmp(txt+rdpos,"par",3)==0) { txt[wrpos++] = '\n'; }
-
- while((c=txt[rdpos++]) && c!=' ' && c!='\\');
- if (c=='\\') rdpos--;
- }
- goto text;
- default :
- wrpos++;
- goto text;
- }
-}
-
-// strips HTML down to plaintext tags in a fairly simple-minded way
-int html2txt(char* txt)
-{
- int rdpos=-1, wrpos=0, bodypos=-1;
- bool intag = false;
-
- // First check if we can find a BODY tag to start at
- while(bodypos == -1 && txt[++rdpos] != 0){
- if(strncmp(txt+rdpos, "<body", 5) == 0) // FIXME: should be case-insensitive, ideally
- bodypos = rdpos;
- }
- if(bodypos != -1)
- rdpos = bodypos;
- else
- rdpos = 0;
-
- // Now we start from our start, and add the non-tag text to the result
- while(txt[rdpos] != 0){
- if(intag){
- if(txt[rdpos++] == '>')
- intag = false;
- }else{
- if(txt[rdpos] == '<'){
- intag = true;
- ++rdpos;
- }else{
- /*
- if(strncmp(txt+rdpos, "&amp;", 5)==0){
- txt[wrpos++] = '&';
- rdpos += 5;
- }else if(strncmp(txt+rdpos, "&nbsp;", 6)==0){
- txt[wrpos++] = ' ';
- rdpos += 6;
- }else if(strncmp(txt+rdpos, "&lt;", 4)==0){
- txt[wrpos++] = '<';
- rdpos += 4;
- }else if(strncmp(txt+rdpos, "&gt;", 4)==0){
- txt[wrpos++] = '>';
- rdpos += 4;
- }else{
- */
- txt[wrpos++] = txt[rdpos++];
- //}
- }
- }
- }
- txt[wrpos] = 0;
- return wrpos;
-
-}
bool startLexer(PyrSymbol *fileSym, int startPos, int endPos, int lineOffset)
{
View
3 platform/mac/CMakeLists.txt
@@ -23,6 +23,7 @@ include_directories(
${CMAKE_SOURCE_DIR}/include/lang
${CMAKE_SOURCE_DIR}/include/plugin_interface
${CMAKE_SOURCE_DIR}/include/server
+ ${CMAKE_SOURCE_DIR}/common
${CMAKE_SOURCE_DIR}/editors/scapp/include
${CMAKE_SOURCE_DIR}/editors/scapp/include/SMLAdvancedFind
${CMAKE_SOURCE_DIR}/editors/scapp/include/SMLAdvancedFind/ICU
@@ -95,6 +96,8 @@ set(scapp_sources
${CMAKE_SOURCE_DIR}/common/SC_StandAloneInfo_Darwin.cpp
${CMAKE_SOURCE_DIR}/common/SC_StringBuffer.cpp
${CMAKE_SOURCE_DIR}/common/SC_StringParser.cpp
+ ${CMAKE_SOURCE_DIR}/common/SC_TextUtils.cpp
+
${CMAKE_SOURCE_DIR}/common/scsynthsend.cpp
${CMAKE_SOURCE_DIR}/common/sc_popen.cpp

0 comments on commit dd13593

Please sign in to comment.