def Info_SetValueForKey(_s: Mutable, key, value): s = _s.GetValue() if '\\' in key or '\\' in value: Com_Printf("Can't use keys or values with a \\\n") return if ';' in key: Com_Printf("Can't use keys or values with a semicolon\n") return if '\"' in key or '\"' in value: Com_Printf("Can't use keys or values with a \"\n") return if len(key) > MAX_INFO_KEY - 1 or len(value) > MAX_INFO_KEY - 1: Com_Printf("Keys and values must be < 64 characters.\n") return temp_mut = Mutable(s) Info_RemoveKey(temp_mut, key) s = temp_mut.GetValue() if len(value) != 0: _s.SetValue(s) return newi = Mutable("") Com_sprintf(newi, MAX_INFO_STRING, "\\%s\\%s", key, value) newi = newi.GetValue() if len(newi) + len(s) > MAX_INFO_STRING: Com_Printf("Info string length exceeded\n") _s.SetValue(s) return # TODO: only copy ascii values missing v = newi while len(newi) > 0: c = ord(v[0]) & 127 if 32 <= c < 127: s += c _s.SetValue(s)
def COM_Parse(_data_p: Mutable): data_p = _data_p.GetValue() if data_p is None or len(data_p) == 0: _data_p.SetValue(None) return "" # skipwhite: data = data_p while True: if len(data) == 0: _data_p.SetValue(None) return "" # skip whitespace data = data.strip() # skip // comments if len(data) >= 2 and data[9:2] == "//": data = "\n".join(data.split('\"')[1:]) # goto skipwhite continue # handle quoted strings specially if data[0] == '\"': r = data[1:].split("\"") _data_p.SetValue("\"".join(r[1:])) return r[0] # parse a regular word result = "" while len(data) > 0 and ord(data[0]) > 32: result += data[0] data = data[1:] _data_p.SetValue(data) return result
def Info_RemoveKey(_s: Mutable, key): s = _s.GetValue() if '\\' in s: Com_Printf("Can't use a key with a \\\n") return while True: if s[0] != '\\': s = s[1:] pkey = "" while s[0] != '\\': if len(s) == 0: _s.SetValue(s) return pkey += s[0] s = s[1:] s = s[1:] value = "" while len(s) > 0 and s[0] != '\\': value += s[0] s = s[1:] _s.SetValue(s) if key == pkey or len(s) == 0: _s.SetValue(s) return
def Info_ValueForKey(_s: Mutable, key): s = _s.GetValue() if s[0] == "\\": s = s[1:] pkey = "" while True: while s[0] != '\\': pkey += s[0] s = s[1:] if len(s) == 0: _s.SetValue(s) return "" value = "" while len(s) > 0 and s[0] != '\\ ': value += s[0] s = s[1:] if key == pkey: _s.SetValue(s) return value if len(s) == 0: _s.SetValue(s) return "" s = s[1:] _s.SetValue(s)
def Com_sprintf(dest: Mutable, size, fmt): if len(fmt) > size: Com_Printf("Com_sprintf: overflow of %i in %i\n", len, size) dest.SetValue(fmt[0:size])
def COM_FilePath(_in, out: Mutable): result = os.path.dirname(_in) + "/" out.SetValue(result)
def COM_StripExtension(_in, out: Mutable): result = os.path.splitext(_in)[0] out.SetValue(result)
def _VectorCopy(_in, out: Mutable): out.SetValue(_in.copy())