Qucs-core  0.0.19
range.cpp
Go to the documentation of this file.
00001 /*
00002  * range.cpp - range class implementation
00003  *
00004  * Copyright (C) 2006 Stefan Jahn <stefan@lkcc.org>
00005  *
00006  * This is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2, or (at your option)
00009  * any later version.
00010  *
00011  * This software is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this package; see the file COPYING.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  * $Id$
00022  *
00023  */
00024 
00025 #if HAVE_CONFIG_H
00026 # include <config.h>
00027 #endif
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 
00033 #include "range.h"
00034 
00035 namespace qucs {
00036 
00037 // Constructor creates an instance of the range class.
00038 range::range () {
00039   il = ih = '.';
00040   l = h = 0.0;
00041   txt = NULL;
00042 }
00043 
00044 // Constructor creates an fully qualified instance of the range class.
00045 range::range (char ilo, nr_double_t lo, nr_double_t hi, char ihi) {
00046   il = ilo;
00047   ih = ihi;
00048   if (lo > hi) {
00049     h = lo;
00050     l = hi;
00051   } else {
00052     l = lo;
00053     h = hi;
00054   }
00055   txt = NULL;
00056 }
00057 
00058 /* This copy constructor creates a instance of the range class based
00059    on the given range. */
00060 range::range (const range & r) {
00061   txt = r.txt ? strdup (r.txt) : NULL;
00062   il = r.il;
00063   ih = r.ih;
00064   l = r.l;
00065   h = r.h;
00066 }
00067 
00068 /* Checks whether the given value is outside the range. */
00069 bool range::outside (nr_double_t value) {
00070   return !inside (value);
00071 }
00072 
00073 /* Checks whether the given value is inside the range. */
00074 bool range::inside (nr_double_t value) {
00075   int err = 0;
00076   if (il == '[' &&  (value < l))
00077     err++;
00078   if (il == ']' && !(value > l))
00079     err++;
00080   if (ih == '[' && !(value < h))
00081     err++;
00082   if (ih == ']' &&  (value > h))
00083     err++;
00084   return err == 0;
00085 }
00086 
00087 // Destructor deletes an instance of the range class.
00088 range::~range () {
00089   free (txt);
00090 }
00091 
00092 /* Returns a text representation of the range object. */
00093 char * range::toString (void) {
00094   char str[64];
00095   sprintf (str, "%c%g,%g%c", il, l, h, ih);
00096   free (txt);
00097   txt = strdup (str);
00098   return txt;
00099 }
00100 
00101 } // namespace qucs