VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/RTStrSplit.cpp@ 85154

Last change on this file since 85154 was 85154, checked in by vboxsync, 5 years ago

IPRT: Added RTStrSplit + testcases.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
Line 
1/* $Id: RTStrSplit.cpp 85154 2020-07-09 17:22:51Z vboxsync $ */
2/** @file
3 * IPRT - RTStrSplit.
4 */
5
6/*
7 * Copyright (C) 2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/mem.h>
32#include <iprt/string.h>
33#include "internal/iprt.h"
34
35
36RTDECL(int) RTStrSplit(const char *pcszStrings, size_t cbStrings,
37 const char *pcszSeparator, char ***ppapszStrings, size_t *pcStrings)
38{
39 AssertPtrReturn(pcszStrings, VERR_INVALID_POINTER);
40 AssertReturn(cbStrings, VERR_INVALID_PARAMETER);
41 AssertPtrReturn(pcszSeparator, VERR_INVALID_POINTER);
42 AssertPtrReturn(ppapszStrings, VERR_INVALID_POINTER);
43 AssertPtrReturn(pcStrings, VERR_INVALID_POINTER);
44
45 size_t cStrings = 0;
46
47 /* Determine the number of paths in buffer first. */
48 size_t cch = cbStrings - 1;
49 char const *pszTmp = pcszStrings;
50 const size_t cchSep = strlen(pcszSeparator);
51 while (cch > 0)
52 {
53 char const *pszNext = RTStrStr(pszTmp, pcszSeparator);
54 if (!pszNext)
55 break;
56 const size_t cchNext = pszNext - pszTmp;
57 if (cchNext + cchSep > cch)
58 break;
59 pszTmp += cchNext + cchSep;
60 cch -= cchNext + cchSep;
61 if (cchNext)
62 ++cStrings;
63 }
64
65 if (!cStrings)
66 {
67 *pcStrings = 0;
68 return VINF_SUCCESS;
69 }
70
71 char **papszStrings = (char **)RTMemAllocZ(cStrings * sizeof(char *));
72 if (!papszStrings)
73 return VERR_NO_MEMORY;
74
75 int rc = VINF_SUCCESS;
76
77 cch = cbStrings - 1;
78 pszTmp = pcszStrings;
79
80 for (size_t i = 0; i < 3;)
81 {
82 char const *pszNext = RTStrStr(pszTmp, pcszSeparator);
83 if (!pszNext)
84 break;
85 const size_t cchNext = pszNext - pszTmp;
86 if (cchNext + cchSep > cch)
87 break;
88 if (cchNext)
89 {
90 papszStrings[i] = RTStrDupN(pszTmp, cchNext);
91 if (!papszStrings[i])
92 {
93 rc = VERR_NO_MEMORY;
94 break;
95 }
96 i++;
97 }
98 pszTmp += cchNext + cchSep;
99 cch -= cchNext + cchSep;
100 }
101
102 if (RT_SUCCESS(rc))
103 {
104 *ppapszStrings = papszStrings;
105 *pcStrings = cStrings;
106
107 return VINF_SUCCESS;
108 }
109
110 for (size_t i = 0; i < cStrings; ++i)
111 RTStrFree(papszStrings[i]);
112 RTMemFree(papszStrings);
113
114 return rc;
115}
116RT_EXPORT_SYMBOL(RTStrSplit);
117
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette