Example #1
0
char *itoa(int i, char *a, int base)
{
    if ((base < 2) || (base > 36))
        base = 10;
    if (i < 0) {
        *a = '-';
        *_i2a(-(unsigned) i, a + 1, base) = 0;
    } else
        *_i2a(i, a, base) = 0;
    return a;
}
Example #2
0
static char *_i2a(unsigned i, char *a, unsigned base)
{
    if (i / base > 0)
        a = _i2a(i / base, a, base);
    *a = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i % base];
    return a + 1;
}
Example #3
0
struct tm *Time_toDateTime(const char *s, struct tm *t) {
        assert(t);
        assert(s);
        struct tm tm = {.tm_isdst = -1}; 
        int has_date = false, has_time = false;
        const char *limit = s + strlen(s), *marker, *token, *cursor = s;
	while (true) {
		if (cursor >= limit) {
                        if (has_date || has_time) {
                                *(struct tm*)t = tm;
                                return t;
                        }
                        THROW(SQLException, "Invalid date or time");
                }
                token = cursor;
                
#line 187 "<stdout>"
{
	unsigned char yych;
	unsigned int yyaccept = 0;
	static const unsigned char yybm[] = {
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		128, 128, 128, 128, 128, 128, 128, 128, 
		128, 128,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
		  0,   0,   0,   0,   0,   0,   0,   0, 
	};
	yych = *cursor;
	if (yych <= ',') {
		if (yych == '+') goto yy4;
	} else {
		if (yych <= '-') goto yy4;
		if (yych <= '/') goto yy2;
		if (yych <= '9') goto yy5;
	}
yy2:
	++cursor;
yy3:
#line 243 "src/system/Time.re"
	{
                        continue;
                 }
#line 240 "<stdout>"
yy4:
	yyaccept = 0;
	yych = *(marker = ++cursor);
	if (yych <= '/') goto yy3;
	if (yych <= '9') goto yy6;
	goto yy3;
yy5:
	yyaccept = 0;
	yych = *(marker = ++cursor);
	if (yych <= '/') goto yy3;
	if (yych <= '9') goto yy8;
	goto yy3;
yy6:
	yych = *++cursor;
	if (yych <= '/') goto yy7;
	if (yych <= '9') goto yy9;
yy7:
	cursor = marker;
	if (yyaccept <= 1) {
		if (yyaccept == 0) {
			goto yy3;
		} else {
			goto yy10;
		}
	} else {
		if (yyaccept == 2) {
			goto yy26;
		} else {
			goto yy32;
		}
	}
yy8:
	yych = *++cursor;
	if (yych <= '/') goto yy11;
	if (yych <= '9') goto yy12;
	goto yy11;
yy9:
	yyaccept = 1;
	yych = *(marker = ++cursor);
	if (yych == '\n') goto yy10;
	if (yych <= '/') goto yy13;
	if (yych <= '9') goto yy14;
	goto yy13;
yy10:
#line 230 "src/system/Time.re"
	{ // Timezone: +-HH:MM, +-HH or +-HHMM is offset from UTC in seconds
                        if (has_time) { // Only set timezone if time has been seen
                                tm.TM_GMTOFF = _a2i(token + 1, 2) * 3600;
                                if (isdigit(token[3]))
                                        tm.TM_GMTOFF += _a2i(token + 3, 2) * 60;
                                else if (isdigit(token[4]))
                                        tm.TM_GMTOFF += _a2i(token + 4, 2) * 60;
                                if (token[0] == '-')
                                        tm.TM_GMTOFF *= -1;
                        }
                        continue;
                 }
#line 298 "<stdout>"
yy11:
	yych = *++cursor;
	if (yych <= '/') goto yy7;
	if (yych <= '9') goto yy15;
	goto yy7;
yy12:
	yych = *++cursor;
	if (yych <= '/') goto yy7;
	if (yych <= '9') goto yy16;
	goto yy7;
yy13:
	yych = *++cursor;
	if (yych <= '/') goto yy7;
	if (yych <= '9') goto yy17;
	goto yy7;
yy14:
	yych = *++cursor;
	if (yych <= '/') goto yy7;
	if (yych <= '9') goto yy18;
	goto yy7;
yy15:
	yych = *++cursor;
	if (yych <= '/') goto yy7;
	if (yych <= '9') goto yy19;
	goto yy7;
yy16:
	yych = *++cursor;
	if (yych <= '/') goto yy20;
	if (yych <= '9') goto yy21;
	goto yy20;
yy17:
	yych = *++cursor;
	if (yych <= '/') goto yy7;
	if (yych <= '9') goto yy22;
	goto yy7;
yy18:
	yych = *++cursor;
	if (yych <= '/') goto yy10;
	if (yych <= '9') goto yy22;
	goto yy10;
yy19:
	yych = *++cursor;
	if (yych <= '/') goto yy23;
	if (yych <= '9') goto yy7;
	goto yy23;
yy20:
	yych = *++cursor;
	if (yych <= '/') goto yy7;
	if (yych <= '9') goto yy24;
	goto yy7;
yy21:
	yych = *++cursor;
	if (yych <= '/') goto yy7;
	if (yych <= '9') goto yy25;
	goto yy7;
yy22:
	yych = *++cursor;
	goto yy10;
yy23:
	yych = *++cursor;
	if (yych <= '/') goto yy7;
	if (yych <= '9') goto yy27;
	goto yy7;
yy24:
	yych = *++cursor;
	if (yych <= '/') goto yy7;
	if (yych <= '9') goto yy28;
	goto yy7;
yy25:
	yyaccept = 2;
	yych = *(marker = ++cursor);
	if (yych <= '-') {
		if (yych == ',') goto yy29;
	} else {
		if (yych <= '.') goto yy29;
		if (yych <= '/') goto yy26;
		if (yych <= '9') goto yy30;
	}
yy26:
#line 222 "src/system/Time.re"
	{ // Compressed Time: HHMMSS
                        tm.tm_hour = _a2i(token, 2);
                        tm.tm_min  = _a2i(token + 2, 2);
                        tm.tm_sec  = _a2i(token + 4, 2);
                        has_time = true;
                        continue;
                 }
#line 386 "<stdout>"
yy27:
	yych = *++cursor;
	if (yych <= '/') goto yy7;
	if (yych <= '9') goto yy31;
	goto yy7;
yy28:
	yych = *++cursor;
	if (yych <= '/') goto yy33;
	if (yych <= '9') goto yy7;
	goto yy33;
yy29:
	yych = *++cursor;
	if (yybm[0+yych] & 128) {
		goto yy34;
	}
	goto yy7;
yy30:
	yych = *++cursor;
	if (yych <= '/') goto yy7;
	if (yych <= '9') goto yy36;
	goto yy7;
yy31:
	yyaccept = 3;
	yych = *(marker = ++cursor);
	if (yych == ',') goto yy38;
	if (yych == '.') goto yy38;
yy32:
#line 214 "src/system/Time.re"
	{ // Time: HH:MM:SS
                        tm.tm_hour = _a2i(token, 2);
                        tm.tm_min  = _a2i(token + 3, 2);
                        tm.tm_sec  = _a2i(token + 6, 2);
                        has_time = true;
                        continue;
                 }
#line 422 "<stdout>"
yy33:
	yych = *++cursor;
	if (yych <= '/') goto yy7;
	if (yych <= '9') goto yy39;
	goto yy7;
yy34:
	++cursor;
	yych = *cursor;
	if (yybm[0+yych] & 128) {
		goto yy34;
	}
	goto yy26;
yy36:
	++cursor;
#line 206 "src/system/Time.re"
	{ // Compressed Date: YYYYMMDD
                        tm.tm_year  = _a2i(token, 4);
                        tm.tm_mon   = _a2i(token + 4, 2) - 1;
                        tm.tm_mday  = _a2i(token + 6, 2);
                        has_date = true;
                        continue;
                 }
#line 445 "<stdout>"
yy38:
	yych = *++cursor;
	if (yych <= '/') goto yy7;
	if (yych <= '9') goto yy40;
	goto yy7;
yy39:
	yych = *++cursor;
	if (yych <= '/') goto yy7;
	if (yych <= '9') goto yy42;
	goto yy7;
yy40:
	++cursor;
	yych = *cursor;
	if (yych <= '/') goto yy32;
	if (yych <= '9') goto yy40;
	goto yy32;
yy42:
	++cursor;
#line 198 "src/system/Time.re"
	{ // Date: YYYY-MM-DD
                        tm.tm_year  = _a2i(token, 4);
                        tm.tm_mon   = _a2i(token + 5, 2) - 1;
                        tm.tm_mday  = _a2i(token + 8, 2);
                        has_date = true;
                        continue;
                 }
#line 472 "<stdout>"
}
#line 246 "src/system/Time.re"

        }
	return NULL;
}


char *Time_toString(time_t time, char result[20]) {
        assert(result);
        char x[2];
        struct tm ts = {.tm_isdst = -1};
        gmtime_r(&time, &ts);
        memcpy(result, "YYYY-MM-DD HH:MM:SS\0", 20);
        /*              0    5  8  11 14 17 */
        _i2a((ts.tm_year+1900)/100);
        result[0] = x[0];
        result[1] = x[1];
        _i2a((ts.tm_year+1900)%100);
        result[2] = x[0];
        result[3] = x[1];
        _i2a(ts.tm_mon + 1); // Months in 01-12
        result[5] = x[0];
        result[6] = x[1];
        _i2a(ts.tm_mday);
        result[8] = x[0];
        result[9] = x[1];
        _i2a(ts.tm_hour);
        result[11] = x[0];
        result[12] = x[1];
        _i2a(ts.tm_min);
        result[14] = x[0];
        result[15] = x[1];
        _i2a(ts.tm_sec);
        result[17] = x[0];
        result[18] = x[1];
	return result;
}


time_t Time_now(void) {
	struct timeval t;
	if (gettimeofday(&t, NULL) != 0)
                THROW(AssertException, "%s", System_getLastError());
	return t.tv_sec;
}


long long Time_milli(void) {
	struct timeval t;
	if (gettimeofday(&t, NULL) != 0)
                THROW(AssertException, "%s", System_getLastError());
	return (long long)t.tv_sec * 1000  +  (long long)t.tv_usec / 1000;
}


int Time_usleep(long u) {
        struct timeval t;
        t.tv_sec = u / USEC_PER_SEC;
        t.tv_usec = (suseconds_t)(u % USEC_PER_SEC);
        select(0, 0, 0, 0, &t);
        return true;
}
Example #4
0
static __x UREG GET_hook(UREG state, UREG len, UINT8 *data, TCP_SOCK *_s)
{
  GET_SOCK *s=(GET_SOCK *)_s;
  UINT32 freeFIFO;
//  UREG i;
//  UREG j;
  switch(state)
  {
  case TCP_EVENT_CLOSE:
  case TCP_EVENT_ABORT:
    s->state=_GET_STOP;
    s->header_pos=0;
    s->block_length=0;
    s->metadata_interval=0;
    PLAYER_STATE=PLAYER_STATE_STOPED;
    if (PLAYER_STATE==PLAYER_STATE_STOPED) 
    {
      LCD_fprintline(1,"Stoped");     
      LCD_softCLR();     
    }
    return 0;
  case TCP_EVENT_CONREQ:
    s->state=_GET_CONREQ;
    return 0;
  case TCP_EVENT_ASYNC_REQ:
    s->sock.async_req=0;
    freeFIFO=MP3fifo_free();
    //if (freeFIFO<SPEC_MAX_WIN)
    if (freeFIFO<4*TCP_MAX_DATA_LEN)
    {
      // пока нет места для приема полноценного окна не делаем асинхронный старт      
      GET_WINDOW_STATE=GET_WINDOW_STATE_ZERO;
      #ifdef CONSOLE_DEBUG
      _print_fstr("\r\nNO_Async_start");
      #endif
      return 0;
    }
    #ifdef CONSOLE_DEBUG
    _print_fstr("\r\nAsync_start");
    #endif
    GET_WINDOW_STATE=GET_WINDOW_STATE_UPDATE;
    if (freeFIFO>=SPEC_MAX_WIN) freeFIFO=SPEC_MAX_WIN;
    s->sock.win=htons((UINT16)freeFIFO); // запишем фактическое окно
    s->sock.txreq=TCP_TXREQ_SEND;
    return 0;
  case TCP_EVENT_DATA:
    if (PLAYER_STATE==PLAYER_STATE_STOPREQ)
    {
      StopGET();
      return 0;
    }
    if (s->state==_GET_CONREQ)
    {
      s->state=_GET_SENDREQ;
      return 1; //Запрос передачи
    }
    if (s->state<_GET_HEADER) return 0;
    return GET_hook_DATA_RX(len,data,s);
  case TCP_EVENT_ACK:
    //Подтверждение данных
    if (PLAYER_STATE==PLAYER_STATE_STOPREQ)
    {
      StopGET();
      return 0;
    }
    if (s->state==_GET_WAITACK&&len)
    {
      s->state=_GET_HEADER;
    }
    return 0;
  case TCP_EVENT_REGENERATE:
    if (s->state==_GET_WAITACK) s->state=_GET_SENDREQ;
  case TCP_EVENT_SEND:
  if (s->state==_GET_SENDREQ)
    {
      s->state=_GET_WAITACK;
      static __flash char req1[]="GET /";     
      static __flash char req2[]=" HTTP/1.0\r\nHost:";        
      static __flash char req3[]="\r\nIcy-MetaData:1\r\nUser-Agent: uEthRadio/0.1\r\nConnection: close\r\n\r\n";
      UREG i=sizeof(req1)-1;
      char __flash *s=req1;
      UINT8 *d=data;
      do
      {
	*d++=*s++;// "GET/"
      }
      while(--i);
      UREG s1=0;      
      char __eeprom *es;
      es=station_list[stationNum].req;
      
       while(*es)      
      {
            *d++=*es++;//  "запрос"
            s1++;
       };
          
      i=sizeof(req2)-1;
      s=req2;
      do
      {
	*d++=*s++;// "/HTTP1.0 Host:"
      }
      while(--i);     
      
      __no_init char i2a_buf[4];
      char *sIP = i2a_buf;
      UINT32 sockIP = station_list[stationNum].IP;
      UREG s2=0;
      i=0;
      
      while (i++<4)
      {
        _i2a(i2a_buf, (UREG)(sockIP));
        do
        {
  	  *d++=*sIP++;// "IP"
          s2++;
        }
        while (*sIP);                
        sIP=i2a_buf;
        if (i<4) {*d++='.';s2++;}
        sockIP=sockIP>>8;
      };           
      
      i=sizeof(req3)-1;
      s=req3;
      do
      {
	*d++=*s++;// ""
      }
      while(--i);                  
      return (sizeof(req1)-1)+s1+s2+(sizeof(req2)-1)+(sizeof(req3)-1);
    }
    return 0;
  }