Пример #1
0
/**
 * Append formatted string to the end of the source str
 *
 * @param str       a pointer of original string
 * @param format    string format to append
 *
 * @return a pointer of str if successful, otherwise returns NULL
 */
char *qstrcatf(char *str, const char *format, ...)
{
    char *buf;
    DYNAMIC_VSPRINTF(buf, format);
    if (buf == NULL) return NULL;

    char *ret = strcat(str, buf);
    free(buf);
    return ret;
}
Пример #2
0
/**
 * qdb->execute_queryf(): Executes the formatted query
 *
 * @param db        a pointer of qdb_t object
 * @param format    query string format
 *
 * @return a pointer of qdbresult_t if successful, otherwise returns NULL
 */
static qdbresult_t *execute_queryf(qdb_t *db, const char *format, ...)
{
    char *query;
    DYNAMIC_VSPRINTF(query, format);
    if (query == NULL) return NULL;

    qdbresult_t *ret = db->execute_query(db, query);
    free(query);
    return ret;
}
Пример #3
0
/**
 * Duplicate a formatted string.
 *
 * @param format    string format
 *
 * @return a pointer of malloced string if successful, otherwise returns NULL
 */
char *qstrdupf(const char *format, ...)
{
    char *str;
    DYNAMIC_VSPRINTF(str, format);
    if (str == NULL) return NULL;

    char *dup = strdup(str);
    free(str);

    return dup;
}
Пример #4
0
/**
 * qentry_t->_getstrf():  Find string object with given formatted name.
 *
 * @param   entry   qentry_t pointer
 * @param   newmem  whether or not to allocate memory for the data.
 * @param   namefmt formatted name string
 *
 * @return  a pointer of malloced data if key is found, otherwise returns NULL.
 */
static char *_getstrf(qentry_t *entry, bool newmem, const char *namefmt, ...)
{
    char *name;
    DYNAMIC_VSPRINTF(name, namefmt);
    if (name == NULL) return NULL;

    char *data = (char *)_get(entry, name, NULL, newmem);
    free(name);

    return data;
}
Пример #5
0
/**
 * qdb->execute_updatef(): Executes the formatted update DML
 *
 * @param db        a pointer of qdb_t object
 * @param format    query string format
 *
 * @return a number of affected rows, otherwise returns -1
 */
static int execute_updatef(qdb_t *db, const char *format, ...)
{
    char *query;
    DYNAMIC_VSPRINTF(query, format);
    if (query == NULL) return -1;

    int affected = execute_update(db, query);
    free(query);

    return affected;
}
Пример #6
0
/**
 * qentry_t->putstrf(): Add formatted string object into linked-list structure.
 *
 * @param   entry   qentry_t pointer
 * @param   replace in case of false, just insert. in case of true, remove all
 *                  same key then insert object if found.
 * @param   name    key name.
 * @param   format  formatted value string
 *
 * @return  true if successful, otherwise returns false.
 */
static bool _putstrf(qentry_t *entry, bool replace, const char *name,
                     const char *format, ...)
{
    char *str;
    DYNAMIC_VSPRINTF(str, format);
    if (str == NULL) return false;

    bool ret = _putstr(entry, name, str, replace);
    free(str);

    return ret;
}
Пример #7
0
/**
 * qhashtbl->putstrf(): Put a formatted string into this table.
 *
 * @param tbl       qhashtbl_t container pointer.
 * @param name      key name.
 * @param format    formatted string data.
 *
 * @return true if successful, otherwise returns false.
 * @retval errno will be set in error condition.
 *  - EINVAL : Invalid argument.
 *  - ENOMEM : Memory allocation failure.
 */
bool qhashtbl_putstrf(qhashtbl_t *tbl, const char *name, const char *format, ...) {
    char *str;
    DYNAMIC_VSPRINTF(str, format);
    if (str == NULL) {
        errno = ENOMEM;
        return false;
    }

    bool ret = qhashtbl_putstr(tbl, name, str);
    free(str);
    return ret;
}
Пример #8
0
/**
 * qhasharr->putstrf(): Put a formatted string into this table.
 *
 * @param tbl       qhasharr_t container pointer.
 * @param key       key name.
 * @param format    formatted string data.
 *
 * @return true if successful, otherwise returns false.
 * @retval errno will be set in error condition.
 *  - ENOBUFS   : Table doesn't have enough space to store the object.
 *  - ENOMEM    : System memory allocation failure.
 *  - EINVAL    : Invalid argument.
 *  - EFAULT    : Unexpected error. Data structure is not constant.
 */
static bool putstrf(qhasharr_t *tbl, const char *key, const char *format, ...) {
    char *str;
    DYNAMIC_VSPRINTF(str, format);
    if (str == NULL) {
        errno = ENOMEM;
        return false;
    }

    bool ret = putstr(tbl, key, str);
    free(str);
    return ret;
}
Пример #9
0
/**
 * qvector->addstrf(): Stack formatted string
 *
 * @param vector    qvector_t container pointer.
 * @param format    string format
 *
 * @return true if successful, otherwise returns false
 * @retval errno will be set in error condition.
 *  - EINVAL    : Invalid argument.
 *  - ENOMEM    : Memory allocation failure.
 */
static bool addstrf(qvector_t *vector, const char *format, ...) {
    char *str;
    DYNAMIC_VSPRINTF(str, format);
    if (str == NULL) {
        errno = ENOMEM;
        return false;
    }

    bool ret = addstr(vector, str);
    free(str);

    return ret;
}
Пример #10
0
/**
 * qlog->writef(): Log messages
 *
 * @param log       a pointer of qlog_t
 * @param format    messages format
 *
 * @return true if successful, otherewise returns false
 */
static bool writef(qlog_t *log, const char *format, ...) {
    if (log == NULL || log->fp == NULL)
        return false;

    char *str;
    DYNAMIC_VSPRINTF(str, format);
    if (str == NULL)
        return false;

    bool ret = write_(log, str);

    free(str);
    return ret;
}
Пример #11
0
ssize_t streamPrintf(int nSockFd, const char *format, ...)
{
    char *pszBuf;
    DYNAMIC_VSPRINTF(pszBuf, format);
    if (pszBuf == NULL) return -1;

    ssize_t nSent = qio_write(nSockFd, pszBuf, strlen(pszBuf), 0);

#ifdef ENABLE_DEBUG
    if (nSent > 0) DEBUG("[TX] %s", pszBuf);
    else DEBUG("[TX-ERR] %s", pszBuf);
#endif
    free(pszBuf);

    return nSent;
}
Пример #12
0
bool listablePutAsStringf(listtable* tbl, const char* name, const char* format, ...) {

	char* str;

	DYNAMIC_VSPRINTF(str, format);

	if (str == NULL) {
		errno = ENOMEM;
		return false;
	}

	bool ret = listablePutAsString(tbl, name, str);

	free(str);

	return ret;
}