Skip to content
This repository
Browse code

added C API

  • Loading branch information...
commit ca854eb8e03a48679eaceadf0c70a4fe6b9039f5 1 parent 96e902f
Marcin Wojdyr authored

Showing 2 changed files with 205 additions and 28 deletions. Show diff stats Hide diff stats

  1. 128  src/fityk.cpp
  2. 105  src/fityk.h
128  src/fityk.cpp
@@ -5,6 +5,7 @@
5 5
 
6 6
 #include <cassert>
7 7
 #include <cctype>
  8
+#include <cstring>
8 9
 #include "fityk.h"
9 10
 #include "common.h"
10 11
 #include "ui.h"
@@ -147,11 +148,16 @@ realt Fityk::calculate_expr(string const& s, int dataset)  throw(SyntaxError,
147 148
     return 0.;
148 149
 }
149 150
 
150  
-int Fityk::get_dataset_count()
  151
+int Fityk::get_dataset_count() const
151 152
 {
152 153
     return ftk_->get_dm_count();
153 154
 }
154 155
 
  156
+int Fityk::get_parameter_count() const
  157
+{
  158
+    return ftk_->parameters().size();
  159
+}
  160
+
155 161
 realt Fityk::get_model_value(realt x, int dataset)  throw(ExecuteError)
156 162
 {
157 163
     try {
@@ -192,7 +198,7 @@ int Fityk::get_variable_nr(string const& name)  throw(ExecuteError)
192 198
         return ftk_->find_variable(vname)->get_nr();
193 199
     }
194 200
     CATCH_EXECUTE_ERROR
195  
-    return 0;
  201
+    return -1;
196 202
 }
197 203
 
198 204
 void Fityk::load_data(int dataset,
@@ -308,5 +314,123 @@ vector<vector<realt> > Fityk::get_covariance_matrix(int dataset)
308 314
     return vector<vector<realt> >();
309 315
 }
310 316
 
  317
+realt* Fityk::get_covariance_matrix_as_array(int dataset)
  318
+{
  319
+    try {
  320
+        vector<DataAndModel*> dss = get_datasets_(ftk_, dataset);
  321
+        vector<realt> c = ftk_->get_fit()->get_covariance_matrix(dss);
  322
+        realt* array = (realt*) malloc(c.size() * sizeof(realt));
  323
+        for (size_t i = 0; i != c.size(); ++i)
  324
+            array[i] = c[i];
  325
+        return array;
  326
+    }
  327
+    CATCH_EXECUTE_ERROR
  328
+    return NULL;
  329
+}
  330
+
  331
+
311 332
 } //namespace fityk
312 333
 
  334
+
  335
+// C API, not recommended for use in C++ and other languages
  336
+using fityk::Fityk;
  337
+
  338
+extern "C" {
  339
+
  340
+Fityk* fityk_create()
  341
+{
  342
+    Fityk *f = new Fityk;
  343
+    f->set_throws(false);
  344
+    return f;
  345
+}
  346
+
  347
+void fityk_delete(Fityk *f)
  348
+{
  349
+    delete f;
  350
+}
  351
+
  352
+bool fityk_execute(Fityk *f, const char* command)
  353
+{
  354
+    try {
  355
+        f->execute(command);
  356
+    }
  357
+    catch(ExitRequestedException) {
  358
+        return false;
  359
+    }
  360
+    return true;
  361
+}
  362
+
  363
+void fityk_load_data(Fityk *f, int dataset,
  364
+                     realt *x, realt *y, realt *sigma, int num,
  365
+                     const char* title)
  366
+{
  367
+    f->load_data(dataset, vector<realt>(x, x+num), vector<realt>(y, y+num),
  368
+                 vector<realt>(sigma, sigma+num), title);
  369
+}
  370
+
  371
+const char* fityk_last_error(const Fityk *f)
  372
+{
  373
+    if (f->last_error().empty())
  374
+        return NULL;
  375
+    return f->last_error().c_str();
  376
+}
  377
+
  378
+void fityk_clear_last_error(Fityk *f)
  379
+{
  380
+    f->clear_last_error();
  381
+}
  382
+
  383
+char* fityk_get_info(Fityk *f, const char *s, int dataset)
  384
+{
  385
+    const string info = f->get_info(s, dataset);
  386
+    char* ret = (char*) malloc(info.size() + 1);
  387
+    strcpy(ret, info.c_str());
  388
+    return ret;
  389
+}
  390
+
  391
+realt fityk_calculate_expr(Fityk *f, const char* s, int dataset)
  392
+{
  393
+    return f->calculate_expr(s, dataset);
  394
+}
  395
+
  396
+int fityk_get_dataset_count(const Fityk *f)
  397
+{
  398
+    return f->get_dataset_count();
  399
+}
  400
+
  401
+int fityk_get_parameter_count(const Fityk* f)
  402
+{
  403
+    return f->get_parameter_count();
  404
+}
  405
+
  406
+const Point* fityk_get_data_point(Fityk *f, int dataset, int index)
  407
+{
  408
+    const vector<Point>& data = f->get_data(dataset);
  409
+    if (index >= 0 && (size_t) index < data.size())
  410
+        return &data[index];
  411
+    else
  412
+        return NULL;
  413
+}
  414
+
  415
+realt fityk_get_model_value(Fityk *f, realt x, int dataset)
  416
+{
  417
+    return f->get_model_value(x, dataset);
  418
+}
  419
+
  420
+int fityk_get_variable_nr(Fityk *f, const char* name)
  421
+{
  422
+    return f->get_variable_nr(name);
  423
+}
  424
+
  425
+realt fityk_get_wssr(Fityk *f, int dataset) { return f->get_wssr(dataset); }
  426
+realt fityk_get_ssr(Fityk *f, int dataset) { return f->get_ssr(dataset); }
  427
+realt fityk_get_rsquared(Fityk *f, int dataset)
  428
+                                        { return f->get_rsquared(dataset); }
  429
+int fityk_get_dof(Fityk *f, int dataset) { return f->get_dof(dataset); }
  430
+
  431
+realt* fityk_get_covariance_matrix(Fityk *f, int dataset)
  432
+{
  433
+    return f->get_covariance_matrix_as_array(dataset);
  434
+}
  435
+
  436
+} // extern "C"
105  src/fityk.h
... ...
@@ -1,17 +1,25 @@
1  
-// This file is part of fityk program. Copyright (C) Marcin Wojdyr
2  
-// Licence: GNU General Public License ver. 2+
  1
+/* This file is part of fityk program. Copyright (C) Marcin Wojdyr
  2
+ * Licence: GNU General Public License ver. 2+
  3
+ */
3 4
 
4  
-#ifndef FITYK__API__H__
5  
-#define FITYK__API__H__
  5
+#ifndef FITYK_FITYK_H_
  6
+#define FITYK_FITYK_H_
6 7
 
7  
-#ifndef __cplusplus
8  
-#error "This library does not have C API."
  8
+/* set precision used for storing data and fitting functions */
  9
+#define USE_LONG_DOUBLE 0
  10
+#if USE_LONG_DOUBLE
  11
+  typedef long double realt;
  12
+# define REALT_LENGTH_MOD "L"
  13
+#else
  14
+  typedef double realt;
  15
+# define REALT_LENGTH_MOD ""
9 16
 #endif
10 17
 
11  
-// C++ exception specifications are used by SWIG bindings
  18
+#ifdef __cplusplus
  19
+
12 20
 #ifdef _MSC_VER
13  
-// ignore warning "C++ exception specification ignored..."
14  
-#pragma warning( disable : 4290 )
  21
+// C++ exception specifications are used by SWIG bindings
  22
+#pragma warning( disable : 4290 ) // C++ exception specification ignored...
15 23
 #endif
16 24
 
17 25
 #include <string>
@@ -20,16 +28,6 @@
20 28
 #include <stdexcept>
21 29
 class Ftk;
22 30
 
23  
-/// set precision used for storing data and fitting functions
24  
-#define USE_LONG_DOUBLE 0
25  
-#if USE_LONG_DOUBLE
26  
-typedef long double realt;
27  
-#define REALT_LENGTH_MOD "L"
28  
-#else
29  
-typedef double realt;
30  
-#define REALT_LENGTH_MOD ""
31  
-#endif
32  
-
33 31
 
34 32
 /// Public C++ API of libfityk: class Fityk and helpers.
35 33
 ///
@@ -56,25 +54,26 @@ struct ExitRequestedException : std::exception
56 54
 {
57 55
 };
58 56
 
  57
+/// used to get statistics for all datasets together, e.g. in Fityk::get_wssr()
  58
+const int all_datasets=-1;
  59
+
59 60
 /// data point
60 61
 struct Point
61 62
 {
62 63
     realt x, y, sigma;
63 64
     bool is_active;
64  
-
65 65
     Point();
66 66
     Point(realt x_, realt y_);
67 67
     Point(realt x_, realt y_, realt sigma_);
68 68
     std::string str() const;
69 69
 };
  70
+
70 71
 inline bool operator< (Point const& p, Point const& q) { return p.x < q.x; }
71 72
 
72 73
 /// type of function passed to Fityk::set_show_message()
  74
+/// (C++ specific, ignore this if you fityk API from other languages)
73 75
 typedef void t_show_message(std::string const& s);
74 76
 
75  
-/// used to get statistics for all datasets together, e.g. in Fityk::get_wssr()
76  
-const int all_datasets=-1;
77  
-
78 77
 
79 78
 /// the public API to libfityk
80 79
 class Fityk
@@ -132,7 +131,7 @@ class Fityk
132 131
     /// redirect output to ...(e.g. stdout/stderr); cancels set_show_message()
133 132
     void redir_messages(std::FILE *stream);
134 133
 
135  
-    /// print string in the program's output (useful for embedded Lua)
  134
+    /// print string in the output of GUI/CLI (useful for embedded Lua)
136 135
     void out(std::string const& s) const;
137 136
 
138 137
     // @}
@@ -150,7 +149,10 @@ class Fityk
150 149
                                             throw(SyntaxError, ExecuteError);
151 150
 
152 151
     /// returns number of datasets n, always n >= 1
153  
-    int get_dataset_count();
  152
+    int get_dataset_count() const;
  153
+
  154
+    /// returns number of simple-variables (parameters that can be fitted)
  155
+    int get_parameter_count() const;
154 156
 
155 157
     /// get data points
156 158
     std::vector<Point> const& get_data(int dataset=0)  throw(ExecuteError);
@@ -192,6 +194,10 @@ class Fityk
192 194
     get_covariance_matrix(int dataset=all_datasets)  throw(ExecuteError);
193 195
     // @}
194 196
 
  197
+    // implementation details (for internal use)
  198
+    //Ftk *get_ftk() { return ftk_; } // access to underlying data
  199
+    realt* get_covariance_matrix_as_array(int dataset);
  200
+
195 201
 private:
196 202
     Ftk *ftk_;
197 203
     bool throws_, owns_;
@@ -200,5 +206,52 @@ class Fityk
200 206
 
201 207
 } // namespace
202 208
 
203  
-#endif
  209
+#else /* !__cplusplus */
  210
+/* C API.
  211
+ * Functions below correspond to member functions of class Fityk.
  212
+ * To check for errors use fityk_last_error().
  213
+ * bool and Point here should be ABI-compatible with C++ bool and fityk::Point.
  214
+ */
  215
+
  216
+#define bool _Bool
  217
+
  218
+typedef struct Fityk_ Fityk;
  219
+
  220
+typedef struct
  221
+{
  222
+    realt x, y, sigma;
  223
+    bool is_active;
  224
+} Point;
  225
+
  226
+
  227
+Fityk* fityk_create();
  228
+void fityk_delete(Fityk *f);
  229
+/* returns false on ExitRequestedException */
  230
+bool fityk_execute(Fityk *f, const char* command);
  231
+void fityk_load_data(Fityk *f, int dataset,
  232
+                     realt *x, realt *y, realt *sigma, int num,
  233
+                     const char* title);
  234
+/* returns NULL if no error happened since fityk_clear_last_error() */
  235
+const char* fityk_last_error(const Fityk *f);
  236
+void fityk_clear_last_error(Fityk *f);
  237
+/* caller is responsible to free() returned string */
  238
+char* fityk_get_info(Fityk *f, const char *s, int dataset);
  239
+realt fityk_calculate_expr(Fityk *f, const char* s, int dataset);
  240
+int fityk_get_dataset_count(const Fityk *f);
  241
+int fityk_get_parameter_count(const Fityk* f);
  242
+/* get data point, returns NULL if index is out of range */
  243
+const Point* fityk_get_data_point(Fityk *f, int dataset, int index);
  244
+realt fityk_get_model_value(Fityk *f, realt x, int dataset);
  245
+int fityk_get_variable_nr(Fityk *f, const char* name);
  246
+realt fityk_get_wssr(Fityk *f, int dataset);
  247
+realt fityk_get_ssr(Fityk *f, int dataset);
  248
+realt fityk_get_rsquared(Fityk *f, int dataset);
  249
+int fityk_get_dof(Fityk *f, int dataset);
  250
+/* returns matrix in array, which caller is responsible to free(); */
  251
+/* length of the array is parameter_count^2                        */
  252
+realt* fityk_get_covariance_matrix(Fityk *f, int dataset);
  253
+
  254
+#endif /* __cplusplus */
  255
+
  256
+#endif /* FITYK_FITYK_H_ */
204 257
 

0 notes on commit ca854eb

Please sign in to comment.
Something went wrong with that request. Please try again.