File: strhandle.cc

package info (click to toggle)
intel2gas 1.3.3-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 460 kB
  • ctags: 128
  • sloc: cpp: 1,286; makefile: 71
file content (138 lines) | stat: -rw-r--r-- 3,450 bytes parent folder | download | duplicates (4)
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
/*
    strhandle.cc   string manipulating functions
    Copyright (c) 1999 Mikko Tiihonen (mikko.tiihonen@hut.fi)
    This source code is licensed under the GNU LGPL
  
    Please refer to the file COPYING.LIB contained in the distribution for
    licensing conditions
*/

#include "strhandle.h"

/* Cuts the str at first occurance of cut.
 * Returns: pointer following the cut.
 * Modifies: str */
char *str_cutat(char *str, char cut)
{
    char *pos = strchr(str,cut);
    if (!pos) return NULL;
    *pos = '\0';
    return pos+1;
}


/* Like str_cutat, but the beginning of str is copied to buffer at left */
char *str_splitat(char const * const str, char *left, char cut)
{
    char *pos = strchr(str,cut);
    if (!pos) {
        strcpy(left,str);
        return NULL;
    }
    strncpy(left,str,pos-str);
    left[pos-str] = '\0';
    return pos+1;
}


/* Used to find the matching close parenthesis, using inc=(, dec=) */
char const *str_find_close(char const *str, char inc, char dec)
{
    int counter = 1;
    char const *pos = str;
    while (counter && *pos) {
        if (*pos == inc) counter++;
        else if (*pos == dec) counter--;
	pos++;
    }
    if (counter)
        return NULL;
    return pos-1;
}

/* Like str_splitat, but splits the string after the matching 
 * parenthesis. Again return value points to match+1 and the
 * beginning of str is copied to left */
char *str_splitat_close(char const * const str, char *left, char inc, char dec)
{
    char const *pos = str_find_close(str,inc,dec);
    if (!pos) {
        strcpy(left,str);
	return NULL;
    }
    strncpy(left,str,pos-str);
    left[pos-str] = '\0';
    return (char *)pos+1;
}


/* At first occurance of cut copies beginning of str to buffer left and the 
 * rest to right. str is not modified */
void str_splitstrat(char const * const str, char *left, char cut, char *right)
{
    char *pos = str_splitat(str,left,cut);
    if (!pos) {
	*right = '\0';
	return;
    }
    strcpy(right,pos);
}

/* Returns a pointer to first non-space character in str */
char *str_skipspace(char *str)
{
    while (*str == ' ') str++;
    return str;
}

/* Appends a character to the string. Make sure buffer is large enough. */
void str_catchar(char *str, char ch)
{
    const int size = strlen(str);
    str[size] = ch;
    str[size+1] = '\0';
} 

/* Removes a trailing number from str and returns it 
 * If str does not end in number returns \0 */
char str_striplastnumber(char *str)
{
    int size = strlen(str);
    if (size <= 1) return '\0';
    char number = str[size-1];
    if (!isdigit(number)) return '\0';
    str[size-1] = '\0';
    return number;
}

/* Copies source to dest until findst end1, end2 */
void str_copyuntil(char *dest, char const *source, char end1, char end2)
{
    while (tolower(*source) != tolower(end1) && 
	   tolower(*source) != tolower(end2)) {
	*dest = *source;
	if (*source == '\0') return;
	dest++; source++;
    }
    *dest = '\0';
}

/* Copies source to dest at position pos.
 * If dest is longer a single space is added between
 * else dest is padded with spaces */
void str_addatpos(char *dest, char const *source, int pos)
{
    char *str = dest;
    while (*str) {
        if (*str++ == '\n')
            dest = str;
    }
    int size = strlen(dest);
    if (pos > 0 && size >= pos) {
      str_catchar(dest, ' ');
      strcat(dest,source);
      return;
    }
    memset(dest+size,' ',pos-size);
    strcpy(dest+pos,source);
}