[Gambas-user] Eval

Steven Lobbezoo steven at ...1652...
Tue Apr 17 23:29:19 CEST 2007


Ok, here's part, as You can see I'm working on your idea with open office

Regards


Le mardi 17 avril 2007 23:22, Benoit Minisini a écrit :
> On mardi 17 avril 2007, Steven Lobbezoo wrote:
> > Hi,
> > I try to user Eval as follows :
> >
> > HString = Eval(Var22)
> >
> > Where Var22 is a string with "Mglobal.U_Vra"  in it
> >
> > In MGlobal.U_Vra is a string of characters ("test test test")
> >
> > Whatever I do, i get an empty string in HString.
> > Also if I make a breakpoint, and look in the vars, they are empty (exept
> > Var22, this is correctly filled).
> >
> > Any ideas ?
> > Thanks
> > Steven
>
> Please send your project, or if possible a piece of your project that shows
> the problems.
>
> Otherwise it is impossible for me to catch the bug!
>
> Regards,
-------------- next part --------------
' Gambas module file

PUBLIC db AS NEW Connection
PUBLIC rs AS Result
PUBLIC rs1 AS Result

PUBLIC btnok AS String
PUBLIC btnno AS String
PUBLIC Connected AS Integer

' the tree fields of the question form
PUBLIC QFld1 AS String
PUBLIC QFld2 AS String
PUBLIC QFld3 AS String

' these fields define the current user !
PUBLIC U_No AS Integer
PUBLIC U_Nom AS String
PUBLIC U_Agency AS Integer
PUBLIC U_AgenNom AS String
PUBLIC U_AgenRep AS String
PUBLIC U_AgenChef AS Integer ' the boss of the person logged in
PUBLIC U_Level AS Integer ' the level code of the person logged in
PUBLIC U_Mail AS String ' the e-mail of the user

PUBLIC V_Code AS Integer ' the current ville, selected from villes
PUBLIC M_Code AS String ' the current mandat code (new ?)
PUBLIC M_Sel_X AS Integer
PUBLIC M_Sel_Y AS Integer

PUBLIC F_Ch AS NEW String[]

PUBLIC FPath AS String = USER.Home ' the last path of the fileselector, remember this

PUBLIC JobTbl AS NEW Collection ' all Jobs available

PUBLIC Rev_Web AS String ' base path to the revimmo website


''''''''''''''''''''''''''''''''''
''sub to connect to mysql server''
''''''''''''''''''''''''''''''''''
PUBLIC FUNCTION conn(host AS String, lgn AS String, pass AS String) AS Boolean
  db.close
  db.Type = "mysql"
  Connected = FALSE
  db.Host = host
  db.Login = lgn
  db.Password = pass
  db.Open
  Connected = TRUE
  RETURN TRUE
CATCH
  Message.error(Error.text)
  RETURN FALSE
END

''''''''''''''''''''''''''''''''''''''''
''sub to fill the tableview w/ records''
''''''''''''''''''''''''''''''''''''''''
PUBLIC SUB fill_view(tbv AS GridView, qry AS String)
DIM i AS Float
  rs1 = db.exec(qry)
  WITH rs1
    tbv.rows.count = 0
    IF .count <> 0 THEN
      tbv.columns.count = .fields.count
      tbv.rows.count = .count
    END IF
  END WITH
END

PUBLIC SUB set_jobs()
     Mglobal.JobTbl.Add("1", "Négociatrice")
     Mglobal.JobTbl.Add("2", "Chef d'agence")
     Mglobal.JobTbl.Add("3", "Chef secteur")
     Mglobal.JobTbl.Add("4", "Directeur")
     Mglobal.JobTbl.Add("5", "Suppliant")
     Mglobal.JobTbl.Add("6", "Carte seul")
     Mglobal.JobTbl.Add("7", "Sécretaire")
     Mglobal.JobTbl.Add("8", "Comptable")
     Mglobal.JobTbl.Add("9", "Diverse")
END

-------------- next part --------------
' Gambas module file

' Here we handle all xfers to and from open office files 
' we have to do unzip, replace and zip into a new file
' we have to replace text items and images
' we have to insert images (replace really)
' the structure of an Oofile (unzipped) is analysed and data is transferred
' the tags are of the form {{TagType:String}}
' where Tagtype =   'db:'   use DbFieldType
'                   'cu:'   Currency
'                   'im:'   Image
'                   'tb:'   Use translate table
'                   'fo:'   Form (use + - / etc. for numeric database fields, all Mysql expressions are ok)
'                   'ft:'   Formatted text (nl etc !)
'                   'li:'   Litteral (program variable)
'
' The Mtranslate table is as followes :
' FieldName (VarChar) = Name of the query field
' FieldVal (VarChar) = Value of the field to translate
' Replacement (VarChar) = What to print (can be one of the above enumerated types, or just a value)
'
'
' We call this as OOInsert( Input file name, Output filename, SQl query string) -> returns TRUE or FALSE
' All the other parms are in the document.
' In case of an image it's in the image-name field (so, you have to put in a 'dummy' image)
'
' Attention : we presume someone opened a database db publicly
'
'


RsA AS Result '     the result of the databse quiry (if there are more records, you'll have more forms filles)
Fi AS File '        Input file
Fo AS File '        Output file
MYes AS String = "Oui"
MNo AS String = "Non"
sData AS String '   The input string of the XML file (the whole file)
FNam AS String '    The name of the temporary (work) file
MyDir AS String '   The working directory name
OSql AS String '    The original Quiry asked for

PUBLIC FUNCTION OOInsert(IFile AS String, OFile AS String, Sql AS String) AS Integer
DIM ZipCmd AS String
DIM Ctr AS Integer

  ' Main function, called from the outside
  ' IFile is the input openoffice text file name, OFile is the output target filename wanted
  ' here we do the main tasks, we will devide afterwards
  ' we presume that someone opened a database db
  
  RsA = db.Exec(Sql)
  IF NOT RsA.available THEN   
    message.Error(" There must be something wrong in your SQL statement:\n" & Sql & "\nor your database is not open", "ok")
    RETURN FALSE
  END IF
  IF NOT Access(IFile, gb.READ) THEN 
    message.Error(" There must be something wrong in your Filename:\n" & IFile & "\nI cannot read it", "ok")
    RETURN FALSE
  END IF  
  OSql = Sql ' save me, you never know !
  FOR EACH RsA
    ' so we have our tools, lets try it
    ' we use unzip, since it knows how to handle directory structures
    RANDOMIZE
    FNam = Str(CInt(Rnd(100000000, 2000000000)))
    MyDir = USER.Home & "/" & FNam
    MKDIR MyDir
    ZipCmd = "unzip -d " & MyDir & "  " & IFile
    ' lets do the unzip
    SHELL ZipCmd WAIT 
    ' we have the structure unpacked now
    ' we have the following files to consider :
    ' content.xml = main content (text) with the tags
    ' ./Pictures directory with the images
    ' We start to read the content into a string 
    sData = File.Load(MyDir & "/content.xml")
    sData = DoLine(sData) ' this is where the work is done
    File.Save(MyDir & "/content.xml", sData)
    ' that main file is updated, we presume allso that all interesting photos are replaced in the Pictures directory
    ' and now we re-compress all into an odt (zip) file
    SHELL "cd " & MyDir & ";zip -r " & FNam & ".odt *" WAIT
    ' ok, he's there now, lets move um and delete the working directory
    IF Ctr = 0 THEN
      SHELL "cp " & MYDir & "/" & FNam & ".odt " & OFile WAIT
    ELSE 
      SHELL "cp " & MYDir & "/" & FNam & "_" & Str(Ctr) & ".odt " & OFile WAIT    
    END IF 
    SHELL "rm -rf " & MyDir ' we don't wait for this to end
    Ctr = Ctr + 1
  NEXT
  RETURN TRUE

'CATCH
'  Message.Error(Error.Text)    
'    SHELL "rm -rf " & MyDir ' try to clean up at least
'  RETURN FALSE
END
 
PRIVATE FUNCTION DoLine(sData AS String) AS String
DIM s, e AS Integer
DIM Tmp AS String
DIM Tag AS String[]

  ' we do the replace/find data 
  Tmp = sData
  s = InStr(Tmp, "{{")
  WHILE s > 0
    e = InStr(Tmp, "}}", s)
    Tag = Split(Mid(Tmp, s + 2, e - s - 2), ":")
    Tmp = Mid(Tmp, 1, s - 1) & DoField(Tag) & Mid(Tmp, e + 2)
    s = InStr(Tmp, "{{")
  WEND
  RETURN Tmp
END

PRIVATE FUNCTION DoField(Tag AS String[]) AS String
DIM Rstr AS String ' the return string
DIM tmp AS String
DIM RsX AS Result
DIM TTag AS String[]

  ' we have the field to translate in tag, lets analyse it
  ' 0 is the command, 1 the value
  SELECT Tag[0]
  CASE "db" ' we take the field from the database, and try to print it as nice as possible
    SELECT RsA.Fields[Tag[1]].Type
    CASE db.Boolean
      IF RsA[Tag[1]] THEN
        Rstr = MYes
      ELSE  
        Rstr = MNo
      END IF
    CASE db.integer
      Rstr = Format$(RsA[Tag[1]], "(#.#)")
    CASE db.float
      Rstr = Format$(RsA[Tag[1]], "($,#)")
    CASE db.date
      Rstr = Format$(RsA[Tag[1]], "d mmm yyyy")   
    CASE db.string
      Rstr = XMLCor(RsA[Tag[1]])
    CASE db.serial
      Rstr = Str(RsA[Tag[1]])
    CASE ELSE ' suppose this can be a blob or a string, w'll handle it as strings
      Rstr = XMLCor(RsA[Tag[1]])    
    END SELECT  
  CASE "cu" '                                   currency "($,#.###)"
    Rstr = Format$(Str(RsA[Tag[1]]), "($,#.###)")
  CASE "ft" '                                   formatted text
      Rstr = XMLCor(RsA[Tag[1]])    
  CASE "im" '                                   image
    DoPhotos(Tag)
    Rstr = Tag[1] 
  CASE "li" '                                   Literal program variable
    Rstr = XMLCor(Eval(Tag[1]))
'    message.Info(Tag[1])
    RStr = Mglobal.U_AgenNom
  CASE "tb" '                                   translate table
    ' we read the table with Tag and value as the two keys
    RsX = db.Exec("Select * from MTranslate where FieldName = '" & Tag[1] & "' AND FieldVal = '" & Str(RsA[Tag[1]]) & "'")
    IF RsX.Available THEN
      RStr = XMLCor(RsX!Replacement)
      IF InStr(Rstr, ":") THEN 
        RStr = DoField(Split(Rstr, ":")) ' we go deeeeep here, lets take the risc !
      END IF
    ELSE 
      RStr = ""
    END IF
  CASE "fo" '                                   we have to calculate something
    ' we have a string with + - / * etc in it and field names
    ' we adapt the first query, to add this calculation (field names must be ok !)
    ' we presume that the first query is like 'Select blablabla'
    Tmp = "Select ( " & Replace$(Tag[1], "'", "'") & " ) as tmpnam2334, " & Mid(Osql, 7)
TRY RsX = db.Exec(Tmp)
    IF RsX.available THEN 
      Rstr = XMLCor(RsX!tmpnam2334)
    ELSE 
      RStr = ""
    END IF
  CASE ELSE
    ' we just try to give back the field (like string) after translation for xml  
      Rstr = XMLCor(RsA[Tag[1]])    
  END SELECT 
  RETURN Rstr
END


PRIVATE SUB DoPhotos(Tag AS String[])
    ' the problem is that there are more then one per document / record
    ' we will just do it in sequence : first photo = first blobfield, second = 2ième etc.
    ' the images in the xml files are as : href="Pictures/100000000000019D000001DC284D3F35.jpg" 
DIM s, e, b AS Integer
DIM Tmp AS String
DIM Tag2 AS String[]
DIM hResultField AS ResultField

  Tmp = sData
  s = InStr(Tmp, "{{" & Tag[0] & ":" & Tag[1] & "}}")
  s = InStr(Tmp, "href=\"Pictures/", s)
  e = InStr(Tmp, " ", s)
  Tag2 = Split(Mid(Tmp, s + 15, e - s - 15), ".")
    ' we donnot change the text, we just move the picture in place
    ' the filedname is in Tag[1]
  IF RsA.Fields[Tag[1]].Type = db.Blob THEN ' we have one direct
    
  ELSE 'we have a filename
      
  END IF    
  RETURN  
END

PRIVATE FUNCTION XMLCor(Inp AS String) AS String
DIM tmp AS String
'scan a string and prepare it for use with XML  
  Tmp = Replace$(Inp, "'", "'") '          Apostrophes (') are NOT allowed
  Tmp = Replace$(Inp, "\"", """) '         Double quotes neither
  Tmp = Replace$(Tmp, Chr$(9), "&#x09;") '      Horizontal tabs
  Tmp = Replace$(Tmp, Chr$(10), "&#x0A;") '     Line feeds
  Tmp = Replace$(Tmp, Chr$(13), "") '           No carriage returns
    
  RETURN Tmp
END




More information about the User mailing list