Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- create or alter function STR_UNRTF (
- P_SOURCE_RTF blob sub_type text)
- returns blob sub_type text
- AS
- declare variable tagstart integer;
- declare variable tagfinish integer;
- declare variable i integer;
- declare variable tagfound blob sub_type text;
- declare variable result_text blob sub_type text;
- declare variable tag_open varchar(255)='';
- declare variable tag_close varchar(255)='';
- declare variable char_test varchar(255);
- declare variable char_hex varchar(255);
- declare variable char_hex_to_str varchar(255);
- declare variable lquote varchar(1);
- declare variable hexa_len integer=2;
- BEGIN
- -- essa procedure/função retorna um texto (blob) sem a porção de tags rtf, ex:
- -- ret=STR_UNRTF(string_rtf); // resultado: texto sem as tags rtf
- -- Util para tornar um texto RTF texto pesquisável.
- result_text='';
- lquote='''';
- p_source_rtf=trim(:p_source_rtf);
- tag_open='{';
- tag_close='}';
- -- porém o primeiro caractere se começar com { ou o ultimo caracter terminar com }
- -- deverá ser removido
- char_test=left(:p_source_rtf,char_length(:tag_open));
- if (char_test=:tag_open) then
- begin
- p_source_rtf=substring(:p_source_rtf from 2 for char_length(:p_source_rtf));
- p_source_rtf=trim(:p_source_rtf);
- end
- char_test=right(:p_source_rtf,char_length(:tag_close));
- if (char_test=:tag_close) then
- begin
- p_source_rtf=substring(:p_source_rtf from 1 for char_length(:p_source_rtf)-char_length(:tag_close));
- p_source_rtf=trim(:p_source_rtf);
- end
- -- Remove tudo que estiver em {tag}
- tagstart = position (:tag_open, :p_source_rtf);
- while (:tagstart > 0) do
- begin
- tagfinish = position (:tag_close, :p_source_rtf, :tagstart);
- if (:tagfinish<:tagstart) then
- tagfinish=char_length(:p_source_rtf);
- tagfound = substring (:p_source_rtf from :tagstart for ((:tagfinish - :tagstart) + 1));
- p_source_rtf = replace (:p_source_rtf, :tagfound, '');
- tagstart = position (:tag_open, :p_source_rtf);
- end
- -- RTF tem \escape para caracteres especiais, ex: fabrica\'e7\'e3o = fabricação
- -- È preciso localizar todos os escapes e trocá-los pelas suas referencias Hexa->Ascii
- tag_open='\'||:lquote;
- tag_close=' ';
- tagstart = position (:tag_open, :p_source_rtf);
- char_hex='';
- hexa_len=2+(char_length(:tag_open));
- while (:tagstart > 0) do
- begin
- char_hex=substring(:p_source_rtf from :tagstart for :hexa_len);
- char_hex_to_str='0x'||substring(:char_hex from char_length(:tag_open)+1);
- i=cast(:char_hex_to_str as int);
- if (i>0) then
- begin
- char_hex_to_str=ascii_char(i);
- end
- else
- begin
- char_hex_to_str='{'||:char_hex_to_str||'}';
- end
- p_source_rtf = replace (:p_source_rtf, :char_hex, :char_hex_to_str);
- tagstart = position (:tag_open, :p_source_rtf);
- end
- p_source_rtf=trim(:p_source_rtf);
- -- RTF tem tags assim:
- -- \viewkind4\uc1\pard\sa200\sl276\slmult1\lang1046\fs20 blabla bla bla
- -- É preciso localizar essas tags e trocar por vazios
- tag_open='\';
- tag_close=' ';
- tagstart = position (:tag_open, :p_source_rtf);
- while (:tagstart > 0) do
- begin
- tagfinish = position (:tag_close, :p_source_rtf, :tagstart);
- if (:tagfinish<:tagstart) then
- tagfinish=char_length(:p_source_rtf);
- tagfound = substring (:p_source_rtf from :tagstart for ((:tagfinish - :tagstart) + 1));
- p_source_rtf = replace (:p_source_rtf, :tagfound, '');
- tagstart = position (:tag_open, :p_source_rtf);
- end
- -- finaliza
- result_text = trim(:p_source_rtf);
- return :result_text;
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement