ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
adstring.hpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
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 #include "clist.h"
00045 #include <string.h>
00046 //#include <fvar.hpp>
00047 #include <ostream>
00048 
00049 #include <stdio.h>
00050 
00051 class adstring_shape
00052 {
00053   size_t sz;
00054 
00055 protected:
00056   adstring_shape(const size_t size)
00057   {
00058     sz = size;
00059   }
00060   size_t& size()
00061   {
00062     return sz;
00063   }
00064 
00065   friend class adstring;
00066 };
00067 
00068 class adstring_array;
00069 
00070 class adstring : public clist
00071 {
00072   adstring_shape * shape;
00073 #if (defined __ZTC__) || (defined __NDPX__)
00074   char * s;
00075 #else
00076   unsigned char * s;
00077 #endif
00078   void allocate(const size_t sz);
00079   void deallocate(void);
00080   friend class adstring_array;
00081 public :
00082   adstring();
00083   adstring(const adstring &v);
00084   adstring(const char v);
00085   adstring(const char* t);
00086   adstring(const unsigned char v);
00087   adstring(const unsigned char* t);
00088   adstring(const size_t lb, const size_t ub);
00089   adstring(const int size);
00090   ~adstring();
00091 
00092   void allocate(const char* t);
00093   void realloc(const char* t);
00094   //adstring(const unsigned char v);
00095 
00096   size_t size() const;
00097   size_t buff_size() const;
00098   unsigned char &operator()(const size_t i);
00099   unsigned char &operator[](const size_t i);
00100   const unsigned char& operator()(const size_t i) const;
00101   const unsigned char& operator[](const size_t i) const;
00102 
00103   adstring& operator+=(const adstring &u);
00104   inline adstring& operator+=(const char u)
00105     { return(*this += adstring(u)); }
00106 
00107   int operator==(const adstring &u);
00108 
00109   inline int operator == (const char* u)
00110     { return *this == adstring(u); }
00111 
00112   inline int operator!=(const adstring &u)
00113     { return(!(*this==u)); }
00114 
00115   adstring operator()(const size_t i, const size_t j);
00116   adstring operator()(const size_t i, const size_t j) const;
00117 
00118   int operator==(const char* u) const;
00119   int operator==(const adstring &u) const;
00120 
00121   adstring& operator=(const adstring &t);
00122   adstring& operator=(const char t);
00123    // { return (*this = adstring(t)); }
00124 
00125   operator unsigned char * ();
00126   operator char * ();
00127   operator const unsigned char*() const;
00128   operator const char*() const;
00129 
00130   // Pascal-like adstring functions
00131 
00132   // returns the starting position of substr
00133   size_t pos(const adstring& substr) const;
00134 
00135   // converts a double into a adstring
00136   friend adstring str(double x, int minwidth, int decplaces);
00137   friend adstring str(const int x);
00138   friend void val(const adstring& s, int& v, int& code);
00139   friend int val(const adstring& s);
00140 
00141   void to_upper(void);
00142   void to_lower(void);
00143   adstring to_upper(adstring& s);
00144   adstring to_lower(adstring& s);
00145 
00146   friend std::ostream& operator<<(std::ostream& c, const adstring& t);
00147   friend std::istream& operator>>(std::istream& c, adstring& t);
00148   friend adstring operator+(const adstring &u, const adstring &v);
00149   friend adstring operator+(const adstring &u, const unsigned char v);
00150   friend adstring operator+(const adstring &u, const char v);
00151   friend adstring operator+(const adstring &u, const unsigned char *v);
00152   friend adstring operator+(const adstring & u, const char *v);
00153   friend adstring operator+(const unsigned char *v, const adstring& u);
00154   friend adstring operator+(const char *v, const adstring &u);
00155   friend adstring operator+(const unsigned char u, const adstring &v);
00156 };
00157 adstring to_lower(adstring& s);
00158 adstring to_upper(adstring& s);
00159 adstring str(double x, int minwidth=17, int decplaces=-1);
00160 adstring str(const int x);
00161 void val(const adstring& s, int& v, int& code);
00162 int val(const adstring& s);
00163 
00164 class cifstream;
00165 
00166 class line_adstring : public adstring
00167 {
00168 public:
00169   line_adstring(void) : adstring() {}
00170   line_adstring(const adstring& s) : adstring(s) {}
00171   line_adstring& operator=(const adstring& s);
00172   line_adstring& operator=(const char *s);
00173   friend std::istream & operator >> (std::istream & c, line_adstring & t);
00174 };
00175 
00176 adstring itoa(int n,int d);
00177 
00178 void str(const int, adstring&);
00179 adstring chr(int c);
00180 size_t length(const adstring& t);
00181 size_t pos(const adstring& substr, const adstring& s);
00182 
00183 //adstring operator+(const char u, const char v)
00184 //  { return (adstring(u) + adstring(v) ); }
00185 #if defined(__BORLANDC__)
00186 #  if (__BORLANDC__  <= 0x0520)
00187      class ifstream;
00188      class ofstream;
00189      class cifstream;
00190 #  endif
00191 #endif
00192 
00193 class vector_shape;
00194 
00195 class adstring_array : public clist
00196 {
00197   vector_shape* shape;
00198   adstring** ptr;
00199 public:
00200   int size() const;
00201   int indexmin(void) const;
00202   int indexmax(void) const;
00203   adstring_array(const adstring_array& sa);
00204   ~adstring_array();
00205   adstring_array(int min,int max);
00206   void allocate(int min,int max);
00207   adstring_array(void);
00208   const adstring& operator[](int i) const;
00209   const adstring& operator()(int i) const;
00210   adstring& operator [] (int i);
00211   adstring& operator () (int i);
00212   adstring_array& operator += (const adstring& s);
00213   adstring_array& append_distinct(const adstring& s);
00214   friend std::ifstream& operator >> (std::ifstream& ifs,adstring_array& sa);
00215   friend std::ostream& operator<<(const std::ostream& ifs,
00216     const adstring_array& sa);
00217   friend cifstream& operator >> (cifstream& ifs,adstring_array& sa);
00218 
00219   void to_upper(void);
00220   void to_lower(void);
00221   adstring_array to_upper(adstring_array& s);
00222   adstring_array to_lower(adstring_array& s);
00223   adstring_array& operator=(const adstring_array&);
00224 };   // end class adstring_array
00225 adstring_array to_lower(adstring_array& s);
00226 adstring_array to_upper(adstring_array& s);
00227 
00228 std::ostream& operator<<(const std::ostream& ifs, const adstring_array& sa);
00229 
00230 int atoi(adstring& s);
00231 #endif //#ifndef __STRING_HPP__