/** * writeFile(path, data, size [,mode=0644]) * @param args * @return */ static JSVAL fs_writefile(JSARGS args) { HandleScope scope; String::Utf8Value path(args[0]->ToString()); String::Utf8Value data(args[1]->ToString()); ssize_t size; if (args.Length() > 2) { size = args[2]->IntegerValue(); } else { size = strlen(*data); } mode_t mode = 0644; if (args.Length() > 3) { mode = args[3]->IntegerValue(); } int fd = open(*path, O_WRONLY | O_CREAT | O_TRUNC, mode); if (fd == -1) { return scope.Close(False()); } if (write(fd, *data, size) != size) { close(fd); return scope.Close(False()); } close(fd); return scope.Close(True()); }
Handle<Value> COEquals(const Arguments& args) { if(!IsComponent(args[0])) { return False(); } dtEntity::Component* component = UnwrapComponent(args.This()); dtEntity::Component* other = UnwrapComponent(args[0]); return (other == component) ? True() : False(); }
static JSVAL fs_isdir(JSARGS args) { HandleScope scope; String::Utf8Value path(args[0]->ToString()); struct stat buf; if (stat(*path, &buf)) { return scope.Close(False()); } if (S_ISDIR(buf.st_mode)) { return scope.Close(True()); } else { return scope.Close(False()); } }
JSVAL getDataRows(JSARGS args) { HandleScope scope; String::Utf8Value sql(args[0]->ToString()); mysql_ping(handle); int failure = mysql_query(handle, *sql); if (failure) { return ThrowException(String::New(mysql_error(handle))); } MYSQL_RES *result = mysql_use_result(handle); if (!result) { return scope.Close(False()); } unsigned int num_fields = mysql_num_fields(result); MYSQL_FIELD *fields = mysql_fetch_fields(result); Handle<Array>a = Array::New(mysql_num_rows(result)); Local<String> names[num_fields]; int types[num_fields]; for (unsigned int n=0; n<num_fields; n++) { names[n] = String::New(fields[n].name); types[n] = fields[n].type; } unsigned long rowNdx = 0; unsigned int i; MYSQL_ROW row; while ((row = mysql_fetch_row(result))) { unsigned long *lengths = mysql_fetch_lengths(result); JSOBJ o = Object::New(); for (i=0; i<num_fields; i++) { if (row[i] == NULL) { o->Set(names[i], Null()); } else { switch (types[i]) { case MYSQL_TYPE_NULL: o->Set(names[i], Null()); break; case MYSQL_TYPE_TINY: case MYSQL_TYPE_SHORT: case MYSQL_TYPE_LONG: case MYSQL_TYPE_TIMESTAMP: case MYSQL_TYPE_LONGLONG: case MYSQL_TYPE_INT24: o->Set(names[i], Integer::New(atoi(row[i]))); break; case MYSQL_TYPE_FLOAT: case MYSQL_TYPE_DOUBLE: o->Set(names[i], Number::New(atof(row[i]))); break; default: o->Set(names[i], String::New(row[i], lengths[i])); break; } } } a->Set(rowNdx++, o); } mysql_free_result(result); return scope.Close(a); }
void PrecisionModel::IsFloating(const FunctionCallbackInfo<Value>& args) { Isolate* isolate = Isolate::GetCurrent(); HandleScope scope(isolate); PrecisionModel *model = ObjectWrap::Unwrap<PrecisionModel>(args.This()); args.GetReturnValue().Set(model->_model->isFloating() ? True(isolate) : False(isolate)); }
Handle<Value> Geometry::Relate(const Arguments& args) { HandleScope scope; Geometry *geom = ObjectWrap::Unwrap<Geometry>(args.This()); if (args.Length() < 1) return ThrowException(String::New("requires at least one argument")); Geometry *other = ObjectWrap::Unwrap<Geometry>(args[0]->ToObject()); if (args.Length() == 1) { // Gets a relation pattern (string) from the two geometries char *pattern = GEOSRelate(geom->geos_geom_, other->geos_geom_); if (pattern == NULL) return ThrowException(String::New("couldn't get relate pattern")); Local<Value> pattern_obj = String::New(pattern); GEOSFree(pattern); return scope.Close(pattern_obj); } else if (args.Length() == 2) { // Returns a boolean if the two geometries relate according to the pattern argument String::Utf8Value pattern(args[1]->ToString()); unsigned char r = GEOSRelatePattern(geom->geos_geom_, other->geos_geom_, *pattern); if (r == 2) { return ThrowException(String::New("relate by pattern failed")); } return r ? True() : False(); } return ThrowException(String::New("invalid number of arguments")); }
/** * Cast the element to a False. * * @param element The element to cast. * @param success Is the cast successful? */ False CastToFalse(Element const& element, bool& success) { Element_* element_ = const_cast<Element_*>(element.ElementHandle()); False_* f = dynamic_cast<False_*>(element_); success = (0 != f); return False(); }
JSVAL connect(JSARGS args) { HandleScope scope; String::AsciiValue host(args[0]->ToString()); String::AsciiValue user(args[1]->ToString()); String::AsciiValue passwd(args[2]->ToString()); String::AsciiValue db(args[3]->ToString()); int port = 3306; if (args.Length() > 4) { port = args[4]->IntegerValue(); } if (!handle) { handle = mysql_init(NULL); // handle = mysql_real_connect(handle, "localhost", "mschwartz", "", "sim", 3306, NULL, 0); handle = mysql_real_connect(handle, *host, *user, *passwd, *db, port, NULL, 0); if (!handle) { printf("MYSQL ERROR '%d'\n", mysql_errno(handle)); return False(); } currentDb = strdup(*db); my_bool reconnect = 1; mysql_options(handle, MYSQL_OPT_RECONNECT, &reconnect); } else if (strcmp(*db, currentDb)) { if (mysql_select_db(handle, *db)) { return ThrowException(Exception::Error(String::New(mysql_error(handle)))); } delete [] currentDb; currentDb = strdup(*db); } mysql_ping(handle); return Undefined(); }
Element ExpectEqBuiltinImplementation::Interpret_( Environment& /*environment*/ , std::vector<Element> const& parms , Element const& /*addParms*/ ) { typedef std::vector<Element>::const_iterator Iterator; Iterator iter = parms.begin(); Iterator end = parms.end(); bool success = true; String title = CastToString(*iter, success); ++iter; std::string first = iter->ToString(); for(; iter != end; ++iter) { success = success && (first == iter->ToString()); } if (success) { Tests::Instance().Success(title.Value()); return True(); } else { std::stringstream ss; ss << title.Value() << ": " << parms[1].ToString() << " != " << parms[2].ToString(); Tests::Instance().Failure(ss.str()); return False(); } }
/** * Cast the element to a False. * * @param element The element to cast. * @param success Is the cast successful? */ False CastToFalse(Element const& element, bool& success) { std::shared_ptr<Element_> element_ = element.ElementHandle(); std::shared_ptr<False_> f = std::dynamic_pointer_cast<False_>(element_); success = (0 != f.get()); return False(); }
Handle<Value> Geometry::IsWithinDistance(const Arguments& args) { HandleScope scope; Geometry* geom = ObjectWrap::Unwrap<Geometry>(args.This()); Geometry* geom2 = ObjectWrap::Unwrap<Geometry>(args[0]->ToObject()); double distance = args[0]->NumberValue(); return geom->_geom->isWithinDistance(geom2->_geom, distance) ? True() : False(); }
/** * When you have an comparison operation, we will handle it differently * if it is all numbers. * * @version * - JR Lewis 2012.03.07 * - Initial version. */ Element ComparisonBuiltinsImplementation::Interpret_( Environment& , std::vector<Element> const& parms , Element const&) { bool result = true; bool all_numbers = true; size_t const PARMS_SIZE = parms.size(); for(size_t i=0; i<PARMS_SIZE && all_numbers; ++i) { Element const& element = parms[i]; all_numbers = (all_numbers && (element.Type() == Types::NUMBER)); } if (all_numbers) { result = InterpretAllNumbers_(parms); } else { for(size_t i=1; result && i<PARMS_SIZE; ++i) { result = InterpretNext_(parms[i - 1], parms[i] ); } } if (result) { return True(); } else { return False(); } }
/** * @function net.connect * * ### Synopsis * var sock = net.connect(host, port); * * This function creates a socket and connects to the specified host and port. * * @param {string} host - name of host to connect to * @param {int} port - port number to connect to * @return {int} sock - file descriptor or false if error occurred. */ static JSVAL net_connect (JSARGS args) { HandleScope scope; String::Utf8Value host(args[0]); int port = args[1]->IntegerValue(); struct hostent *h = gethostbyname(*host); if (h == NULL) { /* gethostbyname returns NULL on error */ #ifdef WIN32 perror("gethostbyname failed"); { FILE* fp=fopen("c:\\error.txt", "a+b"); fprintf(fp, "%d", __LINE__); fclose(fp); } #else herror("gethostbyname failed"); #endif return False(); } struct sockaddr_in sock_addr; /* memcpy(dest, src, length) */ memcpy(&sock_addr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); /* copy the address to the sockaddr_in struct. */ /* set the family type (PF_INET) */ sock_addr.sin_family = h->h_addrtype; /* * addr->sin_port = port won't work because they are different byte * orders */ sock_addr.sin_port = htons(port); int fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { { FILE* fp=fopen("c:\\error.txt", "a+b"); fprintf(fp, "%d", __LINE__); fclose(fp); } perror("socket"); return False(); } if (connect(fd, (struct sockaddr *) &sock_addr, sizeof (struct sockaddr_in)) < 0) { /* connect returns -1 on error */ { FILE* fp=fopen("c:\\error.txt", "a+b"); fprintf(fp, "%d", __LINE__); fclose(fp); } perror("connect(...) error"); close(fd); return False(); } return scope.Close(Integer::New(fd)); }
JSVAL getDataRow(JSARGS args) { HandleScope scope; String::Utf8Value sql(args[0]->ToString()); mysql_ping(handle); int failure = mysql_query(handle, *sql); if (failure) { return ThrowException(String::New(mysql_error(handle))); } MYSQL_RES *result = mysql_store_result(handle); if (!result) { return scope.Close(False()); } unsigned int num_fields = mysql_num_fields(result); MYSQL_FIELD *fields = mysql_fetch_fields(result); MYSQL_ROW row = mysql_fetch_row(result); unsigned long *lengths = mysql_fetch_lengths(result); JSOBJ o = Object::New(); for (unsigned int i=0; i<num_fields; i++) { if (row[i] == NULL) { o->Set(String::New(fields[i].name), Null()); } else { switch (fields[i].type) { case MYSQL_TYPE_TINY: o->Set(String::New(fields[i].name), Integer::New(atoi(row[i]))); break; case MYSQL_TYPE_SHORT: o->Set(String::New(fields[i].name), Integer::New(atoi(row[i]))); break; case MYSQL_TYPE_LONG: o->Set(String::New(fields[i].name), Integer::New(atoi(row[i]))); break; case MYSQL_TYPE_FLOAT: o->Set(String::New(fields[i].name), Integer::New(atoi(row[i]))); break; case MYSQL_TYPE_DOUBLE: o->Set(String::New(fields[i].name), Integer::New(atoi(row[i]))); break; case MYSQL_TYPE_NULL: o->Set(String::New(fields[i].name), Null()); break; case MYSQL_TYPE_TIMESTAMP: o->Set(String::New(fields[i].name), Integer::New(atoi(row[i]))); break; case MYSQL_TYPE_LONGLONG: o->Set(String::New(fields[i].name), Integer::New(atoi(row[i]))); break; case MYSQL_TYPE_INT24: o->Set(String::New(fields[i].name), Integer::New(atoi(row[i]))); break; default: o->Set(String::New(fields[i].name), String::New(row[i], lengths[i])); break; } } } mysql_free_result(result); return scope.Close(o); }
/** * @function SFTP.writeFile * * ### Synopsis * * var status = SFTP.writeFile(handle, srcPath, dstPath); * var status = SFTP.writeFile(handle, srcPath, dstPath, mode); * * Write file to remote server via SFTP. * * @param {object} handle - opaque handle to already open SFTP connection. * @param {string} srcPath - path to file in local file system to send. * @param {string} dstPath - path to file in remote file system to create. * @param {int} mode - desired resulting file permissions of file on remote end. * @return {boolean} success - true if the transfer succeeded. * * ### Note * If mode is not provided, the file mode of the file being sent will be used. */ static JSVAL sftp_writeFile (JSARGS args) { HandleScope scope; SFTP *handle = HANDLE(args[0]); String::Utf8Value srcPath(args[1]); String::Utf8Value dstPath(args[2]); int mode; struct stat fileinfo; if (stat(*srcPath, &fileinfo) != 0) { return scope.Close(String::New(strerror(errno))); } if (args.Length() > 3) { mode = args[3]->IntegerValue(); } else { mode = fileinfo.st_mode; } mode &= 0777; int fd = open(*srcPath, O_RDONLY); if (fd < 0) { return scope.Close(String::New(strerror(errno))); } LIBSSH2_SFTP_HANDLE *sftp_handle = libssh2_sftp_open(handle->sftp_session, *dstPath, LIBSSH2_FXF_WRITE | LIBSSH2_FXF_CREAT | LIBSSH2_FXF_TRUNC, mode); char mem[1024]; ssize_t toWrite = fileinfo.st_size; while (toWrite > 0) { ssize_t nRead = read(fd, mem, 1024); if (nRead < 0) { int eNum = errno; libssh2_sftp_close(sftp_handle); close(fd); errno = eNum; return scope.Close(False()); } int rc = libssh2_sftp_write(sftp_handle, mem, nRead); if (rc < 0) { libssh2_sftp_close(sftp_handle); close(fd); return scope.Close(False()); } toWrite -= nRead; } close(fd); libssh2_sftp_close(sftp_handle); return scope.Close(True()); }
static JSVAL fs_rmdir(JSARGS args) { HandleScope scope; String::Utf8Value path(args[0]->ToString()); if (rmdir(*path) == -1) { return scope.Close(False()); } return scope.Close(True()); }
static JSVAL fs_fstat(JSARGS args) { HandleScope scope; int fd = args[0]->IntegerValue(); struct stat buf; if (fstat(fd, &buf) == -1) { return scope.Close(False()); } return scope.Close(format_stat(buf)); }
static JSVAL popen_puts (JSARGS args) { HandleScope scope; FILE *fp = (FILE *) JSEXTERN(args[0]); String::Utf8Value s(args[1]->ToString()); if (fputs(*s, fp) == EOF) { return scope.Close(False()); } return scope.Close(True()); }
static JSVAL sem_Init(JSARGS args) { HandleScope scope; int ret; if ((ret = sem_init(&mutex, 1, 1)) < 0) { perror("sem_init"); return False(); } return True(); }
static JSVAL fs_lstat(const Arguments & args) { HandleScope scope; String::Utf8Value path(args[0]->ToString()); struct stat buf; if (lstat(*path, &buf) == -1) { return scope.Close(False()); } return scope.Close(format_stat(buf)); }
int ShellCmdArgToggle::operator()(const char * & arg, CmdLine & cmd) { CmdArgToggle bool_arg(*this); int badval = bool_arg(arg, cmd); if (! badval) { set((bool_arg) ? True() : False()); } return badval; }
static JSVAL fs_mtime(JSARGS args) { HandleScope scope; String::Utf8Value path(args[0]->ToString()); struct stat buf; if (stat(*path, &buf)) { return scope.Close(False()); } return scope.Close(Integer::New(buf.st_mtime)); }
/** * @function ssh2.exec * * ### Synopsis * * var success = ssh2.exec(conn, command); * * Execute command at remote host. * * @param {object} conn - connection returned from ssh.connect() * @param {string} command - command line to execute on remote host * @return {boolean} success - true if the command was successfuly executed. */ static JSVAL ssh2_exec(JSARGS args) { HandleScope scope; Local<External>wrap = Local<External>::Cast(args[0]); SSH2 *ssh2 = (SSH2 *)wrap->Value(); String::Utf8Value command(args[1]); if (!ssh2->RunCommand(*command)) { return scope.Close(False()); } return scope.Close(True()); }
static JSVAL fs_exists(JSARGS args) { HandleScope scope; String::Utf8Value path(args[0]->ToString()); struct stat buf; if (stat(*path, &buf)) { return scope.Close(False()); } return scope.Close(True()); }
static JSVAL net_socketpair(JSARGS args) { int sv[2]; if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == -1) { return False(); } JSARRAY a = Array::New(); a->Set(0, Integer::New(sv[0])); a->Set(1, Integer::New(sv[1])); return a; }
static JSVAL fs_readlink(JSARGS args) { HandleScope scope; String::Utf8Value path(args[0]->ToString()); char pathBuf[PATH_MAX]; ssize_t size = readlink(*path, pathBuf, PATH_MAX); if (size < 0) { return scope.Close(False()); } return scope.Close(String::New(pathBuf)); }
Handle<Value> ASIsAnimationPlaying(const Arguments& args) { dtEntity::AnimationSystem* ss = UnwrapAnimationSystem(args.This()); dtEntity::Component* acomp = ss->GetComponent(args[0]->Uint32Value()); if(acomp == NULL) { return False(); } dtEntity::AnimationComponent* animComp = static_cast<dtEntity::AnimationComponent*>(acomp); std::string animname = ToStdString(args[1]); return Boolean::New(animComp->IsAnimationPlaying(animname)); }
/** * writeFile(path, data, size [,mode=0644]) * @param args * @return */ static JSVAL fs_writefile64(JSARGS args) { HandleScope scope; String::Utf8Value path(args[0]->ToString()); String::Utf8Value data(args[1]->ToString()); mode_t mode = 0644; if (args.Length() > 2) { mode = args[2]->IntegerValue(); } string out = Base64Decode(*data); int fd = open(*path, O_WRONLY | O_CREAT | O_TRUNC, mode); if (fd == -1) { return scope.Close(False()); } ssize_t size = out.size(); if (write(fd, out.c_str(), size) != size) { close(fd); return scope.Close(False()); } close(fd); return scope.Close(True()); }
static JSVAL fs_mkdir(JSARGS args) { HandleScope scope; String::Utf8Value path(args[0]->ToString()); mode_t mode = 0700; if (args.Length() > 1) { mode = args[1]->IntegerValue(); } if (mkdir(*path, mode) == -1) { return scope.Close(False()); } return scope.Close(True()); }
static JSVAL fs_realpath(JSARGS args) { HandleScope scope; String::Utf8Value path(args[0]->ToString()); char *absolutePath = realpath(*path, NULL); if (!absolutePath) { return scope.Close(False()); } Handle<String>s = String::New(absolutePath); free(absolutePath); return scope.Close(s); }