]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/common/util.qc
Merge remote branch 'origin/fruitiex/csqc_polyblend'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / util.qc
index 1efc0e3738a06a4d9e5c33cd5681571c0d1b0444..8c87b7fdad4f0e81a26e66633d9bb9032c7020a8 100644 (file)
@@ -309,7 +309,8 @@ float db_load(string pFilename)
        fh = fopen(pFilename, FILE_READ);
        if(fh < 0)
                return db;
-       if(stof(fgets(fh)) == DB_BUCKETS)
+       l = fgets(fh);
+       if(stof(l) == DB_BUCKETS)
        {
                i = 0;
                while((l = fgets(fh)))
@@ -321,14 +322,18 @@ float db_load(string pFilename)
        }
        else
        {
-               // different count of buckets?
+               // different count of buckets, or a dump?
                // need to reorganize the database then (SLOW)
-               while((l = fgets(fh)))
+               //
+               // note: we also parse the first line (l) in case the DB file is
+               // missing the bucket count
+               do
                {
                        n = tokenizebyseparator(l, "\\");
                        for(j = 2; j < n; j += 2)
                                db_put(db, argv(j-1), uri_unescape(argv(j)));
                }
+               while((l = fgets(fh)));
        }
        fclose(fh);
        return db;
@@ -1935,3 +1940,54 @@ vector NearestPointOnBox(entity box, vector org)
        return nearest;
 }
 #endif
+
+float vercmp_recursive(string v1, string v2)
+{
+       float dot1, dot2;
+       string s1, s2;
+       float r;
+
+       dot1 = strstrofs(v1, ".", 0);
+       dot2 = strstrofs(v2, ".", 0);
+       if(dot1 == -1)
+               s1 = v1;
+       else
+               s1 = substring(v1, 0, dot1);
+       if(dot2 == -1)
+               s2 = v2;
+       else
+               s2 = substring(v2, 0, dot2);
+
+       r = stof(s1) - stof(s2);
+       if(r != 0)
+               return r;
+
+       r = strcasecmp(s1, s2);
+       if(r != 0)
+               return r;
+
+       if(dot1 == -1)
+               if(dot2 == -1)
+                       return 0;
+               else
+                       return -1;
+       else
+               if(dot2 == -1)
+                       return 1;
+               else
+                       return vercmp_recursive(substring(v1, dot1 + 1, 999), substring(v2, dot2 + 1, 999));
+}
+
+float vercmp(string v1, string v2)
+{
+       if(strcasecmp(v1, v2) == 0) // early out check
+               return 0;
+
+       // "git" beats all
+       if(v1 == "git")
+               return 1;
+       if(v2 == "git")
+               return -1;
+
+       return vercmp_recursive(v1, v2);
+}