Attribute VB_Name = "ObjMdl"
Option Explicit

Public Enum IMFERRORCODE
    IME_OK = 0
    
    IME_NODDID = 10
    IME_NULLDDID = 11
    IME_NULLLPDDID = 12
    IME_NULLLPHANDLE = 13
    IME_NOGLOBAL = 16
    IME_CREATENULLNAME = 17
    IME_BADID = 18
    IME_NAMETOOLONG = 19
    
    IME_DIAGFINDFAILED = 20
    IME_DIAGOPENFAILED = 21
    IME_DIAGDUP = 22
    IME_DIAGCREATEFAILED = 23
    IME_DIAGLOADFAILED = 24
    IME_DIAGSAVEFAILED = 25
    IME_DIAGCLOSEFAILED = 26
    IME_DIAGBADHANDLE = 27
    
    IME_DIAGNOCURRENT = 30
    
    IME_DIAGBADSIGNATURE = 35
    IME_DIAGBADPROPSET = 38
    
    IME_SYMFINDFAILED = 40
    IME_SYMOPENFAILED = 41
    IME_NORELATION = 42
    
    IME_SYMCREATEFAILED = 43
    IME_SYMLOADFAILED = 44
    IME_SYMSAVEFAILED = 45
    IME_SYMCLOSEFAILED = 46
    IME_SYMBADHANDLE = 47
    IME_SYMNOMEMORY = 50
    IME_SYMSEGNOMEMORY = 51
    
    IME_SYMBADSIGNATURE = 55
    IME_SYMDUPLICATE = 56
    
    IME_DEFFINDFAILED = 60
    IME_DEFOPENFAILED = 61
    IME_DEFDUP = 62
    IME_DEFCREATEFAILED = 63
    IME_DEFLOADFAILED = 64
    IME_DEFSAVEFAILED = 65
    IME_DEFCLOSEFAILED = 66
    IME_DEFBADHANDLE = 67
    IME_DEFADDFAILED = 70
    IME_DEFMODFAILED = 71
    IME_DEFSETATTRFAILED = 72
    IME_DEFNOSUCHATTR = 73
    IME_DEFDELFAILED = 74
    
    IME_DEFBADSIGNATURE = 75
    
    IME_UPDATEDICTFAILED = 80
    IME_SETPATHFAILED = 81
    IME_GETPATHINVALID = 82
    IME_NOFIRSTSEGMENT = 90
    IME_NOLASTSEGMENT = 91
    IME_BADCONNECTCODE = 92
    IME_ADDCONFAILED = 93
    IME_NOCONNECTION = 94
    IME_WRONGCONNECTION = 95
    IME_NOMATCHSEGMENT = 96
    
    IME_ENCYLOCKFAILED = 100
    IME_ENCYOPENFAILED = 101
    IME_ENCYREPOSFAILED = 102
    IME_ENCYGNFAILED = 103
    IME_ENCYGFFAILED = 104
    IME_CLOSEALLFAILED = 105
    IME_NULLHANDLE = 106
    IME_NOPERMISSION = 107
    
    IME_ADDRELFAILED = 120
    IME_RELFINDFAILED = 121
    IME_NOTSAMENULLNESS = 122
    
    IME_GETSAVEFAILED = 140
    
    IME_PNLNOPANEL = 160
    IME_PNLNOFILE = 161
    
    IME_DEVICECONTEXT = 170
    IME_BITMAPTOOBIG = 171
    IME_FIELDTOOSHORT = 172
    
    IME_PARSEERROR = 180
    
    IME_BADUIPTR = 199
    IME_BADOBJECTCLASS = 198
    IME_NOTAHIERARCHY = 175
    IME_NOTACHILD = 176
    IME_NOTAPARENT = 177
    
    IME_UNKNOWNOBJECT = 190
    IME_INVALIDVALUE = 191
    
    IME_ERROR = 192
    IME_RETURNFILENAMETOOLONG = 200
    IME_FILEALREADYEXISTS = 201
    IME_FILENOTFOUND = 202
    IME_FILETOOLARGE = 203

    IME_NOSUCHREPORT = 204
    IME_UNABLETOWRITEOUTPUT = 205
    IME_UNABLETOGENERATEREPORT = 206
    IME_INVALIDREPORT = 207
    IME_UNABLETOTRANSFORMXML = 208
    IME_INVALIDOUTPUTFILE = 209
    IME_BADPARAMETER = 210
    IME_EMPTYREPORT = 211
    
    IME_BADPARSE = 212
    IME_XSLUNABLETOCREATEDOC = 213
    IME_XSLBADLYFORMEDXML = 214
    IME_XSLUNABLETOCREATETEMPLATE = 215
    IME_XSLERRORINSTYLESHEET = 216
    IME_MISSINGSTYLESHEET = 217
    IME_REPORTALREADYRUNNING = 218
    IME_BADLYDECLAREDDEFINES = 219
    IME_SUBREPORTSNOTALLOWED = 220
    IME_UNABLETOLOCKALLITEMS = 222
    IME_MISSINGREPORTCLASS = 223
    IME_MISSINGREPORTTYPE = 224
    IME_MISSINGREPORTID = 225
    IME_SCREENREPORTINUSE = 226
    IME_SAISBUSY = 227
    IME_PLUGGUIDFAILED = 228
    IME_PLUGASGUIDFAILED = 229
    IME_MISSINGKEYPROPERTIES = 230
    IME_FAILEDTOLOCKMIRRORS = 231
    IME_EXTRACTFILECANTCREATE = 232
    IME_EXTRACTFILECANTWRITETO = 233
    IME_RENAMEREKEYFAILED = 234
    IME_FAILEDTOADDMIRRORS = 235
    IME_DEFSETATTROVERRUNSMEMO = 236
    IME_CONCRETEABSTRACTCROSSREF = 237
    IME_PROPREFNOTINCONNECTS = 238
End Enum


Private g_intRevertQuoting As Integer

'encyclopedia link
Public Function IMF_Error(ern As IMFERRORCODE) As IMFERRORCODE
    IMF_Error = ern
    If ern = 0 Then Exit Function
    
    Err.Raise ern + SAERR_SA20001_IMF_ERROR + vbObjectError, SAXRes.GetLocalString(IDS_sa2001objmdlbas_____saobjectmodel), SAXRes.GetLocalString(IDS_sa2001objmdlbas_____saimferror) + " (" + CStr(ern) + ")"
    
End Function

Public Function RaiseError(ern As Long, Module As String, Description As String)
    
    Dim lngErr As Long
    
    lngErr = (ern And &HFFFF0000)     ' Mask out the Loword to check the hiword (vbObjectError is only in the hiword)
    If lngErr = 0 Or lngErr = vbObjectError Then
        ' The ern passed in was a valid SA error number or an SA error number + vbobjecterror
        Err.Raise ern Or vbObjectError, SAXRes.GetLocalString(IDSProductAppName) & SA_VB_FullStop & Module, Description
    Else
        Err.Raise SAERR_UNKNOWNERROR Or vbObjectError, SAXRes.GetLocalString(IDSProductAppName) & SA_VB_FullStop & Module, SAXRes.GetLocalString(IDS_sa2001objmdlbas_____unknownerror)
    End If
    
End Function

Public Function GetTF(b As Boolean) As String
    If b Then
        GetTF = "T" 'DEV_STRING
    Else
        GetTF = "F" 'DEV_STRING
    End If
End Function

Public Function TestTF(s As String) As Boolean
    If s = "T" Or s = "1" Or s = "-1" Then 'DEV_STRING "1" has been observed from Diagram.locked
        TestTF = True
    Else
        TestTF = False
    End If
End Function

Public Function QuoteIfNeeded(Str As String) As String
    If g_intRevertQuoting = 0 Then  ' TODO: Remove this after 11.4.1.0 as we should not need to revert
        Dim strRet As String
        ' Not initialized - assume we are not going to revert
        g_intRevertQuoting = 1
        Call SAGetSetting("RevertQuotingChange", strRet, 1, "SystemArchitect")  ' DEV_STRING
        If UCase(strRet) = "Y" Then     ' DEV_STRING
            g_intRevertQuoting = 2  ' Do Reversion
        End If
    End If

    If g_intRevertQuoting = 2 Then  ' TODO: Remove this after 11.4.1.0 as we should not need to revert
        If Left(Str, 1) = Chr(34) And Right(Str, 1) = Chr(34) Then
            ' assume string is quoted already
            QuoteIfNeeded = Str
        Else
            If InStr(Str, " ") <> 0 Or InStr(Str, ".") <> 0 _
                Or InStr(Str, "/") <> 0 Or InStr(Str, "+") <> 0 _
                Or InStr(Str, ",") <> 0 Or InStr(Str, "\r") <> 0 _
                Or InStr(Str, "\n") <> 0 Or InStr(Str, "\t") <> 0 _
                Or InStr(Str, ";") <> 0 Or InStr(Str, "[") <> 0 _
                Or InStr(Str, "{") <> 0 Or InStr(Str, "(") <> 0 _
                Or InStr(Str, ")") <> 0 Or InStr(Str, "}") <> 0 _
                Or InStr(Str, "]") <> 0 Or InStr(Str, "@") <> 0 _
                Or InStr(Str, "'") <> 0 Or InStr(Str, "\") <> 0 _
                Or InStr(Str, "*") <> 0 Or InStr(Str, "|") <> 0 _
                Or InStr(Str, "`") <> 0 Then 'DEV_STRING
                QuoteIfNeeded = Chr(34) & Str & Chr(34)
            Else
                QuoteIfNeeded = Str
            End If
        End If
    Else
        QuoteIfNeeded = SAUtilities.SAQuoteIfNeeded(Str)
    End If
End Function

Public Function UnQuote(Str As String) As String
    If Left(Str, 1) = Chr(34) And Right(Str, 1) = Chr(34) Then
        ' string is quoted
        UnQuote = Mid(Str, 2, Len(Str) - 2)
    Else
        ' string is not quoted
        UnQuote = Str
    End If
End Function

Public Function CountChars(Str As String, Char As String) As Long
    Dim pos As Long
    pos = 1
    CountChars = 0
    Do While pos <> 0
        pos = InStr(pos, Str, Char, vbTextCompare)
        If pos <> 0 Then
            CountChars = CountChars + 1
            pos = pos + 1 ' bump past it
        End If
    Loop
End Function

Public Function XMLEncode(Str As String) As String
    Dim pos As Long
    Dim strRet As String
    strRet = strReplace(Str, "&", "&amp;") 'DEV_STRING
    strRet = strReplace(strRet, ">", "&gt;") 'DEV_STRING
    strRet = strReplace(strRet, "<", "&lt;") 'DEV_STRING
    XMLEncode = strReplace(strRet, """", "&quot;") 'DEV_STRING
End Function

Private Function strReplace(str_In As String, str_Comp As String, str_Replace As String) As String
    Dim pos As Long
    Dim Strtmp As String
    pos = InStr(str_In, str_Comp)
    If pos = 0 Then
        strReplace = str_In
        Exit Function
    End If
    Strtmp = str_In
    Do While pos <> 0
        Strtmp = Left(Strtmp, pos - 1) & str_Replace & Mid(Strtmp, pos + Len(str_Comp))
        pos = InStr(pos + Len(str_Replace), Strtmp, str_Comp)
    Loop
    strReplace = Strtmp
End Function

