void* bsearch(const void* key, const void* base, size_t num, size_t size, comparator fcn) { // handle special cases if(num == 0) { return NULL; } else if(num == 1) { if(!fcn(key, base)) { return (void*)base; } return NULL; } size_t lower = 0, upper = num - 1; void* ptr = NULL; while(lower < upper) { size_t index = (lower + upper) / 2; ptr = (char*)base + index * size; int val = fcn(key, ptr); // key is somewhere below ptr if(val < 0) { upper = index; } else if(val > 0) { lower = index + 1; } else { // found it! return ptr; } } return NULL; }
inline Scalar soln(Scalar x, Scalar y, Scalar z, int max_p, int max_q) { Scalar sum = 0; for(int p=0; p<=max_p; ++p) { const Scalar p21y = fcn(p, y); const Scalar sin_py = std::sin(p21y)/(2*p+1); for(int q=0; q<=max_q; ++q) { const Scalar q21z = fcn(q, z); const Scalar sin_qz = std::sin(q21z)/(2*q+1); const Scalar l = fcn_l(p, q); const Scalar sinh1 = std::sinh(l*x); const Scalar sinh2 = std::sinh(l); const Scalar tmp = (sinh1*sin_py)*(sin_qz/sinh2); //if the scalar l gets too big, sinh(l) becomes inf. //if that happens, tmp is a NaN. //crude check for NaN: //if tmp != tmp, tmp is NaN if (tmp == tmp) { sum += tmp; } else { //if we got a NaN, break out of this inner loop and go to //the next iteration of the outer loop. break; } } } return term0*sum; }
int main() { int i, ldfjac; real x[3], fvec[15], fjac[15*3], xp[3], fvecp[15], err[15]; const int m = 15; const int n = 3; /* auxiliary data (e.g. measurements) */ real y[15] = {1.4e-1, 1.8e-1, 2.2e-1, 2.5e-1, 2.9e-1, 3.2e-1, 3.5e-1, 3.9e-1, 3.7e-1, 5.8e-1, 7.3e-1, 9.6e-1, 1.34, 2.1, 4.39}; #ifdef BOX_CONSTRAINTS real xmin[3] = {0., 0.1, 0.5}; real xmax[3] = {2., 1.5, 2.3}; #endif fcndata_t data; data.m = m; data.y = y; #ifdef BOX_CONSTRAINTS data.xmin = xmin; data.xmax = xmax; #endif /* the following values should be suitable for */ /* checking the jacobian matrix. */ x[0] = 9.2e-1; x[1] = 1.3e-1; x[2] = 5.4e-1; ldfjac = 15; /* compute xp from x */ __cminpack_func__(chkder)(m, n, x, NULL, NULL, ldfjac, xp, NULL, 1, NULL); /* compute fvec at x (all components of fvec should be != 0).*/ fcn(&data, m, n, x, fvec, NULL, ldfjac, 1); /* compute fjac at x */ fcn(&data, m, n, x, NULL, fjac, ldfjac, 2); /* compute fvecp at xp (all components of fvecp should be != 0)*/ fcn(&data, m, n, xp, fvecp, NULL, ldfjac, 1); /* check Jacobian, put the result in err */ __cminpack_func__(chkder)(m, n, x, fvec, fjac, ldfjac, NULL, fvecp, 2, err); /* Output values: err[i] = 1.: i-th gradient is correct err[i] = 0.: i-th gradient is incorrect err[I] > 0.5: i-th gradient is probably correct */ for (i=0; i<m; ++i) { fvecp[i] = fvecp[i] - fvec[i]; } printf("\n fvec\n"); for (i=0; i<m; ++i) printf("%s%15.7g",i%3==0?"\n ":"", (double)fvec[i]); printf("\n fvecp - fvec\n"); for (i=0; i<m; ++i) printf("%s%15.7g",i%3==0?"\n ":"", (double)fvecp[i]); printf("\n err\n"); for (i=0; i<m; ++i) printf("%s%15.7g",i%3==0?"\n ":"", (double)err[i]); printf("\n"); return 0; }
int main(int argc, char *argv[]) { FILE *fp; void (*fcn)(FILE *, char *); int ch; setlocale (LC_ALL, ""); dchar = '\t'; /* default delimiter is \t */ /* Since we don't support multi-byte characters, the -c and -b options are equivalent, and the -n option is meaningless. */ while ((ch = getopt(argc, argv, "b:c:d:f:sn")) != -1) switch(ch) { case 'b': case 'c': fcn = c_cut; get_list(optarg); cflag = 1; break; case 'd': dchar = *optarg; dflag = 1; break; case 'f': get_list(optarg); fcn = f_cut; fflag = 1; break; case 's': sflag = 1; break; case 'n': break; case '?': default: usage(); } argc -= optind; argv += optind; if (fflag) { if (cflag) usage(); } else if (!cflag || dflag || sflag) usage(); if (*argv) for (; *argv; ++argv) { if (!(fp = fopen(*argv, "r"))) err(1, "%s", *argv); fcn(fp, *argv); (void)fclose(fp); } else fcn(stdin, "stdin"); exit(0); }
/* * set_default_cmd() - set the default debug entry and $all */ static void set_default_cmd(void) { if (!g_kernelmode) fcn(strdup("debug"), DEBUG_ENTRY); #ifdef TESTING fcn(strdup("empty"), EMPTY_ENTRY); #endif (void) set(strdup("all"), expr(spec(strdup("keys"), SPEC_EXACT), spec(strdup(".*"), SPEC_REGEXP))); }
Matrix<double> evalf(const SX &ex, const SX &v, const Matrix<double> &vdef) { SXFunction fcn(v, ex); fcn.init(); fcn.input(0).set(vdef); fcn.evaluate(); return fcn.output(); }
static int lang_lib_file_run (RLang *user, const char *file) { char *libpath; void *lib; if (!(libpath = r_str_new (file))) { return -1; } if (!r_str_startswith (libpath, "/") && !r_str_startswith (libpath, "./")) { libpath = r_str_prefix (libpath, "./"); } if (!r_file_exists (libpath)) { if (!r_str_endswith (libpath, R_LIB_EXT)) { libpath = r_str_appendf (libpath, ".%s", R_LIB_EXT); } } if (!r_file_exists (libpath)) { free (libpath); return -1; } lib = r_lib_dl_open (libpath); if (lib) { void (*fcn)(RCore *); fcn = r_lib_dl_sym (lib, "entry"); if (fcn) { fcn (user->user); } else { eprintf ("Cannot find 'entry' symbol in library\n"); } r_lib_dl_close (lib); } free (libpath); return 0; }
BTGE_API int bt2dHandleInput(void) { static int (*fcn)()=NULL; BTGE_Tile2DInitFunc("Tile2D_HandleInput", (void **)(&fcn)); if(fcn)return(fcn()); return(-1); }
//____________________________________________________________________ void fitCircle(Int_t n=10000) { //generates n points around a circle and fit them TCanvas *c1 = new TCanvas("c1","c1",600,600); c1->SetGrid(); gr = new TGraph(n); if (n> 999) gr->SetMarkerStyle(1); else gr->SetMarkerStyle(3); TRandom3 r; Double_t x,y; for (Int_t i=0;i<n;i++) { r.Circle(x,y,r.Gaus(4,0.3)); gr->SetPoint(i,x,y); } c1->DrawFrame(-5,-5,5,5); gr->Draw("p"); auto chi2Function = [&](const Double_t *par) { //minimisation function computing the sum of squares of residuals // looping at the graph points Int_t np = gr->GetN(); Double_t f = 0; Double_t *x = gr->GetX(); Double_t *y = gr->GetY(); for (Int_t i=0;i<np;i++) { Double_t u = x[i] - par[0]; Double_t v = y[i] - par[1]; Double_t dr = par[2] - std::sqrt(u*u+v*v); f += dr*dr; } return f; }; // wrap chi2 funciton in a function object for the fit // 3 is the number of fit parameters (size of array par) ROOT::Math::Functor fcn(chi2Function,3); ROOT::Fit::Fitter fitter; double pStart[3] = {0,0,1}; fitter.SetFCN(fcn, pStart); fitter.Config().ParSettings(0).SetName("x0"); fitter.Config().ParSettings(1).SetName("y0"); fitter.Config().ParSettings(2).SetName("R"); // do the fit bool ok = fitter.FitFCN(); if (!ok) { Error("line3Dfit","Line3D Fit failed"); } const ROOT::Fit::FitResult & result = fitter.Result(); result.Print(std::cout); //Draw the circle on top of the points TArc *arc = new TArc(result.Parameter(0),result.Parameter(1),result.Parameter(2)); arc->SetLineColor(kRed); arc->SetLineWidth(4); arc->Draw(); }
void do_done(CHAR_DATA * ch, char * thedir) { CHAR_DATA *rch; if(!ch->infight) { printf_to_char(ch,"You're not in a fight."); return; } if(!TURN(ch)) { printf_to_char(ch,"It's not your turn."); return; } check_winner(ch->in_room); for (rch = ch->in_room->people; rch != NULL; rch = rch->next_in_room) { printf_to_char(rch,"%s ends %s turn.\n",fcn(ch), ch->sex ? "his" : "her"); do_map(rch,"forced"); rch->AT++; } ch->AT = 0; advance_turn(ch); }
static PyObject * devices(struct pyalsacontrol *self, PyObject *args, int (*fcn)(snd_ctl_t *, int *)) { int dev, err, size = 0; PyObject *t; dev = -1; t = PyTuple_New(size); do { err = fcn(self->handle, &dev); if (err < 0) { PyErr_Format(PyExc_IOError, "Control hwdep_next_device error: %s", strerror(-err)); Py_DECREF(t); return NULL; } if (dev >= 0) { _PyTuple_Resize(&t, ++size); if (t) PyTuple_SetItem(t, size-1, PyInt_FromLong(dev)); } } while (dev >= 0); return t; }
int main() { int i, m, n, ldfjac, mode; real epsfcn; real x[3], fvec[15], fjac[15*3], fdjac[15*3], xp[3], fvecp[15], err[15], errd[15], wa[15]; int one=1, iflag=1; m = 15; n = 3; /* the following values should be suitable for */ /* checking the jacobian matrix. */ x[1-1] = 9.2e-1; x[2-1] = 1.3e-1; x[3-1] = 5.4e-1; ldfjac = 15; epsfcn = 0.; mode = 1; __minpack_func__(chkder)(&m, &n, x, NULL, NULL, &ldfjac, xp, NULL, &mode, NULL); mode = 2; fcn(&m, &n, x, fvec, &one); __minpack_func__(fdjac2)(fcn, &m, &n, x, fvec, fdjac, &ldfjac, &iflag, &epsfcn, wa); fcnjac(m, n, x, fjac, ldfjac); fcn(&m, &n, xp, fvecp, &one); __minpack_func__(chkder)(&m, &n, x, fvec, fdjac, &ldfjac, NULL, fvecp, &mode, errd); __minpack_func__(chkder)(&m, &n, x, fvec, fjac, &ldfjac, NULL, fvecp, &mode, err); for (i=1; i<=m; i++) { fvecp[i-1] = fvecp[i-1] - fvec[i-1]; } printf("\n fvec\n"); for (i=1; i<=m; i++) printf("%s%15.7g",i%3==1?"\n ":"", (double)fvec[i-1]); printf("\n fvecp - fvec\n"); for (i=1; i<=m; i++) printf("%s%15.7g",i%3==1?"\n ":"", (double)fvecp[i-1]); printf("\n errd\n"); for (i=1; i<=m; i++) printf("%s%15.7g",i%3==1?"\n ":"", (double)errd[i-1]); printf("\n err\n"); for (i=1; i<=m; i++) printf("%s%15.7g",i%3==1?"\n ":"", (double)err[i-1]); printf("\n"); return 0; }
void advance_turn(CHAR_DATA * ch) { CHAR_DATA *rch; int q=0; for (rch = ch->in_room->people; rch != NULL; rch = rch->next_in_room) { if(rch->infight) q++; } if(q<1); return; check_winner(ch->in_room); q = 0; for (rch = ch->in_room->people; rch != NULL; rch = rch->next_in_room) { if(rch == NULL || !rch->infight) continue; do_map(rch,"forced"); } ch->in_room->next = NULL; while(ch->in_room->next == NULL) { for (rch = ch->in_room->people; rch != NULL; rch = rch->next_in_room) { if(rch == NULL || !rch->infight) continue; if(rch->ATToCast < rch->AT) { say_spell (rch, rch->SnToCast); tile_spell(rch, rch->SnToCast); rch->ATToCast = 0; rch->SnToCast = 0; rch->casting = FALSE; rch->CastTargY = 0; rch->CastTargX = 0; } rch->AT += rch->speed; if(rch->AT >= 100) { rch->AT = 100; ch->in_room->turn = rch; break; } } } ch->in_room->turn->MoveLeft = ch->in_room->turn->base_move; ch->in_room->turn->AttackLeft = 1; printf_to_char(rch,"%s's turn.\n",fcn(ch->in_room->turn)); }
//----------------------------------------------------------------------------- // Return true if timeout occurs. static bool waitTimeout(bool (*fcn)()) { uint32_t m = micros(); while (fcn()) { if ((micros() - m) > BUSY_TIMEOUT_MICROS) { return true; } } return false; // Caller will set errorCode. }
static void local_traverse(struct tnode *p, void (*fcn) (struct command *, char *), char *data) { if (!p) return; if (p->data) fcn(p->data, data); local_traverse(p->low, fcn, data); local_traverse(p->eq, fcn, data); local_traverse(p->hi, fcn, data); }
int system_layer2_multithreaded_callback::proc_poll_loop() { // POLL_COUNT struct kevent chlist[POLL_COUNT]; // events we want to monitor struct kevent evlist[POLL_COUNT]; // events that were triggered int nev, i, kq; kq = kqueue(); if (kq == -1) { perror("kqueue"); } EV_SET(&chlist[0], netif_obj_in_system->get_fd(), EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, (void *)&system_layer2_multithreaded_callback::fn_netif_cb); EV_SET(&chlist[1], tx_pipe[PIPE_RD], EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, (void *)&system_layer2_multithreaded_callback::fn_tx_cb); EV_SET(&chlist[2], 0, EVFILT_TIMER, EV_ADD | EV_ENABLE, 0, TIME_PERIOD_25_MILLISECONDS, (void *)&system_layer2_multithreaded_callback::fn_timer_cb); do { nev = kevent(kq, chlist, POLL_COUNT, evlist, POLL_COUNT, NULL); if (local_system == NULL) { // System has been shut down sem_post(shutdown_sem); return 0; } if (nev == -1) { perror("kevent()"); exit(EXIT_FAILURE); } else if (nev > 0) { for (i = 0; i < nev; i++) { int (*fcn)(struct kevent *) = (int (*)(struct kevent *))evlist[i].udata; int rv = fcn(&evlist[i]); if (rv < 0) { return -1; } } } } while (1); return 0; }
static QImage decodeNotificationSpecImageHint(const QDBusArgument& arg) { int width, height, rowStride, hasAlpha, bitsPerSample, channels; QByteArray pixels; char* ptr; char* end; arg.beginStructure(); arg >> width >> height >> rowStride >> hasAlpha >> bitsPerSample >> channels >> pixels; arg.endStructure(); //qDebug() << width << height << rowStride << hasAlpha << bitsPerSample << channels; #define SANITY_CHECK(condition) \ if (!(condition)) { \ qWarning() << "Sanity check failed on" << #condition; \ return QImage(); \ } SANITY_CHECK(width > 0); SANITY_CHECK(width < 2048); SANITY_CHECK(height > 0); SANITY_CHECK(height < 2048); SANITY_CHECK(rowStride > 0); #undef SANITY_CHECK QImage::Format format = QImage::Format_Invalid; void (*fcn)(QRgb*, const char*, int) = 0; if (bitsPerSample == 8) { if (channels == 4) { format = QImage::Format_ARGB32; fcn = copyLineARGB32; } else if (channels == 3) { format = QImage::Format_RGB32; fcn = copyLineRGB32; } } if (format == QImage::Format_Invalid) { qWarning() << "Unsupported image format (hasAlpha:" << hasAlpha << "bitsPerSample:" << bitsPerSample << "channels:" << channels << ")"; return QImage(); } QImage image(width, height, format); ptr = pixels.data(); end = ptr + pixels.length(); for (int y=0; y<height; ++y, ptr += rowStride) { if (ptr + channels * width > end) { qWarning() << "Image data is incomplete. y:" << y << "height:" << height; break; } fcn((QRgb*)image.scanLine(y), ptr, width); } return image; }
static int wizard_idnode_save_simple ( access_t *perm, void *opaque, const char *op, htsmsg_t *args, htsmsg_t **resp ) { int r; wizard_build_fcn_t fcn = opaque; wizard_page_t *page = fcn(); r = api_idnode_save_simple(perm, &page->idnode, op, args, resp); page->free(page); return r; }
void announce_move(CHAR_DATA * ch, char * thedir) { CHAR_DATA *rch; ch->MoveLeft--; for (rch = ch->in_room->people; rch != NULL; rch = rch->next_in_room) { printf_to_char(rch,"%s moves %s.{x\n",fcn(ch),thedir); // do_map(rch,"forced"); } }
void function(double par[]){ int npar; double *gin; //double *par; double f; int iflag; //fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag){ fcn(npar,gin,f,par,iflag); cout<<"f: " << f <<endl; }
static void Cd1fcn(int n, const double x[], double *g, function_info *state) { int ind; if ((ind = FT_lookup(n, x, state)) < 0) { /* shouldn't happen */ fcn(n, x, g, state); if ((ind = FT_lookup(n, x, state)) < 0) { error(_("function value caching for optimization is seriously confused")); } } Memcpy(g, state->Ftable[ind].grad, n); }
_shd_static_fixture::_shd_static_fixture(void (*fcn)(void), const char *name){ try{ fcn(); } catch(const std::exception &e){ std::cerr << "Exception in static block " << name << std::endl; std::cerr << " " << e.what() << std::endl; } catch(...){ std::cerr << "Exception in static block " << name << std::endl; } }
static int function(const string & name, stack<double> & par, double & result, const dic_type & dictionary) /*********************************************************************** * * * Name: function Date: 03.10.00 * * Author: Evgeni Chernyaev Revised: * * * * Function: Finds value of the function. * * This function is used by operand(). * * * * Parameters: * * name - name of the function. * * par - stack of parameters. * * result - value of the function. * * dictionary - dictionary of available variables and functions. * * * ***********************************************************************/ { int npar = par.size(); if (npar > MAX_N_PAR) return EVAL::ERROR_UNKNOWN_FUNCTION; dic_type::const_iterator iter = dictionary.find(sss[npar]+name); if (iter == dictionary.end()) return EVAL::ERROR_UNKNOWN_FUNCTION; Item item = iter->second; double pp[MAX_N_PAR]; for(int i=0; i<npar; i++) { pp[i] = par.top(); par.pop(); } errno = 0; if (item.function == 0) return EVAL::ERROR_CALCULATION_ERROR; FCN fcn(item.function); switch (npar) { case 0: result = (*fcn.f0)(); break; case 1: result = (*fcn.f1)(pp[0]); break; case 2: result = (*fcn.f2)(pp[1], pp[0]); break; case 3: result = (*fcn.f3)(pp[2],pp[1],pp[0]); break; case 4: result = (*fcn.f4)(pp[3],pp[2],pp[1],pp[0]); break; case 5: result = (*fcn.f5)(pp[4],pp[3],pp[2],pp[1],pp[0]); break; } return (errno == 0) ? EVAL::OK : EVAL::ERROR_CALCULATION_ERROR; }
void QgsMeshLayer::assignDefaultStyleToDatasetGroup( int groupIndex ) { double groupMin, groupMax; QgsMeshLayerUtils::calculateMinMaxForDatasetGroup( groupMin, groupMax, mDataProvider, groupIndex ); QgsColorRampShader fcn( groupMin, groupMax, _createDefaultColorRamp() ); fcn.classifyColorRamp( 5, -1, QgsRectangle(), nullptr ); QgsMeshRendererScalarSettings scalarSettings; scalarSettings.setClassificationMinimumMaximum( groupMin, groupMax ); scalarSettings.setColorRampShader( fcn ); mRendererSettings.setScalarSettings( groupIndex, scalarSettings ); }
//----------------------------------------------------------------------------- // Return true if timeout occurs. static bool yieldTimeout(bool (*fcn)()) { m_busyFcn = fcn; uint32_t m = micros(); while (fcn()) { if ((micros() - m) > BUSY_TIMEOUT_MICROS) { m_busyFcn = 0; return true; } yield(); } m_busyFcn = 0; return false; // Caller will set errorCode. }
static int lang_c_file(RLang *lang, const char *file) { void *lib; char *cc, *p, name[512], buf[512]; const char *libpath, *libname; if (strlen (file) > (sizeof(name)-10)) return R_FALSE; if (!strstr (file, ".c")) sprintf (name, "%s.c", file); else strcpy (name, file); if (!r_file_exists (name)) { eprintf ("file not found (%s)\n", name); return R_FALSE; } { char *a = (char*)r_str_lchr (name, '/'); if (a) { *a = 0; libpath = name; libname = a+1; } else { libpath = "."; libname = name; } } p = strstr (name, ".c"); if (p) *p=0; cc = r_sys_getenv ("CC"); if (!cc || !*cc) cc = strdup ("gcc"); snprintf (buf, sizeof (buf), "%s -fPIC -shared %s -o %s/lib%s."R_LIB_EXT " $(pkg-config --cflags --libs r_core)", cc, file, libpath, libname); free (cc); if (r_sandbox_system (buf, 1) != 0) return R_FALSE; snprintf (buf, sizeof (buf), "%s/lib%s."R_LIB_EXT, libpath, libname); lib = r_lib_dl_open (buf); if (lib!= NULL) { void (*fcn)(RCore *); fcn = r_lib_dl_sym (lib, "entry"); if (fcn) fcn (lang->user); else eprintf ("Cannot find 'entry' symbol in library\n"); r_lib_dl_close (lib); } else eprintf ("Cannot open library\n"); r_file_rm (buf); // remove lib return 0; }
static void Cd2fcn(int nr, int n, const double x[], double *h, function_info *state) { int j, ind; if ((ind = FT_lookup(n, x, state)) < 0) { /* shouldn't happen */ fcn(n, x, h, state); if ((ind = FT_lookup(n, x, state)) < 0) { error(_("function value caching for optimization is seriously confused")); } } for (j = 0; j < n; j++) { /* fill in lower triangle only */ Memcpy( h + j*(n + 1), state->Ftable[ind].hess + j*(n + 1), n - j); } }
void theta_phi_grid(){ TCanvas * c1 = new TCanvas("c1", "c1", 1200, 500); TFile *f1 = TFile::Open("muon.root"); TTree *muon_tree = (TTree*)gDirectory->Get("muon_tree"); muon_tree->SetBranchAddress("evt", &evt); muon_tree->SetBranchAddress("n_mu", &n_mu); muon_tree->SetBranchAddress("e", &e); muon_tree->SetBranchAddress("theta", &theta); muon_tree->SetBranchAddress("phi", &phi); muon_tree->SetBranchAddress("x", x); muon_tree->SetBranchAddress("y", y); muon_tree->SetBranchAddress("z", z); muon_tree->SetBranchAddress("t", t); int nentries = (int) muon_tree ->GetEntries(); TH2F * h2 = new TH2F("h2", "h2", 360, 0, 360, 90, 0, 90); for (int i = 0; i < 1; ++i) { muon_tree ->GetEntry(i); i_mu_ref = TMath::LocMin(n_mu, t); double dummy[100]; int iflag = 0; int npar = 3; double par[npar]; for (int itheta = 0; itheta < 180; ++itheta) { for (int iphi = 0; iphi < 360; ++iphi) { double chi2; par[0] = double(itheta)*TMath::DegToRad(); par[1] = double(iphi)*TMath::DegToRad(); par[2] = 0; fcn(npar, dummy, chi2, par, iflag); h2->Fill(iphi, itheta, chi2); } } } // h2 ->SetAxisRange(20, 70, "X"); // h2 ->SetAxisRange(20, 70, "Y"); h2 ->SetMaximum(500); h2 ->Draw("surf3"); }
int main(int argc, char** argv) { LambdaFactory* c = new LambdaFactory(1); std::function<int(int)> fcn = c->add(); delete c; c = new LambdaFactory(100); std::cout << "value: " << fcn(1) << "\n"; std::cout << "c->a: " << c->aa() << "\n"; std::cout << "\n\n"; CtorPrinter printer; std::cout << "&printer: " << &printer << std::endl; forward_str(printer); //const Obj obj; //Obj& pobj = std::decay<Obj&>(obj); return 0; }
static int r_vala_file(RLang *lang, const char *file) { void *lib; char *p, name[512], buf[512]; char *vapidir; if (!strstr (file, ".vala")) sprintf (name, "%s.vala", file); else strcpy (name, file); if (!r_file_exists (name)) { eprintf ("file not found (%s)\n", name); return R_FALSE; } vapidir = r_sys_getenv ("VAPIDIR"); if (vapidir) { if (*vapidir) { snprintf (buf, sizeof (buf), "valac --vapidir=%s --pkg r_core -C %s", vapidir, name); } free (vapidir); } else sprintf (buf, "valac --pkg r_core -C %s", name); if (system (buf) != 0) return R_FALSE; p = strstr (name, ".vala"); if (p) *p=0; p = strstr (name, ".gs"); if (p) *p=0; snprintf (buf, sizeof (buf), "gcc -fPIC -shared %s.c -o lib%s."R_LIB_EXT " $(pkg-config --cflags --libs r_core gobject-2.0)", name, name); if (system (buf) != 0) return R_FALSE; snprintf (buf, sizeof (buf), "./lib%s."R_LIB_EXT, name); lib = r_lib_dl_open (buf); if (lib!= NULL) { void (*fcn)(RCore *); fcn = r_lib_dl_sym (lib, "entry"); if (fcn) fcn (lang->user); else eprintf ("Cannot find 'entry' symbol in library\n"); r_lib_dl_close (lib); } else eprintf ("Cannot open library\n"); r_file_rm (buf); // remove lib sprintf (buf, "%s.c", name); // remove .c r_file_rm (buf); return 0; }