1
1

- Use "const" for regular strings in judicious places

- Use LAM_MALLOC and LAM_FREE
- Use NULL instead of 0

This commit was SVN r445.
Этот коммит содержится в:
Jeff Squyres 2004-01-16 01:33:12 +00:00
родитель 47329fd86e
Коммит a3316f2712
2 изменённых файлов: 18 добавлений и 18 удалений

Просмотреть файл

@ -2,17 +2,18 @@
* $HEADER$ * $HEADER$
*/ */
/** @file **/
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "lam/constants.h" #include "lam/constants.h"
#include "lam/mem/malloc.h"
#include "lam/util/argv.h" #include "lam/util/argv.h"
#include "lam/util/strncpy.h" #include "lam/util/strncpy.h"
#define ARGSIZE 128 #define ARGSIZE 128
/** @file **/
/** /**
* Append a string to an new or existing NULL-terminated argv array. * Append a string to an new or existing NULL-terminated argv array.
* *
@ -38,12 +39,12 @@
* string (i.e., the arg parameter) after invoking this function. * string (i.e., the arg parameter) after invoking this function.
*/ */
int int
lam_argv_add(int *argc, char ***argv, char *arg) lam_argv_add(int *argc, char ***argv, const char *arg)
{ {
/* Create new argv. */ /* Create new argv. */
if (NULL == *argv) { if (NULL == *argv) {
*argv = (char **) malloc(2 * sizeof(char *)); *argv = LAM_MALLOC(2 * sizeof(char *));
if (NULL == *argv) if (NULL == *argv)
return LAM_ERROR; return LAM_ERROR;
*argc = 0; *argc = 0;
@ -54,15 +55,14 @@ lam_argv_add(int *argc, char ***argv, char *arg)
/* Extend existing argv. */ /* Extend existing argv. */
else { else {
*argv = (char **) realloc((char *) *argv, *argv = realloc(*argv, (*argc + 2) * sizeof(char *));
(unsigned) (*argc + 2) * sizeof(char *));
if (NULL == *argv) if (NULL == *argv)
return LAM_ERROR; return LAM_ERROR;
} }
/* Set the newest element to point to a copy of the arg string */ /* Set the newest element to point to a copy of the arg string */
(*argv)[*argc] = (char *) malloc((unsigned) strlen(arg) + 1); (*argv)[*argc] = LAM_MALLOC(strlen(arg) + 1);
if (NULL == (*argv)[*argc]) if (NULL == (*argv)[*argc])
return LAM_ERROR; return LAM_ERROR;
@ -95,10 +95,10 @@ lam_argv_free(char **argv)
return; return;
for (p = argv; NULL != *p; ++p) { for (p = argv; NULL != *p; ++p) {
free(*p); LAM_FREE(*p);
} }
free(argv); LAM_FREE(argv);
} }
@ -117,11 +117,11 @@ lam_argv_free(char **argv)
* invalidating the output argv). * invalidating the output argv).
*/ */
char ** char **
lam_argv_split(char *src_string, int delimiter) lam_argv_split(const char *src_string, int delimiter)
{ {
char arg[ARGSIZE]; char arg[ARGSIZE];
char **argv = 0; char **argv = NULL;
char *p; const char *p;
char *argtemp; char *argtemp;
int argc = 0; int argc = 0;
size_t arglen; size_t arglen;
@ -151,7 +151,7 @@ lam_argv_split(char *src_string, int delimiter)
/* long argument, malloc buffer, copy and add */ /* long argument, malloc buffer, copy and add */
else if (arglen > (ARGSIZE - 1)) { else if (arglen > (ARGSIZE - 1)) {
argtemp = (char *) malloc(arglen + 1); argtemp = LAM_MALLOC(arglen + 1);
if (NULL == argtemp) if (NULL == argtemp)
return NULL; return NULL;
@ -159,11 +159,11 @@ lam_argv_split(char *src_string, int delimiter)
argtemp[arglen] = '\0'; argtemp[arglen] = '\0';
if (LAM_ERROR == lam_argv_add(&argc, &argv, argtemp)) { if (LAM_ERROR == lam_argv_add(&argc, &argv, argtemp)) {
free(argtemp); LAM_FREE(argtemp);
return NULL; return NULL;
} }
free(argtemp); LAM_FREE(argtemp);
} }
/* short argument, copy to buffer and add */ /* short argument, copy to buffer and add */
@ -245,7 +245,7 @@ lam_argv_join(char **argv, int delimiter)
/* Allocate the string. */ /* Allocate the string. */
if (NULL == (str = (char *) malloc(str_len))) if (NULL == (str = LAM_MALLOC(str_len)))
return NULL; return NULL;
/* Loop filling in the string. */ /* Loop filling in the string. */

Просмотреть файл

@ -10,9 +10,9 @@
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
int lam_argv_add(int *argc, char ***argv, char *arg); int lam_argv_add(int *argc, char ***argv, const char *arg);
void lam_argv_free(char **argv); void lam_argv_free(char **argv);
char **lam_argv_split(char *src_string, int delimiter); char **lam_argv_split(const char *src_string, int delimiter);
int lam_argv_count(char **argv); int lam_argv_count(char **argv);
char *lam_argv_join(char **argv, int delimiter); char *lam_argv_join(char **argv, int delimiter);
size_t lam_argv_len(char **argv); size_t lam_argv_len(char **argv);