Advertisement
hamacker

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

Nov 17th, 2017
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.50 KB | None | 0 0
  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.   -- Util para tornar um texto RTF 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.   -- Remove tudo que estiver em {tag}
  43.   tagstart = position (:tag_open, :p_source_rtf);
  44.   while (:tagstart > 0) do
  45.   begin
  46.     tagfinish = position (:tag_close, :p_source_rtf, :tagstart);
  47.     if (:tagfinish<:tagstart) then
  48.       tagfinish=char_length(:p_source_rtf);
  49.     tagfound = substring (:p_source_rtf from :tagstart for ((:tagfinish - :tagstart) + 1));
  50.     p_source_rtf = replace (:p_source_rtf, :tagfound, '');
  51.     tagstart = position (:tag_open, :p_source_rtf);
  52.   end
  53.  
  54.   -- RTF tem \escape para caracteres especiais, ex: fabrica\'e7\'e3o = fabricação
  55.   -- È preciso localizar todos os escapes e trocá-los pelas suas referencias Hexa->Ascii
  56.   tag_open='\'||:lquote;
  57.   tag_close=' ';
  58.   tagstart = position (:tag_open, :p_source_rtf);
  59.   char_hex='';
  60.   hexa_len=2+(char_length(:tag_open));
  61.   while (:tagstart > 0) do
  62.   begin
  63.     char_hex=substring(:p_source_rtf from :tagstart for :hexa_len);
  64.     char_hex_to_str='0x'||substring(:char_hex from char_length(:tag_open)+1);
  65.     i=cast(:char_hex_to_str as int);
  66.     if (i>0) then
  67.     begin
  68.       char_hex_to_str=ascii_char(i);
  69.     end
  70.     else
  71.     begin
  72.       char_hex_to_str='{'||:char_hex_to_str||'}';
  73.     end
  74.     p_source_rtf = replace (:p_source_rtf, :char_hex, :char_hex_to_str);
  75.     tagstart = position (:tag_open, :p_source_rtf);
  76.   end
  77.   p_source_rtf=trim(:p_source_rtf);
  78.  
  79.   -- RTF tem tags assim:
  80.   -- \viewkind4\uc1\pard\sa200\sl276\slmult1\lang1046\fs20 blabla bla bla
  81.   -- É preciso localizar essas tags e trocar por vazios
  82.  
  83.   tag_open='\';
  84.   tag_close=' ';
  85.   tagstart = position (:tag_open, :p_source_rtf);
  86.   while (:tagstart > 0) do
  87.   begin
  88.     tagfinish = position (:tag_close, :p_source_rtf, :tagstart);
  89.     if (:tagfinish<:tagstart) then
  90.       tagfinish=char_length(:p_source_rtf);
  91.     tagfound = substring (:p_source_rtf from :tagstart for ((:tagfinish - :tagstart) + 1));
  92.     p_source_rtf = replace (:p_source_rtf, :tagfound, '');
  93.     tagstart = position (:tag_open, :p_source_rtf);
  94.   end
  95.  
  96.   -- finaliza
  97.   result_text = trim(:p_source_rtf);
  98.  
  99.   return :result_text;
  100. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement