gambas-source-code/comp/src/gb.settings/Main.module
Benoît Minisini ba19f3c1dd * Copy https://gambas.svn.sourceforge.net/svnroot/gambas/2.0 to https://gambas.svn.sourceforge.net/svnroot/gambas/gambas
git-svn-id: svn://localhost/gambas/trunk@893 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2007-12-30 16:41:49 +00:00

118 lines
2.6 KiB
Text

' Gambas module file
PUBLIC SUB Main()
DIM hSettings AS Settings
hSettings = NEW Settings("~/.config/gambas/gb.form.conf")
PRINT hSettings["/Bookmarks/Count", 0]
'PRINT hSettings["/test/Array"].Join(" ")
' hSettings["PROJECTLANG"] = "toto"
'hConfig.WriteString("Name", "Benoit")
'hConfig.WriteInteger("Age", 31)
'hConfig.WriteBoolean("Man", TRUE)
'PRINT hSettings["External/Browser", "mozilla"]
END
PUBLIC FUNCTION QuoteString(sVal AS String) AS String
sVal = Replace(sVal, "\n", "\\n")
sVal = Replace(sVal, "\t", "\\t")
sVal = Replace(sVal, Chr$(34), "\\" & Chr$(34))
sVal = Replace(sVal, "\\", "\\\\")
sVal = Chr$(34) & sVal & Chr$(34)
RETURN sVal
END
PUBLIC FUNCTION UnQuoteString(sVal AS String) AS String
IF Left$(sVal) = Chr$(34) THEN sVal = Mid$(sVal, 2, -1)
sVal = Replace(sVal, "\\n", "\n")
sVal = Replace(sVal, "\\t", "\t")
sVal = Replace(sVal, "\\" & Chr$(34), Chr$(34))
sVal = Replace(sVal, "\\\\", "\\")
RETURN 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 Array
IF Left(sVal) = "\"" THEN
RETURN UnQuoteString(sVal)
ELSE IF Left(sVal) = "[" THEN
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