예제 #1
0
String String::operator+(const char* str_) {
    String new_str;
    new_str.leng=leng+length_of(str_);
    new_str.str=new char[new_str.leng+1];
    for(int i=0; i<leng; i++) {
        new_str.str[i]=str[i];
    }
    for(int i=0; i<=length_of(str_); i++) {
        new_str.str[i+leng]=str_[i];
    }
    return new_str;
}
예제 #2
0
static int mk_ins_attrs(NODE *list, ATTR_VAL ins_attrs[])
{
  int i, type, len;
  NODE *attr;
  
  // add the attributes to the list
  for(i = 0; list != NULL && i < MAXATTRS; ++i, list = list->u.LIST.next) {
    attr = list->u.LIST.self;
    
    // make sure string attributes aren't too long
    type = type_of(attr->u.ATTRVAL.value);
    len = length_of(attr->u.ATTRVAL.value);
    if (type == STRING && len > MAXSTRINGLEN)
      return E_STRINGTOOLONG;
    
    ins_attrs[i].attrName = attr->u.ATTRVAL.attrname;
    ins_attrs[i].valType = type;
    ins_attrs[i].valLength = len;
    ins_attrs[i].value = value_of(attr->u.ATTRVAL.value);
  }
  
  // if list is too long then error
  if (i == MAXATTRS)
    return E_TOOMANYATTRS;
  
  return i;
}
예제 #3
0
파일: piranha.c 프로젝트: pcwalton/piranha
bool ebml_start_tag(struct ebml_writer *writer, uint32_t tag_id)
{
    assert(writer->tag_stack_size < length_of(writer->tag_offsets));

    uint32_t buf = htonl(tag_id);

    bool ok;
    if (tag_id & 0xff000000)
        ok = !!fwrite(&buf, 4, 1, writer->f);
    else if (tag_id & 0x00ff0000)
        ok = !!fwrite((char *)&buf + 1, 3, 1, writer->f);
    else if (tag_id & 0x0000ff00)
        ok = !!fwrite((char *)&buf + 2, 2, 1, writer->f);
    else
        ok = !!fwrite((char *)&buf + 3, 1, 1, writer->f);

    if (!ok)
        return false;

    writer->tag_offsets[writer->tag_stack_size++] = ftell(writer->f);

    // Write a placeholder size
    uint32_t zero = 0;
    if (!fwrite(&zero, 4, 1, writer->f))
        return false;

    return true;
}
예제 #4
0
String::String(const char* str_) {
    leng=length_of(str_);
    str=new char[leng+1];
    for(int i=0; i<leng; i++) {
        str[i]=str_[i];
    }
    str[leng]='\0';
}
예제 #5
0
void String::operator=(const char* str_) {
    delete str;
    leng=length_of(str_);
    str=new char[leng+1];
    for(int i=0; i<leng; i++) {
        str[i]=str_[i];
    }
    str[leng]='\0';
}
예제 #6
0
bool String::operator==(const char* str_) {
    int temp_leng=length_of(str_);
    if(leng==temp_leng) {
        for(int i=0; i<leng; i++) {
            if(str[i]!=str_[i])return false;
        }
    } else {
        return false;
    }
    return true;
}
예제 #7
0
bool BarPlayer2Init(player2_t* outPlayer, const char* defaultPlayer)
{
    player2_t player;
    struct _player_t result;
    int i;

    memset(&result, 0, sizeof(struct _player_t));

    for (i = 0; i < length_of(player2_backends); ++i)
    {
        player2_iface* backend = player2_backends[i];

        bool acceptPlayer = true;
        if (defaultPlayer && !(strcmp(backend->Id, defaultPlayer) == 0))
            acceptPlayer = false;

        if (acceptPlayer)
            result.player = backend->Create();

        if (result.player)
        {
            result.backend = backend;
            break;
        }
    }

    if (!result.backend)
        return false;

    player = malloc(sizeof(struct _player_t));
    if (!player)
        return false;

    *player = result;

    *outPlayer = player;

    return true;
}
예제 #8
0
void interp(NODE *n)
{
  int nattrs;				// number of attributes 
  int type;				// attribute type
  int len;				// attribute length
  int op;				// comparison operator
  NODE *temp, *temp1, *temp2;		// temporary node pointers
  char *attrname;			// temp attribute names
  void *value;			        // temp value	
  int nbuckets;			        // temp number of buckets
  int errval;				// returned error value
  RelDesc relDesc;
  Status status;
  int attrCnt, i, j;
  AttrDesc *attrs;
  string resultName;
  static int counter = 0;

  // if input not coming from a terminal, then echo the query

  if (!isatty(0))
    echo_query(n);

  switch(n->kind) {
  case N_QUERY:

    // First check if the result relation is specified

    if (n->u.QUERY.relname)
      {
	resultName = n->u.QUERY.relname;

	// Check if the result relation exists.
	status = attrCat->getRelInfo(resultName, attrCnt, attrs);
	if (status != OK && status != RELNOTFOUND)
	  {
	    error.print(status);
	    return;
	  }
      }
    else
      {
	resultName = "Tmp_Minirel_Result";

	status = relCat->getInfo(resultName, relDesc);
	if (status != OK && status != RELNOTFOUND)
	  {
	    error.print(status);
	    return;
	  }

	if (status == OK)
	  {
	    error.print(TMP_RES_EXISTS);
	    return;
	  }
      }


    // if no qualification then this is a simple select
    temp = n->u.QUERY.qual;
    if (temp == NULL) {

      // make a list of attribute names suitable for passing to select
      nattrs = mk_attrnames(temp1 = n->u.QUERY.attrlist, names, NULL);
      if (nattrs < 0) {
	print_error("select", nattrs);
	break;
      }

      for(int acnt = 0; acnt < nattrs; acnt++) {
	strcpy(attrList[acnt].relName, names[nattrs]);
	strcpy(attrList[acnt].attrName, names[acnt]);
	attrList[acnt].attrType = -1;
	attrList[acnt].attrLen = -1;
	attrList[acnt].attrValue = NULL;
      }
      
      if (status == RELNOTFOUND)
	{
	  // Create the result relation
	  attrInfo *createAttrInfo = new attrInfo[nattrs];
	  for (i = 0; i < nattrs; i++)
	    {
	      AttrDesc attrDesc;

	      strcpy(createAttrInfo[i].relName, resultName.c_str());
	      strcpy(createAttrInfo[i].attrName, attrList[i].attrName);
	      
	      status = attrCat->getInfo(attrList[i].relName,
					attrList[i].attrName,
					attrDesc);
	      if (status != OK)
		{
		  error.print(status);
		  return;
		}
	      createAttrInfo[i].attrType = attrDesc.attrType;
	      createAttrInfo[i].attrLen = attrDesc.attrLen;
	    }

	  status = relCat->createRel(resultName, nattrs, createAttrInfo);
	  delete []createAttrInfo;

	  if (status != OK)
	    {
	      error.print(status);
	      return;
	    }
	}
      else
	{
	  // Check to see that the attribute types match
	  if (nattrs != attrCnt)
	    {
	      error.print(ATTRTYPEMISMATCH);
	      return;
	    }

	  for (i = 0; i < nattrs; i++)
	    {
	      AttrDesc attrDesc;

	      status = attrCat->getInfo(attrList[i].relName,
					attrList[i].attrName,
					attrDesc);
	      if (status != OK)
		{
		  error.print(status);
		  return;
		}

	      if (attrDesc.attrType != attrs[i].attrType || 
		  attrDesc.attrLen != attrs[i].attrLen)
		{
		  error.print(ATTRTYPEMISMATCH);
		  return;
		}
	    }
	  free(attrs);
	}

      // make the call to QU_Select

      errval = QU_Select(resultName,
			 nattrs,
			 attrList,
			 NULL,
			 (Operator)0,
			 NULL);

      if (errval != OK)
	error.print((Status)errval);
    }

    // if qual is `attr op value' then this is a regular select
    else if (temp->kind == N_SELECT) {
	  
      temp1 = temp->u.SELECT.selattr;

      // make a list of attribute names suitable for passing to select
      nattrs = mk_attrnames(n->u.QUERY.attrlist, names,
			    temp1->u.QUALATTR.relname);
      if (nattrs < 0) {
	print_error("select", nattrs);
	break;
      }

      for(int acnt = 0; acnt < nattrs; acnt++) {
	strcpy(attrList[acnt].relName, names[nattrs]);
	strcpy(attrList[acnt].attrName, names[acnt]);
	attrList[acnt].attrType = -1;
	attrList[acnt].attrLen = -1;
	attrList[acnt].attrValue = NULL;
      }
      
      strcpy(attr1.relName, names[nattrs]);
      strcpy(attr1.attrName, temp1->u.QUALATTR.attrname);
      attr1.attrType = type_of(temp->u.SELECT.value);
      attr1.attrLen = -1;
      attr1.attrValue = (char *)value_of(temp->u.SELECT.value);

      if (status == RELNOTFOUND)
	{
	  // Create the result relation
	  attrInfo *createAttrInfo = new attrInfo[nattrs];
	  for (i = 0; i < nattrs; i++)
	    {
	      AttrDesc attrDesc;

	      strcpy(createAttrInfo[i].relName, resultName.c_str());
	      strcpy(createAttrInfo[i].attrName, attrList[i].attrName);
	      
	      status = attrCat->getInfo(attrList[i].relName,
					attrList[i].attrName,
					attrDesc);
	      if (status != OK)
		{
		  error.print(status);
		  return;
		}
	      createAttrInfo[i].attrType = attrDesc.attrType;
	      createAttrInfo[i].attrLen = attrDesc.attrLen;
	    }

	  status = relCat->createRel(resultName, nattrs, createAttrInfo);
	  delete []createAttrInfo;

	  if (status != OK)
	    {
	      error.print(status);
	      return;
	    }
	}
      else
	{
	  // Check to see that the attribute types match
	  if (nattrs != attrCnt)
	    {
	      error.print(ATTRTYPEMISMATCH);
	      return;
	    }

	  for (i = 0; i < nattrs; i++)
	    {
	      AttrDesc attrDesc;

	      status = attrCat->getInfo(attrList[i].relName,
					attrList[i].attrName,
					attrDesc);
	      if (status != OK)
		{
		  error.print(status);
		  return;
		}

	      if (attrDesc.attrType != attrs[i].attrType || 
		  attrDesc.attrLen != attrs[i].attrLen)
		{
		  error.print(ATTRTYPEMISMATCH);
		  return;
		}
	    }
	  free(attrs);
	}

      // make the call to QU_Select
      char * tmpValue = (char *)value_of(temp->u.SELECT.value);

      errval = QU_Select(resultName,
			 nattrs,
			 attrList,
			 &attr1,
			 (Operator)temp->u.SELECT.op,
			 tmpValue);

      delete [] tmpValue;
      delete [] attr1.attrValue;

      if (errval != OK)
	error.print((Status)errval);
    }

    // if qual is `attr1 op attr2' then this is a join
    else {

      temp1 = temp->u.JOIN.joinattr1;
      temp2 = temp->u.JOIN.joinattr2;

      // make an attribute list suitable for passing to join
      nattrs = mk_qual_attrs(n->u.QUERY.attrlist,
			     qual_attrs,
			     temp1->u.QUALATTR.relname,
			     temp2->u.QUALATTR.relname);
      if (nattrs < 0) {
	print_error("select", nattrs);
	break;
      }

      // set up the joined attributes to be passed to Join
      qual_attrs[nattrs].relName = temp1->u.QUALATTR.relname;
      qual_attrs[nattrs].attrName = temp1->u.QUALATTR.attrname;
      qual_attrs[nattrs + 1].relName = temp2->u.QUALATTR.relname;
      qual_attrs[nattrs + 1].attrName = temp2->u.QUALATTR.attrname;
      
      for(int acnt = 0; acnt < nattrs; acnt++) {
	strcpy(attrList[acnt].relName, qual_attrs[acnt].relName);
	strcpy(attrList[acnt].attrName, qual_attrs[acnt].attrName);
	attrList[acnt].attrType = -1;
	attrList[acnt].attrLen = -1;
	attrList[acnt].attrValue = NULL;
      }
      
      strcpy(attr1.relName, qual_attrs[nattrs].relName);
      strcpy(attr1.attrName, qual_attrs[nattrs].attrName);
      attr1.attrType = -1;
      attr1.attrLen = -1;
      attr1.attrValue = NULL;

      strcpy(attr2.relName, qual_attrs[nattrs+1].relName);
      strcpy(attr2.attrName, qual_attrs[nattrs+1].attrName);
      attr2.attrType = -1;
      attr2.attrLen = -1;
      attr2.attrValue = NULL;

      if (status == RELNOTFOUND)
	{
	  // Create the result relation
	  attrInfo *createAttrInfo = new attrInfo[nattrs];
	  for (i = 0; i < nattrs; i++)
	    {
	      AttrDesc attrDesc;

	      strcpy(createAttrInfo[i].relName, resultName.c_str());

	      // Check if there is another attribute with same name
	      for (j = 0; j < i; j++)
		if (!strcmp(createAttrInfo[j].attrName, attrList[i].attrName))
		  break;

	      strcpy(createAttrInfo[i].attrName, attrList[i].attrName);

	      if (j != i)
		sprintf(createAttrInfo[i].attrName, "%s_%d", 
			createAttrInfo[i].attrName, counter++);
	      
	      status = attrCat->getInfo(attrList[i].relName,
					attrList[i].attrName,
					attrDesc);
	      if (status != OK)
		{
		  error.print(status);
		  return;
		}
	      createAttrInfo[i].attrType = attrDesc.attrType;
	      createAttrInfo[i].attrLen = attrDesc.attrLen;
	    }

	  status = relCat->createRel(resultName, nattrs, createAttrInfo);
	  delete []createAttrInfo;

	  if (status != OK)
	    {
	      error.print(status);
	      return;
	    }
	}
      else
	{
	  // Check to see that the attribute types match
	  if (nattrs != attrCnt)
	    {
	      error.print(ATTRTYPEMISMATCH);
	      return;
	    }

	  for (i = 0; i < nattrs; i++)
	    {
	      AttrDesc attrDesc;

	      status = attrCat->getInfo(attrList[i].relName,
					attrList[i].attrName,
					attrDesc);
	      if (status != OK)
		{
		  error.print(status);
		  return;
		}

	      if (attrDesc.attrType != attrs[i].attrType || 
		  attrDesc.attrLen != attrs[i].attrLen)
		{
		  error.print(ATTRTYPEMISMATCH);
		  return;
		}
	    }
	  free(attrs);
	}

      // make the call to QU_Join

      errval = QU_Join(resultName,
		       nattrs,
		       attrList,
		       &attr1,
		       (Operator)temp->u.JOIN.op,
		       &attr2);

      if (errval != OK)
	error.print((Status)errval);
    }

    if (resultName == string( "Tmp_Minirel_Result"))
      {
	// Print the contents of the result relation and destroy it
	status = UT_Print(resultName);
	if (status != OK)
	  error.print(status);

	status = relCat->destroyRel(resultName);
	if (status != OK)
	  error.print(status);
      }

    break;

  case N_INSERT:

    // make attribute and value list to be passed to QU_Insert
    nattrs = mk_ins_attrs(n->u.INSERT.attrlist, ins_attrs);
    if (nattrs < 0) {
      print_error("insert", nattrs);
      break;
    }
    
    // make the call to QU_Insert
    int acnt;
    for(acnt = 0; acnt < nattrs; acnt++) {
      strcpy(attrList[acnt].relName, n->u.INSERT.relname);
      strcpy(attrList[acnt].attrName, ins_attrs[acnt].attrName);
      attrList[acnt].attrType = (Datatype)ins_attrs[acnt].valType;
      attrList[acnt].attrLen = -1;
      attrList[acnt].attrValue = ins_attrs[acnt].value;
    }
      
    errval = QU_Insert(n->u.INSERT.relname,
		       nattrs,
		       attrList);

    for (acnt = 0; acnt < nattrs; acnt++)
      delete [] attrList[acnt].attrValue;

    if (errval != OK)
      error.print((Status)errval);
    
    break;

  case N_DELETE:

    // set up the name of deletion relation
    qual_attrs[0].relName = n->u.DELETE.relname;
    
    // if qualification given...
    if ((temp1 = n->u.DELETE.qual) != NULL) {
      // qualification must be a select, not a join
      if (temp1->kind != N_SELECT) {
	cerr << "Syntax Error" << endl;
	break;
      }
	    
      temp2 = temp1->u.SELECT.selattr;
/*      
      // make sure attribute in qualification is from deletion rel
      if (strcmp(n->u.DELETE.relname, temp2->u.QUALATTR.relname)) {
	print_error("delete", E_INCOMPATIBLE);
	break;
      }
*/      
      // set up qualification
      attrname = temp2->u.QUALATTR.attrname;
      op = temp1->u.SELECT.op;
      type = type_of(temp1->u.SELECT.value);
      len = length_of(temp1->u.SELECT.value);
      value = value_of(temp1->u.SELECT.value);
    }
    
    // otherwise, set up for no qualification
    else {
      attrname = NULL;
      op = (Operator)0;
      type = 0;
      len = 0;
      value = NULL;
    }

    // make the call to QU_Delete

    if (attrname)
      errval = QU_Delete(n -> u.DELETE.relname,
			 attrname,
			 (Operator)op,
			 (Datatype)type,
			 (char *)value);
    else
      errval = QU_Delete(n -> u.DELETE.relname,
			 "",
			 (Operator)op,
			 (Datatype)type,
			 (char *)value);

    delete [] value;

    if (errval != OK)
      error.print((Status)errval);

    break;

  case N_CREATE:

    // make a list of ATTR_DESCRS suitable for sending to UT_Create
    nattrs = mk_attr_descrs(n->u.CREATE.attrlist, attr_descrs);
    if (nattrs < 0) {
      print_error("create", nattrs);
      break;
    }

    // get info about primary attribute, if there is one
    if ((temp = n->u.CREATE.primattr) == NULL) {
      attrname = NULL;
      nbuckets = 1;
    } else {
      attrname = temp->u.PRIMATTR.attrname;
      nbuckets = temp->u.PRIMATTR.nbuckets;
    }

    for(acnt = 0; acnt < nattrs; acnt++) {
      strcpy(attrList[acnt].relName, n -> u.CREATE.relname);
      strcpy(attrList[acnt].attrName, attr_descrs[acnt].attrName);
      attrList[acnt].attrType = attr_descrs[acnt].attrType;
      attrList[acnt].attrLen = attr_descrs[acnt].attrLen;
      attrList[acnt].attrValue = NULL;
    }
      
    // make the call to UT_Create
    errval = relCat->createRel(n -> u.CREATE.relname,
			       nattrs,
			       attrList);

    if (errval != OK)
      error.print((Status)errval);


    break;

  case N_DESTROY:

    errval = relCat->destroyRel(n -> u.DESTROY.relname);
    if (errval != OK)
      error.print((Status)errval);

    break;

  case N_LOAD:

    errval = UT_Load(n -> u.LOAD.relname, n -> u.LOAD.filename);

    if (errval != OK)
      error.print((Status)errval);

    break;

  case N_PRINT:

    errval = UT_Print(n -> u.PRINT.relname);

    if (errval != OK)
      error.print((Status)errval);

    break;
    
  case N_HELP:

    if (n -> u.HELP.relname)
      errval = relCat->help(n -> u.HELP.relname);
    else
      errval = relCat->help("");

    if (errval != OK)
      error.print((Status)errval);

    break;

  default:                              // so that compiler won't complain
    assert(0);
  }
}
예제 #9
0
 static inline void format(const char *text, ios::ostream &fp)
 {
     format(text, length_of(text), fp);
 }
예제 #10
0
 static inline void write(const char *text, ios::ostream &fp)
 {
     write(text, length_of(text), fp);
 }
예제 #11
0
int main(void)
{
	int *a = malloc(sizeof(int)), *b = malloc(sizeof(int)), *c = malloc(sizeof(int));
	Stack s = NULL;
    Elem* temp = NULL;
	*a = 12;
    *b = 15;
    *c = 4;

    s = create_stack((void*)a);
    temp = (Elem*) calloc(1, sizeof(Elem));

	printf("1 - create_stack\tLength: %d\n", length_of(s));
    print_list_of_int(s);

	if(!is_empty(s))
	{
		s  = push(s, (void*)b);
	    printf("\n2 - push\tLength: %d\n", length_of(s));
        print_list_of_int(s);
    }
	else
	{
		printf("Unable to allocate a new stack\n");
		return 0;
	}

    s = push(s, (void*)c);
	printf("\n3 - push\tLength: %d\n", length_of(s));
    print_list_of_int(s);

    temp = pop(&s);
    printf("\n4 - pop\tLength: %d\n", length_of(s));
    if(!is_empty(temp))
        printf("temp->data = %d\n", *(int*)temp->data);
    print_list_of_int(s);
    free(temp);
    temp = NULL;

    temp = pop(&s);
    printf("\n5 - pop\tLength: %d\n", length_of(s));
    if(!is_empty(temp));
        printf("temp->data = %d\n", *(int*)temp->data);
    print_list_of_int(s);
    free(temp);
    temp = NULL;

    temp = pop(&s);
    printf("\n6 - pop\tLength: %d\n", length_of(s));
    if(!is_empty(temp));
        printf("temp->data = %d\n", *(int*)temp->data);
    print_list_of_int(s);
    free(temp);
    temp = NULL;

	s  = push(s, (void*)b);
	printf("\n7 - push\tLength: %d\n", length_of(s));
    print_list_of_int(s);

    delete_stack(s);
	free(a);
    free(b);
    free(c);
	return 0;
}