OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [posix/] [mm0/] [lib/] [pathstr.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
/*
2
 * Functions to manipulate path strings.
3
 *
4
 * Copyright (C) 2008 Bahadir Balban
5
 */
6
#include <string.h>
7
#include <alloca.h>
8
 
9
/* Reverses a string by allocating on stack. Not super-efficient but easy. */
10
char *strreverse(char *str)
11
{
12
        int length = strlen(str);
13
        char *tmp = alloca(length);
14
 
15
        strcpy(tmp, str);
16
 
17
        for (int i = 0; i < length; i++)
18
                str[i] = tmp[length - 1 - i];
19
 
20
        return str;
21
}
22
 
23
/*
24
 * Splits the string str according to the given seperator, returns the
25
 * first component, and modifies the str so that it points at the next
26
 * available component (or a leading separator which can be filtered
27
 * out on a subsequent call to this function).
28
 */
29
char *splitpath(char **str, char sep)
30
{
31
        char *cursor = *str;
32
        char *end;
33
 
34
        /* Move forward until no seperator */
35
        while (*cursor == sep) {
36
                *cursor = '\0';
37
                cursor++;       /* Move to first char of component */
38
        }
39
 
40
        end = cursor;
41
        while (*end != sep && *end != '\0')
42
                end++;          /* Move until end of component */
43
 
44
        if (*end == sep) {      /* if ended with separator */
45
                *end = '\0';    /* finish component by null */
46
                if (*(end + 1) != '\0') /* if more components after, */
47
                        *str = end + 1; /* assign beginning to str */
48
                else
49
                        *str = end; /* else str is also depleted, give null */
50
        } else /* if end was null, that means the end for str, too. */
51
                *str = end;
52
 
53
        return cursor;
54
}
55
 
56
/* Same as split path, but splits components from the end. Slow. */
57
char *splitpath_end(char **path, char sep)
58
{
59
        char *component;
60
 
61
        /* Reverse the string */
62
        strreverse(*path);
63
 
64
        /* Pick one from the start */
65
        component = splitpath(path, sep);
66
 
67
        /* Reverse the rest back to original. */
68
        strreverse(*path);
69
 
70
        /* Reverse component back to original */
71
        strreverse(component);
72
 
73
        return component;
74
}
75
 
76
/* Splitpath test program. Tests all 3 functions.
77
int main()
78
{
79
        char str1[256] = "/a/b/c/d/////e/f";
80
        char *str2 = malloc(strlen(str1) + 1);
81
        char *comp;
82
 
83
        strcpy(str2, str1);
84
 
85
        comp = splitpath_end(&str2, '/');
86
        while (*comp) {
87
                printf("%s and %s\n", comp, str2);
88
                comp = splitpath_end(&str2, '/');
89
        }
90
}
91
*/
92
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.