PARAMETERS davka

sql_file = "sqlfile"
crlf = CHR(13)+CHR(10)

STRTOFILE(m.davka, m.gcpri_path + m.sql_file + ".sql")
fh = fopen(        m.gcpri_path + m.sql_file + ".sql" )

preprocess_blok()
preprocess_def ()

RETURN FILETOSTR(m.gcpri_path + m.sql_file + "_2.sql")


* * * * * * * * * * *     PREPROCESOR BLOKU     * * * * * * * * * * * *
PROCEDURE preprocess_blok

sql_file_obsah = ""
DO WHILE ! feof(m.fh)
   qq = fgets(m.fh, 8190)

   DO CASE 
   CASE EMPTY(m.qq)
       sql_file_obsah = m.sql_file_obsah + m.qq + m.crlf

   CASE AT("#[", m.qq) = 1 .and. AT("]#", m.qq) > 2
       qq = rozbal_makro(m.qq)
       sql_file_obsah = m.sql_file_obsah + m.qq + m.crlf

   OTHERWISE 
       sql_file_obsah = m.sql_file_obsah + m.qq + m.crlf
   ENDCASE  
ENDDO
fclose(m.fh)
STRTOFILE( m.sql_file_obsah, m.gcpri_path + m.sql_file + "_1.sql")


* * * * * * * * * * *     PREPROCESOR #DEF     * * * * * * * * * * * *
PROCEDURE preprocess_def
fh   = fopen(m.gcpri_path + m.sql_file + "_1.sql")
sql_file_obsah = ""
kus_nazvu_souboru = ""
CREATE TABLE (m.gcpri_path + "nahrady") FREE (co c(33), cim c(250))
DO WHILE ! feof(m.fh)
   qq = fgets(m.fh, 8190)
*   qq = strtr(m.qq, CHR(9), " ")    && nahrada tabulatoru mezerou
   q1 = allt(subst(m.qq, at(" ", m.qq) + 1))
   co = left(m.q1, at(" ", m.q1) - 1)
   cim = allt(subst(m.q1, at(" ", m.q1) + 1))

   DO CASE 
   CASE EMPTY(m.qq)
       sql_file_obsah = m.sql_file_obsah + m.qq + m.crlf

   CASE UPPER(LEFT(m.qq, 4)) = "#DEF"
       SCAN            && v rozbalovanem makru mohou byt pouzita drive definovana makra
          cim = STRTRAN(m.cim, ALLTRIM(nahrady.co), ALLTRIM(nahrady.cim))
       ENDSCAN 

       LOCATE FOR ALLTRIM(co) = m.co
       IF .not. FOUND()
           APPEND BLANK 
       ENDIF 
       GATHER MEMVAR 
       
       IF ATC("TABLENAME", m.co) > 0
          kus_nazvu_souboru = ALLTRIM(m.cim)
       ENDIF 

   OTHERWISE 
       SCAN
          qq = STRTRAN(m.qq, ALLTRIM(nahrady.co), ALLTRIM(nahrady.cim))
       ENDSCAN 
       sql_file_obsah = m.sql_file_obsah + m.qq + m.crlf
   ENDCASE  
ENDDO
fclose(m.fh)
sql_file_obsah = STRTRAN(ALLTRIM(m.sql_file_obsah), m.crlf+m.crlf+m.crlf, m.crlf+m.crlf)
STRTOFILE( m.sql_file_obsah, m.gcpri_path + m.sql_file + "_2.sql")
**STRTOFILE( m.sql_file_obsah, m.gcpri_path + m.sql_file + "_"+ m.kus_nazvu_souboru + ".sql")


*!*****************************************************************************
PROCEDURE rozbal_makro(nazev_makra)

rm_prom = ALLTRIM(SUBST(m.nazev_makra, AT("]#", m.nazev_makra)+2)) + " "

makroprikaz = LEFT(m.nazev_makra,  AT("]#", m.nazev_makra)-1)
makroprikaz = ALLTRIM(SUBST(m.makroprikaz, 3))    && prvni 2 znaky radku jsou #[

*rozbalene_makro = "---- #[ " + m.makroprikaz + " ]# (" + ALLTRIM(m.rm_prom) + ") ----"
rozbalene_makro = ""

DIMENSION macroparams(9)
STORE "" TO macroparams
poradi_param = 1
DO WHILE AT(" ", m.rm_prom) > 1
   macroparams(poradi_param) = macroparams(poradi_param) + ALLTRIM(LEFT(m.rm_prom, AT(" ", m.rm_prom)))
   rm_prom = ALLTRIM(SUBSTR(m.rm_prom, AT(" ", m.rm_prom))) + " "
   poradi_param = m.poradi_param + 1
ENDDO 

fhmac = fopen("views\macro.txt")
IF m.fhmac > 0
     rozbalovat = .F.
     DO WHILE ! feof(m.fhmac)
         mcradek = ALLTRIM(fgets(m.fhmac, 333))
         zacatek_nazvu_makra = AT("#[", m.mcradek)
         konec_nazvu_makra   = AT("]#", m.mcradek)

         DO CASE 
         CASE EMPTY(m.mcradek)
             *nic

         CASE BETWEEN(m.zacatek_nazvu_makra, 1, m.konec_nazvu_makra)
             rozbalovat = BETWEEN(AT(m.makroprikaz, m.mcradek), 3, m.konec_nazvu_makra)
             
         CASE m.rozbalovat
             FOR iii = 1 TO 9
                 IF .not. EMPTY(macroparams(iii))
                     mcradek = STRTRAN(m.mcradek, "<" + TRANSFORM(m.iii, "9") + ">", macroparams(iii))
                 ENDIF 
             ENDFOR 
             rozbalene_makro = m.rozbalene_makro + m.crlf + m.mcradek
         ENDCASE 
     ENDDO
     = fclose(m.fhmac)
     rozbalene_makro = m.rozbalene_makro + m.crlf
ENDIF 
RETURN m.rozbalene_makro
