ap

Queue manager meant to be used as an audio player
git clone git://jacobedwards.org/ap
Log | Files | Refs | README | LICENSE

split.c (1260B)


      1 /*
      2  * Copyright 2021-2023 Jacob R. Edwards
      3  *
      4  * ap -- audio player
      5  *
      6  * This program is free software: you can redistribute it and/or modify
      7  * it under the terms of the GNU General Public License as published by
      8  * the Free Software Foundation, either version 3 of the License, or
      9  * (at your option) any later version.
     10  *
     11  * This program is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14  * GNU General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU General Public License
     17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
     18  */
     19 
     20 #include <ap/quote.h>
     21 
     22 #include <stdlib.h>
     23 #include <string.h>
     24 
     25 #include "arg.h"
     26 
     27 int
     28 countsplits(char *s, int (*sep)(int))
     29 {
     30 	int n;
     31 
     32 	for (n = 0; apfindquote(&s, sep); ++n)
     33 		;
     34 	if (s)
     35 		return -1;
     36 	return n;
     37 }
     38 
     39 char **
     40 split(char *s, int (*sep)(int))
     41 {
     42 	char **args, **ap;
     43 	int len;
     44 
     45 	len = countsplits(s, sep);
     46 	if (len < 0)
     47 		return NULL;
     48 
     49 	args = calloc(++len, sizeof(*args));
     50 	if (args == NULL)
     51 		return NULL;
     52 
     53 	ap = args;
     54 	while ((*ap = apunquote(&s, sep)))
     55 		++ap;
     56 
     57 	if (s) {
     58 		argfree(args);
     59 		return NULL;
     60 	}
     61 
     62 	return args;
     63 }