KiCad PCB EDA Suite
common.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2014-2020 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2007-2015 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
6  * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
7  * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
32 #ifndef INCLUDE__COMMON_H_
33 #define INCLUDE__COMMON_H_
34 
35 #include <vector>
36 
37 #include <wx/wx.h>
38 #include <wx/confbase.h>
39 #include <wx/fileconf.h>
40 #include <wx/dir.h>
41 
42 #include <richio.h>
43 #include <gal/color4d.h>
44 
45 #include <atomic>
46 #include <limits>
47 #include <memory>
48 #include <type_traits>
49 #include <typeinfo>
50 #include <boost/uuid/uuid.hpp>
51 
52 class SEARCH_STACK;
53 class REPORTER;
54 
63 typedef uint32_t timestamp_t;
64 
65 class KIID
66 {
67 public:
68  KIID();
69  KIID( int null );
70  KIID( const wxString& aString );
71  KIID( timestamp_t aTimestamp );
72 
73  void Clone( const KIID& aUUID );
74 
75  size_t Hash() const;
76 
77  bool IsLegacyTimestamp() const;
79 
80  wxString AsString() const;
81  wxString AsLegacyTimestampString() const;
82 
83  bool operator==( KIID const& rhs) const
84  {
85  return m_uuid == rhs.m_uuid;
86  }
87 
88  bool operator!=( KIID const& rhs) const
89  {
90  return m_uuid != rhs.m_uuid;
91  }
92 
93  bool operator<( KIID const& rhs) const
94  {
95  return m_uuid < rhs.m_uuid;
96  }
97 
98 private:
99  boost::uuids::uuid m_uuid;
100 
102 };
103 
104 
105 extern KIID niluuid;
106 
107 
108 class KIID_PATH : public std::vector<KIID>
109 {
110 public:
112  {}
113 
114  KIID_PATH( const wxString& aString )
115  {
116  for( const wxString& pathStep : wxSplit( aString, '/' ) )
117  {
118  if( !pathStep.empty() )
119  emplace_back( KIID( pathStep ) );
120  }
121  }
122 
123  wxString AsString() const
124  {
125  wxString path;
126 
127  for( const KIID& pathStep : *this )
128  path += '/' + pathStep.AsString();
129 
130  return path;
131  }
132 
133  bool operator==( KIID_PATH const& rhs) const
134  {
135  if( size() != rhs.size() )
136  return false;
137 
138  for( size_t i = 0; i < size(); ++i )
139  {
140  if( at( i ) != rhs.at( i ) )
141  return false;
142  }
143 
144  return true;
145  }
146 
147  bool operator<( KIID_PATH const& rhs) const
148  {
149  if( size() != rhs.size() )
150  return size() < rhs.size();
151 
152  for( size_t i = 0; i < size(); ++i )
153  {
154  if( at( i ) < rhs.at( i ) )
155  return true;
156 
157  if( at( i ) != rhs.at( i ) )
158  return false;
159  }
160 
161  return false;
162  }
163 };
164 
165 
167 #define NAMELESS_PROJECT wxT( "noname" )
168 
171 #define TEXT_ANGLE_HORIZ 0
172 #define TEXT_ANGLE_VERT 900
173 
177 enum class EDA_DATA_TYPE
178 {
179  DISTANCE = 0,
180  AREA = 1,
181  VOLUME = 2
182 };
183 
184 enum class EDA_UNITS
185 {
186  INCHES = 0,
187  MILLIMETRES = 1,
188  UNSCALED = 2,
189  DEGREES = 3,
190  PERCENT = 4,
191 };
192 
193 
203 {
204 public:
205  LOCALE_IO();
206  ~LOCALE_IO();
207 
208 private:
209  // allow for nesting of LOCALE_IO instantiations
210  static std::atomic<unsigned int> m_c_count;
211 
212  // The locale in use before switching to the "C" locale
213  // (the locale can be set by user, and is not always the system locale)
214  std::string m_user_locale;
215  wxLocale* m_wxLocale;
216 };
217 
222 wxSize GetTextSize( const wxString& aSingleLine, wxWindow* aWindow );
223 
238 bool EnsureTextCtrlWidth( wxTextCtrl* aCtrl, const wxString* aString = NULL );
239 
243 void SelectReferenceNumber( wxTextEntry* aTextEntry );
244 
256 int ProcessExecute( const wxString& aCommandLine, int aFlags = wxEXEC_ASYNC,
257  wxProcess *callback = NULL );
258 
266 void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter );
267 
287 wxString SearchHelpFileFullPath( const SEARCH_STACK& aSearchStack, const wxString& aBaseName );
288 
298 bool EnsureFileDirectoryExists( wxFileName* aTargetFullFileName,
299  const wxString& aBaseFilename,
300  REPORTER* aReporter = NULL );
301 
303 const wxString PrePendPath( const wxString& aEnvVar, const wxString& aPriorityPath );
304 
311 const wxString ExpandEnvVarSubstitutions( const wxString& aString );
312 
316 const wxString ResolveUriByEnvVars( const wxString& aUri );
317 
318 
319 #ifdef __WXMAC__
320 
324 wxString GetOSXKicadUserDataDir();
325 
330 wxString GetOSXKicadMachineDataDir();
331 
336 wxString GetOSXKicadDataDir();
337 #endif
338 
339 // Some wxWidgets versions (for instance before 3.1.0) do not include
340 // this function, so add it if missing
341 #if !wxCHECK_VERSION( 3, 1, 0 )
342 #define USE_KICAD_WXSTRING_HASH // for common.cpp
343 namespace std
345 {
346  template<> struct hash<wxString>
347  {
348  size_t operator()( const wxString& s ) const;
349  };
350 }
351 #endif
352 
354 #define USE_KICAD_WXPOINT_LESS_AND_HASH // for common.cpp
355 namespace std
356 {
357  template <> struct hash<wxPoint>
358  {
359  size_t operator() ( const wxPoint& k ) const;
360  };
361 }
362 
363 namespace std
364 {
365  template<> struct less<wxPoint>
366  {
367  bool operator()( const wxPoint& aA, const wxPoint& aB ) const;
368  };
369 }
370 
377 std::ostream& operator<<( std::ostream& out, const wxSize& size );
378 
385 std::ostream& operator<<( std::ostream& out, const wxPoint& pt );
386 
387 
392 {
393 public:
394  WX_FILENAME( const wxString& aPath, const wxString& aFilename );
395 
396  void SetFullName( const wxString& aFileNameAndExtension );
397 
398  wxString GetName() const;
399  wxString GetFullName() const;
400  wxString GetPath() const;
401  wxString GetFullPath() const;
402 
403  // Avoid multiple calls to stat() on POSIX kernels.
404  long long GetTimestamp();
405 
406 private:
407  // Write cached values to the wrapped wxFileName. MUST be called before using m_fn.
408  void resolve();
409 
410  wxFileName m_fn;
411  wxString m_path;
412  wxString m_fullName;
413 };
414 
415 
416 long long TimestampDir( const wxString& aDirPath, const wxString& aFilespec );
417 
418 
419 
420 
421 #endif // INCLUDE__COMMON_H_
EDA_UNITS
Definition: common.h:184
WX_FILENAME(const wxString &aPath, const wxString &aFilename)
Performance enhancements to file and directory operations.
Definition: common.cpp:710
~LOCALE_IO()
Definition: common.cpp:243
KIID()
Definition: common.cpp:57
Instantiate the current locale within a scope in which you are expecting exceptions to be thrown.
Definition: common.h:202
EDA_DATA_TYPE
The type of unit.
Definition: common.h:177
bool operator!=(KIID const &rhs) const
Definition: common.h:88
wxLocale * m_wxLocale
Definition: common.h:215
const wxString ResolveUriByEnvVars(const wxString &aUri)
Replace any environment variables in file-path uris (leaving network-path URIs alone).
Definition: common.cpp:538
const wxString PrePendPath(const wxString &aEnvVar, const wxString &aPriorityPath)
Put aPriorityPath in front of all paths in the value of aEnvVar.
Definition: prependpath.cpp:32
wxString GetName() const
Definition: common.cpp:723
bool operator==(KIID const &rhs) const
Definition: common.h:83
wxString AsString() const
Definition: common.cpp:170
Template specialization to enable wxStrings for certain containers (e.g. unordered_map)
Definition: bitmap.cpp:56
wxString GetFullName() const
Definition: common.cpp:730
wxString m_path
Definition: common.h:411
boost::uuids::uuid m_uuid
Definition: common.h:99
KIID_PATH(const wxString &aString)
Definition: common.h:114
SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
REPORTER is a pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:62
void SetFullName(const wxString &aFileNameAndExtension)
Definition: common.cpp:717
bool operator==(KIID_PATH const &rhs) const
Definition: common.h:133
long long TimestampDir(const wxString &aDirPath, const wxString &aFilespec)
A copy of ConvertFileTimeToWx() because wxWidgets left it as a static function private to src/common/...
Definition: common.cpp:916
bool IsLegacyTimestamp() const
Definition: common.cpp:137
timestamp_t m_cached_timestamp
Definition: common.h:101
wxFileName m_fn
Definition: common.h:410
bool operator<(KIID const &rhs) const
Definition: common.h:93
static std::atomic< unsigned int > m_c_count
Definition: common.h:210
int ProcessExecute(const wxString &aCommandLine, int aFlags=wxEXEC_ASYNC, wxProcess *callback=NULL)
Run a command in a child process.
Definition: common.cpp:359
Definition: common.h:65
long long GetTimestamp()
Definition: common.cpp:757
wxString SearchHelpFileFullPath(const SEARCH_STACK &aSearchStack, const wxString &aBaseName)
Return the help file's full path.
void resolve()
Definition: common.cpp:749
size_t Hash() const
Definition: common.cpp:149
#define NULL
wxSize GetTextSize(const wxString &aSingleLine, wxWindow *aWindow)
Return the size of aSingleLine of text when it is rendered in aWindow using whatever font is currentl...
Definition: common.cpp:263
std::ostream & operator<<(std::ostream &out, const wxSize &size)
Helper function to print the given wxSize to a stream.
Definition: common.cpp:681
KIID_PATH()
Definition: common.h:111
timestamp_t AsLegacyTimestamp() const
Definition: common.cpp:143
void wxStringSplit(const wxString &aText, wxArrayString &aStrings, wxChar aSplitter)
Split aString to a string list separated at aSplitter.
Definition: common.cpp:336
bool EnsureTextCtrlWidth(wxTextCtrl *aCtrl, const wxString *aString=NULL)
Set the minimum pixel width on a text control in order to make a text string be fully visible within ...
Definition: common.cpp:278
KIID niluuid
LOCALE_IO()
Definition: common.cpp:222
const wxString ExpandEnvVarSubstitutions(const wxString &aString)
Replace any environment variable references with their values.
Definition: common.cpp:525
wxString GetPath() const
Definition: common.cpp:736
void Clone(const KIID &aUUID)
Definition: common.cpp:163
wxString GetFullPath() const
Definition: common.cpp:742
A wrapper around a wxFileName which is much more performant with a subset of the API.
Definition: common.h:391
wxString AsLegacyTimestampString() const
Definition: common.cpp:176
wxString AsString() const
Definition: common.h:123
void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
Definition: common.cpp:307
bool EnsureFileDirectoryExists(wxFileName *aTargetFullFileName, const wxString &aBaseFilename, REPORTER *aReporter=NULL)
Make aTargetFullFileName absolute and create the path of this file if it doesn't yet exist.
Definition: common.cpp:552
uint32_t timestamp_t
timestamp_t is our type to represent unique IDs for all kinds of elements; historically simply the ti...
Definition: common.h:53
wxString m_fullName
Definition: common.h:412
bool operator<(KIID_PATH const &rhs) const
Definition: common.h:147
std::string m_user_locale
Definition: common.h:214