daily pastebin goal
53%
SHARE
TWEET

Função em FirebirdSQL para converter BLOB/RTF em Texto Puro

hamacker Nov 17th, 2017 (edited) 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. create or alter function STR_UNRTF (
  2.     P_SOURCE_RTF blob sub_type text)
  3. returns blob sub_type text
  4. AS
  5. declare variable tagstart integer;
  6. declare variable tagfinish integer;
  7. declare variable i integer;
  8. declare variable tagfound blob sub_type text;
  9. declare variable result_text blob sub_type text;
  10. declare variable tag_open varchar(255)='';
  11. declare variable tag_close varchar(255)='';
  12. declare variable char_test varchar(255);
  13. declare variable char_hex varchar(255);
  14. declare variable char_hex_to_str varchar(255);
  15. declare variable lquote varchar(1);
  16. declare variable hexa_len integer=2;
  17. BEGIN
  18.   -- essa procedure/função retorna um texto (blob) sem a porção de tags rtf, ex:
  19.   -- ret=STR_UNRTF(string_rtf);   // resultado: texto sem as tags rtf
  20.   -- Isso é util para tornar o texto pesquisável
  21.   result_text='';
  22.   lquote='''';
  23.   p_source_rtf=trim(p_source_rtf);
  24.   tag_open='{';
  25.   tag_close='}';
  26.  
  27.   -- porém o primeiro caractere se começar com {  ou o ultimo caracter terminar com }
  28.   -- deverá ser removido
  29.   char_test=left(p_source_rtf,char_length(:tag_open));
  30.   if (char_test=:tag_open) then
  31.   begin
  32.     p_source_rtf=substring(p_source_rtf from 2 for char_length(:p_source_rtf));
  33.     p_source_rtf=trim(:p_source_rtf);
  34.   end
  35.   char_test=right(p_source_rtf,char_length(:tag_close));
  36.   if (char_test=:tag_close) then
  37.   begin
  38.     p_source_rtf=substring(p_source_rtf from 1 for char_length(:p_source_rtf)-char_length(:tag_close));
  39.     p_source_rtf=trim(:p_source_rtf);
  40.   end
  41.  
  42.  
  43.   -- Remove tudo que estiver em {tag}
  44.   tagstart = position (:tag_open, p_source_rtf);
  45.   while (:tagstart > 0) do
  46.   begin
  47.     tagfinish = position (:tag_close, p_source_rtf, tagstart);
  48.     if (:tagfinish<:tagstart) then
  49.       tagfinish=char_length(p_source_rtf);
  50.     tagfound = substring (p_source_rtf from tagstart for ((tagfinish - tagstart) + 1));
  51.     p_source_rtf = replace (p_source_rtf, tagfound, '');
  52.     tagstart = position (:tag_open, p_source_rtf);
  53.   end
  54.  
  55.   -- RTF tem \escape para caracteres especiais, ex: fabrica\'e7\'e3o = fabricação
  56.   -- È preciso localizar todos os escapes e trocá-los pelas suas referencias Hexa->Ascii
  57.   tag_open='\'||:lquote;
  58.   tag_close=' ';
  59.   tagstart = position (:tag_open, p_source_rtf);
  60.   char_hex='';
  61.   hexa_len=2+(char_length(tag_open));
  62.   while (:tagstart > 0) do
  63.   begin
  64.     char_hex=substring(:p_source_rtf from :tagstart for hexa_len);
  65.     char_hex_to_str='0x'||substring(char_hex from char_length(tag_open)+1);
  66.     i=cast(:char_hex_to_str as int);
  67.     if (i>0) then
  68.     begin
  69.       char_hex_to_str=ascii_char(i);
  70.     end
  71.     else
  72.     begin
  73.       char_hex_to_str='{'||:char_hex_to_str||'}';
  74.     end
  75.     p_source_rtf = replace (p_source_rtf, char_hex, char_hex_to_str);
  76.     tagstart = position (:tag_open, p_source_rtf);
  77.   end
  78.   p_source_rtf=trim(p_source_rtf);
  79.  
  80.   -- RTF tem tags assim:
  81.   -- \viewkind4\uc1\pard\sa200\sl276\slmult1\lang1046\fs20 blabla bla bla
  82.   -- É preciso localizar essas tags e trocar por vazios
  83.  
  84.   tag_open='\';
  85.   tag_close=' ';
  86.   tagstart = position (:tag_open, p_source_rtf);
  87.   while (:tagstart > 0) do
  88.   begin
  89.     tagfinish = position (:tag_close, p_source_rtf, tagstart);
  90.     if (:tagfinish<:tagstart) then
  91.       tagfinish=char_length(p_source_rtf);
  92.     tagfound = substring (p_source_rtf from tagstart for ((tagfinish - tagstart) + 1));
  93.     p_source_rtf = replace (p_source_rtf, tagfound, '');
  94.     tagstart = position (:tag_open, p_source_rtf);
  95.   end
  96.  
  97.   -- finaliza
  98.   result_text = trim(p_source_rtf);
  99.  
  100.   return result_text;
  101. END
RAW Paste Data
Top