2009-07-08 13:19:16 -07:00
|
|
|
|
/*
|
2016-02-09 20:54:03 -08:00
|
|
|
|
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2015, 2016 Nicira, Inc.
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*
|
2009-06-15 15:11:30 -07:00
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at:
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*
|
2009-06-15 15:11:30 -07:00
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*/
|
|
|
|
|
|
2016-03-03 10:20:46 -08:00
|
|
|
|
#ifndef OPENVSWITCH_DYNAMIC_STRING_H
|
|
|
|
|
#define OPENVSWITCH_DYNAMIC_STRING_H 1
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdio.h>
|
2016-03-03 10:20:46 -08:00
|
|
|
|
#include "openvswitch/compiler.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2017-07-30 18:03:24 -07:00
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-02-17 17:00:06 -08:00
|
|
|
|
/* A "dynamic string", that is, a buffer that can be used to construct a
|
|
|
|
|
* string across a series of operations that extend or modify it.
|
|
|
|
|
*
|
|
|
|
|
* The 'string' member does not always point to a null-terminated string.
|
|
|
|
|
* Initially it is NULL, and even when it is nonnull, some operations do not
|
|
|
|
|
* ensure that it is null-terminated. Use ds_cstr() to ensure that memory is
|
|
|
|
|
* allocated for the string and that it is null-terminated. */
|
2009-07-08 13:19:16 -07:00
|
|
|
|
struct ds {
|
|
|
|
|
char *string; /* Null-terminated string. */
|
|
|
|
|
size_t length; /* Bytes used, not including null terminator. */
|
|
|
|
|
size_t allocated; /* Bytes allocated, not including null terminator. */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define DS_EMPTY_INITIALIZER { NULL, 0, 0 }
|
|
|
|
|
|
|
|
|
|
void ds_init(struct ds *);
|
|
|
|
|
void ds_clear(struct ds *);
|
|
|
|
|
void ds_truncate(struct ds *, size_t new_length);
|
|
|
|
|
void ds_reserve(struct ds *, size_t min_length);
|
|
|
|
|
char *ds_put_uninit(struct ds *, size_t n);
|
2010-05-03 12:30:37 -07:00
|
|
|
|
static inline void ds_put_char(struct ds *, char);
|
2009-11-04 14:55:53 -08:00
|
|
|
|
void ds_put_utf8(struct ds *, int uc);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
void ds_put_char_multiple(struct ds *, char, size_t n);
|
|
|
|
|
void ds_put_buffer(struct ds *, const char *, size_t n);
|
|
|
|
|
void ds_put_cstr(struct ds *, const char *);
|
2009-11-16 16:55:35 -08:00
|
|
|
|
void ds_put_and_free_cstr(struct ds *, char *);
|
2014-12-15 14:10:38 +01:00
|
|
|
|
void ds_put_format(struct ds *, const char *, ...) OVS_PRINTF_FORMAT(2, 3);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
void ds_put_format_valist(struct ds *, const char *, va_list)
|
2014-12-15 14:10:38 +01:00
|
|
|
|
OVS_PRINTF_FORMAT(2, 0);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
void ds_put_printable(struct ds *, const char *, size_t);
|
2015-05-20 18:47:21 -07:00
|
|
|
|
void ds_put_hex(struct ds *ds, const void *buf, size_t size);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
void ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
|
|
|
|
|
uintptr_t ofs, bool ascii);
|
|
|
|
|
int ds_get_line(struct ds *, FILE *);
|
2013-07-08 10:15:00 -07:00
|
|
|
|
int ds_get_preprocessed_line(struct ds *, FILE *, int *line_number);
|
2012-05-09 12:15:11 -07:00
|
|
|
|
int ds_get_test_line(struct ds *, FILE *);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2014-11-12 15:06:08 +01:00
|
|
|
|
void ds_put_strftime_msec(struct ds *, const char *format, long long int when,
|
2015-08-10 13:27:45 -07:00
|
|
|
|
bool utc);
|
2014-11-12 15:06:08 +01:00
|
|
|
|
char *xastrftime_msec(const char *format, long long int when, bool utc);
|
2013-05-02 16:16:06 -07:00
|
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
|
char *ds_cstr(struct ds *);
|
2009-12-16 11:28:13 -08:00
|
|
|
|
const char *ds_cstr_ro(const struct ds *);
|
2009-07-15 10:57:17 -07:00
|
|
|
|
char *ds_steal_cstr(struct ds *);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
void ds_destroy(struct ds *);
|
2010-01-28 13:11:39 -08:00
|
|
|
|
void ds_swap(struct ds *, struct ds *);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
|
|
|
|
int ds_last(const struct ds *);
|
2016-02-09 20:54:03 -08:00
|
|
|
|
bool ds_chomp(struct ds *, int c);
|
2016-07-28 22:17:41 +00:00
|
|
|
|
void ds_clone(struct ds *, struct ds *);
|
2010-05-03 12:30:37 -07:00
|
|
|
|
|
|
|
|
|
/* Inline functions. */
|
|
|
|
|
|
|
|
|
|
void ds_put_char__(struct ds *, char);
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
|
ds_put_char(struct ds *ds, char c)
|
|
|
|
|
{
|
|
|
|
|
if (ds->length < ds->allocated) {
|
|
|
|
|
ds->string[ds->length++] = c;
|
|
|
|
|
ds->string[ds->length] = '\0';
|
|
|
|
|
} else {
|
|
|
|
|
ds_put_char__(ds, c);
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2017-07-30 18:03:24 -07:00
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
|
#endif /* dynamic-string.h */
|