Error with Fork and NodeJS - c

I'm trying to use NodeJS with a C program to http://en.wikipedia.org/wiki/Setvbuf for JS files using Fork().
My program.c :
int readTabStr(int k)
{
char * tabString[] = {"p1.js","p2.js","p3.js","p4.js"};
int taille = sizeof(tabString)/sizeof(char);
int i;
for(i=0; i<taille; i++)
{
if (fork() == 0)
{
execlp("node","node", tabString[i], NULL);
}
else
{
wait(NULL);
}
}
return 0;
}
I'm using this JS file to launch the C file:
var FFI = require("../../lib/ffi");
var test = new FFI.Library("./readTab", {
"readTabStr": ["int", ["int" ]]
});
var resultat = test.readTabStr(0);
The problem is that after executing the p1.js, p2.js, p3.jsn p4.js. I get 4 error messages :
node.js:116
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module '/home/fighter/Documents/ffi/kerrighed/execlp/��$[]�U
`enter code here`��E��E��U��S������1�'
at Function._resolveFilename (module.js:299:11)
at Function._load (module.js:245:25)
at Array.<anonymous> (module.js:402:10)
at EventEmitter._tickCallback (node.js:108:26)
Any ideas?

Solved,
The problem was
int taille = sizeof(tabString)/sizeof(char);
I should do :
int taille = sizeof(tabString)/sizeof(char*);
Thanks anyway.

Related

C & libconfig: config_lookup_bool returns CONFIG_FALSE

I'm trying to read a bool setting from a group. I'm using the following code, but config_lookup_bool always returns CONFIG_FALSE. As far as I understood it should write the value into send_keys and return CONFIG_TRUE instead.
Code:
int send_keys;
config_t cfg;
config_init(&cfg);
config_read_file(&cfg, "config.cfg")
if (config_lookup_bool(&cfg, "settings.send_keys", &send_keys))
{
// do something here
}
config.cfg:
settings :
{
send_keys = "true";
start_apps = "false";
sync_clocks = "false";
pc_clock_is_origin = "true";
calibration_start_time = 0L;
};
Is there any mistake in my code or my thoughts?
There is a config_lookup_bool example here that includes the config file and the code: (use this example to compare against what you have.)
config file contents:
# authenticator
name = "JP";
enabled = false;
length = 186;
ldap = {
host = "ldap.example.com";
base = "ou=usr,o=example.com"; /* adapt this */
retries = [10, 15, 20, 60]; // Use more than 2
};
Source to read and process it...
int main(int argc, char **argv)
{
config_t cfg, *cf;
const config_setting_t *retries;
const char *base = NULL;
int count, n, enabled;
cf = &cfg;
config_init(cf);
if (!config_read_file(cf, "ldap.cfg")) {
fprintf(stderr, "%s:%d - %s\n",
config_error_file(cf),
config_error_line(cf),
config_error_text(cf));
config_destroy(cf);
return(EXIT_FAILURE);
}
if (config_lookup_bool(cf, "enabled", &enabled))
printf("Enabled: %s\n", enabled ? "Yep" : "Nope");
else
printf("Enabled is not defined\n");
if (config_lookup_string(cf, "ldap.base", &base))
printf("Host: %s\n", base);
retries = config_lookup(cf, "ldap.retries");
count = config_setting_length(retries);
printf("I have %d retries:\n", count);
for (n = 0; n < count; n++) {
printf("\t#%d. %d\n", n + 1,
config_setting_get_int_elem(retries, n));
}
config_destroy(cf);
return 0;
}
Thanks for your input. The problem was that I had true/false in "" and therefore it was parsed as string. It should have been
settings :
{
send_keys = true;
start_apps = false;
sync_clocks = false;
pc_clock_is_origin = true;
calibration_start_time = 0L;
};

How to include the size of array into the for loop without triggering error

This is the login function that I want to implement.
The problem is that I want to use the syntax of sizeof(id) in the for loop without triggering error.
Any solution??
int login();
int login()
{
int i, att, num_i, status;
att = 1;
status = 0;
num_i = 999;
char* id[100], * pass[100];
char* inp_id[100], inp_pass[100];
id[0] = "id1"; ///Sample ID
id[1] = "id2";
id[2] = "id3";
pass[0] = "pass1"; ///Sample pass
pass[1] = "pass2";
pass[2] = "pass3";
while (att <= 3)
{
printf("ID:");
scanf("%s", &inp_id);
for (i = 0; i < 3; ++i) /// I wanted this to repeat accordingly to the size of ID that was stored
{
if (strcmp(inp_id, id[i]) == 0) /// Cuz when I declare i > 100 when it call for i[4] and it doesn't exist, error occured.
{
num_i = i;
break; /// wanted it to break out of the loop once it got the id that's similar to what was entered
}
}
printf("Password:");
scanf("%s", &inp_pass);
if (num_i < 100)
{
if (strcmp(inp_pass, pass[num_i]) == 0)///checking pass according to the positon of i on the ID
{
status = 1;
att = 999;
}
}
att++;
}
I've deleted a portion of the code due to it asking for more information.
A simplified example of what I described in my comment:
Or at least these are components to help you understand.
struct account {
char *id;
char *pass;
};
static const struct account account_list[] = {
{ .id = "id1", .pass = "pass1" },
{ .id = "id2", .pass = "pass2" },
{ .id = "id3", .pass = "pass3" },
{ NULL },
};
struct account *a;
for (a = account_list; a.id; a++) {
....
}
Something like this is much easier to work with.

How to wait an input without stop the program in Xlib

The problem is this, I, am writing a chip 8 emulator in C, and i am using a library that use Xlib, for writing sprites attending input etc, the method that the library have for wait an input is this:
char gfx_wait(){
XEvent event;
gfx_flush();
while(1) {
XNextEvent(gfx_display,&event);
if(event.type==KeyPress) {
saved_xpos = event.xkey.x;
saved_ypos = event.xkey.y;
return XLookupKeysym(&event.xkey,0);
} else if(event.type==ButtonPress) {
saved_xpos = event.xkey.x;
saved_ypos = event.xkey.y;
return event.xbutton.button;
}
}
}
when i call this method the program stops waiting for input, I, need a methods that is called just when i press or release a button.
I just solve my problem, using this function :
int gfx_event_waiting(unsigned char *ch)
{
XEvent event;
gfx_flush();
while (1) {
if(XCheckMaskEvent(gfx_display,-1,&event)) {
if(event.type==KeyPress) {
*ch = XLookupKeysym(&event.xkey,0);
return 1;
}else if(event.type==KeyRelease){
return 1;
}else if (event.type==ButtonPress) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
}
}
and this is the main :
int
main(int argc, char *argv[])
{
int x;
int i;
unsigned char key_pressed,key_released;
Init();
LoadRom(SELECTED_ROM);
gfx_open(WIDTH,HEIGHT,"Chip 8 Emulator");
gfx_color(255,250,250);
for(;;){
if(!gfx_event_waiting(&key_pressed)){
opcode_cycle();
key_wait(key_released,0);
#if DEBUG
printf("# %d | %c #",x,key_pressed);
#endif
key_wait(key_pressed,1);
key_released = key_pressed;
gfx_clear();
if(DrawFlag)
Draw();
/*Big for for simulate a delay*/
for(i = 0; i <= 100000; i++)
;
}else{
x++;
}
}
}
I, am sure that there is a better way for do this , but you know, It's work...

Type Coercion Failed in Action Script 3, getting element from an array

I'm getting this error when I'm getting an element from an array and trying to use some functions on it:
TypeError: Error #1034: Type Coercion failed: cannot convert jogador$
to jogador. at laser/mover_tiro_baixo()
Sorry it's in portuguese, just like the code i'll paste below, but I think you get it: when I retrieve the element from an array it's of type 'jogador$', and if I try to use it as being of 'jogador' it doesn't work. I'm trying to manually force the coercion, as it was trying to convert the object to a DisplayObject (because I'm trying to use the hit test function), but that also didn't work:
TypeError: Error #1034: Type Coercion failed: cannot convert jogador$
to flash.display.DisplayObject. at laser/mover_tiro_baixo()
Code:
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.debugger.enterDebugger;
import flash.utils.getDefinitionByName;
import flash.utils.getQualifiedClassName;
import flash.display.DisplayObject;
public class laser extends MovieClip {
private var velo: Number;
private var meuPalco: Stage;
var dono: MovieClip;
var inimigoTipo: Number;
var Inimigos: Array;
var Dano: Number;
var Tam:Number;
var i:Number;
public function laser(palco: Stage, posX: Number, posY: Number, velocidade: Number, dano: Number, CimaBaixo: Number, Dono: MovieClip, vetJogadores: Array) {
this.dono = Dono;
this.Dano = dano;
if (getClass(this.dono) == "jogador") {
inimigoTipo = 0;
Inimigos = jogador(this.dono).VetorInimigos;
} else {
inimigoTipo = 1;
Inimigos = vetJogadores;
}
this.meuPalco = palco;
this.velo = velocidade;
this.x = posX;
this.y = posY;
if (CimaBaixo == 1) {
this.addEventListener(Event.ENTER_FRAME, mover_tiro_cima);
} else {
this.addEventListener(Event.ENTER_FRAME, mover_tiro_baixo);
}
meuPalco.addChild(this);
}
public function mover_tiro_cima(evt: Event) {
this.y -= velo;
if (inimigoTipo == 0) { // Dono do tiro é o player
var Tam: Number = Inimigos.length;
var i: Number = 0;
while (i < Tam) {
if (this.hitTestObject(Inimigos[i])) {
inimigo(Inimigos[i]).vida.Diminuir(this.Dano);
}
i++;
}
} else { // Dono do tiro é um inimigo
Tam = Inimigos.length;
i = 0;
while (i < Tam) {
if (this.hitTestObject(Inimigos[i])) {
jogador(Inimigos[i]).vida.Diminuir(this.Dano);
}
i++;
}
}
if (this.y <= 0) {
this.removeEventListener(Event.ENTER_FRAME, mover_tiro_cima);
meuPalco.removeChild(this);
}
}
public function mover_tiro_baixo(evt: Event) {
this.y += velo;
if (inimigoTipo == 0) { // Dono do tiro é o player
Tam = Inimigos.length;
i = 0;
while (i < Tam) {
if (this.hitTestObject(Inimigos[i])) {
inimigo(Inimigos[i]).vida.Diminuir(this.Dano);
}
i++;
}
} else { // Dono do tiro é um inimigo
Tam = Inimigos.length;
i = 0;
while (i < Tam) {
if (this.hitTestObject(Inimigos[i])) {
jogador(Inimigos[i]).vida.Diminuir(this.Dano);
}
i++;
}
}
if (this.y <= 0) {
this.removeEventListener(Event.ENTER_FRAME, mover_tiro_baixo);
meuPalco.removeChild(this);
}
}
static function getClass(obj: Object): String {
return String(Class(getDefinitionByName(getQualifiedClassName(obj))));
}
}
}
The error happens everytime the laser tests to see if it's hitting an enemy (hittest) in its functions. mover_tiro_baixo() moves the shot down.
Thanks people!
Edit: The way I create the arrays:
var player1:jogador = new jogador(stage,350,700,10,3,1);
var Jogadores:Array = [jogador];
player1.setJogadores(Jogadores);
var inimigo1:et = new et(stage,100,200,Jogadores);
var inimigo2:et = new et(stage,200,100,Jogadores);
var inimigo3:et = new et(stage,350,450,Jogadores);
var todosInimigos:Array = [inimigo1,inimigo2,inimigo3];
player1.DefinirInimigos(todosInimigos);
I've checked some other stack overflow questions that have similar type conversion errors. Most of the other people with a similar problem were actually filling their array with a Class, rather than objects that were instances of a Class. Are you filling those arrays like this?
for(var i:int = 0; i < 10; i++){
Inimigos.push(jogador); //incorrect
}
If so, that is the reason the problem is happening. This is the correct way to do it:
for(var i:int = 0; i < 10; i++){
Inimigos.push(new jogador()); //correct
}
EDIT:
In the new code you added to the first post, this line seems to be the problem:
var Jogadores:Array = [jogador]; //jogador is a class
Flash Actionscript Arrays cannot be "initialized" to only be able to contain a specific type of object. Actionscript Vectors are capable of that, but not Arrays. That line posted above initializes an array in which the first element is a class, not an object.

Powerbuilder ORCA API Deploy

I am trying to deploy my powerbuilder project to EAServer with orca api. But I am gettiing error -21 PBORCA_CBBUILDERROR (Deployment failed with errors). Also, callback function(BuildErrProc) is not being called. How can I overcome this problem? BTW I am using WindowsXP-32bit, Powerbuilder 11.2, EAServer 5.5, QT-4.8.0 opensource vs2010.
void __stdcall BuildErrProc(LPVOID lpUserData) {
PPBORCA_BLDERR blderr = (PPBORCA_BLDERR)lpUserData;
QString s;
s = QString::fromStdWString(blderr->lpszMessageText);
QMessageBox::information(0,"",s);
}
int WINAPI BuildProject(QString currentApplLibName,QString currentApplName,QString projectLibName,QString projectName,QStringList libraryNames)
{
PBORCA_BLDPROC fpBuildErrProc;
PPBORCA_BLDERR pBldErrData;
fpBuildErrProc = (PBORCA_BLDPROC) BuildErrProc;
pBldErrData = (PPBORCA_BLDERR) new PBORCA_BLDERR;
memset(pBldErrData, 0x00, sizeof(PBORCA_BLDERR));
HPBORCA hORCASession;
hORCASession = SessionOpen();
printf("Setting library list..\n");
LPTSTR* pLibNames = new LPTSTR[libraryNames.size()];
for(int i=0;i<libraryNames.size();i++) {
pLibNames[i]=(LPTSTR)libraryNames.at(i).utf16();
}
int r = PBORCA_SessionSetLibraryList(hORCASession, pLibNames, libraryNames.size());
if(r==0) {
printf("Library list OK.\n");
QMessageBox::information(0,"",currentApplName);
r = PBORCA_SessionSetCurrentAppl(hORCASession,(LPTSTR)currentApplLibName.utf16(),(LPTSTR)currentApplName.utf16());
if(r==0) {
printf("Setting current appl OK.\n");
r = PBORCA_BuildProject(hORCASession,(LPTSTR)projectLibName.utf16(),(LPTSTR)projectName.utf16(),fpBuildErrProc,pBldErrData);
if (r==0) {
printf("Building current prj OK.\n");
} else {
printf("Build FAILURE:%d\n",r);
}
} else {
printf("Current appl FAILURE.\n");
}
} else {
printf("Library list FAILURE:%d\n",r);
}
SessionClose(hORCASession);
return r;
}
I resolved it. Library list was wrong. When I set correct lib list it built the project successfully.

Resources