Lhogho
0.0.027
|
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 |
size_t m_strlen | ( | const TCHAR * | str | ) |
str | the string |
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 | ||
) |
str1 | first string |
str2 | second string |
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 | ||
) |
dest | destination string |
src | source string |
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 | ) |
dest_ptr | pointer to destination string |
src | source string |
num_chars | number of characters to copy. |
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; }
TCHAR* m_fgets | ( | FILE * | file, |
TCHAR * | buffer, | ||
size_t | buffer_size, | ||
size_t * | out_size_ptr | ||
) |
file | file to read from |
buffer | buffer to read in |
buffer_size | size of space in the buffer |
out_size_ptr | size of line readed |
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; }
file | file to write in |
buffer | string 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 | ||
) |
file | file to write in |
buffer | string 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 }