FFmpeg
avstring.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <stdio.h>
20 
21 #include "libavutil/common.h"
22 #include "libavutil/mem.h"
23 #include "libavutil/avstring.h"
24 
25 int main(void)
26 {
27  int i;
28  char *fullpath, *ptr;
29  static const char * const strings[] = {
30  "''",
31  "",
32  ":",
33  "\\",
34  "'",
35  " '' :",
36  " '' '' :",
37  "foo '' :",
38  "'foo'",
39  "foo ",
40  " ' foo ' ",
41  "foo\\",
42  "foo': blah:blah",
43  "foo\\: blah:blah",
44  "foo\'",
45  "'foo : ' :blahblah",
46  "\\ :blah",
47  " foo",
48  " foo ",
49  " foo \\ ",
50  "foo ':blah",
51  " foo bar : blahblah",
52  "\\f\\o\\o",
53  "'foo : \\ \\ ' : blahblah",
54  "'\\fo\\o:': blahblah",
55  "\\'fo\\o\\:': foo ' :blahblah"
56  };
57  const char *haystack = "Education consists mainly in what we have unlearned.";
58  const char * const needle[] = {"learned.", "unlearned.", "Unlearned"};
59 
60  printf("Testing av_get_token()\n");
61  for (i = 0; i < FF_ARRAY_ELEMS(strings); i++) {
62  const char *p = strings[i];
63  char *q;
64  printf("|%s|", p);
65  q = av_get_token(&p, ":");
66  printf(" -> |%s|", q);
67  printf(" + |%s|\n", p);
68  av_free(q);
69  }
70 
71  printf("Testing av_append_path_component()\n");
72  #define TEST_APPEND_PATH_COMPONENT(path, component, expected) \
73  fullpath = av_append_path_component((path), (component)); \
74  printf("%s = %s\n", fullpath ? fullpath : "(null)", expected); \
75  av_free(fullpath);
77  TEST_APPEND_PATH_COMPONENT("path", NULL, "path");
78  TEST_APPEND_PATH_COMPONENT(NULL, "comp", "comp");
79  TEST_APPEND_PATH_COMPONENT("path", "comp", "path/comp");
80  TEST_APPEND_PATH_COMPONENT("path/", "comp", "path/comp");
81  TEST_APPEND_PATH_COMPONENT("path", "/comp", "path/comp");
82  TEST_APPEND_PATH_COMPONENT("path/", "/comp", "path/comp");
83  TEST_APPEND_PATH_COMPONENT("path/path2/", "/comp/comp2", "path/path2/comp/comp2");
84 
85  /*Testing av_strnstr()*/
86  #define TEST_STRNSTR(haystack, needle, hay_length, expected) \
87  ptr = av_strnstr(haystack, needle, hay_length); \
88  if (ptr != expected){ \
89  printf("expected: %p, received %p\n", expected, ptr); \
90  }
91  TEST_STRNSTR(haystack, needle [0], strlen(haystack), haystack+44);
92  TEST_STRNSTR(haystack, needle [1], strlen(haystack), haystack+42);
93  TEST_STRNSTR(haystack, needle [2], strlen(haystack), NULL );
94  TEST_STRNSTR(haystack, strings[1], strlen(haystack), haystack );
95 
96  /*Testing av_strireplace()*/
97  #define TEST_STRIREPLACE(haystack, needle, expected) \
98  ptr = av_strireplace(haystack, needle, "instead"); \
99  if (ptr == NULL) { \
100  printf("error, received null pointer!\n"); \
101  } else { \
102  if (strcmp(ptr, expected) != 0) \
103  printf( "expected: %s, received: %s\n", expected, ptr); \
104  av_free(ptr); \
105  }
106 
107  TEST_STRIREPLACE(haystack, needle [0], "Education consists mainly in what we have uninstead");
108  TEST_STRIREPLACE(haystack, needle [1], "Education consists mainly in what we have instead");
109  TEST_STRIREPLACE(haystack, needle [2], "Education consists mainly in what we have instead.");
110  TEST_STRIREPLACE(haystack, needle [1], "Education consists mainly in what we have instead");
111 
112  /*Testing av_d2str()*/
113  #define TEST_D2STR(value, expected) \
114  if((ptr = av_d2str(value)) == NULL){ \
115  printf("error, received null pointer!\n"); \
116  } else { \
117  if(strcmp(ptr, expected) != 0) \
118  printf( "expected: %s, received: %s\n", expected, ptr); \
119  av_free(ptr); \
120  }
121  TEST_D2STR(0 , "0.000000");
122  TEST_D2STR(-1.2333234, "-1.233323");
123  TEST_D2STR(-1.2333237, "-1.233324");
124  return 0;
125 }
#define NULL
Definition: coverity.c:32
Memory handling functions.
#define TEST_D2STR(value, expected)
int main(void)
Definition: avstring.c:25
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition: avstring.c:149
#define TEST_STRIREPLACE(haystack, needle, expected)
#define FF_ARRAY_ELEMS(a)
#define TEST_STRNSTR(haystack, needle, hay_length, expected)
common internal and external API header
#define TEST_APPEND_PATH_COMPONENT(path, component, expected)
#define av_free(p)
printf("static const uint8_t my_array[100] = {\n")