Esempio n. 1
0
File: 0.c Progetto: 0branch/kona
K _0m(K a) {
  I t=a->t; P(4!=t && 3!=ABS(t), TE)
  I b=0,s=0; S v=0; K z=0; S m; if(3==ABS(t))m=CSK(a);

  struct stat sb; I ff=0;
  if(3==ABS(t) && strcmp(m,"/dev/fd/0") && strcmp(m,"/dev/stdin") ){
    if(stat(m,&sb)==-1)R FE;
    if((sb.st_mode & S_IFMT)==S_IFIFO)ff=1;}

  if(ff){                                                                      //read FIFO
    I fn,i,j; C buf[256]; z=newK(0,0);
    fn= open(m, O_RDONLY);
    while (read(fn,&buf,256)>0) {
      j=256; K y=0;
      for(i=0;i<256;i++){
        if(i>j){buf[j]='\0'; break;}
        if(buf[i]=='\r'||buf[i]=='\n')j=i; }
      I n=strlen(buf); y=newK(n<2?3:-3,n);
      memcpy(kC(y),&buf,n); kap(&z,&y); cd(y); }
    GC; }
  else if( 4==t && !**kS(a) ){
    char ss[300]; S adr=fgets(ss,sizeof(ss),stdin); if(adr==NULL)R newK(6,1);    //read stdin 0:`
    I i,j; for(i=0;i<300;++i){if(ss[i]=='\012')break;}
    I k=0; for(j=0;j<=i;j++){if(ss[j]!='\004')ss[k++]=ss[j];}
    z=newK(-3,k-1); for(j=0;j<k-1;++j){kC(z)[j]=ss[j];}
    GC; }
  else if( (3==ABS(t) && (!strcmp(m,"/dev/fd/0") || !strcmp(m,"/dev/stdin"))) //read stdin
           || 4==t && (!strcmp(*kS(a),"/dev/fd/0") || !strcmp(*kS(a),"/dev/stdin")) ){
    b=getdelim_(&v,&s,EOF,stdin);
    P(freopen_stdin() == NULL, FE)
    if(b==-1){z=newK(0,0); GC;} }
Esempio n. 2
0
//create ,pass and recieve a simple dictionary. [Note Dictionary will be
// homogenous. This demo gives an idea on how to deal withthe heterogenous
// case.]
int eg6()
{
int i=0,l=2;
K keys=ktn(KS,l);
K vals=ktn(KI,l);
K dict;
K result,resultk,resultv;
//A dictionary is a mapping of two conforming lists.
kI(vals)[0]=1; kI(vals)[1]=2;
kS(keys)[0]=ss("key1"); kS(keys)[1]=ss("key2");
// Turn these into a dictionary
dict=xD(keys,vals);
result=k(c,"{[x]x[`key3]:`int$.z.t;show x;x}",dict,(K)0);

printf( " This return has [type=%i] and contains %i enclosed elements\n\n\n",result->t,result->i);
//Extract the keys and value vectors from the k bject
resultk=kK(result)[0];
resultv=kK(result)[1];
//extract the lenth of the vectors
l=resultk->n;
printf( " The returned dictionary has %i elements.\n",l);
for(i=0;i<l;i++)
{
printf( " The %i key is %s \n",i,kS(resultk)[i]);
printf( " The associated value is %i \n",kI(resultv)[i]);
}

return 1;
}
Esempio n. 3
0
int main(int argc,char*argv[])
{
    K flip,result,columnNames,columnData;

    int row,col,nCols,nRows;
	int handle=khpu("localhost",1234,"user:password");
    if(handle<0) exit(1);
	result = k(handle,"`asc",(K)0);
	std::string str = "([]a:til 10;b:reverse til 10;c:10#01010101010b;d:`a)";
	result = k(handle,str.c_str(),(K)0);
    if(!result) printf("Network Error\n"),perror("Network"),exit(1);
    if(result->t==-128) printf("Server Error %s\n",result->s),kclose(handle),exit(1);
//    kclose(handle);
    if(result->t!=99&&result->t!=98) 
	{
		printf("type %d\n",result->t);
		r0(result);
		exit(1);
	}
    flip = ktd(result); // if keyed table, unkey it. ktd decrements ref count of arg.
    // table (flip) is column names!list of columns (data)
    columnNames = kK(flip->k)[0];
    columnData = kK(flip->k)[1];
    nCols = columnNames->n;
    nRows = kK(columnData)[0]->n;
    for(row=0;row<nRows;row++)
    {
        if(0==row)
        {
            for(col=0;col<nCols;col++)
            {   
                if(col>0)printf(",");
                printf("%s",kS(columnNames)[col]);
            }
            printf("\n");
        }
        for(col=0;col<nCols;col++)
        {
            K obj=kK(columnData)[col];

            if(col>0)printf(",");
            switch(obj->t)
            {
                case(1):{printf("%d",kG(obj)[row]);}break;
                case(4):{printf("%d",kG(obj)[row]);}break;
                case(5):{printf("%d",kH(obj)[row]);}break;
                case(6):{printf("%d",kI(obj)[row]);}break;
                case(7):{printf("%lld",kJ(obj)[row]);}break;
                case(8):{printf("%f",kE(obj)[row]);}break;
                case(9):{printf("%f",kF(obj)[row]);}break;
                case(11):{printf("%s",kS(obj)[row]);}break;
                default:{printf("unknown type");}break;
            }
        }
        printf("\n");
    }
    r0(flip);
    return 0;
}
Esempio n. 4
0
File: qmma.c Progetto: kevinarpe/kx
int mlput(K x,K y){
  int fstype=y->t,fsint=y->i,i=0,funcerr=0;
  K z;
  char b[2]={0,0};
  mlint64 j={0,0}; 
  //printf("%s:%d,%d\n","mlput",x->t,x->n);
  switch(x->t){
  case -KB: 
  case -KG: 
  case -KC:b[0]=x->g;R MLPutString(ml_lp,b); 
  case -KH:R MLPutInteger16(ml_lp,x->h);
  case -KI:R MLPutInteger32(ml_lp,x->i);
  case -KJ:*(J*)&j=x->j;R MLPutInteger64(ml_lp,j);
  case -KE:R MLPutReal32(ml_lp,x->e); 
  case -KF:R MLPutReal64(ml_lp,x->f); 
  case -KS:R MLPutSymbol(ml_lp,x->s);

  case KB: 
  case KG: 
  case KC:R MLPutByteString(ml_lp,kG(x),x->n);
  case KH:R MLPutInteger16List(ml_lp,kH(x),x->n);
  case KI:R MLPutInteger32List(ml_lp,kI(x),x->n);
  case KJ:R MLPutInteger64List(ml_lp,(mlint64*)kJ(x),x->n);
  case KE:R MLPutReal32List(ml_lp,kE(x),x->n); 
  case KF:R MLPutReal64List(ml_lp,kF(x),x->n);

  case KS:if(!MLPutFunction(ml_lp,"List",x->n)){
      R 0;
    }else{
      for(i=0;i<x->n;i++)if(!MLPutSymbol(ml_lp,kS(x)[i]))R 0;
    }
    break;
  case 0:
    if(0==x->n){
      R MLPutFunction(ml_lp, "List",0);
    }else if((3==x->n)&&(fstype==kK(x)[0]->t)){
      z=kK(x)[2];
      if(!MLPutFunction(ml_lp,kK(x)[1]->s,z->n)){R 0;}else{
	switch(z->t){
	case 0:for(i=0;i<z->n;i++)if(!mlput(kK(z)[i],y))R 0;break;
	case KH:for(i=0;i<z->n;i++)if(!MLPutInteger16(ml_lp,kH(z)[i]))R 0;break;
	case KI:for(i=0;i<z->n;i++)if(!MLPutInteger32(ml_lp,kI(z)[i]))R 0;break;
	case KJ:for(i=0;i<z->n;i++){*(J*)&j=kJ(z)[i];if(!MLPutInteger64(ml_lp,j))R 0;}break;
	case KE:for(i=0;i<z->n;i++)if(!MLPutReal32(ml_lp,kE(z)[i]))R 0;break;
	case KF:for(i=0;i<z->n;i++)if(!MLPutReal64(ml_lp,kF(z)[i]))R 0;break;
	case KS:for(i=0;i<z->n;i++)if(!MLPutSymbol(ml_lp,kS(z)[i]))R 0;break;
	case KC:for(i=0;i<z->n;i++){b[0]=kC(z)[i];if(!MLPutString(ml_lp,b))R 0;}break;
	default:break;
	}
      }
    }else{
      if(!MLPutFunction(ml_lp,"List",x->n)){R 0;}else{for(i=0;i<x->n;i++)if(!mlput(kK(x)[i],y)){MLPutSymbol(ml_lp,"ParaErr");funcerr=1;}if(funcerr)R 0;}
    }
    break; 
  default:
    R 0;
  }
  R 1;
}
Esempio n. 5
0
//Createing simple vectors
int eg5()
{

//Create a set of vectors of differing types each of length 5.
int i=0,l=5;
K vsymbol=ktn(KS,l);
K vint=ktn(KI,l);
K vfloat=ktn(KF,l);
K vdate=ktn(KD,l);
K vtime=ktn(KT,l);
K vdatetime=ktn(KZ,l);
K vtimestamp=ktn(KP,l);
for(i=0;i<l;i++)
{

kS(vsymbol)[i]=ss("w");
kI(vint)[i]=i;
kF(vfloat)[i]=i+0.0;
kI(vdate)[i]=i;
kI(vtime)[i]=i;
kF(vdatetime)[i]=i+0.1*i;
kJ(vtimestamp)[i]=i;
}

k(c,"display",vint,(K)0);
k(c,"display",vsymbol,(K)0);
k(c,"display",vfloat,(K)0);
k(c,"display",vdate,(K)0);
k(c,"display",vtime,(K)0);
k(c,"display",vdatetime,(K)0);
k(c,"display",vtimestamp,(K)0);

return 1;

}
Esempio n. 6
0
File: v.c Progetto: bakul/kona
K itemAtIndex(K a, I i) {   // Return i-th item from any type as K - TODO: oom wherever this is used
  I at=a->t;
  if( 0< at)R ci(a);
  if(-4==at)R Ks(kS(a)[i]);  //could refactor all this
  if(-3==at)R Kc(kC(a)[i]);
  if(-2==at)R Kf(kF(a)[i]);
  if(-1==at)R Ki(kI(a)[i]);
  R ci(kK(a)[i]);
}
Esempio n. 7
0
File: ko.c Progetto: elrzn/kona
K kclone(K a)//Deep copy -- eliminate where possible
{
  if(!a) R 0;
  I t=a->t,n=a->n;
  K z= 7==t?Kv():newK(t,n);
  if     (4==ABS(t)) DO(n, kS(z)[i]=kS(a)[i])  //memcpy everywhere is better
  else if(3==ABS(t)) DO(n, kC(z)[i]=kC(a)[i]) 
  else if(2==ABS(t)) DO(n, kF(z)[i]=kF(a)[i]) 
  else if(1==ABS(t)) DO(n, kI(z)[i]=kI(a)[i]) 
  else if(0==    t ) DO(n, kK(z)[i]=kclone(kK(a)[i])) 
  else if(5==    t ) DO(n, kK(z)[i]=kclone(kK(a)[i]))
  else if(7==    t )
  {
    I k=0;

    z->t=a->t; 
    I vt=z->n = a->n;
    K kv;

    V*v;
    SW(vt)
    {
      CS(1, k=((K)kV(a)[CODE])->n-1;
            M(z,kv=newK(-4,k+1))
            v=(V*)kK(kv);
            //v[k]=0;//superfluous reminder
            DO(k, V w=kW(a)[i];
                  if(VA(w))v[i]=w;  //TODO: is this ok for NAMES? see similar code in capture()
                  else
                  {
                    K r=kclone(*(K*)w); //oom
                    V q=newE(LS,r); //oom
                    kap((K*) kV(z)+LOCALS,&q);//oom
                    cd(q);//kap does ci
                    q=EVP(q); //oom free z etc. kap needs checking 
                    v[i]=q;
                  }
              )
      )
      CS(2, M(z,kv=newK(-4,3))
            v=(V*)kK(kv);
            memcpy(v,kW(a),3*sizeof(V));
        )
Esempio n. 8
0
//create ,pass and recieve a simple table.
int eg7()
{
K cc,d,e,v,tab;
K flip,result,columnNames,columnData;
int row,col,nCols,nRows;

cc=ktn(KS,2);kS(cc)[0]=ss("pid");kS(cc)[1]=ss("uid");
d=ktn(KS,3);kS(d)[0]=ss("ibm");kS(d)[1]=ss("gte");kS(d)[2]=ss("kvm");
e=ktn(KI,3);kI(e)[0]=1;kI(e)[1]=2;kI(e)[2]=3;
v=knk(2,d,e);
tab=xT(xD(cc,v));

flip=k(c,"{[x]a:update t:.z.t,y:.z.d from x;.tst.t:a;a}",tab,(K)0);

//Turn into a dictionary. flip->k [transpose?]
//Display table. [Borrowed from code.kx.com: 
// https://code.kx.com/trac/attachment/wiki/Cookbook/InterfacingWithC/csv.c ]
columnNames=kK(flip->k)[0];
columnData=kK(flip->k)[1];
nCols=columnNames->n;
nRows=kK(columnData)[0]->n;

for(row=0;row<nRows;row++)
{
if(0==row)
{
for(col=0;col<nCols;col++)
{
if(col>0)printf(",");
printf("%s",kS(columnNames)[col]);
}
printf("\n");
}
for(col=0;col<nCols;col++)
{
K obj=kK(columnData)[col];
if(col>0)printf(",");
switch(obj->t)
{
case(1):{printf("%d",kG(obj)[row]);}break;
case(4):{printf("%d",kG(obj)[row]);}break;
case(5):{printf("%d",kH(obj)[row]);}break;
case(6):{printf("%d",kI(obj)[row]);}break;
case(7):{printf("%lld",kJ(obj)[row]);}break;
case(8):{printf("%f",kE(obj)[row]);}break;
case(9):{printf("%f",kF(obj)[row]);}break;
case(11):{printf("%s",kS(obj)[row]);}break;
case(19):{printf("%i",kI(obj)[row]);}break;
case(14):{printf("%i",kI(obj)[row]);}break;
default:{printf("unknown type");}break;
}
}
printf("\n");
}

return 1;
}
Esempio n. 9
0
void KviMainWindow::freeAccelleratorKeySequence(QString & key)
{
	QKeySequence kS(key);
	for(QShortcut * pS = m_pAccellerators->first(); pS; pS = m_pAccellerators->next())
	{
		if(pS->key() == kS)
		{
			m_pAccellerators->removeRef(pS);
			return;
		}
	}
}
Esempio n. 10
0
File: 0.c Progetto: rdm/kona
K _0m(K a)
{
  I t=a->t;
  P(4!=t && 3!=ABS(t), TE)

  I b=0,s=0;
  S v=0;
  K z;
  if(4==t && !**kS(a)){
    b=getdelim_(&v,&s,EOF,stdin);
    P(freopen_stdin() == NULL, FE)
    if(b==-1){z=newK(0,0); GC;}
  }
Esempio n. 11
0
std::vector<std::string> q::qList2String(K data) throw(std::string) {
	if (data == K_NIL) {
		throw std::string("nil string or symbol list");
	}
	std::vector<std::string> result;
	switch (data->t) {
	case 0:													// char list list (a.k.a. string list)
		assert(data->n >= 0);
		result.reserve(static_cast<std::size_t>(data->n));
		for (J i = 0; i < data->n; ++i) {
			result.push_back(q2String(kK(data)[i]));
		}
		break;
	case KS:												// symbol list
		assert(data->n >= 0);
		result.reserve(static_cast<std::size_t>(data->n));
		for (J i = 0; i < data->n; ++i) {
			result.push_back(kS(data)[i]);
		}
		break;
	default:
		if ((ENUMmin <= data->t) && (data->t <= ENUMmax)) {	// enumerated symbol list
			K_ptr syms(k(0, "value", r1(data), K_NIL));
			assert(syms && (syms->n == data->n));
			assert(syms->n >= 0);
			result.reserve(static_cast<std::size_t>(syms->n));
			for (J i = 0; i < data->n; ++i) {
				result.push_back(kS(syms)[i]);
			}
		}
		else {
			throw std::string("not a string or symbol list");
		}
	}
	assert(result.size() == data->n);
	return result;
}
Esempio n. 12
0
File: k.c Progetto: geocar/qlua
static int dec(lua_State* L,K x)
{
	if(xt >= 0) {
		switch(xt) {
		case 0: LD(kK,dec); break;
		case 1: LD(kG,lua_pushboolean);break;
		//case 2: /* guid */
		case 4: LD(kG,lua_pushinteger);break;
		case 5: LD(kH,lua_pushinteger);break;
		case 6: LD(kI,lua_pushinteger);break;
		case 7: LD(kJ,lua_pushnumber);break;
		case 8: LD(kE,lua_pushinteger);break;
		case 9: LD(kF,lua_pushinteger);break;
		case 10:lua_pushlstring(L,kG(x),xn);break;
		case 11:LD(kS,lua_pushstring);break;
		//case 12: /* timestamp */
		//case 13: /* month */
		//case 14: /* date */
		//case 15: /* datetime */
		//case 16: /* timespan */
		//case 17: /* minute */
		//case 18: /* second */
		//case 19: /* time */
		//case 98: /* dict */
		//case 99: /* table */

		default:luaL_error(L, "unsupported array %d (nyi?)", xt);R 0;
		};
		R 1; /* create table */
	}
	switch(xt) {
	case -1: lua_pushboolean(L,x->g); break;
	//case -2: /* scalar guid */
	case -4: lua_pushinteger(L,x->g); break;
	case -5: lua_pushinteger(L,x->h); break;
	case -6: lua_pushinteger(L,x->i); break;
	case -7: lua_pushnumber(L,x->j); break;
	case -8: lua_pushnumber(L,x->e); break;
	case -9: lua_pushnumber(L,x->f); break;
	case -10: lua_pushlstring(L,&x->g,1); break;
	case -11: lua_pushstring(L,x->s); break;
	case -128:luaL_error(L,"K: %s",kS(x));R 0;
	default:luaL_error(L, "unsupported scalar %d (nyi?)", xt);R 0;
	};
	R 1;
}
Esempio n. 13
0
static K printtable(K x)
{
    K flip = ktd(x);
    K columns = kK(flip->k)[0];
    K rows = kK(flip->k)[1];
    
    int colcount = columns->n;
    int rowcount = kK(rows)[0]->n;
    
    for (int i = 0; i < colcount; i++)
        printf("%s\t", kS(columns)[i]);
    printf("\n");
    
    for (int i = 0; i < rowcount; i++) {
        for (int j = 0; j < colcount; j++) {
            printitem(kK(rows)[j], i);
            printf("\t");
        }
        printf("\n");
    }
    
    return (K) 0;
}
Esempio n. 14
0
File: k.c Progetto: geocar/qlua
static int enc(K*k,lua_State *L)
{
	switch (lua_type(L, -1)) {
	case LUA_TSTRING: 	{ size_t len;const char *str = lua_tolstring(L,-1,&len);(*k)=kpn(str,len);R 1;}	break;
	case LUA_TNUMBER:	{ F num = lua_tonumber(L,-1);(*k) = (num==floor(num))?kj((J)num):kf(num);R 1;} break;
	case LUA_TBOOLEAN:	{ (*k)=kb( lua_toboolean(L,-1) );R 1;}	break;
	case LUA_TNIL:		{ (*k)=ktn(0,0);R 1;}	break;
	case LUA_TTABLE:	{
		double p;
		int max = 0;
		int items = 0;
		int t_integer = 0, t_number = 0, t_boolean = 0, t_other= 0;

		lua_pushnil(L);
		/* table, startkey */
		while (lua_next(L, -2) != 0) {
			items++;

			/* table, key, value */
			switch (lua_type(L, -1)) {
			case LUA_TNUMBER:  t_number++; p = lua_tonumber(L,-1);  t_integer += (floor(p) == p); break;
			case LUA_TBOOLEAN: t_boolean++; break;
			default:   t_other++; break;	/* or anything else */
			};

			if (lua_type(L, -2) == LUA_TNUMBER &&
					(p = lua_tonumber(L, -2))) {
				/* Integer >= 1 ? */
				if (floor(p) == p && p >= 1) {
					if (p > max)
						max = p;
					lua_pop(L, 1);
					continue;
				}
			}

			/* Must not be an array (non integer key) */
			for (lua_pop(L,1); lua_next(L, -2) != 0; lua_pop(L,1)) ++items;
			max = 0;
			break;
		}
		lua_pushnil(L);
		if (max != items) {
			/* build K dictionary */
			K keys = ktn(KS,items);
			K values = ktn(0,items);
			int n = 0;
			/* table, startkey */
			while (lua_next(L, -2) != 0) {
				kS(keys)[n] = ss(lua_tostring(L, -2));
				if(!enc(kK(values)+n,L))R 0;
				lua_pop(L,1);
				++n;
			}
			*k = xD(keys,values);
			R 1;
		}
		/* build K list */
		if(t_other || ((!!t_boolean)+(!!t_number)) > 1) {
			K a = ktn(0,items);
			while (lua_next(L, -2) != 0) {
				p = lua_tonumber(L, -2);
				if(!enc(kK(a)+LI(p),L))R 0;
				lua_pop(L, 1);
			}
			*k = a;
			R 1;
		}
		if(t_boolean) {
			K a = ktn(KB,items);
			while (lua_next(L, -2) != 0) {
				p = lua_tonumber(L, -2);
				kG(a)[LI(p)] = lua_toboolean(L,-1);
				lua_pop(L, 1);
			}
			*k = a;
			R 1;
		}
		if(t_number == t_integer) {
			K a = ktn(KJ,items);
			while (lua_next(L, -2) != 0) {
				p = lua_tonumber(L, -2);
				kJ(a)[LI(p)] = (int)floor(lua_tonumber(L,-1));
				lua_pop(L, 1);
			}
			*k = a;
			R 1;
		}
		if(t_number) {
			K a = ktn(KF,items);
			while (lua_next(L, -2) != 0) {
				p = lua_tonumber(L, -2);
				kF(a)[LI(p)] = lua_tonumber(L,-1);
				lua_pop(L, 1);
			}
			*k = a;
			R 1;
		}
		*k = ktn(0,0);
		R 1;
	}; break;
	default:
		luaL_error(L, "Cannot serialise %s: %s", lua_typename(L, lua_type(L, -1)), "can't serialize type");
		R 0;
	};
}
Esempio n. 15
0
File: v.c Progetto: bakul/kona
S ES(K d){ R *kS(kK(d)[0]);}        //dictionary entry's symbol
Esempio n. 16
0
File: v.c Progetto: bakul/kona
K glue(K a, K b) { R Ks(sp(glueSS(*kS(a),*kS(b)))); } //oom