src/dbs/atrshmlogconvert_db.c
int operate(atrshmlog_dbsession_t *i_db,
	    char * const i_buffer,
	    atrshmlog_io_head_t head,
	    int sbyteorder,
	    int *io_insert_head,
	    int *io_insert_ucs2,
	    int *io_insert_cstring)

{
  int result = -1;

  const int horder_is_n_order = ATRSHMLOG_H_ORDER_IS_N_ORDER;

  /* we calc the starttime and endtime in real */

  long long tscstart = head.inittsc_after;

  tscstart += head.inittsc_before;

  tscstart /= 2;

  long long tscend = head.lasttsc_before;

  tscend += head.lasttsc_after;

  tscend /= 2;

  long long tscdiff = tscend - tscstart;

  long double tscstart_d = tscstart;

  long double tscend_d =  tscend;

  long double tscdiff_d = tscdiff;
  
  /* we can now start to give the realtime a try
   * we assume the tsc is in the middle of the fetch of the real time
   * and so we give all tsc now a new realtime 
   */

  /* ok. we have now the tsc. we now need the real */

  long double realstart;

  realstart = head.inittime.tv_sec;
  
  realstart *=  1000000000.0;

  realstart += head.inittime.tv_nsec;

  long double realend;

  realend = head.lasttime.tv_sec;
  
  realend *=  1000000000.0;

  realend += head.lasttime.tv_nsec;

  long double realdiff = realend - realstart;
  
  /* and now we can start with a realtime adder */
  

  int chunkindex = 0;

  int tlen = head.tlen;
  
  if (!horder_is_n_order && atrshmlog_need_data_in_network_order_db())
    {
      atrshmlog_io_head_change_order(&head);
    }

  uint64_t head_id;

  int retdb = atrshmlog_get_head_seq_db(i_db, &head_id);

  if (retdb != 0)
    {
      return -1;
    }

  retdb = atrshmlog_insert_db_head(i_db, &head_id, &head);
	
  if (retdb != 0)
    {
      return -1;
    }

  ++*io_insert_head;
  
  atrshmlog_chunk_head_t c;

  int totallen ;
  
  for (int akindex = 0; akindex < tlen; akindex += totallen)
    {
      chunkindex++;

      atrshmlog_fill_chunk_head (i_buffer + akindex, &c);

      if (sbyteorder)
	{
	  atrshmlog_chunk_head_change_order(&c);
	}

      totallen = c.totallen;
      
      if (totallen < ATRSHMLOGCONTROLDATASIZE || totallen > ATRSHMLOGBUFFER_INFOSIZE)
	{
	  fprintf(stderr, "error c.totallen : size :  in %d : %d.\n", chunkindex, totallen);
	  return -1;
	}

      size_t lcount = totallen - ATRSHMLOGCONTROLDATASIZE;

      char payloadbuffer [ ATRSHMLOGBUFFER_INFOSIZE + 1];
 
      if (lcount > 0)
	{
	  memcpy(payloadbuffer, i_buffer + akindex + ATRSHMLOGCONTROLDATASIZE, lcount);
	}
    
      payloadbuffer[lcount] = '\0';

      
      int ucs2string = 0;
      
      if (c.eventflag == ATRSHMLOGPOINTINTIMEp || c.eventflag == ATRSHMLOGPOINTINTIMEi)
	ucs2string = 1;
	
      /* we now calc the real time
       * (reals - realstart)/  (realend - realstart)  
       * eq (s - tstart) / (tend - tstart)  
       * so 
       * (reals - realstart) = (s - tstart) * (realend - realstart) / (tend - tstart)
       * so
       * reals = realstart + ((s - tstart) * (realend - realstart) / (tend - tstart))
       */
      long double reals_d =  realstart;

      long double quots_d = c.starttime;

      quots_d -= tscstart_d;
	
      quots_d *= realdiff;
        
      reals_d += quots_d / tscdiff_d;

      c.startreal = reals_d;
	
      long double reale_d =  realstart;

      long double quote_d = c.endtime;

      quote_d -= tscstart_d;
	
      quote_d *= realdiff;
        
      reale_d += quote_d / tscdiff_d;

      c.endreal = reale_d;

      c.deltareal = c.endreal - c.startreal;

      c.deltatime = c.endtime - c.starttime;
      
      
      if (ucs2string)
	{
	  if (!horder_is_n_order && atrshmlog_need_data_in_network_order_db())
	    {
	      atrshmlog_chunk_head_change_order(&c);
	    }
	  
	  c.payload = payloadbuffer;
	      
	  int retdb = atrshmlog_insert_db_ucs2string(i_db, head_id, &head, &c, (totallen - ATRSHMLOGCONTROLDATASIZE));

	  ++*io_insert_ucs2;
	  
	  if (retdb != 0)
	    {
	      return -1;
	    }
	}
      else
	{
	  char* lastpos = payloadbuffer;
	  char* nextcontrol = payloadbuffer;

	  if (!horder_is_n_order && atrshmlog_need_data_in_network_order_db())
	    {
	      atrshmlog_chunk_head_change_order(&c);
	    }
	  
	  /* simple text convert, somehow dirty done */ 
	  while ( nextcontrol != NULL )
	    {
	      nextcontrol = strchr(lastpos, '\n');

	      if (nextcontrol)
		*nextcontrol = '\0';

		
	      for (char *k = lastpos; *k; k++)
		{
		  if (!isprint(*k))
		    *k = ' ';
		  
		  if (*k == '\t' || *k == '\r')
		    *k = ' ';
		    
		}

	      c.payload = lastpos;
	      
	      int retdb = atrshmlog_insert_db_cstring(i_db, head_id, &head, &c);

	      ++*io_insert_cstring;
	      
	      if (retdb != 0)
		{
		  return -1;
		}
	      
	      lastpos = nextcontrol + 1;
	    }
	}
      
    }
  
  /* ok. we are out normal 
  * we adjust end and newend and memove the bufer rest
  */
 leave1:

  return 0;
}
/** 
 * we convert the content of buffer into output
 * we start with the controldata
 * then we use the length and calc the rest
 * can be empty 
 * we write this one info chunk.
 * we add a line number and write the lines out (?)
 * then we advandce 
 * we check if the thing is still complete.
 * if not we memove the rest t the beginning of the buffer and 
 * adjust end and newend
 * if anything goes wrong we return with -1
 */
we convert the content of buffer into output
we start with the controldata
then we use the length and calc the rest
can be empty
we write this one info chunk.
we add a line number and write the lines out (?)
then we advandce
we check if the thing is still complete.
if not we memove the rest t the beginning of the buffer and
adjust end and newend
if anything goes wrong we return with -1
we convert the content of buffer into output we start with the controldata then we use the length and calc the rest can be empty we write this one info chunk . we add a line number and write the lines out ( ? ) then we advandce we check if the thing is still complete . if not we memove the rest t the beginning of the buffer and adjust end and newend if anything goes wrong we return with - 1
int operate(atrshmlog_dbsession_t *i_db,
	    char * const i_buffer,
	    atrshmlog_io_head_t head,
	    int sbyteorder,
	    int *io_insert_head,
	    int *io_insert_ucs2,
	    int *io_insert_cstring)
{
  int result = -1;
  const int horder_is_n_order = ATRSHMLOG_H_ORDER_IS_N_ORDER;
  long long tscstart = head.inittsc_after;
  tscstart += head.inittsc_before;
  tscstart /= 2;
  long long tscend = head.lasttsc_before;
  tscend += head.lasttsc_after;
  tscend /= 2;
  long long tscdiff = tscend - tscstart;
  long double tscstart_d = tscstart;
  long double tscend_d =  tscend;
  long double tscdiff_d = tscdiff;
  long double realstart;
  realstart = head.inittime.tv_sec;
  realstart *=  1000000000.0;
  realstart += head.inittime.tv_nsec;
  long double realend;
  realend = head.lasttime.tv_sec;
  realend *=  1000000000.0;
  realend += head.lasttime.tv_nsec;
  long double realdiff = realend - realstart;
  int chunkindex = 0;
  int tlen = head.tlen;
  if (!horder_is_n_order && atrshmlog_need_data_in_network_order_db())
    {
      atrshmlog_io_head_change_order(&head);
    }
  uint64_t head_id;
  int retdb = atrshmlog_get_head_seq_db(i_db, &head_id);
  if (retdb != 0)
    {
      return -1;
    }
  retdb = atrshmlog_insert_db_head(i_db, &head_id, &head);
  if (retdb != 0)
    {
      return -1;
    }
  ++*io_insert_head;
  atrshmlog_chunk_head_t c;
  int totallen ;
  for (int akindex = 0; akindex < tlen; akindex += totallen)
    {
      chunkindex++;
      atrshmlog_fill_chunk_head (i_buffer + akindex, &c);
      if (sbyteorder)
	{
	  atrshmlog_chunk_head_change_order(&c);
	}
      totallen = c.totallen;
      if (totallen < ATRSHMLOGCONTROLDATASIZE || totallen > ATRSHMLOGBUFFER_INFOSIZE)
	{
	  fprintf(stderr, "error c.totallen : size :  in %d : %d.\n", chunkindex, totallen);
	  return -1;
	}
      size_t lcount = totallen - ATRSHMLOGCONTROLDATASIZE;
      char payloadbuffer [ ATRSHMLOGBUFFER_INFOSIZE + 1];
      if (lcount > 0)
	{
	  memcpy(payloadbuffer, i_buffer + akindex + ATRSHMLOGCONTROLDATASIZE, lcount);
	}
      payloadbuffer[lcount] = '\0';
      int ucs2string = 0;
      if (c.eventflag == ATRSHMLOGPOINTINTIMEp || c.eventflag == ATRSHMLOGPOINTINTIMEi)
	ucs2string = 1;
      long double reals_d =  realstart;
      long double quots_d = c.starttime;
      quots_d -= tscstart_d;
      quots_d *= realdiff;
      reals_d += quots_d / tscdiff_d;
      c.startreal = reals_d;
      long double reale_d =  realstart;
      long double quote_d = c.endtime;
      quote_d -= tscstart_d;
      quote_d *= realdiff;
      reale_d += quote_d / tscdiff_d;
      c.endreal = reale_d;
      c.deltareal = c.endreal - c.startreal;
      c.deltatime = c.endtime - c.starttime;
      if (ucs2string)
	{
	  if (!horder_is_n_order && atrshmlog_need_data_in_network_order_db())
	    {
	      atrshmlog_chunk_head_change_order(&c);
	    }
	  c.payload = payloadbuffer;
	  int retdb = atrshmlog_insert_db_ucs2string(i_db, head_id, &head, &c, (totallen - ATRSHMLOGCONTROLDATASIZE));
	  ++*io_insert_ucs2;
	  if (retdb != 0)
	    {
	      return -1;
	    }
	}
      else
	{
	  char* lastpos = payloadbuffer;
	  char* nextcontrol = payloadbuffer;
	  if (!horder_is_n_order && atrshmlog_need_data_in_network_order_db())
	    {
	      atrshmlog_chunk_head_change_order(&c);
	    }
	  while ( nextcontrol != NULL )
	    {
	      nextcontrol = strchr(lastpos, '\n');
	      if (nextcontrol)
		*nextcontrol = '\0';
	      for (char *k = lastpos; *k; k++)
		{
		  if (!isprint(*k))
		    *k = ' ';
		  if (*k == '\t' || *k == '\r')
		    *k = ' ';
		}
	      c.payload = lastpos;
	      int retdb = atrshmlog_insert_db_cstring(i_db, head_id, &head, &c);
	      ++*io_insert_cstring;
	      if (retdb != 0)
		{
		  return -1;
		}
	      lastpos = nextcontrol + 1;
	    }
	}
    }
 leave1:
  return 0;
}
int operate ( atrshmlog_dbsession_t * i_db , char * const i_buffer , atrshmlog_io_head_t head , int sbyteorder , int * io_insert_head , int * io_insert_ucs2 , int * io_insert_cstring ) { int result = -1 ; const int horder_is_n_order = ATRSHMLOG_H_ORDER_IS_N_ORDER ; long long tscstart = head . inittsc_after ; tscstart += head . inittsc_before ; tscstart /= 2 ; long long tscend = head . lasttsc_before ; tscend += head . lasttsc_after ; tscend /= 2 ; long long tscdiff = tscend - tscstart ; long double tscstart_d = tscstart ; long double tscend_d = tscend ; long double tscdiff_d = tscdiff ; long double realstart ; realstart = head . inittime . tv_sec ; realstart *= 1000000000.0 ; realstart += head . inittime . tv_nsec ; long double realend ; realend = head . lasttime . tv_sec ; realend *= 1000000000.0 ; realend += head . lasttime . tv_nsec ; long double realdiff = realend - realstart ; int chunkindex = 0 ; int tlen = head . tlen ; if ( ! horder_is_n_order && atrshmlog_need_data_in_network_order_db ( ) ) { atrshmlog_io_head_change_order ( & head ) ; } uint64_t head_id ; int retdb = atrshmlog_get_head_seq_db ( i_db , & head_id ) ; if ( retdb != 0 ) { return -1 ; } retdb = atrshmlog_insert_db_head ( i_db , & head_id , & head ) ; if ( retdb != 0 ) { return -1 ; } ++ * io_insert_head ; atrshmlog_chunk_head_t c ; int totallen ; for ( int akindex = 0 ; akindex < tlen ; akindex += totallen ) { chunkindex ++ ; atrshmlog_fill_chunk_head ( i_buffer + akindex , & c ) ; if ( sbyteorder ) { atrshmlog_chunk_head_change_order ( & c ) ; } totallen = c . totallen ; if ( totallen < ATRSHMLOGCONTROLDATASIZE || totallen > ATRSHMLOGBUFFER_INFOSIZE ) { fprintf ( stderr , " \n " , chunkindex , totallen ) ; return -1 ; } size_t lcount = totallen - ATRSHMLOGCONTROLDATASIZE ; char payloadbuffer [ ATRSHMLOGBUFFER_INFOSIZE + 1 ] ; if ( lcount > 0 ) { memcpy ( payloadbuffer , i_buffer + akindex + ATRSHMLOGCONTROLDATASIZE , lcount ) ; } payloadbuffer [ lcount ] = ' \0 ' ; int ucs2string = 0 ; if ( c . eventflag == ATRSHMLOGPOINTINTIMEp || c . eventflag == ATRSHMLOGPOINTINTIMEi ) ucs2string = 1 ; long double reals_d = realstart ; long double quots_d = c . starttime ; quots_d -= tscstart_d ; quots_d *= realdiff ; reals_d += quots_d / tscdiff_d ; c . startreal = reals_d ; long double reale_d = realstart ; long double quote_d = c . endtime ; quote_d -= tscstart_d ; quote_d *= realdiff ; reale_d += quote_d / tscdiff_d ; c . endreal = reale_d ; c . deltareal = c . endreal - c . startreal ; c . deltatime = c . endtime - c . starttime ; if ( ucs2string ) { if ( ! horder_is_n_order && atrshmlog_need_data_in_network_order_db ( ) ) { atrshmlog_chunk_head_change_order ( & c ) ; } c . payload = payloadbuffer ; int retdb = atrshmlog_insert_db_ucs2string ( i_db , head_id , & head , & c , ( totallen - ATRSHMLOGCONTROLDATASIZE ) ) ; ++ * io_insert_ucs2 ; if ( retdb != 0 ) { return -1 ; } } else { char * lastpos = payloadbuffer ; char * nextcontrol = payloadbuffer ; if ( ! horder_is_n_order && atrshmlog_need_data_in_network_order_db ( ) ) { atrshmlog_chunk_head_change_order ( & c ) ; } while ( nextcontrol != NULL ) { nextcontrol = strchr ( lastpos , ' \n ' ) ; if ( nextcontrol ) * nextcontrol = ' \0 ' ; for ( char * k = lastpos ; * k ; k ++ ) { if ( ! isprint ( * k ) ) * k = ' ' ; if ( * k == ' \t ' || * k == ' \r ' ) * k = ' ' ; } c . payload = lastpos ; int retdb = atrshmlog_insert_db_cstring ( i_db , head_id , & head , & c ) ; ++ * io_insert_cstring ; if ( retdb != 0 ) { return -1 ; } lastpos = nextcontrol + 1 ; } } } leave1 : return 0 ; }
we convert the content of buffer into output
 we start with the controldata
 then we use the length and calc the rest
 can be empty
 we write this one info chunk.
we convert the content of buffer into output we start with the controldata then we use the length and calc the rest can be empty we write this one info chunk .
/* we calc the starttime and endtime in real */ /* we can now start to give the realtime a try
   * we assume the tsc is in the middle of the fetch of the real time
   * and so we give all tsc now a new realtime 
   */ /* ok. we have now the tsc. we now need the real */ /* and now we can start with a realtime adder */ /* we now calc the real time
       * (reals - realstart)/  (realend - realstart)  
       * eq (s - tstart) / (tend - tstart)  
       * so 
       * (reals - realstart) = (s - tstart) * (realend - realstart) / (tend - tstart)
       * so
       * reals = realstart + ((s - tstart) * (realend - realstart) / (tend - tstart))
       */ /* simple text convert, somehow dirty done */ /* ok. we are out normal 
  * we adjust end and newend and memove the bufer rest
  */