Beispiel #1
0
static
int find_end_idx(btree_raw_t* bt, btree_range_cursor_t *c, btree_range_meta_t* rmeta)
{
	btree_raw_node_t *node = c->node->pnode;
	char* key;
	int keylen;
	bool found;
	btree_metadata_t smeta;

	/* Handle open-right-end of a query */
	if(!rmeta->key_end)
		return node->nkeys;

	get_key_val(bt, node, node->nkeys - 1, &key, &keylen);

	int x = bt->cmp_cb(bt->cmp_cb_data, rmeta->key_end, rmeta->keylen_end,
		           key, keylen);

	if(x > 0 || (x == 0 && (rmeta->flags & RANGE_END_LE)))
		return node->nkeys;

	smeta.flags       = rmeta->flags;
	smeta.start_seqno = rmeta->start_seq;
	smeta.end_seqno   = rmeta->end_seq;

	x = bsearch_key_low(bt, node, rmeta->key_end, rmeta->keylen_end, &smeta, 0,
			c->cur_idx - 1, node->nkeys,
			(rmeta->flags & RANGE_END_LT) ? BSF_LATEST: BSF_NEXT,
	                &found);

	return x;
}
Beispiel #2
0
		uint8 *ParseFindText(const String &cText, int *pnLength)
		{
			// Allow spaces or valid hex chars.
			std::vector<uint8> cBytes;
			bool bHigh = true;
			bool bErr = false;
			uint8 nTmp = 0;
			for( uint32 i = 0; i < cText.size(); i++ )
				if( is_valid_hex_char(cText[i] ) )
					if( bHigh )
					{
						nTmp = get_key_val(cText[i]) << 4;
						bHigh = false;
					} else {
						nTmp |= get_key_val(cText[i]);
						bHigh = true;
						cBytes.push_back(nTmp);
					}
				else if( cText[i] != ' ' )
				{
					bErr = true;
					break;
				}
				
			if( bErr )
			{
				SetMessage(STR_MSG_INVALID_HEX_STRING);
				return NULL;
			}
			
			uint8 *pnBytes = (uint8 *)malloc(cBytes.size());
			*pnLength = cBytes.size();
			for( int i = 0; i < *pnLength; i++ )
				*(pnBytes + i) = cBytes[i];
			return pnBytes;
		}
Beispiel #3
0
static int parse(struct ini_config *ini, char *buff, unsigned long size)
{
	int curr_section;
	char *curr_pos;
	char *tmp;
	char line[128];
	int section_nr;
	int key_nr;
	int key_cnt;
	int val_cnt;
	int cnt;
	curr_section = 0;

	curr_pos = buff;
	while (size) {
		cnt = get_section_key_nr(curr_pos, size, &section_nr, &key_nr);
		if (cnt == -1)
			return 0;
		tmp = curr_pos;
		ini->section_tbl[curr_section].key_nr = key_nr;
		ini->section_tbl[curr_section].key_tbl = (struct key *)malloc(sizeof(struct key));
		if (!ini->section_tbl[curr_section].key_tbl)
			return 0;
		ini->section_tbl[curr_section].name = (char *)malloc(section_nr);
		tmp = get_line(tmp, line, 128);
		get_section(line, 128, ini->section_tbl[curr_section].name);
		for (int i = 0; i < key_nr; i++) {
			tmp = get_line(tmp, line, 128);
			get_key_val_nr(line, 128, &key_cnt, &val_cnt);
			ini->section_tbl[curr_section].key_tbl[i].key_name = (char *)malloc(key_cnt);
			ini->section_tbl[curr_section].key_tbl[i].key_val = (char *)malloc(val_cnt);
			get_key_val(line, 128, 
				ini->section_tbl[curr_section].key_tbl[i].key_name,
				ini->section_tbl[curr_section].key_tbl[i].key_val);
			printf("section:%s,key_name:%s, key_val%s\n", ini->section_tbl[curr_section].name,
				ini->section_tbl[curr_section].key_tbl[i].key_name,
				ini->section_tbl[curr_section].key_tbl[i].key_val);
		}
		curr_section++;
		curr_pos += cnt;
		size -= cnt;
	}
	return 1;
}
Beispiel #4
0
int main(int argc, char**argv)
{
  if (argc < 2)
    panic("Too few arguments!\n");
  if (!strcmp(argv[1], "create") && argc == 3)
    create_db(argv[2]);
  if (!strcmp(argv[1], "get") && argc == 4)
    get_key_val(argv[2], argv[3]);
  if (!strcmp(argv[1], "add") && argc == 5)
    add_key_val(argv[2], argv[3], argv[4], strlen(argv[4]));
  printf("Usage i.e.:\n");
  printf("   tikdb create todo.db\n");
  printf(" Add key-value:\n");
  printf("   tikdb add todo.db \"25.02.2012\" \"Watch new Dr Who episodes\"\n");
  printf(" Update key-value:\n");
  printf("   tikdb add todo.db \"25.02.2012\" \"Watch old Dr Who episodes\"\n");
  printf(" Retrival:\n");
  printf("   tikdb get todo.db \"25.02.2012\"\n");
  printf("Note: max key size is 31 ASCII chars\n");
  return 0;
}
Beispiel #5
0
		bool WriteByte(uint8 *pCursor, uint8 nValue, uint8 nRawValue)
		{
			if( ! is_valid_hex_char(nRawValue) )
				return false;
				
			nValue = get_key_val(nRawValue);
				
			if( m_bSecondChar )
			{
				nValue = (*pCursor & 0xF0) + nValue;
				AddUndoEntry(1, pCursor, *pCursor, nValue);
				*pCursor = nValue;
				m_bSecondChar = false;
				GetOwner()->MoveCursorBy(1);
			} else {
				nValue = (*pCursor & 0x0F) + (nValue << 4);
				AddUndoEntry(0, pCursor, *pCursor, nValue);
				*pCursor = nValue;
				m_bSecondChar = true;
			}
				
			return true;
		}	
Beispiel #6
0
int convert_csv(char *csv_in, char *csv_out){
  FILE *stream;
  int i;
  int nlines;
  int ncommas, maxcommas;
  char line[1024];
  int icol, irow;
  float *times;
  int ntimes,ntimes2;
  float dt_max;

#define IJ(irow,icol) (icol) + (maxcommas+1)*(irow)

  read_cfg();

  stream = fopen(csv_in,"r");
  if(stream==NULL)return -1;

  // pass 1 - count lines, and fields

  ncommas=0;
  maxcommas=0;
  nlines=0;
  while(!feof(stream)){
    if(fgets(line,1024,stream)==NULL)break;
    nlines++;
    trim(line);
    ncommas=0;
    for(i=0;i<strlen(line);i++){
      if(line[i]==',')ncommas++;
    }
    if(ncommas>maxcommas)maxcommas=ncommas;
  }

  // allocate memory
  csv_fields=NULL;
  NewMemory((void **)&csv_fields,nlines*(maxcommas+1)*sizeof(char *));
  for(i=0;i<nlines*(maxcommas+1);i++){
    csv_fields[i]=NULL;
  }

  rewind(stream);
  irow=0;
  while(!feof(stream)){
    int nlength;
    char *line2;
    int ncommas=0;
    int icol;
    int ij;

    if(fgets(line,1024,stream)==NULL)break;

    trim(line);
    nlength = strlen(line);

    NewMemory((void **)&line2,nlength+1);
    strcpy(line2,line);


    ncommas=0;
    icol=0;
    ij = IJ(irow,icol);
    csv_fields[ij]=line2;
    for(i=0;i<nlength;i++){
      if(line2[i]=='"')line2[i]=' ';
      if(line2[i]==','){
        line2[i]=0;
        icol++;
        ij = IJ(irow,icol);
        csv_fields[ij]=line2+i+1;
      }
    }
    line2[nlength]=0;
    irow++;
  }
  fclose(stream);
  for(i=0;i<nlines*(maxcommas+1);i++){
    csv_fields[i]=trim_both(csv_fields[i]);
  }


  ntimes = nlines-2;
  NewMemory((void **)&times,ntimes*sizeof(float));
  for(i=0;i<ntimes;i++){
    char *field;
    int ij;

    ij=IJ(i+2,0);
    field = csv_fields[ij];
    sscanf(field,"%f",times+i);
  }
  dt_max=times[1]-times[0];
  for(i=2;i<ntimes;i++){
    float dt;

    dt = times[i]-times[i-1];
    if(dt>dt_max)dt_max=dt;
  }
  if(dt_skip<dt_max)dt_skip=dt_max+1;
  ntimes2=0;
  for(i=0;i<ntimes;i++){
    if(i*dt_skip<=times[ntimes-1])ntimes2++;
  }


  stream=fopen(csv_out,"w");
  if(stream==NULL)return -1;

  if(key_unit!=NULL){
    fprintf(stream,"%s,",key_unit);
  }
  else{
    fprintf(stream,",");
  }
  for(i=0;i<ntimes2;i++){
    fprintf(stream,"%i",(int)i*dt_skip);
    if(i!=ntimes2-1)fprintf(stream,",");
  }
  fprintf(stream,"\n");
  for(icol=0;icol<=maxcommas;icol++){
    if(icol!=0&&handle_key(csv_fields[IJ(1,icol)])==0)continue;
    for(irow=0;irow<nlines;irow++){
      char *field;
      int ij;

      //  times[i] int(f2)*dt_skip times[i+1]
      //   v1         vx              v2
      //   (vx-v1)/(v2-v1) = (ts-t1)/(t2-t1)
      //  vx = v1 + (v2-v1)*(ts-t1)/(t2-t1)
      //      = (v1*t2 - v1*t1 - v2*t1 + v1*t1 + ts*(v2-v1))/(t2-t1)
      //      = (v1*t2 - v2*t1 + ts*(v2-v1))/(t2-t1)
      //      = (v1*(t2-ts) + v2*(ts - t1) )/(t2-t1)
      //      = w1*v1 + w2*v2
      //   where w1 = (t2-ts)/(t2-t1) and w2 = 1 - w1

      if(irow==2){
        float val;

        if(icol==0){
          if(key_label!=NULL){
            fprintf(stream,"%s",key_label);
          }
          else{
            fprintf(stream,"");
          }
        }
        ij=IJ(1,icol);
        field = csv_fields[ij];
        if(handle_key(field)==1){
          val = get_key_val(field);
          fprintf(stream,"%f,",val);
        }
        else{
          fprintf(stream,",",val);
        }
      }
      if(irow>1&&irow<nlines-1){
        float t1, ts, t2, w1, w2, v1,v2;

        t1 = times[irow-2];
        t2 = times[irow-1];
        if(irow==2){
          ts = 0.0;
        }
        else{
          if((int)(t1/dt_skip)==(int)(t2/dt_skip))continue;
          ts = (int)(t2/dt_skip)*dt_skip;
        }
        w1 = (t2-ts)/(t2-t1);
        w2 = 1.0 - w1;
        ij=IJ(irow,icol);
        field = csv_fields[ij];
        sscanf(field,"%f",&v1);
        ij=IJ(irow+1,icol);
        field = csv_fields[ij];
        sscanf(field,"%f",&v2);
        if(icol==0){
          fprintf(stream,"DT_%04i,",(int)ts);
        }
        else{
          fprintf(stream,"%f,",w1*v1+w2*v2);
        }
        continue;
      }
      if(irow==nlines-1){
        fprintf(stream,"\n");
      }
      if(irow!=nlines-1&&irow>=2){
        fprintf(stream,",");
      }
    }
  }

  fclose(stream);
  return 0;
}