Atrinik Client 2.5
client/misc.c
Go to the documentation of this file.
00001 /************************************************************************
00002 *            Atrinik, a Multiplayer Online Role Playing Game            *
00003 *                                                                       *
00004 *    Copyright (C) 2009-2011 Alex Tokar and Atrinik Development Team    *
00005 *                                                                       *
00006 * Fork from Daimonin (Massive Multiplayer Online Role Playing Game)     *
00007 * and Crossfire (Multiplayer game for X-windows).                       *
00008 *                                                                       *
00009 * This program is free software; you can redistribute it and/or modify  *
00010 * it under the terms of the GNU General Public License as published by  *
00011 * the Free Software Foundation; either version 2 of the License, or     *
00012 * (at your option) any later version.                                   *
00013 *                                                                       *
00014 * This program is distributed in the hope that it will be useful,       *
00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00017 * GNU General Public License for more details.                          *
00018 *                                                                       *
00019 * You should have received a copy of the GNU General Public License     *
00020 * along with this program; if not, write to the Free Software           *
00021 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             *
00022 *                                                                       *
00023 * The author can be reached at admin@atrinik.org                        *
00024 ************************************************************************/
00025 
00030 #include <global.h>
00031 
00036 unsigned long isqrt(unsigned long n)
00037 {
00038     unsigned long op = n, res = 0, one;
00039 
00040     /* "one" starts at the highest power of four <= than the argument. */
00041     one = 1 << 30;
00042 
00043     while (one > op)
00044     {
00045         one >>= 2;
00046     }
00047 
00048     while (one != 0)
00049     {
00050         if (op >= res + one)
00051         {
00052             op -= res + one;
00053             /* Faster than 2 * one. */
00054             res += one << 1;
00055         }
00056 
00057         res >>= 1;
00058         one >>= 2;
00059     }
00060 
00061     return res;
00062 }
00063 
00072 size_t split_string(char *str, char *array[], size_t array_size, char sep)
00073 {
00074     char *p;
00075     size_t pos;
00076 
00077     if (array_size <= 0)
00078     {
00079         return 0;
00080     }
00081 
00082     if (*str == '\0')
00083     {
00084         array[0] = str;
00085         return 1;
00086     }
00087 
00088     pos = 0;
00089     p = str;
00090 
00091     while (pos < array_size)
00092     {
00093         array[pos++] = p;
00094 
00095         while (*p != '\0' && *p != sep)
00096         {
00097             p++;
00098         }
00099 
00100         if (pos >= array_size)
00101         {
00102             break;
00103         }
00104 
00105         if (*p != sep)
00106         {
00107             break;
00108         }
00109 
00110         *p++ = '\0';
00111     }
00112 
00113     return pos;
00114 }
00115 
00123 void *reallocz(void *ptr, size_t old_size, size_t new_size)
00124 {
00125     void *new_ptr = realloc(ptr, new_size);
00126 
00127     if (new_ptr && new_size > old_size)
00128     {
00129         memset(((char *) new_ptr) + old_size, 0, new_size - old_size);
00130     }
00131 
00132     return new_ptr;
00133 }
00134 
00140 void convert_newline(char *str)
00141 {
00142     char *next, buf[HUGE_BUF * 10];
00143 
00144     while ((next = strstr(str, "\\n")))
00145     {
00146         *next = '\n';
00147         *(next + 1) = '\0';
00148         snprintf(buf, sizeof(buf), "%s%s", str, next + 2);
00149         strcpy(str, buf);
00150     }
00151 }
00152 
00156 void browser_open(const char *url)
00157 {
00158 #if defined(LINUX)
00159     char buf[HUGE_BUF];
00160 
00161     snprintf(buf, sizeof(buf), "xdg-open \"%s\"", url);
00162 
00163     if (system(buf) != 0)
00164     {
00165         snprintf(buf, sizeof(buf), "x-www-browser \"%s\"", url);
00166 
00167         if (system(buf) != 0)
00168         {
00169             LOG(llevBug, "browser_open(): Could not open '%s'.\n", url);
00170         }
00171     }
00172 #elif defined(WIN32)
00173     ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOWDEFAULT);
00174 #else
00175     LOG(llevDebug, "browser_open(): Unknown platform, cannot open '%s'.\n", url);
00176 #endif
00177 }
00178 
00190 int rndm(int min, int max)
00191 {
00192     if (max < 1 || max - min + 1 < 1)
00193     {
00194         LOG(llevBug, "BUG: Calling rndm() with min=%d max=%d\n", min, max);
00195         return min;
00196     }
00197 
00198     return min + RANDOM() / (RAND_MAX / (max - min + 1) + 1);
00199 }
00200 
00208 char *package_get_version_full(char *dst, size_t dstlen)
00209 {
00210 #if PACKAGE_VERSION_PATCH == 0
00211     package_get_version_partial(dst, dstlen);
00212 #else
00213     snprintf(dst, dstlen, "%d.%d.%d", PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCH);
00214 #endif
00215     return dst;
00216 }
00217 
00224 char *package_get_version_partial(char *dst, size_t dstlen)
00225 {
00226     snprintf(dst, dstlen, "%d.%d", PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR);
00227     return dst;
00228 }