strtok/strtok_init_str.c

Go to the documentation of this file.
00001 // Copyright (C) 2006-2007 Benedikt Böhm <hollow@gentoo.org>
00002 //
00003 // This program is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU General Public License
00005 // as published by the Free Software Foundation; either version 2
00006 // of the License, or (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
00016 
00017 #include "mem.h"
00018 #include "str.h"
00019 #include "strtok.h"
00020 
00021 strtok_t *strtok_init_str(strtok_t *st, const char *str, char *delim, int empty)
00022 {
00023         strtok_t *new;
00024         char *scpy, *cur, *token;
00025 
00026         INIT_LIST_HEAD(&(st->list));
00027 
00028         if (!str)
00029                 return st;
00030 
00031         scpy = cur = token = str_dup(str);
00032 
00033         if (!scpy)
00034                 return 0;
00035 
00036         while (token) {
00037                 cur = str_str(cur, delim);
00038 
00039                 if (cur) {
00040                         mem_set(cur, 0, str_len(delim));
00041                         cur += str_len(delim);
00042                 }
00043 
00044                 if (empty || !str_isempty(token)) {
00045                         if (!(new = mem_alloc(sizeof(strtok_t))))
00046                                 goto free;
00047 
00048                         if (!(new->token = str_dup(token)))
00049                                 goto free;
00050 
00051                         list_add_tail(&(new->list), &(st->list));
00052                 }
00053 
00054                 token = cur;
00055         }
00056 
00057         goto out;
00058 
00059 free:
00060         strtok_free(st);
00061         st = 0;
00062 
00063 out:
00064         mem_free(scpy);
00065         return st;
00066 }

Generated on Tue Jun 19 20:38:27 2007 for lucid by  doxygen 1.5.2