Source

vlocity-vinstall-gambas / src / StsMain.module

Full commit
' Gambas module file
' Gambas class file

'    This file is part of vinstall-ng

'    vinstall-ng is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    vinstall-ng  is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with vinstall-ng.  If not, see <http://www.gnu.org/licenses/>.

'================= SETTINGS CLASS FUNCTIONS =============================


PUBLIC FUNCTION QuoteString(sVal AS String) AS String

  RETURN Quote(sVal)

END

PUBLIC FUNCTION UnQuoteString(sVal AS String) AS String

  RETURN UnQuote(sVal)

END

PUBLIC FUNCTION ToString(vVal AS Variant) AS String

  DIM sVal AS String
  DIM iInd AS Integer

  IF IsString(vVal) THEN
    RETURN QuoteString(vVal)
  ELSE IF IsBoolean(vVal) THEN
    RETURN IIf(vVal, "True", "False")
  ELSE IF IsObject(vVal)             
    IF vVal IS String[] OR IF vVal IS Integer[] OR IF vVal IS Float[] THEN
      sVal = "["                                                          
      FOR iInd = 0 TO vVal.Max                                            
        IF iInd THEN sVal &= ","                                          
        sVal &= ToString(vVal[iInd])                                      
      NEXT                                                                
      sVal &= "]"                                                         
    ELSE                                                                  
      Error.Raise("Unsupported datatype: " & Object.Type(vVal))           
    ENDIF                                                                 
  ELSE                                                                    
    sVal = CStr(vVal)                                                     
    IF IsFloat(vVal) THEN                                                 
      IF InStr(sVal, ".") = "0" THEN sVal = sVal & ".0"                   
    ENDIF                                                                 
  ENDIF                                                                   

  RETURN sVal
             
END          

PUBLIC FUNCTION FromString(sVal AS String) AS Variant

  DIM aVal AS String[]
  DIM iInd AS Integer 
  DIM aVar AS Object  

  IF Left(sVal) = "\"" THEN
    RETURN UnQuoteString(sVal)
  ELSE IF Left(sVal) = "[" THEN 
    IF sVal = "[]" THEN RETURN NULL
    aVal = Split(Mid$(sVal, 2, -1), ",", Chr$(34))
    IF Mid$(sVal, 2, 1) = Chr$(34) THEN           
      RETURN aVal                                 
    ELSE                                          
      SELECT CASE TypeOf(FromString(aVal[0]))     
        CASE gb.Integer                           
          aVar = NEW Integer[aVal.Count]          
        CASE gb.Float                             
          aVar = NEW Float[aVal.Count]            
        CASE ELSE                                 
          RETURN NULL                             
      END SELECT                                  
      FOR iInd = 0 TO aVal.Max
        aVar[iInd] = FromString(aVal[iInd])
      NEXT
      RETURN aVar
    ENDIF
  ELSE IF InStr(sVal, "/") THEN
    RETURN CDate(sVal)
  ELSE IF InStr(sVal, ".") THEN
    RETURN CFloat(sVal)
  ELSE IF LCase(sVal) = "true" THEN
    RETURN TRUE
  ELSE IF LCase(sVal) = "false" THEN
    RETURN FALSE
  ELSE
    RETURN CInt(sVal)
  ENDIF

END
' ============== END OF SETTINGS CLASS ============================