#include "sync_helper.h"

/*
 * Description: 将char[5]解析成long long
 * Method:      parse_time
 * Returns:     long long 
 * Parameter:   char * time msg中携带的时间,长度为5的char数组
*/
long long host_server::sync_helper::parse_time(const char * c)
{
	long long ttt0 = (((long long)(c[0]))<<32)  & (0x000000FF00000000);
	long long ttt1 = (((long long)(c[1]))<<24) & (0x00000000FF000000);
	long long ttt2 = (((long long)(c[2]))<<16) & (0x0000000000FF0000);
	long long ttt3 = (((long long)(c[3]))<<8)  & (0x000000000000FF00);
	long long  all = ttt0 + ttt1 + ttt2 + ttt3 + ((long long)(c[4]&(0xFF)));
	return all;
}

/*
 * Description: 将基站号与天线号拼接成一个编码
 * Method:      parse_id
 * Returns:     unsigned long long 拼接后的编码
 * Parameter:   unsigned int id 基站号
 * Parameter:   unsigned char ant_num 天线号
*/
unsigned long long host_server::sync_helper::parse_id(unsigned int id, unsigned char antNum)
{
	return (id << 8) + antNum;
}

/*
 * Description: 从文件中分析数据
 * Method:      parse_from_fstream
 * Returns:     unsigned long long
 * Parameter:   ifstream & f
 * Parameter:   int size
*/
unsigned long long host_server::sync_helper::parse_from_fstream(ifstream &f, int size)
{
	unsigned long long x(0);
	for(int i(0); i < size; i++)
	{
		int t;
		f >> hex >> t;
		x = (x << 8) + t;
	}
	return x;
}

/*
 * Description: 从文件中分析时间
 * Method:      parse_time_from_fstream
 * Returns:     unsigned long long
 * Parameter:   ifstream & f
*/
unsigned long long host_server::sync_helper::parse_time_from_fstream(ifstream &f)
{
	char* t = new char[5];
	for (int i = 0; i < 5; i++)
	{
		int tmp;
		f >> hex >> tmp;
		t[i] = tmp;
	}
	auto time = sync_helper::parse_time(t);
	delete[] t;
	return time;
}