ADMB Documentation  11.1.1016
 All Classes Files Functions Variables Typedefs Friends Defines
adstring.hpp
Go to the documentation of this file.
00001 /*
00002  * $Id: adstring.hpp 889 2013-03-29 00:25:06Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California 
00006  *
00007  * ADModelbuilder and associated libraries and documentations are
00008  * provided under the general terms of the "New BSD" license
00009  * 
00010  * License:
00011  * 
00012  * Redistribution and use in source and binary forms, with or without
00013  * modification, are permitted provided that the following conditions are
00014  * met:
00015  * 
00016  * 1. Redistributions of source code must retain the above copyright
00017  * notice, this list of conditions and the following disclaimer.
00018  * 
00019  * 2.  Redistributions in binary form must reproduce the above copyright
00020  * notice, this list of conditions and the following disclaimer in the
00021  * documentation and/or other materials provided with the distribution.
00022  * 
00023  * 3.  Neither the name of the  University of California, Otter Research,
00024  * nor the ADMB Foundation nor the names of its contributors may be used
00025  * to endorse or promote products derived from this software without
00026  * specific prior written permission.
00027  * 
00028  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00029  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00030  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00031  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00032  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00033  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00034  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00035  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00036  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00037  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00038  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039  *
00040  */
00041 #ifndef __ADSTRING_HPP__
00042 #define __ADSTRING_HPP__
00043 
00044 /*
00045 #if defined(__BORLANDC__ )
00046   #if defined(__GNUC__)
00047     #if (__GNUC__ < 3)
00048       #pragma interface
00049     #endif
00050   #else
00051     #pragma interface
00052   #endif
00053 #endif
00054 */
00055 
00056 #include "clist.h"
00057 #include <string.h>
00058 //#include <fvar.hpp>
00059 #include <ostream>
00060 
00061 #include <stdio.h>
00062 
00063 #ifdef HERE
00064   #undef HERE
00065 #endif
00066 //#define HERE cout << "reached line " << __LINE__ << " in " << __FILE__ << endl;
00067 #define HERE 
00068 
00069 #define USE_CONST
00070 #ifdef USE_CONST
00071   #define _CONST const
00072 #else
00073   #define _CONST 
00074 #endif
00075 class adstring_shape
00076 {
00077   unsigned int sz;
00078 protected :
00079   friend class adstring;
00080   adstring_shape(unsigned int n)
00081   {
00082     sz = n;
00083   }
00084   unsigned int & size()
00085   {
00086     return (sz);
00087   }
00088 };
00089 
00090 class adstring_array;
00091 
00092 class adstring : public clist
00093 {
00094   adstring_shape * shape;
00095 #if (defined __ZTC__) || (defined __NDPX__)
00096   char * s;
00097 #else
00098   unsigned char * s;
00099 #endif
00100   void allocate(int sz);
00101   void deallocate(void);
00102   friend class adstring_array;
00103 public :
00104   friend std::ostream & operator<<(std::ostream & c, const adstring & t);
00105   friend std::istream & operator >> (std::istream & c, adstring & t);
00106 
00107   adstring(const char *t);
00108   void allocate(const char *t);
00109   adstring(int lb, int ub);
00110   adstring(const adstring &v);
00111   void realloc(const char * t);
00112   //adstring(const unsigned char v);
00113   adstring(const char v);
00114   adstring(void);
00115   ~adstring();
00116   unsigned int size(void) const;
00117   unsigned int buff_size(void);
00118   unsigned char &operator()(const int i);
00119   unsigned char &operator[](const int i);
00120 #ifdef USE_CONST
00121   const unsigned char& operator()(const int i) const;
00122   const unsigned char& operator[](const int i) const;
00123 #endif
00124 
00125 #ifdef __INTEL_COMPILER
00126   friend adstring operator+(const adstring &u, const char* v)
00127   {
00128     adstring a(v);
00129     return u + a;
00130   }
00131 #endif
00132 
00133   friend adstring operator+(const adstring &u, const adstring &v); 
00134 
00135   friend adstring operator+(const adstring &u, const unsigned char v);
00136 
00137   friend adstring operator+(const adstring &u, const signed char v);
00138 
00139   friend adstring operator+(const adstring &u, const unsigned char *v);
00140 
00141   friend adstring operator+(const adstring & u, const signed char *v);
00142 
00143   friend adstring operator+(const unsigned char *v, const adstring& u);
00144 
00145   friend adstring operator+(const char *v, const adstring &u);
00146 
00147   friend adstring operator+(const unsigned char u, const adstring &v);
00148 
00149   adstring& operator+=(const adstring &u);
00150   inline adstring& operator+=(const char u)
00151     { return(*this += adstring(u)); }
00152 
00153   int operator==(const adstring &u);
00154 
00155   inline int operator == (const char* u)
00156     { return *this == adstring(u); }
00157 
00158   inline int operator!=(const adstring &u)
00159     { return(!(*this==u)); }
00160 
00161   adstring operator()(int i, int j);
00162   #if defined(USE_CONST)
00163   int operator==(const char* u) const;
00164 
00165   int operator==(const adstring &u) const;
00166   adstring operator()(int i, int j) const;
00167   #endif 
00168 
00169   adstring& operator=(const adstring &t);
00170   adstring& operator=(const char t);
00171    // { return (*this = adstring(t)); }
00172 
00173   operator unsigned char * ();
00174   operator char * ();
00175 #ifdef USE_CONST
00176   operator const unsigned char*() const;
00177   operator const char*() const;
00178 #endif
00179 
00180   // Pascal-like adstring functions
00181 
00182   int pos(const adstring& substr) const;  // returns the starting position of substr
00183 
00184   // converts a double into a adstring
00185   friend adstring str(double x, int minwidth, int decplaces);
00186   friend adstring str(const int x);
00187   friend void val(const adstring& s, int& v, int& code);
00188   friend int val(const adstring& s);
00189 
00190   void to_upper(void);
00191   void to_lower(void);
00192   adstring to_upper(adstring& s);
00193   adstring to_lower(adstring& s);
00194 };
00195 adstring to_lower(adstring& s);
00196 adstring to_upper(adstring& s);
00197 adstring str(double x, int minwidth=17, int decplaces=-1);
00198 adstring str(const int x);
00199 void val(const adstring& s, int& v, int& code);
00200 int val(const adstring& s);
00201 
00202 class cifstream;
00203  
00204 class line_adstring : public adstring
00205 {
00206 public:
00207   line_adstring(void) : adstring() {}
00208   line_adstring(const adstring& s) : adstring(s) {}
00209   line_adstring& operator=(const adstring& s);
00210   line_adstring& operator=(const char *s);
00211   friend std::istream & operator >> (std::istream & c, line_adstring & t);
00212 };
00213 
00214 adstring itoa(int n,int d);
00215 
00216 void str(const int, adstring&);
00217 adstring chr(int c);
00218 int length(const adstring& t);
00219 int pos(const adstring& substr, const adstring& s);
00220 
00221 //adstring operator+(const char u, const char v)
00222 //  { return (adstring(u) + adstring(v) ); }
00223 #if defined(__BORLANDC__)
00224 #  if (__BORLANDC__  <= 0x0520) 
00225      class ifstream;
00226      class ofstream;
00227      class cifstream;
00228 #  endif
00229 #endif
00230 
00231 class vector_shape;
00232 
00233 class adstring_array : public clist
00234 {
00235   vector_shape * shape;
00236   adstring ** ptr;
00237 public:
00238   int size() const;
00239   int indexmin(void) const;
00240   int indexmax(void) const;
00241   adstring_array(const adstring_array& sa);
00242   ~adstring_array();
00243   adstring_array(int min,int max);
00244   void allocate(int min,int max);
00245   adstring_array(void); 
00246   #ifdef USE_CONST
00247   const adstring& operator[](int i) const;
00248   const adstring& operator()(int i) const;
00249   #endif
00250   adstring& operator [] (int i);
00251   adstring& operator () (int i);
00252   adstring_array& operator += (const adstring& s);
00253   adstring_array& append_distinct(const adstring& s);
00254   friend std::ifstream& operator >> (std::ifstream& ifs,adstring_array& sa);
00255   friend std::ostream& operator<<(const std::ostream& ifs, const adstring_array& sa);
00256   friend cifstream& operator >> (cifstream& ifs,adstring_array& sa);
00257 
00258   void to_upper(void);
00259   void to_lower(void);
00260   adstring_array to_upper(adstring_array& s);
00261   adstring_array to_lower(adstring_array& s); 
00262   adstring_array& operator=(const adstring_array&);
00263 };   // end class adstring_array
00264 adstring_array to_lower(adstring_array& s);
00265 adstring_array to_upper(adstring_array& s);
00266 
00267 std::ostream& operator<<(const std::ostream& ifs, const adstring_array& sa);
00268 
00269 int atoi(adstring& s);
00270 #endif //#ifndef __STRING_HPP__