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

util.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>
#include <glib.h>
#include <zlib.h>
#include "gcontainer.h"
#include "error.h"
#include "util.h"

Go to the source code of this file.

Functions

int lutil_streq (const char *str1, const char *str2)
 Check if two strings are equal.

int lutil_strcaseeq (const char *str1, const char *str2)
 Check if two strings are equal, ignoring case.

char * lutil_createString (int length)
 Create a new string.

char * lutil_vstrcreate (const char *src1,...)
 Append any number of strings together into one.

char * lutil_mprintf (const char *template,...)
 Like sprintf, but allocate memory for and return the string.

char * lutil_strjoin (const char *delim, const GContainer *strings)
 Join an array of strings together separated by the given delimiter.

GContainerlutil_gsplit (const char *delim, const char *str)
 Split a string by character delim into a GContainer.

char * lutil_valistToString (const char *template, va_list args)
gboolean lutil_findString (const GContainer *array, const char *string)
 See if a string exists in a GContainer of strings (char *).

void lutil_printIndented (int indent, int length, const char *string)
 Print a string to STDOUT indented and limited in per-line length.

gboolean lutil_isCompletelyBlank (const char *input)
 Check if a string is completely whitespace.

void lutil_strToLower (char *str)
 Convert all upper case letters to lower case in a string.

void lutil_strToUpper (char *str)
gboolean lutil_containsAlpha (const char *str)
gboolean lutil_containsDigit (const char *str)
GString * lutil_uncompress (GString *data)
char * lutil_getTempFilename (void)
 Create a valid temporary filename.

gboolean lutil_fileExists (const char *path)
 Check if a file exists.

gboolean lutil_isDirectory (const char *path)
 Check if a "file" is actually a directory.

gint lutil_gintCompare (gpointer p1, gpointer p2)
 Compare two gint pointers.

int lutil_intcmp (int i1, int i2)
 Compare two ints.

char * lutil_sizeToString (int size, int sigfig)


Function Documentation

gboolean lutil_containsAlpha const char *  str  ) 
 

Definition at line 412 of file util.c.

Referenced by luau_versioncmp().

gboolean lutil_containsDigit const char *  str  ) 
 

Definition at line 427 of file util.c.

char* lutil_createString int  length  ) 
 

Create a new string.

Allocate memory for a string of length length+1. One is added to account for the NIL character at the end of a string.

  • length is the length of the new string
    Returns:
    a newly allocated string (must be free'd)

Definition at line 85 of file util.c.

Referenced by luau_install_deb(), luau_install_rpm(), luau_multPackageTypeString(), lutil_getTempFilename(), lutil_sizeToString(), lutil_strjoin(), and lutil_valistToString().

gboolean lutil_fileExists const char *  path  ) 
 

Check if a file exists.

Use stat(2) to check if the given file exists. Note that this function will return TRUE if the path is actually a directory, but FALSE if an error such as "permission denied" or "too many symlinks" is returned, or if the file in question is a symlink to a non-existent file. Use stat(2) directly if more precision is necessary.

  • path is the path to check for existence
    Returns:
    whether the file in question exists

Definition at line 527 of file util.c.

gboolean lutil_findString const GContainer array,
const char *  string
 

See if a string exists in a GContainer of strings (char *).

Find if a certain string is contained in the given array. Does not ignore case, and does not ignore whitespace (or anything else).

  • array is the array to look in
  • string is the string to look for
    Returns:
    whether the string could be found

Definition at line 298 of file util.c.

References g_container_get_iter(), g_iterator_hasNext(), g_iterator_next(), and lutil_streq().

Referenced by luau_checkKeyword().

char* lutil_getTempFilename void   ) 
 

Create a valid temporary filename.

Create a temporary filename using the current process ID, max 255 characters long.

Returns:
the new filename (must be free'd)

Definition at line 511 of file util.c.

References lutil_createString(), and TEMP_DIR.

Referenced by luau_installUpdate().

gint lutil_gintCompare gpointer  p1,
gpointer  p2
 

Compare two gint pointers.

Compare two gint pointers, returning another gint depending on their relation to one another.

  • p1 and p2 are pointers to the gints to compare.
    Returns:
    • 1 if p1 > p2
    • 0 if p1 == p2
    • -1 if p1 < p1

Definition at line 569 of file util.c.

References lutil_intcmp().

GContainer* lutil_gsplit const char *  delim,
const char *  str
 

Split a string by character delim into a GContainer.

Split a string into an array of strings, separating by delim - e.g., passing in the string "John, Mary, and Susan went to the beach" with delim == ", " will return ("John", "Mary", "and Susan went to the beach"). All of the new strings in the returned arrray are newly allocated and must be free'd (along with the pointer array itself.

  • delim is the string to split with
  • str is the string to split
    Returns:
    a newly allocated array of substrings (which must all be free'd).

Definition at line 240 of file util.c.

References DBUGOUT, g_container_add(), g_container_new(), and GCONT_PTR_ARRAY.

Referenced by luau_versioncmp().

int lutil_intcmp int  i1,
int  i2
 

Compare two ints.

Compare two integers, return another integer depending on their relation to one another.

  • i1 and i2 are the integers to compare.
    Returns:
    • 1 if p1 > p2
    • 0 if p1 == p2
    • -1 if p1 < p1

Definition at line 583 of file util.c.

Referenced by luau_datecmp(), and lutil_gintCompare().

gboolean lutil_isCompletelyBlank const char *  input  ) 
 

Check if a string is completely whitespace.

Definition at line 373 of file util.c.

Referenced by luau_parseInterface().

gboolean lutil_isDirectory const char *  path  ) 
 

Check if a "file" is actually a directory.

Use stat(2) to check if the given pathname is a directory. Also returns false if the file doesn't even exist (see lutil_fileExists).

  • path is the path to check
    Returns:
    whether the path is a directory

Definition at line 547 of file util.c.

char* lutil_mprintf const char *  template,
... 
 

Like sprintf, but allocate memory for and return the string.

Allocating printf. Takes arguments in same form as printf, but writes them to a newly allocated string, which is then returned. Returned string must be free'd.

  • template is a printf template
    Returns:
    a newly allocated string (must be free'd)

Definition at line 139 of file util.c.

References lutil_valistToString().

Referenced by luau_dateString(), luau_interfaceString(), and luau_progInfoString().

void lutil_printIndented int  indent,
int  length,
const char *  string
 

Print a string to STDOUT indented and limited in per-line length.

Print a long string to STDOUT using the specified indentation with output no longer than length. This function will split the string up into pieces based on indent and length so that each line will be indented by indent spaces and will not exceed length characters unless there is a word in string that is greater than length - indent characters long. In this case, it will still be outputted, but it will exceed the specified length.

This function is useful for outputting long strings to the terminal (length == 80).

  • indent is the number of spaces to indent each line
  • length is the maximum length of each line
  • string is the string to print out (will not be edited by this function)

Definition at line 329 of file util.c.

char* lutil_sizeToString int  size,
int  sigfig
 

Convert a size (in bytes) to a human readable format, with sigfig significant figures. Depending on the size of size, this function will return the string in units of megabytes, kilobytes, or bytes. Returned string is dynamically allocated and must be free'd.

Example:: lutil_sizeToString(1024*1024, 2) => 1.0M

  • size is the size to convert to a string
  • sigfig is the number of significant figures to display
    Returns:
    a newly allocated string representing the size (must be free'd).

Definition at line 605 of file util.c.

References lutil_createString().

int lutil_strcaseeq const char *  str1,
const char *  str2
[inline]
 

Check if two strings are equal, ignoring case.

Compare two strings for case-insensitive equality using strcasecmp.

  • str1 is the first string
  • str2 is the string to compare it with
    Returns:
    whether they are equal (regardless of case)

Definition at line 72 of file util.c.

Referenced by luau_parseQuantDataType().

int lutil_streq const char *  str1,
const char *  str2
[inline]
 

Check if two strings are equal.

Compare two strings for equality using strcmp.

  • str1 is the first string
  • str2 is the string to compare it with
    Returns:
    whether they are equal

Definition at line 59 of file util.c.

Referenced by luau_getUpdateInfo(), luau_net_downloadUpdate(), luau_net_queryServer(), luau_parsePkgType(), luau_unsetKeyword(), luau_versioncmp(), lutil_findString(), lutil_parse_parseSymbol(), lutil_parse_parseSymbolArray(), and parsePackage().

char* lutil_strjoin const char *  delim,
const GContainer strings
 

Join an array of strings together separated by the given delimiter.

Join an array of strings together with delim separating them - e.g., passing the array ("John", "Mary", "Susan") with delim == ", " will return "John, Mary, Susan".

  • delim is the delimiter to place between the strings
  • strings is an array of strings to join together
    Returns:
    a newly allocated string joining the strings together (must be free'd).

Definition at line 194 of file util.c.

References g_container_get_iter(), g_container_index(), g_iterator_hasNext(), g_iterator_next(), GContainer::len, and lutil_createString().

Referenced by luau_keywordsString().

void lutil_strToLower char *  str  ) 
 

Convert all upper case letters to lower case in a string.

Definition at line 394 of file util.c.

Referenced by luau_versioncmp().

void lutil_strToUpper char *  str  ) 
 

Definition at line 403 of file util.c.

GString* lutil_uncompress GString *  data  ) 
 

Definition at line 443 of file util.c.

References ERROR.

Referenced by luau_net_queryServer().

char* lutil_valistToString const char *  template,
va_list  args
 

Take a va_list of what would be passed to a printf function and turn it into a newly allocated string. The resultant string must be free'd. Used by lutil_mprintf

  • template is a printf template
  • args is a list of args to past to vsnprintf
    Returns:
    a newly allocated string using the template (must be free'd).

Definition at line 278 of file util.c.

References lutil_createString().

Referenced by lutil_error(), and lutil_mprintf().

char* lutil_vstrcreate const char *  src1,
... 
 

Append any number of strings together into one.

Take a variable list of strings and append them together into one new string.

  • src1 is the first string
    Returns:
    a newly allocated string concatenating them all together (must be free'd)

Definition at line 99 of file util.c.

References ERROR.

Referenced by luau_downloadUpdate().


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