Lhogho  0.0.027
Functions
string_functions.c File Reference

Functions

size_t m_strlen (const TCHAR *str)
 calculates string length
int m_strcmp (const TCHAR *str1, const TCHAR *str2)
 compares two strings
TCHAR * m_strcpy (TCHAR *dest, const TCHAR *src)
 copy string
BOOL m_isspace (TCHAR chr)
 test if char is space
RESULT m_strndup (TCHAR **dest_ptr, const TCHAR *src, size_t num_chars)
 duplicate string, but no more than num_chars symbols
RESULT m_strdup (TCHAR **dest_ptr, const TCHAR *src)
 duplicate string. Allocate memory for destination
TCHAR * m_fgets (FILE *file, TCHAR *buffer, size_t buffer_size, size_t *out_size_ptr)
 Read a line from file.
void m_fputs (FILE *file, const TCHAR *buffer)
 prints a line to file
void m_fputs_ascii (FILE *file, const char *buffer)
 prints a line to file, convert it to UNICODE if needed
void m_fputc (FILE *file, TCHAR tchar)
 prints a symbol to file

Function Documentation

size_t m_strlen ( const TCHAR *  str)
Parameters:
strthe string
Returns:
length of the string in symbols

Calculates string length in symbols. Works with char and w_char

{
    const TCHAR * start = str;
    while (*str) ++str;
    return str - start;
}
int m_strcmp ( const TCHAR *  str1,
const TCHAR *  str2 
)
Parameters:
str1first string
str2second string
Returns:
comparison result similar to standart strcmp result

Comare two strings. Return negative value if first is less than second positive value if first is great than second and zero if strings are equal

{
    while (*str1 && *str1 == *str2)
    {
        ++str1;
        ++str2;
    }
    return *str1 - *str2;
}
TCHAR* m_strcpy ( TCHAR *  dest,
const TCHAR *  src 
)
Parameters:
destdestination string
srcsource string
Returns:
pointer to destination

Copy string value to given pointer. User must supply enought memory

{
    TCHAR * mem = dest;
    while (*src)
    {
        *dest++ = *src++;
    }
    *dest = 0;
    return mem;
}
BOOL m_isspace ( TCHAR  chr)
Parameters:
chrchar to test
Returns:
result of the test

Test if symbol is space symbol (Space, new line or tab)

{
    return (chr == TEXT(' ')  || chr == TEXT('\t') || 
        chr == TEXT('\r') || chr == TEXT('\n'));
}
RESULT m_strndup ( TCHAR **  dest_ptr,
const TCHAR *  src,
size_t  num_chars 
)
Parameters:
dest_ptrpointer to destination string
srcsource string
num_charsnumber of characters to copy.
Returns:
RESULT value. Error on memory problems

Copy string value to given pointer, byt no more than num_chars symbols. Allocates memory for destination. Not copy the 0 byte if source is longer than wanted length

{
    *dest_ptr = (TCHAR *)malloc((num_chars + 1) * sizeof(TCHAR));
    if (!*dest_ptr)
    {
        return ERR_MEMORY;
    }
    memcpy(*dest_ptr, src, num_chars * sizeof(TCHAR));
    (*dest_ptr)[num_chars] = 0;
    return SUCCESS_FULL;
}
RESULT m_strdup ( TCHAR **  dest_ptr,
const TCHAR *  src 
)
Parameters:
dest_ptrpointer to destination string
srcsource string
Returns:
RESULT value. Error on memory problems

Duplicate string. Allocate memory for destination. If memory problems occured return error.

{
    size_t len = m_strlen(src);
    return m_strndup(dest_ptr, src, len);
}
TCHAR* m_fgets ( FILE *  file,
TCHAR *  buffer,
size_t  buffer_size,
size_t *  out_size_ptr 
)
Parameters:
filefile to read from
bufferbuffer to read in
buffer_sizesize of space in the buffer
out_size_ptrsize of line readed
Returns:
pointer to readed string or NULL if error

Read characters from given open file until new line symbol is reached or input buffer is full. Returns the count of really readed symbols in parameter and pointer to the buffer. If error occured during read process returns NULL

{
    TCHAR c = 0;
    size_t pos = 0;
    if (!file || !buffer || !buffer_size || !out_size_ptr)
    {
        LOG_ERROR("Invalid args supplied to m_fgets");
        if (out_size_ptr)
        {
            *out_size_ptr = 0;
        }
        return NULL;
    }

    while (pos < buffer_size - 1)
    {
        if (!fread(&c, sizeof(TCHAR), 1, file))   
        {
            break;
        }
        buffer[pos++] = c;

        if (c == TEXT('\r') || c == TEXT('\n'))
        {
            TCHAR c1;
            size_t readed;
            if ((readed = fread(&c1, sizeof(TCHAR), 1, file)) &&
                (c1 == TEXT('\r') || c1 == TEXT('\n')) && c1 != c)
            {
                buffer[pos++] = c1;
            }
            else
            {
                if (readed)
                {
                    fseek(file, -1l, SEEK_CUR);
                }
            }
            break;
        }
    }

    buffer[pos] = 0;
    *out_size_ptr = pos;
    return pos ? buffer : NULL;
}
void m_fputs ( FILE *  file,
const TCHAR *  buffer 
)
Parameters:
filefile to write in
bufferstring to write

Writes a line to the file. Buffer must be a valid nul-terminated string

{
    size_t len = m_strlen(buffer);
    fwrite(buffer, sizeof(TCHAR), len, file);
}
void m_fputs_ascii ( FILE *  file,
const char *  buffer 
)
Parameters:
filefile to write in
bufferstring to write

Writes a line to the file. If program works in unicode mode the string is converted from ASCII to unicode.

{
    char * buf = (char *)buffer;
    size_t len = strlen(buffer) * sizeof(TCHAR);
    
#if defined(UNICODE)
     buf = (char*)malloc(len);
     while (*buffer) *buf++ = *buffer++, *buf++ = 0;
#endif
    fwrite(buf, sizeof(TCHAR), len, file);

#if defined(UNICODE)
    free(buf);
#endif
}
void m_fputc ( FILE *  file,
TCHAR  tchar 
)
Parameters:
filefile to write in
tcharsymbol to write

Writes a symbol to file.

{
#if defined (UNICODE)
    putwc(tchar, file);
#else
    putc(tchar, file);
#endif
}

[ HOME | INDEX | ATOMS | VARS | REFERENCE ]
Lhogho Developer's Documentation
Tue Feb 7 2012