fpsantos

Firebird

Aug 11th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.96 KB | None | 0 0
  1. CREATE OR ALTER PROCEDURE STR_UNRTF (
  2.     P_SOURCE_RTF BLOB sub_type 1 segment SIZE 80)
  3. RETURNS (
  4.     RESULT_TEXT BLOB sub_type 1 segment SIZE 80)
  5. AS
  6. DECLARE variable TAGSTART INTEGER;
  7. DECLARE variable TAGFINISH INTEGER;
  8. DECLARE variable I INTEGER;
  9. DECLARE variable TAGFOUND BLOB sub_type 1 segment SIZE 80;
  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 CHAR(1);
  16. DECLARE variable HEXA_LEN INTEGER;
  17. BEGIN
  18.  
  19.     tag_open = '';
  20.     tag_close = '';
  21.     hexa_len = 2;
  22.  
  23.     -- essa procedure retorna um texto (blob) sem a porcao de tags rtf, ex:
  24.     -- ret=STR_UNRTF(string_rtf);   // resultado: texto sem as tags rtf
  25.     -- Infelizmente, codigos RTFs complexos ou sujos fazem essa procedure capotar
  26.     -- vou debugar mais tarde quando for conveniente.
  27.  
  28.     result_text='';
  29.     lquote='''';
  30.     p_source_rtf=TRIM(p_source_rtf);
  31.     tag_open='{';
  32.     tag_close='}';
  33.  
  34.     -- porem o primeiro caractere se comecar com {  ou o ultimo caracter terminar com }
  35.     -- devera ser removido
  36.     char_test=LEFT(p_source_rtf,CHAR_LENGTH(:tag_open));
  37.     IF (char_test=:tag_open) THEN
  38.         BEGIN
  39.             p_source_rtf=SUBSTRING(p_source_rtf FROM 2 FOR CHAR_LENGTH(:p_source_rtf));
  40.             p_source_rtf=TRIM(:p_source_rtf);
  41.         END
  42.     char_test=RIGHT(p_source_rtf,CHAR_LENGTH(:tag_close));
  43.     IF (char_test=:tag_close) THEN
  44.         BEGIN
  45.             p_source_rtf=SUBSTRING(p_source_rtf FROM 1 FOR CHAR_LENGTH(:p_source_rtf)-CHAR_LENGTH(:tag_close));
  46.             p_source_rtf=TRIM(:p_source_rtf);
  47.         END
  48.  
  49.     -- Remove tudo que estiver em {tag}
  50.     tagstart = POSITION (:tag_open, p_source_rtf);
  51.     while (:tagstart > 0) do
  52.     BEGIN
  53.         tagfinish = POSITION (:tag_close, p_source_rtf, tagstart);
  54.         IF (:tagfinish<:tagstart) THEN
  55.             tagfinish=CHAR_LENGTH(p_source_rtf);
  56.         tagfound = SUBSTRING (p_source_rtf FROM tagstart FOR ((tagfinish - tagstart) + 1));
  57.         p_source_rtf = REPLACE (p_source_rtf, tagfound, '');
  58.         tagstart = POSITION (:tag_open, p_source_rtf);
  59.     END
  60.  
  61.     -- RTF tem \escape para caracteres especiais, ex: fabrica\'e7\'e3o = fabricacao
  62.     -- E preciso localizar todos os escapes e troca-los pelas suas referencias Hexa->Ascii
  63.     tag_open='\'||:lquote;
  64.    tag_close=' ';
  65.    tagstart = position (:tag_open, p_source_rtf);
  66.    char_hex='';
  67.    hexa_len=2+(char_length(tag_open));
  68.    while (:tagstart > 0) do
  69.        begin
  70.            char_hex=substring(:p_source_rtf from :tagstart for hexa_len);
  71.            char_hex_to_str='0x'||substring(char_hex from char_length(tag_open)+1);
  72.            i=cast(:char_hex_to_str as int);
  73.            if (i>0) then
  74.                begin
  75.                    char_hex_to_str=ascii_char(i);
  76.                end
  77.            else
  78.                begin
  79.                    char_hex_to_str='{'||:char_hex_to_str||'}';
  80.                end
  81.            p_source_rtf = replace (p_source_rtf, char_hex, char_hex_to_str);
  82.            tagstart = position (:tag_open, p_source_rtf);
  83.        end
  84.    p_source_rtf=trim(p_source_rtf);
  85.  
  86.    -- RTF tem tags assim:
  87.    -- \viewkind4\uc1\pard\sa200\sl276\slmult1\lang1046\fs20 blabla bla bla
  88.    -- E preciso localizar essas tags e trocar por vazios
  89.  
  90.    tag_open='\';
  91.    tag_close=' ';
  92.    tagstart = position (:tag_open, p_source_rtf);
  93.    while (:tagstart > 0) do
  94.    begin
  95.        tagfinish = position (:tag_close, p_source_rtf, tagstart);
  96.        if (:tagfinish<:tagstart) then
  97.            tagfinish=char_length(p_source_rtf);
  98.            tagfound = substring (p_source_rtf from tagstart for ((tagfinish - tagstart) + 1));
  99.            p_source_rtf = replace (p_source_rtf, tagfound, '');
  100.            tagstart = position (:tag_open, p_source_rtf);
  101.    end
  102.  
  103.    -- finaliza
  104.    result_text = trim(p_source_rtf);
  105.  
  106.    suspend;
  107.  
  108. end
Add Comment
Please, Sign In to add comment