source: branches/MarsISDCBranchBasedOn17887/mbase/MString.cc@ 18477

Last change on this file since 18477 was 8999, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 4.2 KB
Line 
1/*====================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 3/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2008
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MString
28// =======
29//
30// If you are using root Form() command you must be aware of the fact that
31// it uses a global buffer. This buffer is recreated depending on the
32// length which is necessary to form the string. This recreation is not
33// thread safe and it might result in crashes if used in multi-threaded
34// environments.
35//
36// To get around this problem MString implements a Print() member function
37// which form a string like Form does. This formation is done in a
38// internal buffer. Because this buffer must be recreated and
39// deleted each time Print() is called this might be slower than Form().
40// The advantage is, that the buffer is not global and a call to Print()
41// from different threads is safe. However accessing the same
42// M/TString-object must still be locked with a mutex.
43//
44/////////////////////////////////////////////////////////////////////////////
45#include "MString.h"
46
47#include <stdarg.h>
48
49ClassImp(MString);
50
51// --------------------------------------------------------------------------
52//
53// Thread safe replacement for Form, use it like:
54//
55// MString string;
56// string.Print("MyString has %d bytes", 128);
57//
58// As a special feature the function returns the reference to the MString
59// so that you can directly work with it, eg.
60//
61// string.Print(" MyString has %d bytes ", 128).Strip(TString::kBoth);
62//
63MString &MString::Print(const char *fmt, va_list &ap)
64{
65 Int_t n=256;
66
67 char *ret=0;
68
69 while (1)
70 {
71 ret = new char[n+1];
72 Int_t sz = vsnprintf(ret, n, fmt, ap);
73 if (sz<=n)
74 break;
75
76 n *= 2;
77 delete [] ret;
78 };
79
80 va_end(ap);
81
82 *static_cast<TString*>(this) = ret;
83
84 delete [] ret;
85
86 return *this;
87}
88
89// --------------------------------------------------------------------------
90//
91// Thread safe replacement for Form, use it like:
92//
93// MString string;
94// string.Print("MyString has %d bytes", 128);
95//
96// As a special feature the function returns the reference to the MString
97// so that you can directly work with it, eg.
98//
99// string.Print(" MyString has %d bytes ", 128).Strip(TString::kBoth);
100//
101/*
102MString &MString::Print(const char *fmt, ...)
103{
104 va_list ap;
105 va_start(ap, fmt);
106
107 return Print(fmt, ap);
108}
109*/
110
111// --------------------------------------------------------------------------
112//
113// Thread safe replacement for Form, use it like:
114//
115// MString string;
116// string.Print("MyString has %d bytes", 128);
117//
118// As a special feature the function returns the reference to the MString
119// so that you can directly work with it, eg.
120//
121// string.Print(" MyString has %d bytes ", 128).Strip(TString::kBoth);
122//
123// The static version of this function returns a copy(!) of the resulting
124// M/TString.
125//
126#if ROOT_VERSION_CODE<ROOT_VERSION(5,18,00)
127TString MString::Format(const char *fmt, ...)
128{
129 va_list ap;
130 va_start(ap, fmt);
131
132 MString ret;
133 ret.Print(fmt, ap);
134 return ret;
135}
136#endif
137
138// --------------------------------------------------------------------------
139//
140// This is my implementation for root versions prior to 5.12/00 where
141// TString::Form didn't exist.
142//
143/*
144void MString::Form(const char *fmt, ...)
145{
146 va_list ap;
147 va_start(ap, fmt);
148
149 Print(fmt, ap);
150}
151*/
Note: See TracBrowser for help on using the repository browser.