| 1 | /*
|
|---|
| 2 | * Copyright (c) 2025 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup fmgt
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file File list.
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <adt/list.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <stdlib.h>
|
|---|
| 38 | #include <str.h>
|
|---|
| 39 |
|
|---|
| 40 | #include "fmgt.h"
|
|---|
| 41 |
|
|---|
| 42 | /** Create file list.
|
|---|
| 43 | *
|
|---|
| 44 | * @param rflist Place to store pointer to new file list.
|
|---|
| 45 | * @return EOK on success, ENOMEM if out of memory.
|
|---|
| 46 | */
|
|---|
| 47 | errno_t fmgt_flist_create(fmgt_flist_t **rflist)
|
|---|
| 48 | {
|
|---|
| 49 | fmgt_flist_t *flist;
|
|---|
| 50 |
|
|---|
| 51 | flist = calloc(1, sizeof(fmgt_flist_t));
|
|---|
| 52 | if (flist == NULL)
|
|---|
| 53 | return ENOMEM;
|
|---|
| 54 |
|
|---|
| 55 | list_initialize(&flist->files);
|
|---|
| 56 | *rflist = flist;
|
|---|
| 57 | return EOK;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /** Append file name to file list.
|
|---|
| 61 | *
|
|---|
| 62 | * @param flist File list
|
|---|
| 63 | * @param fname File name to append
|
|---|
| 64 | * @return EOK on success, ENOMEM if out of memory
|
|---|
| 65 | */
|
|---|
| 66 | errno_t fmgt_flist_append(fmgt_flist_t *flist, const char *fname)
|
|---|
| 67 | {
|
|---|
| 68 | fmgt_flist_entry_t *entry;
|
|---|
| 69 |
|
|---|
| 70 | entry = calloc(1, sizeof(fmgt_flist_entry_t));
|
|---|
| 71 | if (entry == NULL)
|
|---|
| 72 | return ENOMEM;
|
|---|
| 73 |
|
|---|
| 74 | entry->fname = str_dup(fname);
|
|---|
| 75 | if (entry->fname == NULL) {
|
|---|
| 76 | free(entry);
|
|---|
| 77 | return ENOMEM;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | entry->flist = flist;
|
|---|
| 81 | list_append(&entry->lfiles, &flist->files);
|
|---|
| 82 |
|
|---|
| 83 | return EOK;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | /** Destroy file list.
|
|---|
| 87 | *
|
|---|
| 88 | * @param flist File list
|
|---|
| 89 | */
|
|---|
| 90 | void fmgt_flist_destroy(fmgt_flist_t *flist)
|
|---|
| 91 | {
|
|---|
| 92 | fmgt_flist_entry_t *entry;
|
|---|
| 93 |
|
|---|
| 94 | entry = fmgt_flist_first(flist);
|
|---|
| 95 | while (entry != NULL) {
|
|---|
| 96 | list_remove(&entry->lfiles);
|
|---|
| 97 | free(entry->fname);
|
|---|
| 98 | free(entry);
|
|---|
| 99 | entry = fmgt_flist_first(flist);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | free(flist);
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /** Get first file list entry.
|
|---|
| 106 | *
|
|---|
| 107 | * @param flist File list
|
|---|
| 108 | * @return First file list entry or @c NULL iff list list empty
|
|---|
| 109 | */
|
|---|
| 110 | fmgt_flist_entry_t *fmgt_flist_first(fmgt_flist_t *flist)
|
|---|
| 111 | {
|
|---|
| 112 | link_t *link;
|
|---|
| 113 |
|
|---|
| 114 | link = list_first(&flist->files);
|
|---|
| 115 | if (link == NULL)
|
|---|
| 116 | return NULL;
|
|---|
| 117 |
|
|---|
| 118 | return list_get_instance(link, fmgt_flist_entry_t, lfiles);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | /** Get first new list entry.
|
|---|
| 122 | *
|
|---|
| 123 | * @param entry Current file list entry
|
|---|
| 124 | * @return Next file list entry or @c NULL iff @a entry is last
|
|---|
| 125 | */
|
|---|
| 126 | fmgt_flist_entry_t *fmgt_flist_next(fmgt_flist_entry_t *entry)
|
|---|
| 127 | {
|
|---|
| 128 | link_t *link;
|
|---|
| 129 |
|
|---|
| 130 | link = list_next(&entry->lfiles, &entry->flist->files);
|
|---|
| 131 | if (link == NULL)
|
|---|
| 132 | return NULL;
|
|---|
| 133 |
|
|---|
| 134 | return list_get_instance(link, fmgt_flist_entry_t, lfiles);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /** Return number of entries in file list.
|
|---|
| 138 | *
|
|---|
| 139 | * @param flist File list
|
|---|
| 140 | * @return Number of entries in @a flist.
|
|---|
| 141 | */
|
|---|
| 142 | unsigned fmgt_flist_count(fmgt_flist_t *flist)
|
|---|
| 143 | {
|
|---|
| 144 | unsigned count;
|
|---|
| 145 | fmgt_flist_entry_t *entry;
|
|---|
| 146 |
|
|---|
| 147 | count = 0;
|
|---|
| 148 | entry = fmgt_flist_first(flist);
|
|---|
| 149 | while (entry != NULL) {
|
|---|
| 150 | ++count;
|
|---|
| 151 | entry = fmgt_flist_next(entry);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | return count;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /** @}
|
|---|
| 158 | */
|
|---|