00001 // Copyright (C) 2007 Peter Carbonetto. All Rights Reserved. 00002 // This code is published under the Eclipse Public License. 00003 // 00004 // Author: Peter Carbonetto 00005 // Dept. of Computer Science 00006 // University of British Columbia 00007 // May 19, 2007 00008 00009 #ifndef INCLUDE_MATLABSTRING 00010 #define INCLUDE_MATLABSTRING 00011 00012 #include "mex.h" 00013 #include <string> 00014 #include <cstring> 00015 00016 // Function declarations. 00017 // ----------------------------------------------------------------- 00018 // Copy a C-style string (i.e. a null-terminated character array). 00019 char* copystring (const char* source); 00020 00021 // Class MatlabString. 00022 // ----------------------------------------------------------------- 00023 // This class encapsulates read-only access to a MATLAB character 00024 // array. 00025 class MatlabString { 00026 public: 00027 00028 // The constructor accepts as input a pointer to a Matlab array, 00029 // which must be a valid string (array of type CHAR). 00030 explicit MatlabString (const mxArray* ptr); 00031 00032 // This constructor accepts as input a null-terminated string. 00033 explicit MatlabString (const char* s); 00034 00035 // The copy constructor makes a full copy of the source string. 00036 MatlabString (const MatlabString& source); 00037 00038 // The destructor. 00039 ~MatlabString(); 00040 00041 // Return true if the string is empty. 00042 bool isempty () const { return strlen(s) == 0; }; 00043 00044 // Conversion operator for null-terminated string. 00045 operator const char* () const { return s; }; 00046 00047 // Conversion operator for string object. 00048 operator std::string () const { return std::string(s); }; 00049 00050 protected: 00051 char* s; // The null-terminated string. 00052 00053 // The copy assignment operator is not proper, thus remains 00054 // protected. 00055 MatlabString& operator= (const MatlabString& source) { return *this; }; 00056 }; 00057 00058 #endif