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"
|
|
|
|
|
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;
|
|
|
|
}
|
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));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2011-03-25 13:40:44 +00:00
|
|
|
size_t len;
|
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
|
|
|
char *n;
|
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
len = strlen(str1) + strlen(str2);
|
|
|
|
n = MEM_mallocN(len + 1, "strdupcat");
|
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
|
|
|
strcpy(n, str1);
|
|
|
|
strcat(n, str2);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2012-05-12 15:13:06 +00:00
|
|
|
size_t srclen = strlen(src);
|
|
|
|
size_t cpylen = (srclen > (maxncpy - 1)) ? (maxncpy - 1) : srclen;
|
2012-10-31 04:28:49 +00:00
|
|
|
BLI_assert(maxncpy != 0);
|
2008-12-20 10:02:00 +00:00
|
|
|
|
|
|
|
memcpy(dst, src, cpylen);
|
2012-05-12 15:13:06 +00:00
|
|
|
dst[cpylen] = '\0';
|
2008-12-20 10:02:00 +00:00
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2012-11-23 15:12:13 +00:00
|
|
|
size_t BLI_vsnprintf(char *__restrict buffer, size_t count, 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
|
|
|
|
|
|
|
n = vsnprintf(buffer, count, format, arg);
|
2012-08-26 11:01:14 +00:00
|
|
|
|
2008-12-20 10:02:00 +00:00
|
|
|
if (n != -1 && n < count) {
|
|
|
|
buffer[n] = '\0';
|
2011-09-28 05:53:40 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-05-12 15:13:06 +00:00
|
|
|
buffer[count - 1] = '\0';
|
2008-12-20 10:02:00 +00:00
|
|
|
}
|
2012-08-26 11:01:14 +00:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2012-11-23 15:12:13 +00:00
|
|
|
size_t BLI_snprintf(char *__restrict buffer, size_t count, const char *__restrict format, ...)
|
2012-08-26 11:01:14 +00:00
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
va_list arg;
|
|
|
|
|
|
|
|
va_start(arg, format);
|
|
|
|
n = BLI_vsnprintf(buffer, count, 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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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 '"':
|
|
|
|
|
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;
|
|
|
|
}
|
2012-05-12 15:13:06 +00:00
|
|
|
/* intentionally pass through */
|
2011-08-23 15:08:54 +00:00
|
|
|
default:
|
|
|
|
*dst = *src;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-12 11:27:34 +00:00
|
|
|
/* Makes a copy of the text within the "" that appear after some text 'blahblah'
|
|
|
|
* i.e. for string 'pose["apples"]' with prefix 'pose[', it should grab "apples"
|
|
|
|
*
|
2012-05-12 15:13:06 +00:00
|
|
|
* - str: is the entire string to chop
|
2009-10-12 11:27:34 +00:00
|
|
|
* - 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
|
|
|
|
* data we want...
|
2012-07-09 22:16:50 +00:00
|
|
|
*
|
|
|
|
* TODO, 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);
|
2009-10-12 11:27:34 +00:00
|
|
|
char *startMatch, *endMatch;
|
|
|
|
|
|
|
|
/* 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;
|
2009-10-12 11:27:34 +00:00
|
|
|
|
|
|
|
/* get the end point (i.e. where the next occurance of " is after the starting point) */
|
2012-07-07 22:51:57 +00:00
|
|
|
endMatch = strchr(startMatch, '"'); /* " NOTE: this comment here is just so that my text editor still shows the functions ok... */
|
|
|
|
|
2009-10-12 11:27:34 +00:00
|
|
|
/* return the slice indicated */
|
2012-05-12 15:13:06 +00:00
|
|
|
return BLI_strdupn(startMatch, (size_t)(endMatch - startMatch));
|
2009-10-12 11:27:34 +00:00
|
|
|
}
|
|
|
|
|
2012-03-01 12:20:18 +00:00
|
|
|
/* Replaces all occurrences of oldText with newText in str, returning a new string that doesn't
|
|
|
|
* contain the 'replaced' occurrences.
|
2009-10-09 12:18:32 +00:00
|
|
|
*/
|
2012-07-07 22:51:57 +00:00
|
|
|
|
|
|
|
/* A rather wasteful string-replacement utility, though this shall do for now...
|
|
|
|
* Feel free to replace this with an even safe + nicer alternative */
|
2012-11-23 15:12:13 +00:00
|
|
|
char *BLI_replacestr(char *__restrict str, const char *__restrict oldText, const char *__restrict newText)
|
2009-10-09 12:18:32 +00:00
|
|
|
{
|
2012-05-12 15:13:06 +00:00
|
|
|
DynStr *ds = NULL;
|
|
|
|
size_t lenOld = strlen(oldText);
|
2009-10-09 12:18:32 +00:00
|
|
|
char *match;
|
|
|
|
|
|
|
|
/* sanity checks */
|
2012-05-12 15:13:06 +00:00
|
|
|
if ((str == NULL) || (str[0] == 0))
|
2009-10-09 12:18:32 +00:00
|
|
|
return NULL;
|
2012-05-12 15:13:06 +00:00
|
|
|
else if ((oldText == NULL) || (newText == NULL) || (oldText[0] == 0))
|
2009-10-09 12:18:32 +00:00
|
|
|
return BLI_strdup(str);
|
|
|
|
|
|
|
|
/* while we can still find a match for the old substring that we're searching for,
|
|
|
|
* keep dicing and replacing
|
|
|
|
*/
|
|
|
|
while ( (match = strstr(str, oldText)) ) {
|
|
|
|
/* 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) {
|
|
|
|
/* replace the token at the 'match' position with \0 so that the copied string will be ok,
|
|
|
|
* add the segment of the string from str to match to the buffer, then restore the value at match
|
|
|
|
*/
|
2012-05-12 15:13:06 +00:00
|
|
|
match[0] = 0;
|
2009-10-09 12:18:32 +00:00
|
|
|
BLI_dynstr_append(ds, str);
|
2012-05-12 15:13:06 +00:00
|
|
|
match[0] = oldText[0];
|
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 */
|
|
|
|
BLI_dynstr_append(ds, newText);
|
|
|
|
|
|
|
|
/* advance the current position of the string up to the end of the replaced segment */
|
|
|
|
str += lenOld;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
char *newStr;
|
|
|
|
|
|
|
|
/* add what's left of the string to the assembly buffer
|
|
|
|
* - we've been adjusting str to point at the end of the replaced segments
|
|
|
|
*/
|
|
|
|
if (str != NULL)
|
|
|
|
BLI_dynstr_append(ds, str);
|
|
|
|
|
|
|
|
/* convert to new c-string (MEM_malloc'd), and free the buffer */
|
2012-05-12 15:13:06 +00:00
|
|
|
newStr = BLI_dynstr_get_cstring(ds);
|
2009-10-09 12:18:32 +00:00
|
|
|
BLI_dynstr_free(ds);
|
|
|
|
|
|
|
|
return newStr;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* strcasestr not available in MSVC */
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
2012-03-03 20:19:11 +00:00
|
|
|
|
2009-06-08 20:08:19 +00:00
|
|
|
/* if both chars are numeric, to a strtol().
|
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
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
if (isdigit(c1) && isdigit(c2) ) {
|
2009-06-08 20:08:19 +00:00
|
|
|
int val1, val2;
|
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
val1 = (int)strtol(s1 + d1, (char **)NULL, 10);
|
|
|
|
val2 = (int)strtol(s2 + d2, (char **)NULL, 10);
|
2009-06-08 20:08:19 +00:00
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
if (val1 < val2) {
|
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 (val1 > val2) {
|
2009-06-08 20:08:19 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
d1++;
|
2012-05-12 15:13:06 +00:00
|
|
|
while (isdigit(s1[d1]) )
|
2009-06-08 20:08:19 +00:00
|
|
|
d1++;
|
|
|
|
d2++;
|
2012-05-12 15:13:06 +00: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++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-12-20 10:02:00 +00:00
|
|
|
void BLI_timestr(double _time, char *str)
|
|
|
|
{
|
|
|
|
/* format 00:00:00.00 (hr:min:sec) string has to be 12 long */
|
2012-05-12 15:13:06 +00:00
|
|
|
int hr = ( (int) _time) / (60*60);
|
|
|
|
int min = (((int) _time) / 60 ) % 60;
|
|
|
|
int sec = ( (int) (_time)) % 60;
|
|
|
|
int hun = ( (int) (_time * 100.0)) % 100;
|
2008-12-20 10:02:00 +00:00
|
|
|
|
|
|
|
if (hr) {
|
2012-04-29 15:47:02 +00:00
|
|
|
sprintf(str, "%.2d:%.2d:%.2d.%.2d", hr, min, sec, hun);
|
2012-03-24 06:18:31 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-04-29 15:47:02 +00:00
|
|
|
sprintf(str, "%.2d:%.2d.%.2d", min, sec, hun);
|
2008-12-20 10:02:00 +00:00
|
|
|
}
|
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
str[11] = 0;
|
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 */
|
2012-10-27 02:47:39 +00:00
|
|
|
size_t BLI_strnlen(const char *str, const size_t maxlen)
|
2010-05-08 15:37:29 +00:00
|
|
|
{
|
|
|
|
const char *end = memchr(str, '\0', maxlen);
|
|
|
|
return end ? (size_t) (end - str) : maxlen;
|
|
|
|
}
|
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
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
for (i = 0; i < len; 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
|
|
|
|
2012-05-12 15:13:06 +00:00
|
|
|
for (i = 0; i < len; 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';
|
|
|
|
}
|