112458b28Smrg/*
212458b28Smrg  Copyright (c) 2002-2003 by Juliusz Chroboczek
312458b28Smrg  Copyright (c) 2015 by Thomas Klausner
412458b28Smrg
512458b28Smrg  Permission is hereby granted, free of charge, to any person obtaining a copy
612458b28Smrg  of this software and associated documentation files (the "Software"), to deal
712458b28Smrg  in the Software without restriction, including without limitation the rights
812458b28Smrg  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
912458b28Smrg  copies of the Software, and to permit persons to whom the Software is
1012458b28Smrg  furnished to do so, subject to the following conditions:
1112458b28Smrg
1212458b28Smrg  The above copyright notice and this permission notice shall be included in
1312458b28Smrg  all copies or substantial portions of the Software.
1412458b28Smrg
1512458b28Smrg  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1612458b28Smrg  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1712458b28Smrg  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1812458b28Smrg  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1912458b28Smrg  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2012458b28Smrg  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2112458b28Smrg  THE SOFTWARE.
2212458b28Smrg*/
2312458b28Smrg
2412458b28Smrg#include <stdlib.h>
2512458b28Smrg#include "constlist.h"
2612458b28Smrg
2712458b28SmrgConstListPtr
2812458b28SmrgappendConstList(ConstListPtr first, ConstListPtr second)
2912458b28Smrg{
3012458b28Smrg    ConstListPtr current;
3112458b28Smrg
32b2448f93Smrg    if (second == NULL)
3312458b28Smrg        return first;
3412458b28Smrg
35b2448f93Smrg    if (first == NULL)
3612458b28Smrg        return second;
3712458b28Smrg
38b2448f93Smrg    for (current = first; current->next; current = current->next)
3912458b28Smrg        ;
4012458b28Smrg
4112458b28Smrg    current->next = second;
4212458b28Smrg    return first;
4312458b28Smrg}
4412458b28Smrg
4512458b28SmrgConstListPtr
4612458b28SmrgmakeConstList(const char **a, int n, ConstListPtr old, int begin)
4712458b28Smrg{
48b2448f93Smrg    ConstListPtr first, current;
4912458b28Smrg    int i;
5012458b28Smrg
51b2448f93Smrg    if (n == 0)
5212458b28Smrg        return old;
5312458b28Smrg
5412458b28Smrg    first = malloc(sizeof(ConstListRec));
55b2448f93Smrg    if (!first)
5612458b28Smrg        return NULL;
5712458b28Smrg
5812458b28Smrg    first->value = a[0];
5912458b28Smrg    first->next = NULL;
6012458b28Smrg
6112458b28Smrg    current = first;
62b2448f93Smrg    for (i = 1; i < n; i++) {
63b2448f93Smrg        ConstListPtr next = malloc(sizeof(ConstListRec));
64b2448f93Smrg        if (!next) {
6512458b28Smrg            destroyConstList(first);
6612458b28Smrg            return NULL;
6712458b28Smrg        }
6812458b28Smrg        next->value = a[i];
6912458b28Smrg        next->next = NULL;
7012458b28Smrg
7112458b28Smrg        current->next = next;
7212458b28Smrg        current = next;
7312458b28Smrg    }
74b2448f93Smrg    if (begin) {
7512458b28Smrg        current->next = old;
7612458b28Smrg        return first;
77b2448f93Smrg    }
78b2448f93Smrg    else {
7912458b28Smrg        return appendConstList(old, first);
8012458b28Smrg    }
8112458b28Smrg}
8212458b28Smrg
8312458b28Smrgvoid
8412458b28SmrgdestroyConstList(ConstListPtr old)
8512458b28Smrg{
86b2448f93Smrg    if (!old)
8712458b28Smrg        return;
88b2448f93Smrg    while (old) {
89b2448f93Smrg        ConstListPtr next = old->next;
9012458b28Smrg        free(old);
9112458b28Smrg        old = next;
9212458b28Smrg    }
9312458b28Smrg}
94