]>
git.saurik.com Git - wxWidgets.git/blob - src/common/utilscmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Miscellaneous utility functions and classes
4 // Author: Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "utils.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/string.h"
40 #include "wx/window.h"
43 #include "wx/msgdlg.h"
44 #include "wx/textdlg.h"
45 #include "wx/textctrl.h" // for wxTE_PASSWORD
47 #include "wx/menuitem.h"
54 #include "wx/process.h"
55 #include "wx/txtstrm.h"
63 #if !defined(__WATCOMC__)
64 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
70 #include "wx/colordlg.h"
71 #include "wx/notebook.h"
73 #include "wx/statusbr.h"
74 #include "wx/toolbar.h"
80 #include <sys/types.h>
89 #include "wx/msw/private.h"
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
97 static wxWindow
*wxFindWindowByLabel1(const wxString
& title
, wxWindow
*parent
);
98 static wxWindow
*wxFindWindowByName1 (const wxString
& title
, wxWindow
*parent
);
101 // ============================================================================
103 // ============================================================================
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 #if defined(__WXMAC__) && !defined(__DARWIN__)
110 int strcasecmp(const char *str_1
, const char *str_2
)
112 register char c1
, c2
;
114 c1
= tolower(*str_1
++);
115 c2
= tolower(*str_2
++);
116 } while ( c1
&& (c1
== c2
) );
121 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
124 register char c1
, c2
;
127 c1
= tolower(*str_1
++);
128 c2
= tolower(*str_2
++);
138 #endif // __WXMAC__ && !__DARWIN__
140 #if defined( __VMS__ ) && ( __VMS_VER < 70000000 )
141 // we have no strI functions under VMS, therefore I have implemented
142 // an inefficient but portable version: convert copies of strings to lowercase
143 // and then use the normal comparison
144 static void myLowerString(char *s
)
147 if(isalpha(*s
)) *s
= (char)tolower(*s
);
152 int strcasecmp(const char *str_1
, const char *str_2
)
154 char *temp1
= new char[strlen(str_1
)+1];
155 char *temp2
= new char[strlen(str_2
)+1];
158 myLowerString(temp1
);
159 myLowerString(temp2
);
161 int result
= wxStrcmp(temp1
,temp2
);
168 int strncasecmp(const char *str_1
, const char *str_2
, size_t maxchar
)
170 char *temp1
= new char[strlen(str_1
)+1];
171 char *temp2
= new char[strlen(str_2
)+1];
174 myLowerString(temp1
);
175 myLowerString(temp2
);
177 int result
= strncmp(temp1
,temp2
,maxchar
);
185 #if defined(__WINDOWS__) && !defined(__WXMICROWIN__)
189 #define strcasecmp stricmp
190 #define strncasecmp strnicmp
192 #define strcasecmp _stricmp
193 #define strncasecmp _strnicmp
200 #define strcasecmp stricmp
201 #define strncasecmp strnicmp
204 // This declaration is missing in SunOS!
205 // (Yes, I know it is NOT ANSI-C but its in BSD libc)
206 #if defined(__xlC) || defined(__AIX__) || defined(__GNUG__)
209 int strcasecmp (const char *, const char *);
210 int strncasecmp (const char *, const char *, size_t);
213 #endif /* __WXMSW__ */
216 #define strcasecmp stricmp
217 #define strncasecmp strnicmp
221 #define strcasecmp stricmp
222 #define strncasecmp strnicmp
226 copystring (const wxChar
*s
)
228 if (s
== NULL
) s
= wxT("");
229 size_t len
= wxStrlen (s
) + 1;
231 wxChar
*news
= new wxChar
[len
];
232 memcpy (news
, s
, len
* sizeof(wxChar
)); // Should be the fastest
238 static long wxCurrentId
= 100;
243 return wxCurrentId
++;
247 wxGetCurrentId(void) { return wxCurrentId
; }
250 wxRegisterId (long id
)
252 if (id
>= wxCurrentId
)
253 wxCurrentId
= id
+ 1;
257 StringToFloat (const wxChar
*s
, float *number
)
259 if (s
&& *s
&& number
)
260 *number
= (float) wxStrtod (s
, (wxChar
**) NULL
);
264 StringToDouble (const wxChar
*s
, double *number
)
266 if (s
&& *s
&& number
)
267 *number
= wxStrtod (s
, (wxChar
**) NULL
);
271 FloatToString (float number
, const wxChar
*fmt
)
273 static wxChar buf
[256];
275 wxSprintf (buf
, fmt
, number
);
280 DoubleToString (double number
, const wxChar
*fmt
)
282 static wxChar buf
[256];
284 wxSprintf (buf
, fmt
, number
);
289 StringToInt (const wxChar
*s
, int *number
)
291 if (s
&& *s
&& number
)
292 *number
= (int) wxStrtol (s
, (wxChar
**) NULL
, 10);
296 StringToLong (const wxChar
*s
, long *number
)
298 if (s
&& *s
&& number
)
299 *number
= wxStrtol (s
, (wxChar
**) NULL
, 10);
303 IntToString (int number
)
305 static wxChar buf
[20];
307 wxSprintf (buf
, wxT("%d"), number
);
312 LongToString (long number
)
314 static wxChar buf
[20];
316 wxSprintf (buf
, wxT("%ld"), number
);
320 // Array used in DecToHex conversion routine.
321 static wxChar hexArray
[] = wxT("0123456789ABCDEF");
323 // Convert 2-digit hex number to decimal
324 int wxHexToDec(const wxString
& buf
)
326 int firstDigit
, secondDigit
;
328 if (buf
.GetChar(0) >= wxT('A'))
329 firstDigit
= buf
.GetChar(0) - wxT('A') + 10;
331 firstDigit
= buf
.GetChar(0) - wxT('0');
333 if (buf
.GetChar(1) >= wxT('A'))
334 secondDigit
= buf
.GetChar(1) - wxT('A') + 10;
336 secondDigit
= buf
.GetChar(1) - wxT('0');
338 return (firstDigit
& 0xF) * 16 + (secondDigit
& 0xF );
341 // Convert decimal integer to 2-character hex string
342 void wxDecToHex(int dec
, wxChar
*buf
)
344 int firstDigit
= (int)(dec
/16.0);
345 int secondDigit
= (int)(dec
- (firstDigit
*16.0));
346 buf
[0] = hexArray
[firstDigit
];
347 buf
[1] = hexArray
[secondDigit
];
351 // Convert decimal integer to 2-character hex string
352 wxString
wxDecToHex(int dec
)
355 wxDecToHex(dec
, buf
);
356 return wxString(buf
);
359 // Match a string INDEPENDENT OF CASE
361 StringMatch (const char *str1
, const char *str2
, bool subString
, bool exact
)
363 if (str1
== NULL
|| str2
== NULL
)
370 int len1
= strlen (str1
);
371 int len2
= strlen (str2
);
374 // Search for str1 in str2
375 // Slow .... but acceptable for short strings
376 for (i
= 0; i
<= len2
- len1
; i
++)
378 if (strncasecmp (str1
, str2
+ i
, len1
) == 0)
384 if (strcasecmp (str1
, str2
) == 0)
389 int len1
= strlen (str1
);
390 int len2
= strlen (str2
);
392 if (strncasecmp (str1
, str2
, wxMin (len1
, len2
)) == 0)
399 // Return the current date/time
403 time_t now
= time((time_t *) NULL
);
404 char *date
= ctime(&now
);
406 return wxString(date
);
413 // ----------------------------------------------------------------------------
414 // Menu accelerators related functions
415 // ----------------------------------------------------------------------------
417 wxChar
*wxStripMenuCodes(const wxChar
*in
, wxChar
*out
)
419 wxString s
= wxMenuItem::GetLabelFromText(in
);
422 // go smash their buffer if it's not big enough - I love char * params
423 memcpy(out
, s
.c_str(), s
.length() * sizeof(wxChar
));
433 wxString
wxStripMenuCodes(const wxString
& in
)
437 size_t len
= in
.length();
440 for ( size_t n
= 0; n
< len
; n
++ )
445 // skip it, it is used to introduce the accel char (or to quote
446 // itself in which case it should still be skipped): note that it
447 // can't be the last character of the string
450 wxLogDebug(_T("Invalid menu string '%s'"), in
.c_str());
454 // use the next char instead
458 else if ( ch
== _T('\t') )
460 // everything after TAB is accel string, exit the loop
470 #endif // wxUSE_MENUS
472 // ----------------------------------------------------------------------------
473 // Window search functions
474 // ----------------------------------------------------------------------------
477 * If parent is non-NULL, look through children for a label or title
478 * matching the specified string. If NULL, look through all top-level windows.
483 wxFindWindowByLabel (const wxString
& title
, wxWindow
* parent
)
487 return wxFindWindowByLabel1(title
, parent
);
491 for ( wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
493 node
= node
->GetNext() )
495 wxWindow
*win
= node
->GetData();
496 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
502 return (wxWindow
*) NULL
;
507 wxFindWindowByLabel1 (const wxString
& title
, wxWindow
* parent
)
511 if (parent
->GetLabel() == title
)
517 for ( wxWindowList::Node
* node
= parent
->GetChildren().GetFirst();
519 node
= node
->GetNext() )
521 wxWindow
*win
= (wxWindow
*)node
->GetData();
522 wxWindow
*retwin
= wxFindWindowByLabel1 (title
, win
);
529 return (wxWindow
*) NULL
; // Not found
533 * If parent is non-NULL, look through children for a name
534 * matching the specified string. If NULL, look through all top-level windows.
539 wxFindWindowByName (const wxString
& title
, wxWindow
* parent
)
543 return wxFindWindowByName1 (title
, parent
);
547 for ( wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
549 node
= node
->GetNext() )
551 wxWindow
*win
= node
->GetData();
552 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
559 // Failed? Try by label instead.
560 return wxFindWindowByLabel(title
, parent
);
565 wxFindWindowByName1 (const wxString
& title
, wxWindow
* parent
)
569 if ( parent
->GetName() == title
)
575 for (wxNode
* node
= parent
->GetChildren().First (); node
; node
= node
->Next ())
577 wxWindow
*win
= (wxWindow
*) node
->Data ();
578 wxWindow
*retwin
= wxFindWindowByName1 (title
, win
);
585 return (wxWindow
*) NULL
; // Not found
589 // Returns menu item id or -1 if none.
591 wxFindMenuItemId (wxFrame
* frame
, const wxString
& menuString
, const wxString
& itemString
)
594 wxMenuBar
*menuBar
= frame
->GetMenuBar ();
596 return menuBar
->FindMenuItem (menuString
, itemString
);
597 #endif // wxUSE_MENUS
602 // Try to find the deepest child that contains 'pt'.
603 // We go backwards, to try to allow for controls that are spacially
604 // within other controls, but are still siblings (e.g. buttons within
605 // static boxes). Static boxes are likely to be created _before_ controls
606 // that sit inside them.
607 wxWindow
* wxFindWindowAtPoint(wxWindow
* win
, const wxPoint
& pt
)
612 // Hack for wxNotebook case: at least in wxGTK, all pages
613 // claim to be shown, so we must only deal with the selected one.
615 if (win
->IsKindOf(CLASSINFO(wxNotebook
)))
617 wxNotebook
* nb
= (wxNotebook
*) win
;
618 int sel
= nb
->GetSelection();
621 wxWindow
* child
= nb
->GetPage(sel
);
622 wxWindow
* foundWin
= wxFindWindowAtPoint(child
, pt
);
631 else if (win->IsKindOf(CLASSINFO(wxFrame)))
633 // Pseudo-children that may not be mentioned in the child list
634 wxWindowList extraChildren;
635 wxFrame* frame = (wxFrame*) win;
636 if (frame->GetStatusBar())
637 extraChildren.Append(frame->GetStatusBar());
638 if (frame->GetToolBar())
639 extraChildren.Append(frame->GetToolBar());
641 wxNode* node = extraChildren.First();
644 wxWindow* child = (wxWindow*) node->Data();
645 wxWindow* foundWin = wxFindWindowAtPoint(child, pt);
653 wxNode
* node
= win
->GetChildren().Last();
656 wxWindow
* child
= (wxWindow
*) node
->Data();
657 wxWindow
* foundWin
= wxFindWindowAtPoint(child
, pt
);
660 node
= node
->Previous();
663 wxPoint pos
= win
->GetPosition();
664 wxSize sz
= win
->GetSize();
665 if (win
->GetParent())
667 pos
= win
->GetParent()->ClientToScreen(pos
);
670 wxRect
rect(pos
, sz
);
677 wxWindow
* wxGenericFindWindowAtPoint(const wxPoint
& pt
)
679 // Go backwards through the list since windows
680 // on top are likely to have been appended most
682 wxNode
* node
= wxTopLevelWindows
.Last();
685 wxWindow
* win
= (wxWindow
*) node
->Data();
686 wxWindow
* found
= wxFindWindowAtPoint(win
, pt
);
689 node
= node
->Previous();
697 On Fri, 21 Jul 1995, Paul Craven wrote:
699 > Is there a way to find the path of running program's executable? I can get
700 > my home directory, and the current directory, but I don't know how to get the
701 > executable directory.
704 The code below (warty as it is), does what you want on most Unix,
705 DOS, and Mac platforms (it's from the ALS Prolog main).
707 || Ken Bowen Applied Logic Systems, Inc. PO Box 180,
708 ||==== Voice: +1 (617)965-9191 Newton Centre,
709 || FAX: +1 (617)965-1636 MA 02159 USA
710 Email: ken@als.com WWW: http://www.als.com
711 ------------------------------------------------------------------------
714 // This code is commented out but it may be integrated with wxWin at
715 // a later date, after testing. Thanks Ken!
718 /*--------------------------------------------------------------------*
719 | whereami is given a filename f in the form: whereami(argv[0])
720 | It returns the directory in which the executable file (containing
721 | this code [main.c] ) may be found. A dot will be returned to indicate
722 | the current directory.
723 *--------------------------------------------------------------------*/
729 register char *cutoff
= NULL
; /* stifle -Wall */
736 * See if the file is accessible either through the current directory
737 * or through an absolute path.
740 if (access(name
, R_OK
) == 0) {
742 /*-------------------------------------------------------------*
743 * The file was accessible without any other work. But the current
744 * working directory might change on us, so if it was accessible
745 * through the cwd, then we should get it for later accesses.
746 *-------------------------------------------------------------*/
749 if (!absolute_pathname(name
)) {
750 #if defined(DOS) || defined(__WIN32__)
756 if (*(name
+ 1) == ':') {
757 if (*name
>= 'a' && *name
<= 'z')
758 drive
= (int) (*name
- 'a' + 1);
760 drive
= (int) (*name
- 'A' + 1);
762 *newrbuf
++ = *(name
+ 1);
763 *newrbuf
++ = DIR_SEPARATOR
;
767 *newrbuf
++ = DIR_SEPARATOR
;
769 if (getcwd(newrbuf
, drive
) == 0) { /* } */
771 if (getcwd(newrbuf
, 1024) == 0) { /* } */
775 if (getwd(imagedir
) == 0) { /* } */
776 #else /* !HAVE_GETWD */
777 if (getcwd(imagedir
, 1024) == 0) {
778 #endif /* !HAVE_GETWD */
780 fatal_error(FE_GETCWD
, 0);
782 for (; *t
; t
++) /* Set t to end of buffer */
784 if (*(t
- 1) == DIR_SEPARATOR
) /* leave slash if already
789 cutoff
= t
; /* otherwise put one in */
790 *t
++ = DIR_SEPARATOR
;
793 #if (!defined(__MAC__) && !defined(__DJGPP__) && !defined(__GO32__) && !defined(__WIN32__))
795 (*t
++ = DIR_SEPARATOR
);
798 /*-------------------------------------------------------------*
799 * Copy the rest of the string and set the cutoff if it was not
800 * already set. If the first character of name is a slash, cutoff
801 * is not presently set but will be on the first iteration of the
803 *-------------------------------------------------------------*/
805 for ((*name
== DIR_SEPARATOR
? (s
= name
+1) : (s
= name
));;) {
806 if (*s
== DIR_SEPARATOR
)
815 /*-------------------------------------------------------------*
816 * Get the path list from the environment. If the path list is
817 * inaccessible for any reason, leave with fatal error.
818 *-------------------------------------------------------------*/
821 if ((s
= getenv("Commands")) == (char *) 0)
823 if ((s
= getenv("PATH")) == (char *) 0)
825 fatal_error(FE_PATH
, 0);
828 * Copy path list into ebuf and set the source pointer to the
829 * beginning of this buffer.
837 while (*s
&& *s
!= PATH_SEPARATOR
)
839 if (t
> imagedir
&& *(t
- 1) == DIR_SEPARATOR
)
840 ; /* do nothing -- slash already is in place */
842 *t
++ = DIR_SEPARATOR
; /* put in the slash */
843 cutoff
= t
- 1; /* set cutoff */
845 if (access(imagedir
, R_OK
) == 0)
849 s
++; /* advance source pointer */
851 fatal_error(FE_INFND
, 0);
856 /*-------------------------------------------------------------*
857 | At this point the full pathname should exist in imagedir and
858 | cutoff should be set to the final slash. We must now determine
859 | whether the file name is a symbolic link or not and chase it down
860 | if it is. Note that we reuse ebuf for getting the link.
861 *-------------------------------------------------------------*/
864 while ((cc
= readlink(imagedir
, ebuf
, 512)) != -1) {
867 if (*s
== DIR_SEPARATOR
) {
874 if (*s
== DIR_SEPARATOR
)
875 cutoff
= t
; /* mark the last slash seen */
876 if (!(*t
++ = *s
++)) /* copy the character */
881 #endif /* HAVE_SYMLINK */
883 strcpy(imagename
, cutoff
+ 1); /* keep the image name */
884 *(cutoff
+ 1) = 0; /* chop off the filename part */
891 // ----------------------------------------------------------------------------
893 // ----------------------------------------------------------------------------
896 * N.B. these convenience functions must be separate from msgdlgg.cpp, textdlgg.cpp
897 * since otherwise the generic code may be pulled in unnecessarily.
902 int wxMessageBox(const wxString
& message
, const wxString
& caption
, long style
,
903 wxWindow
*parent
, int WXUNUSED(x
), int WXUNUSED(y
) )
905 wxMessageDialog
dialog(parent
, message
, caption
, style
);
907 int ans
= dialog
.ShowModal();
920 wxFAIL_MSG( _T("unexpected return code from wxMessageDialog") );
925 #endif // wxUSE_MSGDLG
929 wxString
wxGetTextFromUser(const wxString
& message
, const wxString
& caption
,
930 const wxString
& defaultValue
, wxWindow
*parent
,
931 int x
, int y
, bool WXUNUSED(centre
) )
934 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
, wxOK
|wxCANCEL
, wxPoint(x
, y
));
935 if (dialog
.ShowModal() == wxID_OK
)
937 str
= dialog
.GetValue();
943 wxString
wxGetPasswordFromUser(const wxString
& message
,
944 const wxString
& caption
,
945 const wxString
& defaultValue
,
949 wxTextEntryDialog
dialog(parent
, message
, caption
, defaultValue
,
950 wxOK
| wxCANCEL
| wxTE_PASSWORD
);
951 if ( dialog
.ShowModal() == wxID_OK
)
953 str
= dialog
.GetValue();
959 #endif // wxUSE_TEXTDLG
963 wxColour
wxGetColourFromUser(wxWindow
*parent
, const wxColour
& colInit
)
966 data
.SetChooseFull(TRUE
);
969 data
.SetColour((wxColour
&)colInit
); // const_cast
973 wxColourDialog
dialog(parent
, &data
);
974 if ( dialog
.ShowModal() == wxID_OK
)
976 colRet
= dialog
.GetColourData().GetColour();
978 //else: leave it invalid
983 #endif // wxUSE_COLOURDLG
985 // ----------------------------------------------------------------------------
986 // missing C RTL functions (FIXME shouldn't be here at all)
987 // ----------------------------------------------------------------------------
990 char *strdup(const char *s
)
992 return strcpy( (char*) malloc( strlen( s
) + 1 ) , s
) ;
997 return ( c
>= 0 && c
< 128 ) ;
1001 // ----------------------------------------------------------------------------
1002 // wxSafeYield and supporting functions
1003 // ----------------------------------------------------------------------------
1005 void wxEnableTopLevelWindows(bool enable
)
1007 wxWindowList::Node
*node
;
1008 for ( node
= wxTopLevelWindows
.GetFirst(); node
; node
= node
->GetNext() )
1009 node
->GetData()->Enable(enable
);
1012 wxWindowDisabler::wxWindowDisabler(wxWindow
*winToSkip
)
1014 // remember the top level windows which were already disabled, so that we
1015 // don't reenable them later
1016 m_winDisabled
= NULL
;
1018 wxWindowList::Node
*node
;
1019 for ( node
= wxTopLevelWindows
.GetFirst(); node
; node
= node
->GetNext() )
1021 wxWindow
*winTop
= node
->GetData();
1022 if ( winTop
== winToSkip
)
1025 if ( winTop
->IsEnabled() )
1031 if ( !m_winDisabled
)
1033 m_winDisabled
= new wxWindowList
;
1036 m_winDisabled
->Append(winTop
);
1041 wxWindowDisabler::~wxWindowDisabler()
1043 wxWindowList::Node
*node
;
1044 for ( node
= wxTopLevelWindows
.GetFirst(); node
; node
= node
->GetNext() )
1046 wxWindow
*winTop
= node
->GetData();
1047 if ( !m_winDisabled
|| !m_winDisabled
->Find(winTop
) )
1051 //else: had been already disabled, don't reenable
1054 delete m_winDisabled
;
1057 // Yield to other apps/messages and disable user input to all windows except
1059 bool wxSafeYield(wxWindow
*win
)
1061 wxWindowDisabler
wd(win
);
1063 bool rc
= wxYield();
1068 // ----------------------------------------------------------------------------
1070 // ----------------------------------------------------------------------------
1072 // Don't synthesize KeyUp events holding down a key and producing KeyDown
1073 // events with autorepeat. On by default and always on in wxMSW. wxGTK version
1076 bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag
) )
1078 return TRUE
; // detectable auto-repeat is the only mode MSW supports
1084 const wxChar
*wxGetInstallPrefix()
1088 if ( wxGetEnv(wxT("WX_PREFIX"), &prefix
) )
1089 return prefix
.c_str();
1091 #ifdef wxINSTALL_PREFIX
1092 return wxT(wxINSTALL_PREFIX
);
1098 wxString
wxGetDataDir()
1100 wxString format
= wxGetInstallPrefix();
1101 format
<< wxFILE_SEP_PATH
1102 << wxT("share") << wxFILE_SEP_PATH
1103 << wxT("wx") << wxFILE_SEP_PATH
1106 dir
.Printf(format
.c_str(), wxMAJOR_VERSION
, wxMINOR_VERSION
);
1111 // ----------------------------------------------------------------------------
1112 // network and user id functions
1113 // ----------------------------------------------------------------------------
1115 // Get Full RFC822 style email address
1116 bool wxGetEmailAddress(wxChar
*address
, int maxSize
)
1118 wxString email
= wxGetEmailAddress();
1122 wxStrncpy(address
, email
, maxSize
- 1);
1123 address
[maxSize
- 1] = wxT('\0');
1128 wxString
wxGetEmailAddress()
1132 wxString host
= wxGetFullHostName();
1135 wxString user
= wxGetUserId();
1138 email
<< user
<< wxT('@') << host
;
1145 wxString
wxGetUserId()
1147 static const int maxLoginLen
= 256; // FIXME arbitrary number
1150 bool ok
= wxGetUserId(buf
.GetWriteBuf(maxLoginLen
), maxLoginLen
);
1151 buf
.UngetWriteBuf();
1159 wxString
wxGetUserName()
1161 static const int maxUserNameLen
= 1024; // FIXME arbitrary number
1164 bool ok
= wxGetUserName(buf
.GetWriteBuf(maxUserNameLen
), maxUserNameLen
);
1165 buf
.UngetWriteBuf();
1173 wxString
wxGetHostName()
1175 static const size_t hostnameSize
= 257;
1178 bool ok
= wxGetHostName(buf
.GetWriteBuf(hostnameSize
), hostnameSize
);
1180 buf
.UngetWriteBuf();
1188 wxString
wxGetFullHostName()
1190 static const size_t hostnameSize
= 257;
1193 bool ok
= wxGetFullHostName(buf
.GetWriteBuf(hostnameSize
), hostnameSize
);
1195 buf
.UngetWriteBuf();
1203 wxString
wxGetHomeDir()
1206 wxGetHomeDir(&home
);
1213 wxString
wxGetCurrentDir()
1220 ok
= getcwd(dir
.GetWriteBuf(len
+ 1), len
) != NULL
;
1221 dir
.UngetWriteBuf();
1225 if ( errno
!= ERANGE
)
1227 wxLogSysError(_T("Failed to get current directory"));
1229 return wxEmptyString
;
1233 // buffer was too small, retry with a larger one
1245 // ----------------------------------------------------------------------------
1247 // ----------------------------------------------------------------------------
1249 // this is a private function because it hasn't a clean interface: the first
1250 // array is passed by reference, the second by pointer - instead we have 2
1251 // public versions of wxExecute() below
1252 static long wxDoExecuteWithCapture(const wxString
& command
,
1253 wxArrayString
& output
,
1254 wxArrayString
* error
)
1257 wxFAIL_MSG("Sorry, this version of wxExecute not implemented on WIN16.");
1261 // create a wxProcess which will capture the output
1262 wxProcess
*process
= new wxProcess
;
1263 process
->Redirect();
1265 long rc
= wxExecute(command
, TRUE
/* sync */, process
);
1270 wxInputStream
* is
= process
->GetInputStream();
1271 wxCHECK_MSG( is
, -1, _T("if wxExecute() succeded, stream can't be NULL") );
1272 wxTextInputStream
tis(*is
);
1274 wxTextInputStream
*tes
= NULL
;
1275 wxInputStream
*es
= NULL
;
1278 es
= process
->GetErrorStream();
1280 wxCHECK_MSG( es
, -1, _T("stderr can't be NULL") );
1282 tes
= new wxTextInputStream(*es
);
1290 if ( !is
->Eof() && is
->IsOk() )
1292 wxString line
= tis
.ReadLine();
1293 if ( is
->LastError() )
1301 if ( error
&& !es
->Eof() && es
->IsOk() )
1303 wxString line
= tes
->ReadLine();
1304 if ( es
->LastError() )
1316 #endif // wxUSE_STREAMS
1321 #endif // IO redirection supoprted
1324 long wxExecute(const wxString
& command
, wxArrayString
& output
)
1326 return wxDoExecuteWithCapture(command
, output
, NULL
);
1329 long wxExecute(const wxString
& command
,
1330 wxArrayString
& output
,
1331 wxArrayString
& error
)
1333 return wxDoExecuteWithCapture(command
, output
, &error
);
1336 // ----------------------------------------------------------------------------
1337 // wxApp::Yield() wrappers for backwards compatibility
1338 // ----------------------------------------------------------------------------
1343 return wxTheApp
&& wxTheApp
->Yield();
1349 bool wxYieldIfNeeded()
1352 return wxTheApp
&& wxTheApp
->Yield(TRUE
);