Main Page | Data Structures | File List | Data Fields | Globals

util.c

Go to the documentation of this file.
00001 /*
00002  * luau (Lib Update/Auto-Update): Simple Update Library
00003  * Copyright (C) 2003  David Eklund
00004  *
00005  * - This library is free software; you can redistribute it and/or             -
00006  * - modify it under the terms of the GNU Lesser General Public                -
00007  * - License as published by the Free Software Foundation; either              -
00008  * - version 2.1 of the License, or (at your option) any later version.        -
00009  * -                                                                           -
00010  * - This library is distributed in the hope that it will be useful,           -
00011  * - but WITHOUT ANY WARRANTY; without even the implied warranty of            -
00012  * - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         -
00013  * - Lesser General Public License for more details.                           -
00014  * -                                                                           -
00015  * - You should have received a copy of the GNU Lesser General Public          -
00016  * - License along with this library; if not, write to the Free Software       -
00017  * - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA -
00018  */
00019 
00020 #ifdef HAVE_CONFIG_H
00021 #  include <config.h>
00022 #endif
00023 
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <stdarg.h>
00027 #include <string.h>
00028 #include <math.h>
00029 
00030 #include <sys/types.h>
00031 #include <sys/stat.h>
00032 #include <unistd.h>
00033 #include <ctype.h>
00034 
00035 #include <glib.h>
00036 #include <zlib.h>
00037 
00038 #include "gcontainer.h"
00039 #include "error.h"
00040 #include "util.h"
00041 
00042 #ifdef WITH_DMALLOC
00043 #  include <dmalloc.h>
00044 #endif
00045 
00046 #ifdef WITH_LEAKBUG
00047 #  include <leakbug.h>
00048 #endif
00049 
00050 
00058 inline int
00059 lutil_streq(const char *str1, const char *str2) {
00060         return (strcmp(str1, str2) == 0);
00061 }
00062 
00071 inline int
00072 lutil_strcaseeq(const char *str1, const char *str2) {
00073         return (strcasecmp(str1, str2) == 0);
00074 }
00075 
00076 
00084 char *
00085 lutil_createString(int length) {
00086         char *str = g_malloc(length+1);
00087         str[0] = '\0';
00088         return str;
00089 }
00090 
00098 char *
00099 lutil_vstrcreate(const char *src1, ...) {
00100         va_list ap;
00101         int size;
00102         char *temp, *dest;
00103         
00104         size = strlen(src1);
00105         dest = g_malloc(size+1); /*lutil_createString(size);*/
00106         if (dest == NULL) {
00107                 ERROR("Couldn't allocate memory for new string: out of memory?");
00108                 return NULL;
00109         }
00110         strcpy(dest, src1);
00111         
00112         va_start(ap, src1);
00113         
00114         while (1) {
00115                 temp = va_arg(ap, char *);
00116                 
00117                 if (temp == NULL) {
00118                         break;
00119                 } else {
00120                         size += strlen(temp)+1;
00121                         dest = (char*) g_realloc(dest, size);
00122                         strcat(dest, temp);
00123                 }
00124         }
00125 
00126         va_end(ap);
00127         
00128         return dest;
00129 }
00130 
00138 char *
00139 lutil_mprintf(const char *template, ...) {
00140         va_list ap;
00141         char *str;
00142         
00143         va_start(ap, template);
00144         str = lutil_valistToString(template, ap);
00145         va_end(ap);
00146         
00147         return str;
00148 }
00149 
00150 /*
00151 char *
00152 lutil_vstrjoin(const char *delim, const char *src1, ...) {
00153         va_list ap;
00154         int size, delimLen;
00155         char *arg, *dest;
00156         
00157         size = strlen(src1)+1;
00158         delimLen = strlen(delim);
00159         
00160         dest = lutil_createString(size);
00161         strcpy(dest, src1);
00162         
00163         va_start(ap, src1);
00164         while (1) {
00165                 arg = va_arg(ap, char *);
00166                 
00167                 
00168                 if (arg == NULL) {
00169                         break;
00170                 } else {
00171                         size += strlen(arg)+delimLen;
00172                         dest = (char*) g_realloc(dest, size);
00173                         strcat(dest, delim);
00174                         strcat(dest, arg);
00175                 }
00176         }
00177 
00178         va_end(ap);
00179         
00180         return dest;
00181 }
00182 */
00183 
00193 char *
00194 lutil_strjoin(const char *delim, const GContainer *strings) {
00195         GIterator iter;
00196         int size, delimLen;
00197         char *currStr, *dest;
00198         
00199         if (strings == NULL) {
00200                 dest = g_strdup("(null)");
00201                 return dest;
00202         } else if (strings->len == 0) {
00203                 return g_strdup("");
00204         } else if (strings->len == 1) {
00205                 currStr = g_container_index(strings, 0);
00206                 return g_strdup(currStr);
00207         } else {
00208                 g_container_get_iter(&iter, strings);
00209                 currStr = g_iterator_next(&iter);
00210                 
00211                 delimLen = strlen(delim);
00212                 size = strlen(currStr) + 1;
00213                 dest = lutil_createString(size);
00214                 strcpy(dest, currStr);
00215                         
00216                 while (g_iterator_hasNext(&iter)) {
00217                         currStr = g_iterator_next(&iter);
00218                         size += strlen(currStr) + delimLen;
00219                         dest = g_realloc(dest, size*sizeof(char));
00220                         strcat(dest, delim);
00221                         strcat(dest, currStr);
00222                 }
00223         }
00224         
00225         return dest;
00226 }
00227 
00239 GContainer *
00240 lutil_gsplit(const char *delim, const char *str) {
00241         GContainer *result;
00242         char *copy, *tok, *holder;
00243         
00244         if (str == NULL) {
00245                 DBUGOUT("Null pointer passed to luau_gsplit");
00246                 return g_container_new(GCONT_PTR_ARRAY);
00247         }
00248         
00249         result = g_container_new(GCONT_PTR_ARRAY);
00250         if (str[0] != '\0') {
00251                 copy = g_strdup(str);
00252                 
00253                 tok = strtok_r(copy, delim, &holder);
00254                 tok = g_strdup(tok);
00255                 g_container_add(result, tok);
00256                 
00257                 while ((tok = strtok_r(NULL, delim, &holder)) != NULL) {
00258                         tok = g_strdup(tok);
00259                         g_container_add(result, tok);
00260                 }
00261                  
00262                 g_free(copy);
00263         }
00264         
00265         return result;
00266 }
00267 
00277 char *
00278 lutil_valistToString(const char *template, va_list args) {
00279         char *curr, test[1];
00280         int len;
00281         
00282         len = vsnprintf(test, 1, template, args);
00283         curr = lutil_createString(len+1);
00284         vsnprintf(curr, len+1, template, args);
00285         
00286         return curr;
00287 }
00288 
00297 gboolean
00298 lutil_findString(const GContainer *array, const char *string) {
00299         GIterator iter;
00300         const char *curr;
00301         gboolean found = FALSE;
00302         
00303         g_container_get_iter(&iter, array);
00304         while (g_iterator_hasNext(&iter)) {
00305                 curr = g_iterator_next(&iter);
00306                 if (lutil_streq(curr, string)) {
00307                         found = TRUE;
00308                         break;
00309                 }
00310         }
00311         
00312         return found;
00313 }
00314 
00328 void
00329 lutil_printIndented(int indent, int length, const char *string) {
00330         char *curr, *prev, *newString, *lastSpace;
00331         int i;
00332         
00333         if (indent >= length)
00334                 indent = 0;
00335         
00336         newString = g_strdup(string);
00337         lastSpace = prev = curr = newString;
00338         while (*curr != '\0') {
00339                 for (i = 0; i < indent; ++i)
00340                         printf(" ");
00341                 
00342                 while (*curr != '\0' && i < length+1) {
00343                         if (*curr == '\n') {
00344                                 lastSpace = curr;
00345                                 break;
00346                         } else if (isblank(*curr)) {
00347                                 lastSpace = curr;
00348                         }
00349                         ++i;
00350                         ++curr;
00351                 }
00352                 
00353                 if (*curr != '\0') {
00354                         if (lastSpace == prev) { /* Found a string longer than indent-length characters */
00355                                 while (! isblank(*curr))
00356                                         ++curr;
00357                                 lastSpace = curr;
00358                         }
00359                         *lastSpace = '\0';
00360                         lastSpace++;
00361                         curr = lastSpace;
00362                 }
00363                 
00364                 printf("%s\n", prev);
00365                 
00366                 prev = lastSpace;
00367         }
00368         
00369         g_free(newString);
00370 }
00371 
00372 gboolean
00373 lutil_isCompletelyBlank(const char *input) {
00374         int i, len;
00375         gboolean blank = TRUE;
00376         
00377         if (input == NULL)
00378                 return TRUE;
00379         else {
00380                 len = strlen(input);
00381                 
00382                 for (i = 0; i < len; i++) {
00383                         if (!isspace(input[i])) {
00384                                 blank = FALSE;
00385                                 break;
00386                         }
00387                 }
00388         }
00389         
00390         return blank;
00391 }
00392 
00393 void
00394 lutil_strToLower(char *str) {
00395         int i, len;
00396         
00397         len = strlen(str);
00398         for (i = 0; i < len; ++i)
00399                 str[i] = tolower(str[i]);
00400 }
00401 
00402 void
00403 lutil_strToUpper(char *str) {
00404         int i, len;
00405         
00406         len = strlen(str);
00407         for (i = 0; i < len; ++i)
00408                 str[i] = toupper(str[i]);
00409 }
00410 
00411 gboolean
00412 lutil_containsAlpha(const char *str) {
00413         int len, i;
00414         
00415         if (str != NULL) {
00416                 len = strlen(str);
00417                 for (i = 0; i < len; ++i) {
00418                         if (isalpha(str[i]))
00419                                 return TRUE;
00420                 }
00421         }
00422         
00423         return FALSE;
00424 }
00425 
00426 gboolean
00427 lutil_containsDigit(const char *str) {
00428         int len, i;
00429         
00430         if (str != NULL) {
00431                 len = strlen(str);
00432                 for (i = 0; i < len; ++i) {
00433                         if (isdigit(str[i]))
00434                                 return TRUE;
00435                 }
00436         }
00437         
00438         return FALSE;
00439 }
00440 
00441 
00442 GString *
00443 lutil_uncompress(GString *data) {
00444         GString *guncompressed;
00445         z_stream z;
00446         char buffer[1025];
00447         int ret;
00448         uLongf count;
00449         
00450         guncompressed = g_string_new(""); /* sized_new(data->len); */
00451         
00452         /*count = 8000;
00453         printf("heeey\n");
00454         ret = uncompress(newbuffer, &count, data->str, data->len);
00455         
00456         if (ret != Z_OK) {
00457                 ERROR("No worky!");
00458                 printf("data error? %s\n", (ret == Z_DATA_ERROR) ? "yes" : "no");
00459         }*/
00460         /*guncompressed = g_string_new_len(newbuffer, 8000);*/
00461         /*return g_strdup(newbuffer);*/
00462         z.zalloc = NULL;
00463         z.zfree =  NULL;
00464         z.opaque = NULL;
00465         
00466         z.next_out  = buffer;
00467         z.avail_out = 1024;
00468         z.next_in   = data->str;
00469         z.avail_in  = data->len;
00470         ret = inflateInit2(&z, 16+MAX_WBITS);
00471         if (ret != Z_OK) {
00472                 ERROR("Couldn't initialize compressed stream: %s: aborting", z.msg);
00473                 g_string_free(guncompressed, TRUE);
00474                 return NULL;
00475         }
00476         
00477         while (1) {
00478                 ret = inflate(&z, Z_SYNC_FLUSH);
00479                 count = 1024 - z.avail_out;
00480                 if (count > 0)
00481                         g_string_append_len(guncompressed, buffer, count);
00482                 if (ret == Z_STREAM_END)
00483                         break;
00484                 else if (ret != Z_OK) {
00485                         ERROR("Decompression error: %s: aborting", z.msg);
00486                         inflateEnd(&z);
00487                         g_string_free(guncompressed, TRUE);
00488                         return NULL;
00489                 }
00490         z.next_out = buffer;
00491         z.avail_out = 1024;
00492     }
00493         
00494         ret = inflateEnd(&z);
00495         if (ret != Z_OK)
00496                 ERROR("Error shutting down zlib stream: %s", z.msg);
00497         
00498         return guncompressed;
00499 }
00500         
00501         
00502 
00503 
00504 
00510 char *
00511 lutil_getTempFilename(void) {
00512         char* filename = lutil_createString(255);
00513         snprintf(filename, 256, "%s/.luau.%d", TEMP_DIR, getpid());
00514         return filename;
00515 }
00516 
00526 gboolean
00527 lutil_fileExists(const char *path) {
00528         struct stat fileinfo;
00529         int ret;
00530         
00531         ret = stat(path, &fileinfo);
00532 
00533         /* technically this will return FALSE when some other error than 'no entity' occurs
00534            (such as "permission denied" or "too many symlinks"), but I think that's the
00535            desired behavior in this case */
00536         return (ret != -1);
00537 }
00538 
00546 gboolean
00547 lutil_isDirectory(const char *path) {
00548         struct stat fileinfo;
00549         int ret;
00550         
00551         ret = stat(path, &fileinfo);
00552         if ((ret == 0) && S_ISDIR(fileinfo.st_mode))
00553                 return TRUE;
00554         else
00555                 return FALSE;
00556 }
00557 
00558 
00568 gint
00569 lutil_gintCompare(gpointer p1, gpointer p2) {
00570         return (gint) lutil_intcmp(* (int*) p1, * (int*) p2);
00571 }
00572 
00582 int
00583 lutil_intcmp(int i1, int i2) {
00584         if (i1 < i2)
00585                 return -1;
00586         else if (i1 > i2)
00587                 return 1;
00588         else
00589                 return 0;
00590 }
00591 
00604 char *
00605 lutil_sizeToString(int size, int sigfig) {
00606         char *newString, suffix;
00607         float n, base;
00608         int len, precision;
00609         
00610         if (size > 1024*1024) {
00611                 suffix = 'M';
00612                 n = (float) size / (1024*1024);
00613         } else if (size > 1024) {
00614                 suffix = 'K';
00615                 n = (float) size / 1024;
00616         } else {
00617                 suffix ='b';
00618                 n = size;
00619         }
00620         
00621         len = ((int) log10(n)) + 1; 
00622         base = pow(10, len-sigfig);
00623         n = ((int) (n / base)) * base;
00624         precision = sigfig - len;
00625         if (precision < 0) precision = 0;
00626         
00627         len = sigfig + 5;
00628         newString = lutil_createString(len);
00629         snprintf(newString, len, "%.*f %c", precision, n, suffix);
00630         
00631         return newString;
00632 }
00633 
00634 

Generated on Mon Apr 12 22:17:11 2004 for luau by doxygen 1.3.2