Example #1
0
/* Data Access Object (DAO) */
static pdb_t dao (int cmd, FILE *f, pdb_t in_db, sort sortby) {
    pdb_t *pdb=NULL,rec=NULL,hd=NULL;
    int i=0,ret;
    char buf[100];
    switch (cmd) {
        case CREATE:
			fprintf (f,"\"%s\",",in_db->title);
			fprintf (f,"\"%s\",",in_db->first_name);
			fprintf (f,"\"%s\",",in_db->last_name);
			fprintf (f,"\"%s\",",time2str(&in_db->date));
			fprintf (f,"\"%s\" \n",in_db->publ);
			break;
        case PRINT:
			for (;in_db;i++) {
				printf ("Title       : %s\n",     in_db->title);
				printf ("Author      : %s %s\n",  in_db->first_name, in_db->last_name);
				printf ("Date        : %s\n",     time2str(&in_db->date));
				printf ("Publication : %s\n\n",   in_db->publ);
				if (!((i+1)%3)) {
					printf ("Press Enter to continue.\n");
					ret = scanf ("%*[^\n]");
					if (ret<0) return rec; /* handle EOF */
					else getchar();
				}
				in_db=in_db->next;
			}
			break;
        case READLINE:
			if((fscanf(f," \"%[^\"]\",",in_db->title     ))<0)break;
			if((fscanf(f," \"%[^\"]\",",in_db->first_name))<0)break;
			if((fscanf(f," \"%[^\"]\",",in_db->last_name ))<0)break;
			if((fscanf(f," \"%[^\"]\",",buf              ))<0)break;
			if((fscanf(f," \"%[^\"]\" ",in_db->publ      ))<0)break;
			in_db->date=str2time (buf);
			break;
        case READ:
			while (!feof(f)) {
				dao (READLINE,f,in_db,NULL);
				TRY (rec=malloc(sizeof(db_t)));
				*rec=*in_db; /* copy contents */
				rec->next=hd;/* to linked list */
				hd=rec;i++;
			}
			if (i<2) {
				puts ("Empty database. Please create some entries.");
				fclose (f);
				exit (0);
			}
			break;
        case SORT:
			rec=in_db;
			for (;in_db;i++) in_db=in_db->next;
			TRY (pdb=malloc(i*sizeof(pdb_t)));
			in_db=rec;
			for (i=0;in_db;i++) {
				pdb[i]=in_db;
				in_db=in_db->next;
			}
			qsort (pdb,i,sizeof in_db,sortby);
			pdb[i-1]->next=NULL;
			for (i=i-1;i;i--) {
				pdb[i-1]->next=pdb[i];
			}
			rec=pdb[0];
			FREE (pdb);
			pdb=NULL;
			break;
        case DESTROY: {
            while ((rec=in_db)) {
                in_db=in_db->next;
                FREE (rec);
    }   }   }
    return rec;
}
Example #2
0
/*
 * Does not copy strings
 */
int str2val(db_type_t _t, db_val_t* _v, const char* _s, int _l)
{
	static str dummy_string = {"", 0};
	
	if (!_v) {
		LOG(L_ERR, "str2val: Invalid parameter value\n");
		return -1;
	}

	if (!_s) {
		memset(_v, 0, sizeof(db_val_t));
			/* Initialize the string pointers to a dummy empty
			 * string so that we do not crash when the NULL flag
			 * is set but the module does not check it properly
			 */
		VAL_STRING(_v) = dummy_string.s;
		VAL_STR(_v) = dummy_string;
		VAL_BLOB(_v) = dummy_string;
		VAL_TYPE(_v) = _t;
		VAL_NULL(_v) = 1;
		return 0;
	}
	VAL_NULL(_v) = 0;

	switch(_t) {
	case DB_INT:
		if (str2int(_s, &VAL_INT(_v)) < 0) {
			LOG(L_ERR, "str2val: Error while converting integer value from string\n");
			return -2;
		} else {
			VAL_TYPE(_v) = DB_INT;
			return 0;
		}
		break;

	case DB_BITMAP:
		if (str2int(_s, &VAL_INT(_v)) < 0) {
			LOG(L_ERR, "str2val: Error while converting bitmap value from string\n");
			return -3;
		} else {
			VAL_TYPE(_v) = DB_BITMAP;
			return 0;
		}
		break;
	
	case DB_DOUBLE:
		if (str2double(_s, &VAL_DOUBLE(_v)) < 0) {
			LOG(L_ERR, "str2val: Error while converting double value from string\n");
			return -4;
		} else {
			VAL_TYPE(_v) = DB_DOUBLE;
			return 0;
		}
		break;

	case DB_STRING:
		VAL_STRING(_v) = _s;
		VAL_TYPE(_v) = DB_STRING;
		return 0;

	case DB_STR:
		VAL_STR(_v).s = (char*)_s;
		VAL_STR(_v).len = _l;
		VAL_TYPE(_v) = DB_STR;
		return 0;

	case DB_DATETIME:
		if (str2time(_s, &VAL_TIME(_v)) < 0) {
			LOG(L_ERR, "str2val: Error while converting datetime value from string\n");
			return -5;
		} else {
			VAL_TYPE(_v) = DB_DATETIME;
			return 0;
		}
		break;

	case DB_BLOB:
		VAL_BLOB(_v).s = (char*)_s;
		VAL_BLOB(_v).len = _l;
		VAL_TYPE(_v) = DB_BLOB;
		return 0;
	}
	return -6;
}
Example #3
0
File: util.c Project: DonCN/haiku
char *
fetchname (char *at, int strip_leading, time_t *pstamp)
{
    char *name;
    register char *t;
    int sleading = strip_leading;
    time_t stamp = (time_t) -1;

    while (ISSPACE ((unsigned char) *at))
	at++;
    if (debug & 128)
	say ("fetchname %s %d\n", at, strip_leading);

    name = at;
    /* Strip up to `strip_leading' leading slashes and null terminate.
       If `strip_leading' is negative, strip all leading slashes.  */
    for (t = at;  *t;  t++)
      {
	if (ISSLASH (*t))
	  {
	    while (ISSLASH (t[1]))
	      t++;
	    if (strip_leading < 0 || --sleading >= 0)
		name = t+1;
	  }
	else if (ISSPACE ((unsigned char) *t))
	  {
	    char const *u = t;

	    if (set_time | set_utc)
	      stamp = str2time (&u, initial_time,
				set_utc ? 0L : TM_LOCAL_ZONE);
	    else
	      {
		/* The head says the file is nonexistent if the timestamp
		   is the epoch; but the listed time is local time, not UTC,
		   and POSIX.1 allows local time offset anywhere in the range
		   -25:00 < offset < +26:00.  Match any time in that
		   range by assuming local time is -25:00 and then matching
		   any ``local'' time T in the range 0 < T < 25+26 hours.  */
		stamp = str2time (&u, initial_time, -25L * 60 * 60);
		if (0 < stamp && stamp < (25 + 26) * 60L * 60)
		  stamp = 0;
	      }

	    if (*u && ! ISSPACE ((unsigned char) *u))
	      stamp = (time_t) -1;

	    *t = '\0';
	    break;
	  }
      }

    if (!*name)
      return 0;

    /* If the name is "/dev/null", ignore the name and mark the file
       as being nonexistent.  The name "/dev/null" appears in patches
       regardless of how NULL_DEVICE is spelled.  */
    if (strcmp (at, "/dev/null") == 0)
      {
	if (pstamp)
	  *pstamp = 0;
	return 0;
      }

    /* Ignore the name if it doesn't have enough slashes to strip off.  */
    if (0 < sleading)
      return 0;

    if (pstamp)
      *pstamp = stamp;

    return savestr (name);
}
Example #4
0
/* read sp3 body -------------------------------------------------------------*/
static void readsp3b(FILE *fp, char type, int *sats, int ns, double *bfact,
                     char *tsys, int index, int opt, nav_t *nav)
{
    peph_t peph;
    gtime_t time;
    double val,std,base;
    int i,j,sat,sys,prn,n=ns*(type=='P'?1:2),pred_o,pred_c;
    char buff[1024];
    
    trace(3,"readsp3b: type=%c ns=%d index=%d opt=%d\n",type,ns,index,opt);
    
    while (fgets(buff,sizeof(buff),fp)) {
        
        if (!strncmp(buff,"EOF",3)) break;
        
        if (buff[0]!='*'||str2time(buff,3,28,&time)) {
            trace(2,"sp3 invalid epoch %31.31s\n",buff);
            continue;
        }
        if (!strcmp(tsys,"UTC")) time=utc2gpst(time); /* utc->gpst */
        peph.time =time;
        peph.index=index;
        
        for (i=0;i<MAXSAT;i++) for (j=0;j<4;j++) {
            peph.pos[i][j]=0.0;
            peph.std[i][j]=0.0f;
        }
        for (i=0;i<n&&fgets(buff,sizeof(buff),fp);i++) {
            
            if (strlen(buff)<4||buff[0]!='P') continue;
            
            sys=buff[1]==' '?SYS_GPS:code2sys(buff[1]);
            prn=(int)str2num(buff,2,2);
            if      (sys==SYS_SBS) prn+=100;
            else if (sys==SYS_QZS) prn+=192; /* extension to sp3-c */
            
            if (!(sat=satno(sys,prn))) continue;
            
            pred_c=strlen(buff)>=76&&buff[75]=='P';
            pred_o=strlen(buff)>=80&&buff[79]=='P';
            
            for (j=0;j<4;j++) {
                
                /* read option for predicted value */
                if (j< 3&&opt==1&& pred_o) continue;
                if (j< 3&&opt==2&&!pred_o) continue;
                if (j==3&&opt==1&& pred_c) continue;
                if (j==3&&opt==2&&!pred_c) continue;
                
                val=str2num(buff, 4+j*14,14);
                std=str2num(buff,61+j* 3,j<3?2:3);
                
                if (val!=0.0&&fabs(val-999999.999999)>=1E-6) {
                    peph.pos[sat-1][j]=val*(j<3?1000.0:1E-6);
                }
                if ((base=bfact[j<3?0:1])>0.0) {
                    peph.std[sat-1][j]=(float)(pow(base,std)*(j<3?1E-3:1E-12));
                }
            }
        }
        if (!addpeph(nav,&peph)) return;
    }
}
Example #5
0
int
main(int argc, char **argv)
{
	char line[BUFSIZ];
	char *lyric;
	char *ttag;			/* timing tag	*/
	char *tag, *tag_value;
	struct lrc_info lrc_info = LRC_INFO_NULL;
	useconds_t wtime = 0;		/* waiting time	*/
	useconds_t time_sum = 0;

	if (argc < 2)
		usage();
	
	FILE *fh = fopen(argv[1], "r");

	if (fh == NULL)
		return EXIT_FAILURE;

	while (fgets(line, BUFSIZ, fh)) {
		/* check for none conforming lines */
		if (line[0] != '[' || strnlen(line, BUFSIZ) < 5)
			continue;

		if (isdigit(line[1]))	/* check for first lyric line */
			break;

		tag = line + 1;
		char *tag_separator = strchr(line, ':');
		if (tag_separator != NULL) {
			*tag_separator = '\0';
			tag_value = tag_separator + 1;

			char *tag_end = strchr(tag_value, ']');
			*tag_end = '\0';

			set_lrc_info(&lrc_info, tag, tag_value);
		}
	}

	do {
		if (line[0] != '[')	/* check for empty lines */
			continue;

		/* search for begin of lyric */
		if ((lyric = strchr(line, ']')) == NULL)
			continue;
		lyric++;

		wtime = str2time(line);
		time_sum = waiting(wtime, time_sum);

		while ((ttag = strchr(lyric, '<')) != NULL) {
			fwrite(lyric, sizeof *lyric, ttag - lyric, stdout);
			fflush(stdout);

			wtime = str2time(ttag);
			time_sum = waiting(wtime, time_sum);

			/* search for begin of lyric */
			if ((lyric = strchr(ttag, '>')) == NULL)
				continue;
			lyric++;
		}

		printf("%s", lyric);
		fflush(stdout);
	} while (fgets(line, BUFSIZ, fh));

	fclose(fh);

	waiting(lrc_info.length, time_sum);	/* wait for end of the song */

	return EXIT_SUCCESS;
}