1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
/* nih-dbus-tool
*
* indent.c - indentation and other code-style string changes
*
* Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
* Copyright © 2009 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include <string.h>
#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/logging.h>
#include "indent.h"
/**
* indent:
* @str: pointer to string to be indented,
* @parent: parent object of returned string,
* @level: number of indents to add.
*
* Indents the given string @str with @level tab characters preceding
* each non-empty line, including the last even if that has no terminating
* newline.
*
* @str is modified directly, the returned string is simply a pointer to
* it, thus @parent is actually ignored though it usual to pass the parent
* of @str for style reasons.
*
* Returns: modified @str or NULL if insufficient memory.
**/
char *
indent (char ** str,
const void *parent,
int level)
{
char * s;
char * ret;
size_t len;
nih_assert (str != NULL);
nih_assert (*str != NULL);
nih_assert (level > 0);
/* First figure out how many tab characters we have to insert */
len = strlen (*str);
if (**str != '\n')
len += level;
for (s = *str; *s; s++)
if ((*s == '\n') && (s[1] != '\0') && (s[1] != '\n'))
len += level;
/* Increase the length of the string to fit */
ret = nih_realloc (*str, parent, len + 1);
if (! ret)
return NULL;
/* Now put the tab characters in */
*str = ret;
if (**str != '\n') {
memmove (*str + level, *str, len + 1 - level);
memset (*str, '\t', level);
}
for (s = *str; *s; s++) {
if ((*s == '\n') && (s[1] != '\0') && (s[1] != '\n')) {
memmove (s + level + 1, s + 1,
*str + len + 1 - level - s - 1);
memset (s + 1, '\t', level);
}
}
return *str;
}
/**
* comment:
* @str: pointer to string to be commented,
* @parent: parent object of returned string.
*
* Applies commenting to the given @str, prefixing " * " onto each line
* including the first and last.
*
* @str is modified directly, the returned string is simply a pointer to
* it, thus @parent is actually ignored though it usual to pass the parent
* of @str for style reasons.
*
* Returns: modified @str or NULL if insufficient memory.
**/
char *
comment (char ** str,
const void *parent)
{
char * s;
char * ret;
size_t len;
nih_assert (str != NULL);
nih_assert (*str != NULL);
/* First figure out how many characters we have to insert */
len = strlen (*str);
if (**str == '\n') {
len += 2;
} else {
len += 3;
}
for (s = *str; *s; s++) {
if ((*s == '\n') && (s[1] != '\0')) {
if (s[1] == '\n') {
len += 2;
} else {
len += 3;
}
}
}
/* Increase the length of the string to fit */
ret = nih_realloc (*str, parent, len + 1);
if (! ret)
return NULL;
/* Now put the comment characters in */
*str = ret;
if (**str == '\n') {
memmove (*str + 2, *str, len + 1 - 2);
memcpy (*str, " *", 2);
} else {
memmove (*str + 3, *str, len + 1 - 3);
memcpy (*str, " * ", 3);
}
for (s = *str; *s; s++) {
if ((*s == '\n') && (s[1] != '\0')) {
if (s[1] == '\n') {
memmove (s + 2 + 1, s + 1,
*str + len + 1 - 2 - s - 1);
memcpy (s + 1, " *", 2);
} else {
memmove (s + 3 + 1, s + 1,
*str + len + 1 - 3 - s - 1);
memcpy (s + 1, " * ", 3);
}
}
}
return *str;
}
|