+
+int COM_ReadAndTokenizeLine(const char **text, char **argv, int maxargc, char *tokenbuf, int tokenbufsize, const char *commentprefix)
+{
+ int argc, commentprefixlength;
+ char *tokenbufend;
+ const char *l;
+ argc = 0;
+ tokenbufend = tokenbuf + tokenbufsize;
+ l = *text;
+ commentprefixlength = 0;
+ if (commentprefix)
+ commentprefixlength = strlen(commentprefix);
+ while (*l && *l != '\n')
+ {
+ if (*l > ' ')
+ {
+ if (commentprefixlength && !strncmp(l, commentprefix, commentprefixlength))
+ {
+ while (*l && *l != '\n')
+ l++;
+ break;
+ }
+ if (argc >= maxargc)
+ return -1;
+ argv[argc++] = tokenbuf;
+ if (*l == '"')
+ {
+ l++;
+ while (*l && *l != '"')
+ {
+ if (tokenbuf >= tokenbufend)
+ return -1;
+ *tokenbuf++ = *l++;
+ }
+ if (*l == '"')
+ l++;
+ }
+ else
+ {
+ while (*l > ' ')
+ {
+ if (tokenbuf >= tokenbufend)
+ return -1;
+ *tokenbuf++ = *l++;
+ }
+ }
+ if (tokenbuf >= tokenbufend)
+ return -1;
+ *tokenbuf++ = 0;
+ }
+ else
+ l++;
+ }
+ if (*l == '\n')
+ l++;
+ *text = l;
+ return argc;
+}
+
+// written by Elric, thanks Elric!
+char *SearchInfostring(const char *infostring, const char *key)
+{
+ static char value [256];
+ char crt_key [256];
+ size_t value_ind, key_ind;
+ char c;
+
+ if (*infostring++ != '\\')
+ return NULL;
+
+ value_ind = 0;
+ for (;;)
+ {
+ key_ind = 0;
+
+ // Get the key name
+ for (;;)
+ {
+ c = *infostring++;
+
+ if (c == '\0')
+ return NULL;
+ if (c == '\\' || key_ind == sizeof (crt_key) - 1)
+ {
+ crt_key[key_ind] = '\0';
+ break;
+ }
+
+ crt_key[key_ind++] = c;
+ }
+
+ // If it's the key we are looking for, save it in "value"
+ if (!strcmp(crt_key, key))
+ {
+ for (;;)
+ {
+ c = *infostring++;
+
+ if (c == '\0' || c == '\\' || value_ind == sizeof (value) - 1)
+ {
+ value[value_ind] = '\0';
+ return value;
+ }
+
+ value[value_ind++] = c;
+ }
+ }
+
+ // Else, skip the value
+ for (;;)
+ {
+ c = *infostring++;
+
+ if (c == '\0')
+ return NULL;
+ if (c == '\\')
+ break;
+ }
+ }
+}
+
+
+//========================================================
+// strlcat and strlcpy, from OpenBSD
+
+/*
+ * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/* $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $ */
+/* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */
+
+
+#ifndef HAVE_STRLCAT
+size_t
+strlcat(char *dst, const char *src, size_t siz)
+{
+ register char *d = dst;
+ register const char *s = src;
+ register size_t n = siz;
+ size_t dlen;
+
+ /* Find the end of dst and adjust bytes left but don't go past end */
+ while (n-- != 0 && *d != '\0')
+ d++;
+ dlen = d - dst;
+ n = siz - dlen;
+
+ if (n == 0)
+ return(dlen + strlen(s));
+ while (*s != '\0') {
+ if (n != 1) {
+ *d++ = *s;
+ n--;
+ }
+ s++;
+ }
+ *d = '\0';
+
+ return(dlen + (s - src)); /* count does not include NUL */
+}
+#endif // #ifndef HAVE_STRLCAT
+
+
+#ifndef HAVE_STRLCPY
+size_t
+strlcpy(char *dst, const char *src, size_t siz)
+{
+ register char *d = dst;
+ register const char *s = src;
+ register size_t n = siz;
+
+ /* Copy as many bytes as will fit */
+ if (n != 0 && --n != 0) {
+ do {
+ if ((*d++ = *s++) == 0)
+ break;
+ } while (--n != 0);
+ }
+
+ /* Not enough room in dst, add NUL and traverse rest of src */
+ if (n == 0) {
+ if (siz != 0)
+ *d = '\0'; /* NUL-terminate dst */
+ while (*s++)
+ ;
+ }
+
+ return(s - src - 1); /* count does not include NUL */
+}
+
+#endif // #ifndef HAVE_STRLCPY