00001 // Copyright (C) 2007 Peter Carbonetto. All Rights Reserved. 00002 // This code is published under the Common 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 00015 // Function declarations. 00016 // ----------------------------------------------------------------- 00017 // Copy a C-style string (i.e. a null-terminated character array). 00018 char* copystring (const char* source); 00019 00020 // Class MatlabString. 00021 // ----------------------------------------------------------------- 00022 // This class encapsulates read-only access to a MATLAB character 00023 // array. 00024 class MatlabString { 00025 public: 00026 00027 // The constructor accepts as input a pointer to a Matlab array, 00028 // which must be a valid string (array of type CHAR). 00029 explicit MatlabString (const mxArray* ptr); 00030 00031 // This constructor accepts as input a null-terminated string. 00032 explicit MatlabString (const char* s); 00033 00034 // The copy constructor makes a full copy of the source string. 00035 MatlabString (const MatlabString& source); 00036 00037 // The destructor. 00038 ~MatlabString(); 00039 00040 // Return true if the string is empty. 00041 bool isempty () const { return strlen(s) == 0; }; 00042 00043 // Conversion operator for null-terminated string. 00044 operator const char* () const { return s; }; 00045 00046 // Conversion operator for string object. 00047 operator std::string () const { return std::string(s); }; 00048 00049 protected: 00050 char* s; // The null-terminated string. 00051 00052 // The copy assignment operator is not proper, thus remains 00053 // protected. 00054 MatlabString& operator= (const MatlabString& source) { return *this; }; 00055 }; 00056 00057 #endif