Blame source/uds/stringUtils.h

Packit Service 310c69
/*
Packit Service 310c69
 * Copyright (c) 2020 Red Hat, Inc.
Packit Service 310c69
 *
Packit Service 310c69
 * This program is free software; you can redistribute it and/or
Packit Service 310c69
 * modify it under the terms of the GNU General Public License
Packit Service 310c69
 * as published by the Free Software Foundation; either version 2
Packit Service 310c69
 * of the License, or (at your option) any later version.
Packit Service 310c69
 * 
Packit Service 310c69
 * This program is distributed in the hope that it will be useful,
Packit Service 310c69
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 310c69
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 310c69
 * GNU General Public License for more details.
Packit Service 310c69
 * 
Packit Service 310c69
 * You should have received a copy of the GNU General Public License
Packit Service 310c69
 * along with this program; if not, write to the Free Software
Packit Service 310c69
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit Service 310c69
 * 02110-1301, USA. 
Packit Service 310c69
 *
Packit Service 310c69
 * $Id: //eng/uds-releases/jasper/src/uds/stringUtils.h#2 $
Packit Service 310c69
 */
Packit Service 310c69
Packit Service 310c69
#ifndef STRING_UTILS_H
Packit Service 310c69
#define STRING_UTILS_H
Packit Service 310c69
Packit Service 310c69
#include <stdarg.h>
Packit Service 310c69
#ifdef __KERNEL__
Packit Service 310c69
#include <linux/kernel.h>
Packit Service 310c69
#include <linux/string.h>
Packit Service 310c69
#else
Packit Service 310c69
#include <stdio.h>   // for vsnprintf
Packit Service 310c69
#include <stdlib.h>  // for strtol
Packit Service 310c69
#include <string.h>
Packit Service 310c69
#include <strings.h>
Packit Service 310c69
#endif
Packit Service 310c69
Packit Service 310c69
#include "compiler.h"
Packit Service 310c69
#include "typeDefs.h"
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Convert a boolean value to its corresponding "true" or "false" string.
Packit Service 310c69
 *
Packit Service 310c69
 * @param value  The boolean value to convert
Packit Service 310c69
 *
Packit Service 310c69
 * @return "true" if value is true, "false" otherwise.
Packit Service 310c69
 **/
Packit Service 310c69
static INLINE const char *boolToString(bool value)
Packit Service 310c69
{
Packit Service 310c69
  return (value ? "true" : "false");
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Allocate a string built according to format (our version of asprintf).
Packit Service 310c69
 *
Packit Service 310c69
 * @param [in]  what    A description of what is being allocated, for error
Packit Service 310c69
 *                      logging; if NULL doesn't log anything.
Packit Service 310c69
 * @param [out] strp    The pointer in which to store the allocated string.
Packit Service 310c69
 * @param [in]  fmt     The sprintf format parameter.
Packit Service 310c69
 *
Packit Service 310c69
 * @return UDS_SUCCESS, or the appropriately translated asprintf error
Packit Service 310c69
 **/
Packit Service 310c69
int allocSprintf(const char *what, char **strp, const char *fmt, ...)
Packit Service 310c69
  __attribute__((format(printf, 3, 4), warn_unused_result));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Write a printf-style string into a fixed-size buffer, returning
Packit Service 310c69
 * errors if it would not fit. (our version of snprintf)
Packit Service 310c69
 *
Packit Service 310c69
 * @param [in]  what    A description of what is being written, for error
Packit Service 310c69
 *                      logging; if NULL doesn't log anything.
Packit Service 310c69
 * @param [out] buf     The target buffer
Packit Service 310c69
 * @param [in]  bufSize The size of buf
Packit Service 310c69
 * @param [in]  error   Error code to return on overflow
Packit Service 310c69
 * @param [in]  fmt     The sprintf format parameter.
Packit Service 310c69
 * @return UDS_SUCCESS or error
Packit Service 310c69
 **/
Packit Service 310c69
int fixedSprintf(const char *what, char *buf, size_t bufSize,
Packit Service 310c69
                 int error, const char *fmt, ...)
Packit Service 310c69
  __attribute__((format(printf, 5, 6), warn_unused_result));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Write printf-style string into an existing buffer, returning a specified
Packit Service 310c69
 * error code if it would not fit, and setting ``needed`` to the amount of
Packit Service 310c69
 * space that would be required.
Packit Service 310c69
 *
Packit Service 310c69
 * @param [in]  what    A description of what is being written, for logging.
Packit Service 310c69
 * @param [in]  buf     The buffer in which to write the string, or NULL to
Packit Service 310c69
 *                      merely determine the required space.
Packit Service 310c69
 * @param [in]  bufSize The size of buf.
Packit Service 310c69
 * @param [in]  error   The error code to return for exceeding the specified
Packit Service 310c69
 *                      space, UDS_SUCCESS if no logging required.
Packit Service 310c69
 * @param [in]  fmt     The sprintf format specification.
Packit Service 310c69
 * @param [in]  ap      The variable argument pointer (see <stdarg.h>).
Packit Service 310c69
 * @param [out] needed  If non-NULL, the actual amount of string space
Packit Service 310c69
 *                      required, which may be smaller or larger than bufSize.
Packit Service 310c69
 *
Packit Service 310c69
 * @return UDS_SUCCESS if the string fits, the value of the error parameter if
Packit Service 310c69
 *         the string does not fit and a buffer was supplied, or
Packit Service 310c69
 *         UDS_UNEXPECTED_RESULT if vsnprintf fails in some other undocumented
Packit Service 310c69
 *         way.
Packit Service 310c69
 **/
Packit Service 310c69
int wrapVsnprintf(const char *what, char *buf, size_t bufSize,
Packit Service 310c69
                  int error, const char *fmt, va_list ap, size_t *needed)
Packit Service 310c69
  __attribute__((format(printf, 5, 0), warn_unused_result));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Helper to append a string to a buffer.
Packit Service 310c69
 *
Packit Service 310c69
 * @param buffer        the place at which to append the string
Packit Service 310c69
 * @param bufEnd        pointer to the end of the buffer
Packit Service 310c69
 * @param fmt           a printf format string
Packit Service 310c69
 *
Packit Service 310c69
 * @return      the updated buffer position after the append
Packit Service 310c69
 *
Packit Service 310c69
 * if insufficient space is available, the contents are silently truncated
Packit Service 310c69
 **/
Packit Service 310c69
char *appendToBuffer(char *buffer, char *bufEnd, const char *fmt, ...)
Packit Service 310c69
  __attribute__((format(printf, 3, 4)));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Variable-arglist helper to append a string to a buffer.
Packit Service 310c69
 *
Packit Service 310c69
 * @param buffer  the place at which to append the string
Packit Service 310c69
 * @param bufEnd  pointer to the end of the buffer
Packit Service 310c69
 * @param fmt     a printf format string
Packit Service 310c69
 * @param args    printf arguments
Packit Service 310c69
 *
Packit Service 310c69
 * @return the updated buffer position after the append
Packit Service 310c69
 *
Packit Service 310c69
 * if insufficient space is available, the contents are silently truncated
Packit Service 310c69
 **/
Packit Service 310c69
char *vAppendToBuffer(char       *buffer,
Packit Service 310c69
                      char       *bufEnd,
Packit Service 310c69
                      const char *fmt,
Packit Service 310c69
                      va_list     args)
Packit Service 310c69
  __attribute__((format(printf, 3, 0)));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Our version of strtok_r, since some platforma apparently don't define it.
Packit Service 310c69
 *
Packit Service 310c69
 * @param str           On first call, the string to tokenize. On subsequent
Packit Service 310c69
 *                      calls, NULL.
Packit Service 310c69
 * @param delims        The set of delimiter characters.
Packit Service 310c69
 * @param statePtr      The address of a variable which holds the state of
Packit Service 310c69
 *                      the tokenization between calls to nextToken.
Packit Service 310c69
 *
Packit Service 310c69
 * @return the next token if any, or NULL
Packit Service 310c69
 **/
Packit Service 310c69
char *nextToken(char *str, const char *delims, char **statePtr);
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Parse a string representing a decimal uint64_t.
Packit Service 310c69
 *
Packit Service 310c69
 * @param str           The string.
Packit Service 310c69
 * @param num           Where to put the number.
Packit Service 310c69
 *
Packit Service 310c69
 * @return UDS_SUCCESS or the error UDS_INVALID_ARGUMENT if the string
Packit Service 310c69
 *         is not in the correct format.
Packit Service 310c69
 **/
Packit Service 310c69
int parseUint64(const char *str, uint64_t *num)
Packit Service 310c69
  __attribute__((warn_unused_result));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Attempt to convert a string to an integer (base 10)
Packit Service 310c69
 *
Packit Service 310c69
 * @param nptr  Pointer to string to convert
Packit Service 310c69
 * @param num   The resulting integer
Packit Service 310c69
 *
Packit Service 310c69
 * @return UDS_SUCCESS or an error code
Packit Service 310c69
 **/
Packit Service 310c69
int stringToSignedInt(const char *nptr, int *num)
Packit Service 310c69
  __attribute__((warn_unused_result));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Attempt to convert a string to a long integer (base 10)
Packit Service 310c69
 *
Packit Service 310c69
 * @param nptr  Pointer to string to convert
Packit Service 310c69
 * @param num   The resulting long integer
Packit Service 310c69
 *
Packit Service 310c69
 * @return UDS_SUCCESS or an error code
Packit Service 310c69
 **/
Packit Service 310c69
int stringToSignedLong(const char *nptr, long *num)
Packit Service 310c69
  __attribute__((warn_unused_result));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Attempt to convert a string to an unsigned integer (base 10).
Packit Service 310c69
 *
Packit Service 310c69
 * @param nptr  Pointer to string to convert
Packit Service 310c69
 * @param num   The resulting unsigned integer
Packit Service 310c69
 *
Packit Service 310c69
 * @return UDS_SUCCESS or an error code
Packit Service 310c69
 **/
Packit Service 310c69
int stringToUnsignedInt(const char *nptr, unsigned int *num)
Packit Service 310c69
  __attribute__((warn_unused_result));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Attempt to convert a string to an unsigned long integer (base 10).
Packit Service 310c69
 *
Packit Service 310c69
 * @param nptr  Pointer to string to convert
Packit Service 310c69
 * @param num   The resulting long unsigned integer
Packit Service 310c69
 *
Packit Service 310c69
 * @return UDS_SUCCESS or an error code
Packit Service 310c69
 **/
Packit Service 310c69
int stringToUnsignedLong(const char *nptr, unsigned long *num)
Packit Service 310c69
  __attribute__((warn_unused_result));
Packit Service 310c69
Packit Service 310c69
#endif /* STRING_UTILS_H */