From e36dd4078bd5fd9b49e9648b334703ed545d1cc9 Mon Sep 17 00:00:00 2001
From: bastien <bastien@portable-bastien.localdomain>
Date: Tue, 24 Jun 2008 14:37:49 +0200
Subject: [PATCH 5/5] implement loading of dataset

---
 qucs-core/src/applications.h   |    2 +-
 qucs-core/src/evaluate.cpp     |   23 +++++++--
 qucs-core/src/math/complex.cpp |   34 +-------------
 qucs-core/src/parse_netlist.y  |    6 +++
 qucs-core/src/vector.cpp       |   97 ----------------------------------------
 5 files changed, 27 insertions(+), 135 deletions(-)

diff --git a/qucs-core/src/applications.h b/qucs-core/src/applications.h
index a9d0873..f5b7c0e 100644
--- a/qucs-core/src/applications.h
+++ b/qucs-core/src/applications.h
@@ -971,7 +971,7 @@ struct application_t eqn::applications[] = {
   { "vector", TAG_VECTOR, evaluate::vector_x, -1, { TAG_UNKNOWN } },
   { "matrix", TAG_MATRIX, evaluate::matrix_x, -1, { TAG_UNKNOWN } },
 
-  { "loadvector", TAG_VECTOR, evaluate::loadVector, 1, {TAG_STRING}},
+  { "loadvector", TAG_VECTOR, evaluate::loadVector, 2, {TAG_STRING,TAG_STRING}},
 
   { NULL, 0, NULL, 0, { 0 } /* end of list */ }
 };
diff --git a/qucs-core/src/evaluate.cpp b/qucs-core/src/evaluate.cpp
index 0d5a35f..18b28e4 100644
--- a/qucs-core/src/evaluate.cpp
+++ b/qucs-core/src/evaluate.cpp
@@ -50,6 +50,8 @@
 #include "exception.h"
 #include "exceptionstack.h"
 #include "strlist.h"
+#include "dataset.h"
+
 
 using namespace eqn;
 using namespace qucs;
@@ -4306,15 +4308,26 @@ constant * evaluate::vector_x (constant * args) {
 /* from file */
 constant * evaluate::loadVector(constant *args) {
   _DEFV ();
+  vector *v;
   char * path = STR (_ARES(0));
-  char * reason = "";
-  vector * v = new vector();
-
-  if(::vector::simpletxtload(path, *v, reason) == false)
-     THROW_MATH_EXCEPTION (reason);
+  char * variable = STR (_ARES(1));
+ 
+  dataset * data =   dataset::load (path);
+  if(data == NULL) {
+    THROW_MATH_EXCEPTION ("Could not load dataset");
+    goto out;
+  }
 
+  v = data->findVariable(variable);
+  if(v == NULL) {
+    THROW_MATH_EXCEPTION ("Could not find variable");
+    goto out;
+  }
   res->v = v;
   return res;
+ out:
+  res->v = new vector();
+  return res;
 }
 
 // ******************* immediate matrices ******************
diff --git a/qucs-core/src/math/complex.cpp b/qucs-core/src/math/complex.cpp
index f68a17e..a110f7c 100644
--- a/qucs-core/src/math/complex.cpp
+++ b/qucs-core/src/math/complex.cpp
@@ -38,6 +38,7 @@
 #include "consts.h"
 #include "fspecial.h"
 
+#include "../logging.h"
 using namespace fspecial;
 
 /*!\brief Construct a complex number using rectangular notation
@@ -859,36 +860,5 @@ nr_complex_t operator%(const nr_double_t r1, const nr_complex_t z2) {
   return r1 - z2 * floor (r1 / z2);
 }
 
-/*!\brief Read a complex 
-   \return true or false depending of success
-   \param[out] c: complex read
-   \param[in] str: string to read
-*/
-bool readcomplex(nr_complex_t &c, const char * str) 
-{
-  int ret;
-  double real;
-  double imag;
-  char u;
-
-  ret = sscanf(str, "%lf", &real);
-  if(ret != 1)
-    return false;
-
-  ret = sscanf(str, "%lf%c",&imag, &u);
-  // is a real
-  if(ret == 0)
-    imag = 0.0;
-  // no j or i 
-  if(ret == 1)
-    return false;
-  if(ret == 2)
-    /* test imaginary suffix */
-    if(u == 'I' || u == 'i' || u == 'j' || u == 'J')
-      c = rect(real, imag);
-    else 
-      return false;
-
-  return true;
-}
+
 
diff --git a/qucs-core/src/parse_netlist.y b/qucs-core/src/parse_netlist.y
index 352e22f..ca8dbeb 100644
--- a/qucs-core/src/parse_netlist.y
+++ b/qucs-core/src/parse_netlist.y
@@ -390,6 +390,12 @@ Constant:
     $$ = new eqn::constant (eqn::TAG_STRING);
     $$->s = $1;
   }
+  | '\'' STRING '\'' {
+    $$ =  new eqn::constant (eqn::TAG_STRING);
+    $$-> s = $2;
+  }
+
+
 ;
 
 Range:
diff --git a/qucs-core/src/vector.cpp b/qucs-core/src/vector.cpp
index ecdf8b2..0f21329 100644
--- a/qucs-core/src/vector.cpp
+++ b/qucs-core/src/vector.cpp
@@ -1235,100 +1235,3 @@ void vector::print (void) {
 }
 #endif /* DEBUG */
 
-/*! \brief size for memory allocation 
-    \todo use get page size 
-*/
-static const size_t simpletxreadsize = 4096;
-
-/*!\brief Max line size (for pervert user) */
-static const  size_t simplereadgetline = 256;
-
-/*! \brief load a vector from a text file 
-    
-     Each coordinate of vector is on single line 
-     format is %e+j%e
-     the j%e part is optionnal
-     \note Error are not thread safe (not a problem)
-*/
-bool vector::simpletxtload(char * path, vector &v, char * reason)
-{
-  FILE * fd;
-  nr_complex_t * c, * ctmp;
-  char line[simplereadgetline];
-  char * lineptr;
-  nr_complex_t cret;
-  const char * reasonbadcomplex = "Could not read a complex number in file";
-  const char * outofmemory = "Out of memory";
-
-  /* alias */
-  lineptr = (char *) line;
-  
-  /* open file */
-  errno = 0;
-  fd = fopen(path, "r");
-  if( fd == NULL) {
-    reason = strerror(errno);
-    return false;
-  }
-  
-  /* allocate memory */
-  size_t allocatedelement = simpletxreadsize/sizeof(nr_complex_t);
-  size_t elementeincrease = allocatedelement;
-  c =  (nr_complex_t *) calloc(allocatedelement, sizeof(nr_complex_t));
-  size_t used = 0;
-  
-  if(c == NULL) {
-    reason = (char *) outofmemory;
-    return false;
-  }
-
- 
-  /* implement file */
-  do {
-
-    /* allocate new memory */
-    if(used ==  allocatedelement) {
-      ctmp = (nr_complex_t *) realloc(c, (allocatedelement + elementeincrease) * sizeof(nr_complex_t));
-      if (ctmp == NULL) {
-	 reason = (char *) outofmemory;
-	 free(c);
-	 return false;
-      }
-      c = ctmp;
-    }
-
-    /* read line */
-    memset(line,'\0', simplereadgetline);
-    fgets(lineptr, simplereadgetline - 1, fd);
-    if(lineptr == NULL)
-      if(feof(fd))
-	break;
-      else {
-	reason = strerror(errno);
-	goto out_error;
-      }
-	
-    /* read complex */
-    if( readcomplex(cret, lineptr) == false) {
-      c[used] = cret;
-      reason = (char *) reasonbadcomplex;
-      goto out_error;
-    }
-    
-    /* increment used */
-    ++used;
-  } while(true);
-    
-  
-  for(unsigned int i = 0; i < used ; ++i)
-    /* fill vector */
-    v.add(c[i]);
-
-  free(c);
-  return true;
-
-  /* read real part */
- out_error:
-  free(c);
-  return false;
-}
-- 
1.5.5.4

