2011-09-15 08:07:42 +00:00
|
|
|
/*
|
2008-12-20 10:02:00 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2008-12-20 10:02:00 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* The Original Code is: all of this file.
|
|
|
|
*
|
|
|
|
* Contributor(s): none yet.
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-02-27 20:37:56 +00:00
|
|
|
/** \file blender/blenlib/intern/string.c
|
|
|
|
* \ingroup bli
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2008-12-20 10:02:00 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
2008-12-20 12:43:53 +00:00
|
|
|
#include <ctype.h>
|
2008-12-20 10:02:00 +00:00
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
2009-03-25 20:29:01 +00:00
|
|
|
#include "BLI_dynstr.h"
|
2008-12-20 12:43:53 +00:00
|
|
|
#include "BLI_string.h"
|
|
|
|
|
2012-10-31 04:28:49 +00:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
2013-05-12 06:33:21 +00:00
|
|
|
#ifdef __GNUC__
|
|
|
|
# pragma GCC diagnostic error "-Wsign-conversion"
|
|
|
|
#endif
|
|
|
|
|
2013-07-15 03:54:57 +00:00
|
|
|
// #define DEBUG_STRSIZE
|
|
|
|
|
2013-03-14 10:07:05 +00:00
|
|
|
/**
|
|
|
|
* Duplicates the first \a len bytes of cstring \a str
|
|
|
|
* into a newly mallocN'd string and returns it. \a str
|
|
|
|
* is assumed to be at least len bytes long.
|
|
|
|
*
|
|
|
|
* \param str The string to be duplicated
|
|
|
|
* \param len The number of bytes to duplicate
|
|
|
|
* \retval Returns the duplicated string
|
|
|
|
*/
|
2011-09-28 05:53:40 +00:00
|
|
|
char *BLI_strdupn(const char *str, const size_t len)
|
|
|
|
{
|
2012-05-12 15:13:06 +00:00
|
|
|
char *n = MEM_mallocN(len + 1, "strdup");
|
2008-12-20 10:02:00 +00:00
|
|
|
memcpy(n, str, len);
|
2012-05-12 15:13:06 +00:00
|
|
|
n[len] = '\0';
|
2008-12-20 10:02:00 +00:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
2013-03-14 10:07:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Duplicates the cstring \a str into a newly mallocN'd
|
|
|
|
* string and returns it.
|
|
|
|
*
|
|
|
|
* \param str The string to be duplicated
|
|
|
|
* \retval Returns the duplicated string
|
|
|
|
*/
|
2011-09-28 05:53:40 +00:00
|
|
|
char *BLI_strdup(const char *str)
|
|
|
|
{
|
2008-12-20 10:02:00 +00:00
|
|
|
return BLI_strdupn(str, strlen(str));
|
|
|
|
}
|
|
|
|
|
2013-03-14 10:07:05 +00:00
|
|
|
/**
|
|
|
|
* Appends the two strings, and returns new mallocN'ed string
|
|
|
|
* \param str1 first string for copy
|
|
|
|
* \param str2 second string for append
|
|
|
|
* \retval Returns dst
|
|
|
|
*/
|
2012-11-23 15:12:13 +00:00
|
|
|
char *BLI_strdupcat(const char *__restrict str1, const char *__restrict str2)
|
Drag and drop 2.5 integration! Finally, slashdot regulars can use
Blender too now! :)
** Drag works as follows:
- drag-able items are defined by the standard interface ui toolkit
- each button can get this feature, via uiButSetDragXXX(but, ...).
There are calls to define drag-able images, ID blocks, RNA paths,
file paths, and so on. By default you drag an icon, exceptionally
an ImBuf
- Drag items are registered centrally in the WM, it allows more drag
items simultaneous too, but not implemented
** Drop works as follows:
- On mouse release, and if drag items exist in the WM, it converts
the mouse event to an EVT_DROP type. This event then gets the full
drag info as customdata
- drop regions are defined with WM_dropbox_add(), similar to keymaps
you can make a "drop map" this way, which become 'drop map handlers'
in the queues.
- next to that the UI kit handles some common button types (like
accepting ID or names) to be catching a drop event too.
- Every "drop box" has two callbacks:
- poll() = check if the event drag data is relevant for this box
- copy() = fill in custom properties in the dropbox to initialize
an operator
- The dropbox handler then calls its standard Operator with its
dropbox properties.
** Currently implemented
Drag items:
- ID icons in browse buttons
- ID icons in context menu of properties region
- ID icons in outliner and rna viewer
- FileBrowser icons
- FileBrowser preview images
Drag-able icons are subtly visualized by making them brighter a bit
on mouse-over. In case the icon is a button or UI element too (most
cases), the drag-able feature will make the item react to
mouse-release instead of mouse-press.
Drop options:
- UI buttons: ID and text buttons (paste name)
- View3d: Object ID drop copies object
- View3d: Material ID drop assigns to object under cursor
- View3d: Image ID drop assigns to object UV texture under cursor
- Sequencer: Path drop will add either Image or Movie strip
- Image window: Path drop will open image
** Drag and drop Notes:
- Dropping into another Blender window (from same application) works
too. I've added code that passes on mousemoves and clicks to other
windows, without activating them though. This does make using multi-window
Blender a bit friendler.
- Dropping a file path to an image, is not the same as dropping an
Image ID... keep this in mind. Sequencer for example wants paths to
be dropped, textures in 3d window wants an Image ID.
- Although drop boxes could be defined via Python, I suggest they're
part of the UI and editor design (= how we want an editor to work), and
not default offered configurable like keymaps.
- At the moment only one item can be dragged at a time. This is for
several reasons.... For one, Blender doesn't have a well defined
uniform way to define "what is selected" (files, outliner items, etc).
Secondly there's potential conflicts on what todo when you drop mixed
drag sets on spots. All undefined stuff... nice for later.
- Example to bypass the above: a collection of images that form a strip,
should be represented in filewindow as a single sequence anyway.
This then will fit well and gets handled neatly by design.
- Another option to check is to allow multiple options per drop... it
could show the operator as a sort of menu, allowing arrow or scrollwheel
to choose. For time being I'd prefer to try to design a singular drop
though, just offer only one drop action per data type on given spots.
- What does work already, but a tad slow, is to use a function that
detects an object (type) under cursor, so a drag item's option can be
further refined (like drop object on object = parent). (disabled)
** More notes
- Added saving for Region layouts (like split points for toolbar)
- Label buttons now handle mouse over
- File list: added full path entry for drop feature.
- Filesel bugfix: wm_operator_exec() got called there and fully handled,
while WM event code tried same. Added new OPERATOR_HANDLED flag for this.
Maybe python needs it too?
- Cocoa: added window move event, so multi-win setups work OK (didnt save).
- Interface_handlers.c: removed win->active
- Severe area copy bug: area handlers were not set to NULL
- Filesel bugfix: next/prev folder list was not copied on area copies
** Leftover todos
- Cocoa windows seem to hang on cases still... needs check
- Cocoa 'draw overlap' swap doesn't work
- Cocoa window loses focus permanently on using Spotlight
(for these reasons, makefile building has Carbon as default atm)
- ListView templates in UI cannot become dragged yet, needs review...
it consists of two overlapping UI elements, preventing handling icon clicks.
- There's already Ghost library code to handle dropping from OS
into Blender window. I've noticed this code is unfinished for Macs, but
seems to be complete for Windows. Needs test... currently, an external
drop event will print in console when succesfully delivered to Blender's WM.
2010-01-26 18:18:21 +00:00
|
|
|
{
|
2013-07-23 12:49:30 +00:00
|
|
|
/* include the NULL terminator of str2 only */
|
|
|
|
const size_t str1_len = strlen(str1);
|
|
|
|
const size_t str2_len = strlen(str2) + 1;
|
|
|
|
char *str, *s;
|
Drag and drop 2.5 integration! Finally, slashdot regulars can use
Blender too now! :)
** Drag works as follows:
- drag-able items are defined by the standard interface ui toolkit
- each button can get this feature, via uiButSetDragXXX(but, ...).
There are calls to define drag-able images, ID blocks, RNA paths,
file paths, and so on. By default you drag an icon, exceptionally
an ImBuf
- Drag items are registered centrally in the WM, it allows more drag
items simultaneous too, but not implemented
** Drop works as follows:
- On mouse release, and if drag items exist in the WM, it converts
the mouse event to an EVT_DROP type. This event then gets the full
drag info as customdata
- drop regions are defined with WM_dropbox_add(), similar to keymaps
you can make a "drop map" this way, which become 'drop map handlers'
in the queues.
- next to that the UI kit handles some common button types (like
accepting ID or names) to be catching a drop event too.
- Every "drop box" has two callbacks:
- poll() = check if the event drag data is relevant for this box
- copy() = fill in custom properties in the dropbox to initialize
an operator
- The dropbox handler then calls its standard Operator with its
dropbox properties.
** Currently implemented
Drag items:
- ID icons in browse buttons
- ID icons in context menu of properties region
- ID icons in outliner and rna viewer
- FileBrowser icons
- FileBrowser preview images
Drag-able icons are subtly visualized by making them brighter a bit
on mouse-over. In case the icon is a button or UI element too (most
cases), the drag-able feature will make the item react to
mouse-release instead of mouse-press.
Drop options:
- UI buttons: ID and text buttons (paste name)
- View3d: Object ID drop copies object
- View3d: Material ID drop assigns to object under cursor
- View3d: Image ID drop assigns to object UV texture under cursor
- Sequencer: Path drop will add either Image or Movie strip
- Image window: Path drop will open image
** Drag and drop Notes:
- Dropping into another Blender window (from same application) works
too. I've added code that passes on mousemoves and clicks to other
windows, without activating them though. This does make using multi-window
Blender a bit friendler.
- Dropping a file path to an image, is not the same as dropping an
Image ID... keep this in mind. Sequencer for example wants paths to
be dropped, textures in 3d window wants an Image ID.
- Although drop boxes could be defined via Python, I suggest they're
part of the UI and editor design (= how we want an editor to work), and
not default offered configurable like keymaps.
- At the moment only one item can be dragged at a time. This is for
several reasons.... For one, Blender doesn't have a well defined
uniform way to define "what is selected" (files, outliner items, etc).
Secondly there's potential conflicts on what todo when you drop mixed
drag sets on spots. All undefined stuff... nice for later.
- Example to bypass the above: a collection of images that form a strip,
should be represented in filewindow as a single sequence anyway.
This then will fit well and gets handled neatly by design.
- Another option to check is to allow multiple options per drop... it
could show the operator as a sort of menu, allowing arrow or scrollwheel
to choose. For time being I'd prefer to try to design a singular drop
though, just offer only one drop action per data type on given spots.
- What does work already, but a tad slow, is to use a function that
detects an object (type) under cursor, so a drag item's option can be
further refined (like drop object on object = parent). (disabled)
** More notes
- Added saving for Region layouts (like split points for toolbar)
- Label buttons now handle mouse over
- File list: added full path entry for drop feature.
- Filesel bugfix: wm_operator_exec() got called there and fully handled,
while WM event code tried same. Added new OPERATOR_HANDLED flag for this.
Maybe python needs it too?
- Cocoa: added window move event, so multi-win setups work OK (didnt save).
- Interface_handlers.c: removed win->active
- Severe area copy bug: area handlers were not set to NULL
- Filesel bugfix: next/prev folder list was not copied on area copies
** Leftover todos
- Cocoa windows seem to hang on cases still... needs check
- Cocoa 'draw overlap' swap doesn't work
- Cocoa window loses focus permanently on using Spotlight
(for these reasons, makefile building has Carbon as default atm)
- ListView templates in UI cannot become dragged yet, needs review...
it consists of two overlapping UI elements, preventing handling icon clicks.
- There's already Ghost library code to handle dropping from OS
into Blender window. I've noticed this code is unfinished for Macs, but
seems to be complete for Windows. Needs test... currently, an external
drop event will print in console when succesfully delivered to Blender's WM.
2010-01-26 18:18:21 +00:00
|
|
|
|
2013-07-23 12:49:30 +00:00
|
|
|
str = MEM_mallocN(str1_len + str2_len, "strdupcat");
|
|
|
|
s = str;
|
|
|
|
|
|
|
|
memcpy(s, str1, str1_len); s += str1_len;
|
|
|
|
memcpy(s, str2, str2_len);
|
|
|
|
|
|
|
|
return str;
|
Drag and drop 2.5 integration! Finally, slashdot regulars can use
Blender too now! :)
** Drag works as follows:
- drag-able items are defined by the standard interface ui toolkit
- each button can get this feature, via uiButSetDragXXX(but, ...).
There are calls to define drag-able images, ID blocks, RNA paths,
file paths, and so on. By default you drag an icon, exceptionally
an ImBuf
- Drag items are registered centrally in the WM, it allows more drag
items simultaneous too, but not implemented
** Drop works as follows:
- On mouse release, and if drag items exist in the WM, it converts
the mouse event to an EVT_DROP type. This event then gets the full
drag info as customdata
- drop regions are defined with WM_dropbox_add(), similar to keymaps
you can make a "drop map" this way, which become 'drop map handlers'
in the queues.
- next to that the UI kit handles some common button types (like
accepting ID or names) to be catching a drop event too.
- Every "drop box" has two callbacks:
- poll() = check if the event drag data is relevant for this box
- copy() = fill in custom properties in the dropbox to initialize
an operator
- The dropbox handler then calls its standard Operator with its
dropbox properties.
** Currently implemented
Drag items:
- ID icons in browse buttons
- ID icons in context menu of properties region
- ID icons in outliner and rna viewer
- FileBrowser icons
- FileBrowser preview images
Drag-able icons are subtly visualized by making them brighter a bit
on mouse-over. In case the icon is a button or UI element too (most
cases), the drag-able feature will make the item react to
mouse-release instead of mouse-press.
Drop options:
- UI buttons: ID and text buttons (paste name)
- View3d: Object ID drop copies object
- View3d: Material ID drop assigns to object under cursor
- View3d: Image ID drop assigns to object UV texture under cursor
- Sequencer: Path drop will add either Image or Movie strip
- Image window: Path drop will open image
** Drag and drop Notes:
- Dropping into another Blender window (from same application) works
too. I've added code that passes on mousemoves and clicks to other
windows, without activating them though. This does make using multi-window
Blender a bit friendler.
- Dropping a file path to an image, is not the same as dropping an
Image ID... keep this in mind. Sequencer for example wants paths to
be dropped, textures in 3d window wants an Image ID.
- Although drop boxes could be defined via Python, I suggest they're
part of the UI and editor design (= how we want an editor to work), and
not default offered configurable like keymaps.
- At the moment only one item can be dragged at a time. This is for
several reasons.... For one, Blender doesn't have a well defined
uniform way to define "what is selected" (files, outliner items, etc).
Secondly there's potential conflicts on what todo when you drop mixed
drag sets on spots. All undefined stuff... nice for later.
- Example to bypass the above: a collection of images that form a strip,
should be represented in filewindow as a single sequence anyway.
This then will fit well and gets handled neatly by design.
- Another option to check is to allow multiple options per drop... it
could show the operator as a sort of menu, allowing arrow or scrollwheel
to choose. For time being I'd prefer to try to design a singular drop
though, just offer only one drop action per data type on given spots.
- What does work already, but a tad slow, is to use a function that
detects an object (type) under cursor, so a drag item's option can be
further refined (like drop object on object = parent). (disabled)
** More notes
- Added saving for Region layouts (like split points for toolbar)
- Label buttons now handle mouse over
- File list: added full path entry for drop feature.
- Filesel bugfix: wm_operator_exec() got called there and fully handled,
while WM event code tried same. Added new OPERATOR_HANDLED flag for this.
Maybe python needs it too?
- Cocoa: added window move event, so multi-win setups work OK (didnt save).
- Interface_handlers.c: removed win->active
- Severe area copy bug: area handlers were not set to NULL
- Filesel bugfix: next/prev folder list was not copied on area copies
** Leftover todos
- Cocoa windows seem to hang on cases still... needs check
- Cocoa 'draw overlap' swap doesn't work
- Cocoa window loses focus permanently on using Spotlight
(for these reasons, makefile building has Carbon as default atm)
- ListView templates in UI cannot become dragged yet, needs review...
it consists of two overlapping UI elements, preventing handling icon clicks.
- There's already Ghost library code to handle dropping from OS
into Blender window. I've noticed this code is unfinished for Macs, but
seems to be complete for Windows. Needs test... currently, an external
drop event will print in console when succesfully delivered to Blender's WM.
2010-01-26 18:18:21 +00:00
|
|
|
}
|
|
|
|
|
2013-03-14 10:07:05 +00:00
|
|
|
/**
|
|
|
|
* Like strncpy but ensures dst is always
|
|
|
|
* '\0' terminated.
|
|
|
|
*
|
|
|
|
* \param dst Destination for copy
|
|
|
|
* \param src Source string to copy
|
|
|
|
* \param maxncpy Maximum number of characters to copy (generally
|
|
|
|
* the size of dst)
|
|
|
|
* \retval Returns dst
|
|
|
|
*/
|
2012-11-23 15:12:13 +00:00
|
|
|
char *BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy)
|
2011-09-28 05:53:40 +00:00
|
|
|
{
|
2013-03-14 09:49:20 +00:00
|
|
|
size_t srclen = BLI_strnlen(src, maxncpy - 1);
|
2012-10-31 04:28:49 +00:00
|
|
|
BLI_assert(maxncpy != 0);
|
2013-03-14 09:49:20 +00:00
|
|
|
|
2013-07-15 03:54:57 +00:00
|
|
|
#ifdef DEBUG_STRSIZE
|
|
|
|
memset(dst, 0xff, sizeof(*dst) * maxncpy);
|
|
|
|
#endif
|
|
|
|
|
2013-03-14 09:49:20 +00:00
|
|
|
memcpy(dst, src, srclen);
|
|
|
|
dst[srclen] = '\0';
|
2008-12-20 10:02:00 +00:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2015-01-03 10:13:02 +01:00
|
|
|
/**
|
|
|
|
* Like BLI_strncpy but ensures dst is always padded by given char, on both sides (unless src is empty).
|
|
|
|
*
|
|
|
|
* \param dst Destination for copy
|
|
|
|
* \param src Source string to copy
|
|
|
|
* \param pad the char to use for padding
|
|
|
|
* \param maxncpy Maximum number of characters to copy (generally the size of dst)
|
|
|
|
* \retval Returns dst
|
|
|
|
*/
|
|
|
|
char *BLI_strncpy_ensure_pad(char *__restrict dst, const char *__restrict src, const char pad, size_t maxncpy)
|
|
|
|
{
|
|
|
|
BLI_assert(maxncpy != 0);
|
|
|
|
|
|
|
|
#ifdef DEBUG_STRSIZE
|
|
|
|
memset(dst, 0xff, sizeof(*dst) * maxncpy);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (src[0] == '\0') {
|
|
|
|
dst[0] = '\0';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Add heading/trailing wildcards if needed. */
|
|
|
|
size_t idx = 0;
|
|
|
|
size_t srclen;
|
|
|
|
|
|
|
|
if (src[idx] != pad) {
|
|
|
|
dst[idx++] = pad;
|
|
|
|
maxncpy--;
|
|
|
|
}
|
|
|
|
maxncpy--; /* trailing '\0' */
|
|
|
|
|
|
|
|
srclen = BLI_strnlen(src, maxncpy);
|
|
|
|
if ((src[srclen - 1] != pad) && (srclen == maxncpy)) {
|
|
|
|
srclen--;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&dst[idx], src, srclen);
|
|
|
|
idx += srclen;
|
|
|
|
|
|
|
|
if (dst[idx - 1] != pad) {
|
|
|
|
dst[idx++] = pad;
|
|
|
|
}
|
|
|
|
dst[idx] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2013-03-14 10:07:05 +00:00
|
|
|
/**
|
|
|
|
* Like strncpy but ensures dst is always
|
|
|
|
* '\0' terminated.
|
|
|
|
*
|
|
|
|
* \note This is a duplicate of #BLI_strncpy that returns bytes copied.
|
|
|
|
* And is a drop in replacement for 'snprintf(str, sizeof(str), "%s", arg);'
|
|
|
|
*
|
|
|
|
* \param dst Destination for copy
|
|
|
|
* \param src Source string to copy
|
|
|
|
* \param maxncpy Maximum number of characters to copy (generally
|
|
|
|
* the size of dst)
|
|
|
|
* \retval The number of bytes copied (The only difference from BLI_strncpy).
|
|
|
|
*/
|
|
|
|
size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, const size_t maxncpy)
|
|
|
|
{
|
|
|
|
size_t srclen = BLI_strnlen(src, maxncpy - 1);
|
|
|
|
BLI_assert(maxncpy != 0);
|
|
|
|
|
2013-07-15 03:54:57 +00:00
|
|
|
#ifdef DEBUG_STRSIZE
|
|
|
|
memset(dst, 0xff, sizeof(*dst) * maxncpy);
|
|
|
|
#endif
|
|
|
|
|
2013-03-14 10:07:05 +00:00
|
|
|
memcpy(dst, src, srclen);
|
|
|
|
dst[srclen] = '\0';
|
|
|
|
return srclen;
|
|
|
|
}
|
|
|
|
|
2013-06-16 08:29:02 +00:00
|
|
|
size_t BLI_strcpy_rlen(char *__restrict dst, const char *__restrict src)
|
|
|
|
{
|
|
|
|
size_t srclen = strlen(src);
|
|
|
|
memcpy(dst, src, srclen + 1);
|
|
|
|
return srclen;
|
|
|
|
}
|
|
|
|
|
2013-03-14 10:07:05 +00:00
|
|
|
/**
|
|
|
|
* Portable replacement for #vsnprintf
|
|
|
|
*/
|
2013-07-15 03:54:57 +00:00
|
|
|
size_t BLI_vsnprintf(char *__restrict buffer, size_t maxncpy, const char *__restrict format, va_list arg)
|
2008-12-20 10:02:00 +00:00
|
|
|
{
|
2011-03-25 13:40:44 +00:00
|
|
|
size_t n;
|
2008-12-20 10:02:00 +00:00
|
|
|
|
2012-11-28 06:43:04 +00:00
|
|
|
BLI_assert(buffer != NULL);
|
2013-07-15 03:54:57 +00:00
|
|
|
BLI_assert(maxncpy > 0);
|
2012-11-28 06:43:04 +00:00
|
|
|
BLI_assert(format != NULL);
|
|
|
|
|
2013-07-15 03:54:57 +00:00
|
|
|
n = (size_t)vsnprintf(buffer, maxncpy, format, arg);
|
2012-08-26 11:01:14 +00:00
|
|
|
|
2013-07-15 03:54:57 +00:00
|
|
|
if (n != -1 && n < maxncpy) {
|
2008-12-20 10:02:00 +00:00
|
|
|
buffer[n] = '\0';
|
2011-09-28 05:53:40 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-07-15 03:54:57 +00:00
|
|
|
buffer[maxncpy - 1] = '\0';
|
2008-12-20 10:02:00 +00:00
|
|
|
}
|
2012-08-26 11:01:14 +00:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2015-04-22 05:37:22 +10:00
|
|
|
/**
|
|
|
|
* A version of #BLI_vsnprintf that returns ``strlen(buffer)``
|
|
|
|
*/
|
|
|
|
size_t BLI_vsnprintf_rlen(char *__restrict buffer, size_t maxncpy, const char *__restrict format, va_list arg)
|
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
BLI_assert(buffer != NULL);
|
|
|
|
BLI_assert(maxncpy > 0);
|
|
|
|
BLI_assert(format != NULL);
|
|
|
|
|
|
|
|
n = (size_t)vsnprintf(buffer, maxncpy, format, arg);
|
|
|
|
|
|
|
|
if (n != -1 && n < maxncpy) {
|
|
|
|
/* pass */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
n = maxncpy - 1;
|
|
|
|
}
|
|
|
|
buffer[n] = '\0';
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2013-03-14 10:07:05 +00:00
|
|
|
/**
|
|
|
|
* Portable replacement for #snprintf
|
|
|
|
*/
|
2013-07-15 03:54:57 +00:00
|
|
|
size_t BLI_snprintf(char *__restrict dst, size_t maxncpy, const char *__restrict format, ...)
|
2012-08-26 11:01:14 +00:00
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
va_list arg;
|
|
|
|
|
2013-07-15 03:54:57 +00:00
|
|
|
#ifdef DEBUG_STRSIZE
|
|
|
|
memset(dst, 0xff, sizeof(*dst) * maxncpy);
|
|
|
|
#endif
|
|
|
|
|
2012-08-26 11:01:14 +00:00
|
|
|
va_start(arg, format);
|
2013-07-15 03:54:57 +00:00
|
|
|
n = BLI_vsnprintf(dst, maxncpy, format, arg);
|
2008-12-20 10:02:00 +00:00
|
|
|
va_end(arg);
|
2012-08-26 11:01:14 +00:00
|
|
|
|
2008-12-20 10:02:00 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2015-04-22 05:37:22 +10:00
|
|
|
/**
|
|
|
|
* A version of #BLI_snprintf that returns ``strlen(dst)``
|
|
|
|
*/
|
|
|
|
size_t BLI_snprintf_rlen(char *__restrict dst, size_t maxncpy, const char *__restrict format, ...)
|
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
va_list arg;
|
|
|
|
|
|
|
|
#ifdef DEBUG_STRSIZE
|
|
|
|
memset(dst, 0xff, sizeof(*dst) * maxncpy);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
va_start(arg, format);
|
|
|
|
n = BLI_vsnprintf_rlen(dst, maxncpy, format, arg);
|
|
|
|
va_end(arg);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2013-03-14 10:07:05 +00:00
|
|
|
/**
|
|
|
|
* Print formatted string into a newly #MEM_mallocN'd string
|
|
|
|
* and return it.
|
|
|
|
*/
|
2012-11-23 15:12:13 +00:00
|
|
|
char *BLI_sprintfN(const char *__restrict format, ...)
|
2009-03-25 20:29:01 +00:00
|
|
|
{
|
|
|
|
DynStr *ds;
|
|
|
|
va_list arg;
|
|
|
|
char *n;
|
|
|
|
|
2012-11-28 06:43:04 +00:00
|
|
|
BLI_assert(format != NULL);
|
|
|
|
|
2009-03-25 20:29:01 +00:00
|
|
|
va_start(arg, format);
|
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
ds = BLI_dynstr_new();
|
2009-03-25 20:29:01 +00:00
|
|
|
BLI_dynstr_vappendf(ds, format, arg);
|
2012-05-12 15:13:06 +00:00
|
|
|
n = BLI_dynstr_get_cstring(ds);
|
2009-03-25 20:29:01 +00:00
|
|
|
BLI_dynstr_free(ds);
|
|
|
|
|
|
|
|
va_end(arg);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2011-08-23 15:08:54 +00:00
|
|
|
|
|
|
|
/* match pythons string escaping, assume double quotes - (")
|
|
|
|
* TODO: should be used to create RNA animation paths.
|
|
|
|
* TODO: support more fancy string escaping. current code is primitive
|
|
|
|
* this basically is an ascii version of PyUnicode_EncodeUnicodeEscape()
|
|
|
|
* which is a useful reference. */
|
2012-11-23 15:12:13 +00:00
|
|
|
size_t BLI_strescape(char *__restrict dst, const char *__restrict src, const size_t maxncpy)
|
2011-08-23 15:08:54 +00:00
|
|
|
{
|
2012-05-12 15:13:06 +00:00
|
|
|
size_t len = 0;
|
2012-10-31 04:28:49 +00:00
|
|
|
|
|
|
|
BLI_assert(maxncpy != 0);
|
|
|
|
|
|
|
|
while (len < maxncpy) {
|
2012-04-28 06:31:57 +00:00
|
|
|
switch (*src) {
|
2011-08-23 15:08:54 +00:00
|
|
|
case '\0':
|
2011-09-06 07:08:20 +00:00
|
|
|
goto escape_finish;
|
2011-08-23 15:08:54 +00:00
|
|
|
case '\\':
|
|
|
|
case '"':
|
2013-07-21 08:16:37 +00:00
|
|
|
/* fall-through */
|
2011-08-23 15:08:54 +00:00
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
/* less common but should also be support */
|
2011-08-23 15:08:54 +00:00
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
2012-10-31 04:28:49 +00:00
|
|
|
if (len + 1 < maxncpy) {
|
2011-08-23 15:08:54 +00:00
|
|
|
*dst++ = '\\';
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* not enough space to escape */
|
|
|
|
break;
|
|
|
|
}
|
2013-07-21 08:16:37 +00:00
|
|
|
/* fall-through */
|
2011-08-23 15:08:54 +00:00
|
|
|
default:
|
|
|
|
*dst = *src;
|
2013-07-21 08:16:37 +00:00
|
|
|
break;
|
2011-08-23 15:08:54 +00:00
|
|
|
}
|
|
|
|
dst++;
|
|
|
|
src++;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
|
2011-09-06 07:08:20 +00:00
|
|
|
escape_finish:
|
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
*dst = '\0';
|
2011-08-28 09:28:41 +00:00
|
|
|
|
2011-08-23 15:08:54 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2013-03-14 10:07:05 +00:00
|
|
|
/**
|
|
|
|
* Makes a copy of the text within the "" that appear after some text 'blahblah'
|
2009-10-12 11:27:34 +00:00
|
|
|
* i.e. for string 'pose["apples"]' with prefix 'pose[', it should grab "apples"
|
|
|
|
*
|
2013-03-14 10:07:05 +00:00
|
|
|
* - str: is the entire string to chop
|
|
|
|
* - prefix: is the part of the string to leave out
|
|
|
|
*
|
|
|
|
* Assume that the strings returned must be freed afterwards, and that the inputs will contain
|
2009-10-12 11:27:34 +00:00
|
|
|
* data we want...
|
2012-07-09 22:16:50 +00:00
|
|
|
*
|
2013-03-14 10:07:05 +00:00
|
|
|
* \return the offset and a length so as to avoid doing an allocation.
|
2009-10-12 11:27:34 +00:00
|
|
|
*/
|
2012-11-23 15:12:13 +00:00
|
|
|
char *BLI_str_quoted_substrN(const char *__restrict str, const char *__restrict prefix)
|
2009-10-12 11:27:34 +00:00
|
|
|
{
|
2011-03-25 13:40:44 +00:00
|
|
|
size_t prefixLen = strlen(prefix);
|
2014-04-27 00:20:55 +10:00
|
|
|
const char *startMatch, *endMatch;
|
2009-10-12 11:27:34 +00:00
|
|
|
|
|
|
|
/* get the starting point (i.e. where prefix starts, and add prefixLen+1 to it to get be after the first " */
|
2012-05-12 15:13:06 +00:00
|
|
|
startMatch = strstr(str, prefix) + prefixLen + 1;
|
2013-02-17 18:46:50 +00:00
|
|
|
if (startMatch) {
|
|
|
|
/* get the end point (i.e. where the next occurance of " is after the starting point) */
|
2014-05-29 20:42:09 +10:00
|
|
|
|
|
|
|
endMatch = startMatch;
|
|
|
|
while ((endMatch = strchr(endMatch, '"'))) {
|
|
|
|
if (LIKELY(*(endMatch - 1) != '\\')) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
endMatch++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endMatch) {
|
2013-02-17 18:46:50 +00:00
|
|
|
/* return the slice indicated */
|
|
|
|
return BLI_strdupn(startMatch, (size_t)(endMatch - startMatch));
|
2014-05-29 20:42:09 +10:00
|
|
|
}
|
2013-02-17 18:46:50 +00:00
|
|
|
}
|
|
|
|
return BLI_strdupn("", 0);
|
2009-10-12 11:27:34 +00:00
|
|
|
}
|
|
|
|
|
2013-03-14 10:07:05 +00:00
|
|
|
/**
|
2013-07-27 14:05:59 +00:00
|
|
|
* string with all instances of substr_old replaced with substr_new,
|
2013-03-14 10:07:05 +00:00
|
|
|
* Returns a copy of the cstring \a str into a newly mallocN'd
|
|
|
|
* and returns it.
|
|
|
|
*
|
|
|
|
* \note A rather wasteful string-replacement utility, though this shall do for now...
|
|
|
|
* Feel free to replace this with an even safe + nicer alternative
|
|
|
|
*
|
2013-07-27 14:05:59 +00:00
|
|
|
* \param str The string to replace occurrences of substr_old in
|
|
|
|
* \param substr_old The text in the string to find and replace
|
|
|
|
* \param substr_new The text in the string to find and replace
|
2013-03-14 10:07:05 +00:00
|
|
|
* \retval Returns the duplicated string
|
2009-10-09 12:18:32 +00:00
|
|
|
*/
|
2013-07-27 14:05:59 +00:00
|
|
|
char *BLI_replacestrN(const char *__restrict str, const char *__restrict substr_old, const char *__restrict substr_new)
|
2009-10-09 12:18:32 +00:00
|
|
|
{
|
2012-05-12 15:13:06 +00:00
|
|
|
DynStr *ds = NULL;
|
2013-07-27 14:05:59 +00:00
|
|
|
size_t len_old = strlen(substr_old);
|
|
|
|
const char *match;
|
|
|
|
|
|
|
|
BLI_assert(substr_old[0] != '\0');
|
|
|
|
|
2009-10-09 12:18:32 +00:00
|
|
|
/* while we can still find a match for the old substring that we're searching for,
|
|
|
|
* keep dicing and replacing
|
|
|
|
*/
|
2013-07-27 14:05:59 +00:00
|
|
|
while ((match = strstr(str, substr_old))) {
|
2009-10-09 12:18:32 +00:00
|
|
|
/* the assembly buffer only gets created when we actually need to rebuild the string */
|
|
|
|
if (ds == NULL)
|
2012-05-12 15:13:06 +00:00
|
|
|
ds = BLI_dynstr_new();
|
2009-10-09 12:18:32 +00:00
|
|
|
|
|
|
|
/* if the match position does not match the current position in the string,
|
|
|
|
* copy the text up to this position and advance the current position in the string
|
|
|
|
*/
|
|
|
|
if (str != match) {
|
2013-07-27 14:05:59 +00:00
|
|
|
/* add the segment of the string from str to match to the buffer, then restore the value at match
|
2009-10-09 12:18:32 +00:00
|
|
|
*/
|
2013-07-27 14:05:59 +00:00
|
|
|
BLI_dynstr_nappend(ds, str, (match - str));
|
2009-10-09 12:18:32 +00:00
|
|
|
|
|
|
|
/* now our current position should be set on the start of the match */
|
2012-05-12 15:13:06 +00:00
|
|
|
str = match;
|
2009-10-09 12:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add the replacement text to the accumulation buffer */
|
2013-07-27 14:05:59 +00:00
|
|
|
BLI_dynstr_append(ds, substr_new);
|
2009-10-09 12:18:32 +00:00
|
|
|
|
|
|
|
/* advance the current position of the string up to the end of the replaced segment */
|
2013-07-27 14:05:59 +00:00
|
|
|
str += len_old;
|
2009-10-09 12:18:32 +00:00
|
|
|
}
|
|
|
|
|
2012-03-01 12:20:18 +00:00
|
|
|
/* finish off and return a new string that has had all occurrences of */
|
2009-10-09 12:18:32 +00:00
|
|
|
if (ds) {
|
2013-07-27 14:05:59 +00:00
|
|
|
char *str_new;
|
2009-10-09 12:18:32 +00:00
|
|
|
|
|
|
|
/* add what's left of the string to the assembly buffer
|
2013-07-28 06:37:58 +00:00
|
|
|
* - we've been adjusting str to point at the end of the replaced segments
|
2009-10-09 12:18:32 +00:00
|
|
|
*/
|
2013-07-28 06:37:58 +00:00
|
|
|
BLI_dynstr_append(ds, str);
|
2009-10-09 12:18:32 +00:00
|
|
|
|
|
|
|
/* convert to new c-string (MEM_malloc'd), and free the buffer */
|
2013-07-27 14:05:59 +00:00
|
|
|
str_new = BLI_dynstr_get_cstring(ds);
|
2009-10-09 12:18:32 +00:00
|
|
|
BLI_dynstr_free(ds);
|
|
|
|
|
2013-07-27 14:05:59 +00:00
|
|
|
return str_new;
|
2009-10-09 12:18:32 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* just create a new copy of the entire string - we avoid going through the assembly buffer
|
|
|
|
* for what should be a bit more efficiency...
|
|
|
|
*/
|
|
|
|
return BLI_strdup(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-14 10:07:05 +00:00
|
|
|
/**
|
|
|
|
* Compare two strings without regard to case.
|
|
|
|
*
|
|
|
|
* \retval True if the strings are equal, false otherwise.
|
|
|
|
*/
|
2.5
More cleanup!
- removed old UI font completely, including from uiBeginBlock
- emboss hints for uiBlock only have three types now;
Regular, Pulldown, or "Nothing" (only icon/text)
- removed old font path from Userdef
- removed all old button theme hinting
- removed old "auto block" to merge buttons in groups
(was only in use for radiosity buttons)
And went over all warnings. One hooray for make giving clean output :)
Well, we need uniform definitions for warnings, so people at least fix
them... here's the real bad bugs I found:
- in mesh code, a call to editmesh mixed *em and *me
- in armature, ED_util.h was not included, so no warnings for wrong call
to ED_undo_push()
- The extern Py api .h was not included in the bpy_interface.c, showing
a several calls using different args.
Further just added the missing includes, and removed unused vars.
2009-04-14 15:59:52 +00:00
|
|
|
int BLI_strcaseeq(const char *a, const char *b)
|
|
|
|
{
|
2012-05-12 15:13:06 +00:00
|
|
|
return (BLI_strcasecmp(a, b) == 0);
|
2008-12-20 10:02:00 +00:00
|
|
|
}
|
|
|
|
|
2013-03-14 10:07:05 +00:00
|
|
|
/**
|
|
|
|
* Portable replacement for #strcasestr (not available in MSVC)
|
|
|
|
*/
|
2008-12-20 10:02:00 +00:00
|
|
|
char *BLI_strcasestr(const char *s, const char *find)
|
|
|
|
{
|
2010-03-22 09:30:00 +00:00
|
|
|
register char c, sc;
|
|
|
|
register size_t len;
|
2008-12-20 10:02:00 +00:00
|
|
|
|
2010-03-22 09:30:00 +00:00
|
|
|
if ((c = *find++) != 0) {
|
2012-05-12 15:13:06 +00:00
|
|
|
c = tolower(c);
|
2008-12-20 10:02:00 +00:00
|
|
|
len = strlen(find);
|
|
|
|
do {
|
|
|
|
do {
|
|
|
|
if ((sc = *s++) == 0)
|
|
|
|
return (NULL);
|
2012-05-12 15:13:06 +00:00
|
|
|
sc = tolower(sc);
|
2008-12-20 10:02:00 +00:00
|
|
|
} while (sc != c);
|
|
|
|
} while (BLI_strncasecmp(s, find, len) != 0);
|
|
|
|
s--;
|
2010-03-22 09:30:00 +00:00
|
|
|
}
|
|
|
|
return ((char *) s);
|
2008-12-20 10:02:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-28 05:53:40 +00:00
|
|
|
int BLI_strcasecmp(const char *s1, const char *s2)
|
|
|
|
{
|
2012-10-10 04:47:53 +00:00
|
|
|
register int i;
|
|
|
|
register char c1, c2;
|
2008-12-20 10:02:00 +00:00
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
for (i = 0;; i++) {
|
2012-10-10 04:47:53 +00:00
|
|
|
c1 = tolower(s1[i]);
|
|
|
|
c2 = tolower(s2[i]);
|
2008-12-20 10:02:00 +00:00
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
if (c1 < c2) {
|
2008-12-20 10:02:00 +00:00
|
|
|
return -1;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
2012-05-12 15:13:06 +00:00
|
|
|
else if (c1 > c2) {
|
2008-12-20 10:02:00 +00:00
|
|
|
return 1;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
2012-05-12 15:13:06 +00:00
|
|
|
else if (c1 == 0) {
|
2008-12-20 10:02:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-28 05:53:40 +00:00
|
|
|
int BLI_strncasecmp(const char *s1, const char *s2, size_t len)
|
|
|
|
{
|
2012-10-10 04:47:53 +00:00
|
|
|
register size_t i;
|
|
|
|
register char c1, c2;
|
2008-12-20 10:02:00 +00:00
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2012-10-10 04:47:53 +00:00
|
|
|
c1 = tolower(s1[i]);
|
|
|
|
c2 = tolower(s2[i]);
|
2008-12-20 10:02:00 +00:00
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
if (c1 < c2) {
|
2008-12-20 10:02:00 +00:00
|
|
|
return -1;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
2012-05-12 15:13:06 +00:00
|
|
|
else if (c1 > c2) {
|
2008-12-20 10:02:00 +00:00
|
|
|
return 1;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
2012-05-12 15:13:06 +00:00
|
|
|
else if (c1 == 0) {
|
2008-12-20 10:02:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-10 14:52:23 +00:00
|
|
|
/* compare number on the left size of the string */
|
|
|
|
static int left_number_strcmp(const char *s1, const char *s2, int *tiebreaker)
|
|
|
|
{
|
|
|
|
const char *p1 = s1, *p2 = s2;
|
|
|
|
int numdigit, numzero1, numzero2;
|
|
|
|
|
|
|
|
/* count and skip leading zeros */
|
|
|
|
for (numzero1 = 0; *p1 && (*p1 == '0'); numzero1++)
|
|
|
|
p1++;
|
|
|
|
for (numzero2 = 0; *p2 && (*p2 == '0'); numzero2++)
|
|
|
|
p2++;
|
|
|
|
|
|
|
|
/* find number of consecutive digits */
|
|
|
|
for (numdigit = 0; ; numdigit++) {
|
|
|
|
if (isdigit(*(p1 + numdigit)) && isdigit(*(p2 + numdigit)))
|
|
|
|
continue;
|
|
|
|
else if (isdigit(*(p1 + numdigit)))
|
|
|
|
return 1; /* s2 is bigger */
|
|
|
|
else if (isdigit(*(p2 + numdigit)))
|
|
|
|
return -1; /* s1 is bigger */
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* same number of digits, compare size of number */
|
|
|
|
if (numdigit > 0) {
|
2013-05-12 06:33:21 +00:00
|
|
|
int compare = (int)strncmp(p1, p2, (size_t)numdigit);
|
2013-05-10 14:52:23 +00:00
|
|
|
|
|
|
|
if (compare != 0)
|
|
|
|
return compare;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* use number of leading zeros as tie breaker if still equal */
|
|
|
|
if (*tiebreaker == 0) {
|
|
|
|
if (numzero1 > numzero2)
|
|
|
|
*tiebreaker = 1;
|
|
|
|
else if (numzero1 < numzero2)
|
|
|
|
*tiebreaker = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-08 20:08:19 +00:00
|
|
|
/* natural string compare, keeping numbers in order */
|
|
|
|
int BLI_natstrcmp(const char *s1, const char *s2)
|
|
|
|
{
|
2012-10-10 04:47:53 +00:00
|
|
|
register int d1 = 0, d2 = 0;
|
|
|
|
register char c1, c2;
|
2013-05-10 14:52:23 +00:00
|
|
|
int tiebreaker = 0;
|
2012-03-03 20:19:11 +00:00
|
|
|
|
2013-05-10 14:52:23 +00:00
|
|
|
/* if both chars are numeric, to a left_number_strcmp().
|
2012-03-03 20:19:11 +00:00
|
|
|
* then increase string deltas as long they are
|
|
|
|
* numeric, else do a tolower and char compare */
|
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
while (1) {
|
2012-10-10 04:47:53 +00:00
|
|
|
c1 = tolower(s1[d1]);
|
|
|
|
c2 = tolower(s2[d2]);
|
2009-06-08 20:08:19 +00:00
|
|
|
|
2014-01-16 19:15:53 +11:00
|
|
|
if (isdigit(c1) && isdigit(c2)) {
|
2013-05-10 14:52:23 +00:00
|
|
|
int numcompare = left_number_strcmp(s1 + d1, s2 + d2, &tiebreaker);
|
2009-06-08 20:08:19 +00:00
|
|
|
|
2013-05-10 14:52:23 +00:00
|
|
|
if (numcompare != 0)
|
|
|
|
return numcompare;
|
|
|
|
|
2009-06-08 20:08:19 +00:00
|
|
|
d1++;
|
2014-01-16 19:15:53 +11:00
|
|
|
while (isdigit(s1[d1]))
|
2009-06-08 20:08:19 +00:00
|
|
|
d1++;
|
|
|
|
d2++;
|
2014-01-16 19:15:53 +11:00
|
|
|
while (isdigit(s2[d2]))
|
2009-06-08 20:08:19 +00:00
|
|
|
d2++;
|
|
|
|
|
|
|
|
c1 = tolower(s1[d1]);
|
|
|
|
c2 = tolower(s2[d2]);
|
|
|
|
}
|
2010-10-01 13:30:09 +00:00
|
|
|
|
2012-10-21 05:46:41 +00:00
|
|
|
/* first check for '.' so "foo.bar" comes before "foo 1.bar" */
|
2012-05-12 15:13:06 +00:00
|
|
|
if (c1 == '.' && c2 != '.')
|
2010-10-01 13:30:09 +00:00
|
|
|
return -1;
|
2012-05-12 15:13:06 +00:00
|
|
|
if (c1 != '.' && c2 == '.')
|
2010-10-01 13:30:09 +00:00
|
|
|
return 1;
|
2012-05-12 15:13:06 +00:00
|
|
|
else if (c1 < c2) {
|
2009-06-08 20:08:19 +00:00
|
|
|
return -1;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
2012-05-12 15:13:06 +00:00
|
|
|
else if (c1 > c2) {
|
2009-06-08 20:08:19 +00:00
|
|
|
return 1;
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
2012-05-12 15:13:06 +00:00
|
|
|
else if (c1 == 0) {
|
2009-06-08 20:08:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
d1++;
|
|
|
|
d2++;
|
|
|
|
}
|
2014-01-21 14:50:40 +01:00
|
|
|
|
|
|
|
if (tiebreaker)
|
|
|
|
return tiebreaker;
|
|
|
|
|
|
|
|
/* we might still have a different string because of lower/upper case, in
|
|
|
|
* that case fall back to regular string comparison */
|
|
|
|
return strcmp(s1, s2);
|
2009-06-08 20:08:19 +00:00
|
|
|
}
|
|
|
|
|
2015-01-03 10:13:02 +01:00
|
|
|
/**
|
|
|
|
* Like strcmp, but will ignore any heading/trailing pad char for comparison.
|
|
|
|
* So e.g. if pad is '*', '*world' and 'world*' will compare equal.
|
|
|
|
*/
|
|
|
|
int BLI_strcmp_ignore_pad(const char *str1, const char *str2, const char pad)
|
|
|
|
{
|
|
|
|
size_t str1_len, str2_len;
|
|
|
|
|
|
|
|
while (*str1 == pad) {
|
|
|
|
str1++;
|
|
|
|
}
|
|
|
|
while (*str2 == pad) {
|
|
|
|
str2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
str1_len = strlen(str1);
|
|
|
|
str2_len = strlen(str2);
|
|
|
|
|
|
|
|
while (str1_len && (str1[str1_len - 1] == pad)) {
|
|
|
|
str1_len--;
|
|
|
|
}
|
|
|
|
while (str2_len && (str2[str2_len - 1] == pad)) {
|
|
|
|
str2_len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (str1_len == str2_len) {
|
|
|
|
return strncmp(str1, str2, str2_len);
|
|
|
|
}
|
|
|
|
else if (str1_len > str2_len) {
|
|
|
|
int ret = strncmp(str1, str2, str2_len);
|
|
|
|
if (ret == 0) {
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int ret = strncmp(str1, str2, str1_len);
|
|
|
|
if (ret == 0) {
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-29 06:25:22 +00:00
|
|
|
void BLI_timestr(double _time, char *str, size_t maxlen)
|
2008-12-20 10:02:00 +00:00
|
|
|
{
|
|
|
|
/* format 00:00:00.00 (hr:min:sec) string has to be 12 long */
|
2013-01-08 02:06:16 +00:00
|
|
|
int hr = ( (int) _time) / (60 * 60);
|
2012-05-12 15:13:06 +00:00
|
|
|
int min = (((int) _time) / 60 ) % 60;
|
2013-03-29 06:25:22 +00:00
|
|
|
int sec = ( (int) _time) % 60;
|
2012-05-12 15:13:06 +00:00
|
|
|
int hun = ( (int) (_time * 100.0)) % 100;
|
2013-03-29 06:25:22 +00:00
|
|
|
|
2008-12-20 10:02:00 +00:00
|
|
|
if (hr) {
|
2013-03-29 06:25:22 +00:00
|
|
|
BLI_snprintf(str, maxlen, "%.2d:%.2d:%.2d.%.2d", hr, min, sec, hun);
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-03-29 06:25:22 +00:00
|
|
|
BLI_snprintf(str, maxlen, "%.2d:%.2d.%.2d", min, sec, hun);
|
2008-12-20 10:02:00 +00:00
|
|
|
}
|
2009-02-10 17:06:43 +00:00
|
|
|
}
|
2010-02-27 15:39:13 +00:00
|
|
|
|
2010-05-08 15:37:29 +00:00
|
|
|
/* determine the length of a fixed-size string */
|
2013-06-21 12:33:19 +00:00
|
|
|
size_t BLI_strnlen(const char *s, const size_t maxlen)
|
2010-05-08 15:37:29 +00:00
|
|
|
{
|
2013-03-16 18:52:09 +00:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
for (len = 0; len < maxlen; len++, s++) {
|
|
|
|
if (!*s)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return len;
|
2010-05-08 15:37:29 +00:00
|
|
|
}
|
2010-08-28 12:34:22 +00:00
|
|
|
|
2012-10-10 04:47:53 +00:00
|
|
|
void BLI_ascii_strtolower(char *str, const size_t len)
|
2011-05-26 09:58:22 +00:00
|
|
|
{
|
2012-10-10 04:47:53 +00:00
|
|
|
size_t i;
|
2011-05-26 09:58:22 +00:00
|
|
|
|
2013-12-25 01:20:46 +11:00
|
|
|
for (i = 0; (i < len) && str[i]; i++)
|
2012-03-24 06:18:31 +00:00
|
|
|
if (str[i] >= 'A' && str[i] <= 'Z')
|
2011-05-26 09:58:22 +00:00
|
|
|
str[i] += 'a' - 'A';
|
|
|
|
}
|
|
|
|
|
2012-10-10 04:47:53 +00:00
|
|
|
void BLI_ascii_strtoupper(char *str, const size_t len)
|
2011-05-26 09:58:22 +00:00
|
|
|
{
|
2012-10-10 04:47:53 +00:00
|
|
|
size_t i;
|
2011-05-26 09:58:22 +00:00
|
|
|
|
2013-12-25 01:20:46 +11:00
|
|
|
for (i = 0; (i < len) && str[i]; i++)
|
2012-03-24 06:18:31 +00:00
|
|
|
if (str[i] >= 'a' && str[i] <= 'z')
|
2011-05-26 09:58:22 +00:00
|
|
|
str[i] -= 'a' - 'A';
|
|
|
|
}
|
2013-03-04 04:21:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Strip trailing zeros from a float, eg:
|
|
|
|
* 0.0000 -> 0.0
|
|
|
|
* 2.0010 -> 2.001
|
|
|
|
*
|
|
|
|
* \param str
|
2013-05-05 18:41:45 +00:00
|
|
|
* \param pad
|
2013-03-04 04:21:51 +00:00
|
|
|
* \return The number of zeto's stripped.
|
|
|
|
*/
|
|
|
|
int BLI_str_rstrip_float_zero(char *str, const char pad)
|
|
|
|
{
|
|
|
|
char *p = strchr(str, '.');
|
|
|
|
int totstrip = 0;
|
|
|
|
if (p) {
|
|
|
|
char *end_p;
|
|
|
|
p++; /* position at first decimal place */
|
|
|
|
end_p = p + (strlen(p) - 1); /* position at last character */
|
|
|
|
if (end_p > p) {
|
|
|
|
while (end_p != p && *end_p == '0') {
|
|
|
|
*end_p = pad;
|
|
|
|
end_p--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return totstrip;
|
|
|
|
}
|
2014-06-17 15:58:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return index of a string in a string array.
|
|
|
|
*
|
|
|
|
* \param str The string to find.
|
|
|
|
* \param str_array Array of strings.
|
|
|
|
* \param str_array_len The length of the array, or -1 for a NULL-terminated array.
|
|
|
|
* \return The index of str in str_array or -1.
|
|
|
|
*/
|
2014-08-01 14:31:41 +02:00
|
|
|
int BLI_str_index_in_array_n(const char *__restrict str, const char **__restrict str_array, const int str_array_len)
|
2014-06-17 15:58:07 +02:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
const char **str_iter = str_array;
|
|
|
|
|
|
|
|
for (index = 0; index < str_array_len; str_iter++, index++) {
|
|
|
|
if (STREQ(str, *str_iter)) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return index of a string in a string array.
|
|
|
|
*
|
|
|
|
* \param str The string to find.
|
|
|
|
* \param str_array Array of strings, (must be NULL-terminated).
|
|
|
|
* \return The index of str in str_array or -1.
|
|
|
|
*/
|
2014-08-01 14:31:41 +02:00
|
|
|
int BLI_str_index_in_array(const char *__restrict str, const char **__restrict str_array)
|
2014-06-17 15:58:07 +02:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
const char **str_iter = str_array;
|
|
|
|
|
|
|
|
for (index = 0; *str_iter; str_iter++, index++) {
|
|
|
|
if (STREQ(str, *str_iter)) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-01-10 19:13:38 +01:00
|
|
|
bool BLI_strn_endswith(const char *__restrict str, const char *__restrict end, size_t slength)
|
2015-01-07 22:25:33 +01:00
|
|
|
{
|
2015-01-10 19:13:38 +01:00
|
|
|
size_t elength = strlen(end);
|
2015-01-07 22:25:33 +01:00
|
|
|
|
|
|
|
if (elength < slength) {
|
2015-01-09 23:38:23 +11:00
|
|
|
const char *iter = &str[slength - elength];
|
2015-01-07 22:25:33 +01:00
|
|
|
while (*iter) {
|
|
|
|
if (*iter++ != *end++) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find if a string ends with another string.
|
|
|
|
*
|
|
|
|
* \param str The string to search within.
|
|
|
|
* \param end The string we look for at the end.
|
|
|
|
* \return If str ends with end.
|
|
|
|
*/
|
2015-01-10 12:40:09 +11:00
|
|
|
bool BLI_str_endswith(const char *__restrict str, const char *end)
|
2015-01-07 22:25:33 +01:00
|
|
|
{
|
2015-01-10 19:13:38 +01:00
|
|
|
const size_t slength = strlen(str);
|
2015-01-09 23:38:23 +11:00
|
|
|
return BLI_strn_endswith(str, end, slength);
|
2015-01-07 22:25:33 +01:00
|
|
|
}
|
|
|
|
|
2014-07-04 14:14:06 +02:00
|
|
|
/**
|
|
|
|
* Find the first char matching one of the chars in \a delim, from left.
|
|
|
|
*
|
|
|
|
* \param str The string to search within.
|
|
|
|
* \param delim The set of delimiters to search for, as unicode values.
|
|
|
|
* \param sep Return value, set to the first delimiter found (or NULL if none found).
|
|
|
|
* \param suf Return value, set to next char after the first delimiter found (or NULL if none found).
|
|
|
|
* \return The length of the prefix (i.e. *sep - str).
|
|
|
|
*/
|
2014-07-08 06:06:34 +10:00
|
|
|
size_t BLI_str_partition(const char *str, const char delim[], char **sep, char **suf)
|
2014-07-04 14:14:06 +02:00
|
|
|
{
|
|
|
|
return BLI_str_partition_ex(str, delim, sep, suf, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the first char matching one of the chars in \a delim, from right.
|
|
|
|
*
|
|
|
|
* \param str The string to search within.
|
|
|
|
* \param delim The set of delimiters to search for, as unicode values.
|
|
|
|
* \param sep Return value, set to the first delimiter found (or NULL if none found).
|
|
|
|
* \param suf Return value, set to next char after the first delimiter found (or NULL if none found).
|
|
|
|
* \return The length of the prefix (i.e. *sep - str).
|
|
|
|
*/
|
2014-07-08 06:06:34 +10:00
|
|
|
size_t BLI_str_rpartition(const char *str, const char delim[], char **sep, char **suf)
|
2014-07-04 14:14:06 +02:00
|
|
|
{
|
|
|
|
return BLI_str_partition_ex(str, delim, sep, suf, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the first char matching one of the chars in \a delim, either from left or right.
|
|
|
|
*
|
|
|
|
* \param str The string to search within.
|
|
|
|
* \param delim The set of delimiters to search for, as unicode values.
|
|
|
|
* \param sep Return value, set to the first delimiter found (or NULL if none found).
|
|
|
|
* \param suf Return value, set to next char after the first delimiter found (or NULL if none found).
|
|
|
|
* \param from_right If %true, search from the right of \a str, else, search from its left.
|
|
|
|
* \return The length of the prefix (i.e. *sep - str).
|
|
|
|
*/
|
2014-07-08 06:06:34 +10:00
|
|
|
size_t BLI_str_partition_ex(const char *str, const char delim[], char **sep, char **suf, const bool from_right)
|
2014-07-04 14:14:06 +02:00
|
|
|
{
|
2014-07-08 06:06:34 +10:00
|
|
|
const char *d;
|
2014-07-04 14:14:06 +02:00
|
|
|
char *(*func)(const char *str, int c) = from_right ? strrchr : strchr;
|
|
|
|
|
|
|
|
*sep = *suf = NULL;
|
|
|
|
|
|
|
|
for (d = delim; *d != '\0'; ++d) {
|
2014-07-08 06:06:34 +10:00
|
|
|
char *tmp = func(str, *d);
|
2014-07-04 14:14:06 +02:00
|
|
|
|
|
|
|
if (tmp && (from_right ? (*sep < tmp) : (!*sep || *sep > tmp))) {
|
|
|
|
*sep = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*sep) {
|
|
|
|
*suf = *sep + 1;
|
|
|
|
return (size_t)(*sep - str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return strlen(str);
|
|
|
|
}
|
2014-07-17 14:54:12 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format ints with decimal grouping.
|
|
|
|
* 1000 -> 1,000
|
|
|
|
*
|
|
|
|
* \param dst The resulting string
|
|
|
|
* \param num Number to format
|
|
|
|
* \return The length of \a dst
|
|
|
|
*/
|
|
|
|
size_t BLI_str_format_int_grouped(char dst[16], int num)
|
|
|
|
{
|
|
|
|
char src[16];
|
|
|
|
char *p_src = src;
|
|
|
|
char *p_dst = dst;
|
|
|
|
|
|
|
|
const char separator = ',';
|
|
|
|
int num_len, commas;
|
|
|
|
|
|
|
|
num_len = sprintf(src, "%d", num);
|
|
|
|
|
|
|
|
if (*p_src == '-') {
|
|
|
|
*p_dst++ = *p_src++;
|
|
|
|
num_len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (commas = 2 - num_len % 3; *p_src; commas = (commas + 1) % 3) {
|
|
|
|
*p_dst++ = *p_src++;
|
|
|
|
if (commas == 1) {
|
|
|
|
*p_dst++ = separator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*--p_dst = '\0';
|
|
|
|
|
|
|
|
return (size_t)(p_dst - dst);
|
|
|
|
}
|