/ libpkg / yuarel.h
yuarel.h
  1  /**
  2   * Copyright (C) 2016 Jack Engqvist Johansson
  3   *
  4   * Permission is hereby granted, free of charge, to any person obtaining a copy
  5   * of this software and associated documentation files (the "Software"), to deal
  6   * in the Software without restriction, including without limitation the rights
  7   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8   * copies of the Software, and to permit persons to whom the Software is
  9   * furnished to do so, subject to the following conditions:
 10   *
 11   * The above copyright notice and this permission notice shall be included in all
 12   * copies or substantial portions of the Software.
 13   *
 14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 20   * SOFTWARE.
 21   */
 22  #ifndef INC_YUAREL_H
 23  #define INC_YUAREL_H
 24  
 25  #ifdef __cplusplus
 26  extern "C" {
 27  #endif
 28  
 29  /**
 30   * The struct where the parsed values will be stored:
 31   *
 32   * scheme ":" [ "//" ] [ username ":" password "@" ] host [ ":" port ] [ "/" ] [ path ] [ "?" query ]
 33   *
 34   * Note: to make sure that no strings are copied, the first slash "/" in the
 35   * path will be used to null terminate the hostname if no port is supplied.
 36   */
 37  struct yuarel {
 38  	char *scheme; /* scheme, without ":" and "//" */
 39  	char *username; /* username, default: NULL */
 40  	char *password; /* password, default: NULL */
 41  	char *host; /* hostname or IP address */
 42  	int port; /* port, default: 0 */
 43  	char *path; /* path, without leading "/", default: NULL */
 44  	char *query; /* query, default: NULL */
 45  	char *fragment; /* fragment, default: NULL */
 46  };
 47  
 48  /* A struct to hold the query string parameter values. */
 49  struct yuarel_param {
 50  	char *key;
 51  	char *val;
 52  };
 53  
 54  /**
 55   * Parse a URL to a struct.
 56   *
 57   * The URL string should be in one of the following formats:
 58   *
 59   * Absolute URL:
 60   * scheme ":" [ "//" ] [ username ":" password "@" ] host [ ":" port ] [ "/" ] [ path ] [ "?" query ] [ "#" fragment ]
 61   *
 62   * Relative URL:
 63   * path [ "?" query ] [ "#" fragment ]
 64   *
 65   * The following parts will be parsed to the corresponding struct member.
 66   *
 67   * *url:     a pointer to the struct where to store the parsed values.
 68   * *url_str: a pointer to the url to be parsed (null terminated). The string
 69   *           will be modified.
 70   *
 71   * Returns 0 on success, otherwise -1.
 72   */
 73  extern int yuarel_parse(struct yuarel *url, char *url_str);
 74  
 75  /**
 76   * Split a path into several strings.
 77   *
 78   * No data is copied, the slashed are used as null terminators and then
 79   * pointers to each path part will be stored in **parts. Double slashes will be
 80   * treated as one.
 81   *
 82   * *path:     the path to split. The string will be modified.
 83   * **parts:   a pointer to an array of (char *) where to store the result.
 84   * max_parts: max number of parts to parse.
 85   *
 86   * Returns the number of parsed items. -1 on error.
 87   */
 88  extern int yuarel_split_path(char *path, char **parts, int max_parts);
 89  
 90  /**
 91   * Parse a query string into a key/value struct.
 92   *
 93   * The query string should be a null terminated string of parameters separated by
 94   * a delimiter. Each parameter are checked for the equal sign character. If it
 95   * appears in the parameter, it will be used as a null terminator and the part
 96   * that comes after it will be the value of the parameter.
 97   *
 98   * No data are copied, the equal sign and delimiters are used as null
 99   * terminators and then pointers to each parameter key and value will be stored
100   * in the yuarel_param struct.
101   *
102   * *query:     the query string to parse. The string will be modified.
103   * delimiter:  the character that separates the key/value pairs from eachother.
104   * *params:    an array of (struct yuarel_param) where to store the result.
105   * max_values: max number of parameters to parse.
106   *
107   * Returns the number of parsed items. -1 on error.
108   */
109  extern int yuarel_parse_query(char *query, char delimiter, struct yuarel_param *params, int max_params);
110  
111  #ifdef __cplusplus
112  }
113  #endif
114  
115  #endif /* INC_YUAREL_H */