Exemple #1
0
int32_t
der_offs(struct magic_set *ms, struct magic *m, size_t nbytes)
{
	const uint8_t *b = CAST(const void *, ms->search.s);
	size_t offs = 0, len = ms->search.s_len ? ms->search.s_len : nbytes;

	if (gettag(b, &offs, len) == DER_BAD)
		return -1;
	DPRINTF(("%s1: %d %zu %u\n", __func__, ms->offset, offs, m->offset));

	uint32_t tlen = getlength(b, &offs, len);
	if (tlen == DER_BAD)
		return -1;
	DPRINTF(("%s2: %d %zu %u\n", __func__, ms->offset, offs, tlen));

	offs += ms->offset + m->offset;
	DPRINTF(("cont_level = %d\n", m->cont_level));
#ifdef DEBUG_DER
	for (size_t i = 0; i < m->cont_level; i++)
		printf("cont_level[%zu] = %u\n", i, ms->c.li[i].off);
#endif
	if (m->cont_level != 0) {
		if (offs + tlen > nbytes)
			return DER_BAD;
		ms->c.li[m->cont_level - 1].off = offs + tlen;
		DPRINTF(("cont_level[%u] = %u\n", m->cont_level - 1,
		    ms->c.li[m->cont_level - 1].off));
	}
	return offs;
}
// this is an odd function. it is used to get the title and/or
// length of a track.
// if filename is either NULL or of length 0, it means you should
// return the info of lastfn. Otherwise, return the information
// for the file in filename.
// if title is NULL, no title is copied into it.
// if length_in_ms is NULL, no length is copied into it.
void getfileinfo(const char *filename, char *title, int *length_in_ms)
{
	if (!filename || !*filename)  // currently playing file
	{
		if (length_in_ms) *length_in_ms=getlength();
		if (title) // get non-path portion.of filename
		{
			char *p=lastfn+strlen(lastfn);
			while (*p != '\\' && p >= lastfn) p--;
			strcpy(title,++p);
		}
	}
	else // some other file
	{
		if (length_in_ms) // calculate length
		{
			HANDLE hFile;
			hFile = CreateFile(filename,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,
				OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
			if (hFile != INVALID_HANDLE_VALUE)
			{
				*length_in_ms = (GetFileSize(hFile,NULL)*10)/(SAMPLERATE/100*NCH*(BPS/8));
				CloseHandle(hFile);
			}
			else *length_in_ms=-1000; // the default is unknown file length (-1000).
		}
		if (title) // get non path portion of filename
		{
			const char *p=filename+strlen(filename);
			while (*p != '\\' && p >= filename) p--;
			strcpy(title,++p);
		}
	}
}
Exemple #3
0
int
der_cmp(struct magic_set *ms, struct magic *m)
{
	const uint8_t *b = CAST(const void *, ms->search.s);
	const char *s = m->value.s;
	size_t offs = 0, len = ms->search.s_len;
	uint32_t tag, tlen;
	char buf[128];

	tag = gettag(b, &offs, len);
	if (tag == DER_BAD)
		return -1;

	tlen = getlength(b, &offs, len);
	if (tlen == DER_BAD)
		return -1;

	der_tag(buf, sizeof(buf), tag);
	if ((ms->flags & MAGIC_DEBUG) != 0)
		fprintf(stderr, "%s: tag %p got=%s exp=%s\n", __func__, b,
		    buf, s);
	size_t slen = strlen(buf);

	if (strncmp(buf, s, slen) != 0)
		return 0;

	s += slen;

again:
	switch (*s) {
	case '\0':
		return 1;
	case '=':
		s++;
		goto val;
	default:
		if (!isdigit((unsigned char)*s))
			return 0;

		slen = 0;
		do
			slen = slen * 10 + *s - '0';
		while (isdigit((unsigned char)*++s));
		if ((ms->flags & MAGIC_DEBUG) != 0)
			fprintf(stderr, "%s: len %zu %u\n", __func__,
			    slen, tlen);
		if (tlen != slen)
			return 0;
		goto again;
	}
val:
	DPRINTF(("%s: before data %zu %u\n", __func__, offs, tlen));
	der_data(buf, sizeof(buf), tag, b + offs, tlen);
	if ((ms->flags & MAGIC_DEBUG) != 0)
		fprintf(stderr, "%s: data %s %s\n", __func__, buf, s);
	if (strcmp(buf, s) != 0 && strcmp("x", s) != 0)
		return 0;
	strlcpy(ms->ms_value.s, buf, sizeof(ms->ms_value.s));
	return 1;
}
Exemple #4
0
    bool write(const char* data, size_t len)
    {
        bool write_ret = true;

        if(getwritevalidcount() >= len)
        {
            memcpy(getwriteptr(), data, len);
            addwritepos(len);
        }
        else
        {
            size_t left_len = mnDataSize - getlength();
            if(left_len >= len)
            {
                AdjusttoHead();
                write(data, len);
            }
            else
            {
                size_t needLen = len - left_len;
                if(needLen > 0)
                {
                    grow(needLen);
                    write(data, len);
                }
                else
                {
                    write_ret = false;
                }
            }
        }

        return write_ret;
    }
Exemple #5
0
void getfileinfo(char *filename, char *title, int *length_in_msec)
{
	FLAC__StreamMetadata streaminfo;

	if(0 == filename || filename[0] == '\0') {
		filename = lastfn_;
		if(length_in_msec) {
			*length_in_msec = getlength();
			length_in_msec = 0; /* force skip in following code */
		}
	}

	if(!FLAC__metadata_get_streaminfo(filename, &streaminfo)) {
		MessageBox(mod_.hMainWindow, filename, "ERROR: invalid/missing FLAC metadata", 0);
		if(title) {
			static const char *errtitle = "Invalid FLAC File: ";
			sprintf(title, "%s\"%s\"", errtitle, filename);
		}
		if(length_in_msec)
			*length_in_msec = -1;
		return;
	}

	if(title) {
		get_description_(filename, title, MAX_PATH);
	}
	if(length_in_msec)
		*length_in_msec = (int)(streaminfo.data.stream_info.total_samples * 10 / (streaminfo.data.stream_info.sample_rate / 100));
}
Exemple #6
0
static void
printdata(size_t level, const void *v, size_t x, size_t l)
{
	const uint8_t *p = v, *ep = p + l;
	size_t ox;
	char buf[128];

	while (p + x < ep) {
		const uint8_t *q;
		uint8_t c = getclass(p[x]);
		uint8_t t = gettype(p[x]);
		ox = x;
		if (x != 0)
		printf("%.2x %.2x %.2x\n", p[x - 1], p[x], p[x + 1]);
		uint32_t tag = gettag(p, &x, ep - p + x);
		if (p + x >= ep)
			break;
		uint32_t len = getlength(p, &x, ep - p + x);
		
		printf("%zu %zu-%zu %c,%c,%s,%u:", level, ox, x,
		    der_class[c], der_type[t],
		    der_tag(buf, sizeof(buf), tag), len);
		q = p + x;
		if (p + len > ep)
			errx(EXIT_FAILURE, "corrupt der");
		printtag(tag, q, len);
		if (t != DER_TYPE_PRIMITIVE)
			printdata(level + 1, p, x, len + x);
		x += len;
	}
}
Exemple #7
0
void getfileinfo(char *filename, char *title, int *length_in_ms)
{
	CUBEFILE ficube;
	
	if (!filename || !*filename)  // currently playing file
	{
		if (length_in_ms) *length_in_ms=getlength();
		if (title) 
		{
			char *p=lastfn+lstrlen(lastfn);
			while (*p != '\\' && p >= lastfn) p--;
			lstrcpy(title,++p);
		}
	}
	else // some other file
	{
		if (length_in_ms) 
		{
			*length_in_ms=-1000;
			if (!InitADPFILE(filename,&ficube) || !InitADXFILE(filename,&ficube) || !InitDSPFILE(filename,&ficube))
			{
				// these are only second-accurate, but how accurate does this need to be anyway?
				*length_in_ms = ficube.nrsamples/ficube.ch[0].sample_rate*1000;
			}
			CloseCUBEFILE(&ficube);
		}
		if (title) 
		{
			char *p=filename+lstrlen(filename);
			while (*p != '\\' && p >= filename) p--;
			lstrcpy(title,++p);
		}
	}
}
Exemple #8
0
void List<T>::traverse ()
{
    int len = getlength();
    for (int i = 0; i < len; i++)
    {
        cout << "No. " << i + 1 << ":\t" << *get(i + 1) << endl;
    }
}		// -----  end of method List<T>::traverse  -----
Exemple #9
0
 float SVIVector4::normalize(void)
{
	float flength = getlength();
	float fInvlength = 1.0f/flength;
	x *= fInvlength;
	y *= fInvlength;
	z *= fInvlength;
	w *= fInvlength;
	return flength;
}
Exemple #10
0
    inline std::string to_narrow_string(Char const* source)
    {
        typedef std::ctype<Char> ctype_type;

        std::size_t len = getlength(source);
        std::auto_ptr<char> buffer(new char [len+1]);
        std::use_facet<ctype_type>(std::locale())
            .narrow(source, source + len, '.', buffer.get());

        return std::string(buffer.get(), len);
    }
Exemple #11
0
main()
{

	char line[MAXLINE];     //why create the line[] space here? not in routines?
	
	while (getlength(line, MAXLINE) != 0) {
		reverse(line);
		print("%s",line);
	}
	return 0;
}
Exemple #12
0
int main(int argc, char *argv[])
{
  int len;
  char line[MAXLENGTH];
  char reversed[MAXLENGTH];

  while((len = getlength(line, MAXLENGTH)) > 0) {
    reverse(line, reversed, len);
    printf("%s", reversed);
  }

  return 0;
}
int main()
{
    initializeList();
    int i,j,k;
    int c;
    char sr[100];
    i=0;
    k=0;

    scanf("%s",&sr);

    while(sr[i]!='\0')
    {
        if(sr[i]=='(' || sr[i]=='{' || sr[i]=='[')
        {
            insertItem(sr[i]);
        }
        else
        {
            if(length==0)
            {
                k=1;
                break;
            }
            c=list[length-1];
            deleteLast();
            if(sr[i]==')' && c!='(')
            {
                k=1;
                break;
            }
            else if(sr[i]=='}' && c!='{')
            {
                k=1;
                break;
            }
            else if(sr[i]==']' && c!='[')
            {
                k=1;
                break;
            }
        }
        i++;
    }
    j=getlength();
    if(!j && !k)printf("Balanced");
    else printf("Not Balanced");
    clear();
}
Exemple #14
0
void dijkstra(Dict_t * Q, Agraph_t * G, Agnode_t * n)
{
    Agnode_t *u;
    Agedge_t *e;

    pre(G);
    setdist(n, 1);
    dtinsert(Q, n);
    while ((u = extract_min(Q))) {
	for (e = agfstedge(u); e; e = agnxtedge(e, u)) {
	    update(Q, e->node, u, getlength(e));
	}
    }
    post(G);
}
Exemple #15
0
void List<T>::reverse ()
{
    if (getlength() > 1)
    {
        ListNode<T>* p = first->link->link;
        last = first->link;
        last->link = NULL;
        while (NULL != p)
        {
            ListNode<T>* q = p->link;
            p->link = first->link;
            first->link = p;
            p = q;
        }
    }
}		// -----  end of method List<T>::reverse  -----
Exemple #16
0
struct node* Nthfromend(struct node* head,int index){
	printf("entered Nthfromend\n");
	int len=getlength(head);
	printf("len: %d\n",len);
	if(index>len){
		printf("index larger than length\n");
		return NULL;
	}
	int stop=len-index;
	int i;
	for(i=0;i<stop;i++){
		head=head->next;
	}
	return head;

}
Exemple #17
0
void parse_file(const char* filename)
{
    FILE* fp = fopen(filename, "r");
    if (fp == NULL)
        return;
    char ln[0xff * 8] = { 0 };
    char ch = 0;
    int a = 0;
    fread(ln, 0xff*8, 1, fp);
    a = strlen(ln);
    int temp = 0;
    int count = 0;
    int r = 0;
    while (temp <= a)
    {
        int c = 0;
        char* p = ln + temp;
        int len = getlength(p, c);
        if (len == 1 && c == 0)
        {
            res[count] = '#';
            len = 2;
            temp += 1;
            count++;

        }
        else if ((len == 16 || len == 15) && c == 0)
        {
            res[count] = (len + 8) * 4 + (3 - c);
            temp += len;
            count++;
        }
        else if (len > 0 && c > 0)
        {
            res[count] = (len + 8) * 4 + (3 - c);
            temp += len;
            count++;
        }
        else if (len == 0 && c == 0)
        {
            break;
        }

    }
	printf("%s\n", res);
}
Exemple #18
0
    void AdjusttoHead()
    {
        size_t len = 0;

        if(mnReadPos <= 0)
        {
            return;
        }

        len = getlength();
        if(len > 0)
        {
            memmove(mData, mData + mnReadPos, len);
        }

        mnReadPos = 0;
        mWritePos = len;
    }
Exemple #19
0
void getfileinfo(char *filename, char *title, int *length_in_ms)
{
    if (!filename || !*filename)  // currently playing file
    {
        if (length_in_ms)
            *length_in_ms=getlength();

        if (title)
            GetID3FileTitle(lastfn, title, m_format_string);
    }
    else // some other file
    {
        if (length_in_ms)
            *length_in_ms = getsonglength(filename);

        if (title)
            GetID3FileTitle(filename, title, m_format_string);
    }
}
void printlist(LinkList l) {
	LNode *p = l;
	if(!p) {
		printf("LinkList is not being initialized!");
	}
	else if(!(p->next)) {
		printf("there is not any elem in linklist");
	}
	else {
        printf("Length:  %d\n", getlength(l));
		int i = 0;
		while(p) {
			printf("%d\t", p->data);
			p = p->next;
			i++;
		}
        printf("\n\n");
	}
}
Exemple #21
0
int toResult(){
    int val = 0;
    int i = 0;
    int len = getlength(poly_exp);
    int result = 0;
    for(i = 0; i < len; i++){
        char c = poly_exp[i];
        if(isint(c)){
            val = val*10+(c-'0');
        }
        if(c == '~'){
            pushi(val);
            val = 0;
        }
        if(isint(c) && (1-isint(poly_exp[i+1])) && poly_exp[i+1] != '~'){
            pushi(val);
            val = 0;
        }
        if(c == '+'){
            int x = popi();
            int y = popi();
            pushi(x+y);
        }
        if(c == '-'){
            int x = popi();
            int y = popi();
            pushi(y-x);
        }
        if(c == '*'){
            int x = popi();
            int y = popi();
            pushi(y*x);
        }
        if(c == '/'){
            int x = popi();
            int y = popi();
            pushi(y/x);
        }
    }
    result = popi();
    return result;
}
Exemple #22
0
int main(int argc, char *argv[])
{
  int len;
  int max;
  char line[MAXLINE];
  char longest[MAXLINE];

  max = 0;
  while((len = getlength(line, MAXLINE)) > 0) {
    if(len > max) {
      max = len;
      copy(longest, line);
    }
  }

  if(max > 0) {
    printf("%s", longest);
  }

  return 0;
}
	ListNode* removeNthFromEnd(ListNode* head, int n) {
		if (head == NULL) {
			return head;
		}

		int len = getlength(head);
		if (len == n ) {
			return head->next;
		}
		int step = len - n - 1;
		ListNode *p = head; 
		for (int count = 0; p != NULL; p = p->next)	 {
			if (count != step) {
				count++;
			} else {
				break;
			}
		}	

		p->next = p->next->next;
		return head;
	}
Exemple #24
0
int main(int argc, char *argv[])
{
  int i, j, len, spaces, tabs;
  char line[MAXLENGTH];

  while((len = getlength(line, MAXLENGTH)) > 0) {
    tabs = 0;
    for(i = 0; i < len; i++) {
      if(line[i] == '\t') {

        spaces = TABSTOP - ((i - tabs) % TABSTOP);

        for(j = 0; j < spaces; j++) {
          putchar(' ');
        }
        tabs++;
      } else {
        putchar(line[i]);
      }
    }
  }

  return 0;
}
Exemple #25
0
void toBack(char str[]){
    int stillint = 0;
    int str_len = getlength(str);
    int num = 0;
    int i = 0;
    for(i = 0; i < str_len; i++){
        char c = str[i];
        if(isint(c)){
            if(stillint){
                if((i+1) != str_len){
                    if(isint(str[i+1])){
                        poly_exp[num] = c;
                        num++;
                    }
                    else{
                        stillint = 0;
                        poly_exp[num] = c;
                        num++;
                        poly_exp[num] = '~';
                        num++;
                    }
                }
                else{
                    poly_exp[num] = c;
                    num++;
                }
            }
            else{
                poly_exp[num] = c;
                num++;
                stillint = 1;
            }
        }
        else{
            if(stillint){
                poly_exp[num] = '~';
                num++;
                stillint = 0;
            }
            if(c == '('){
                pushc(c);
            }else{
                if(c == ')'){
                    while(ctop() != '('){
                        poly_exp[num] = ctop();
                        num++;
                        popc();
                    }
                    popc();
                }else{
                    if(empytc()){
                        pushc(c);
                    }else{
                        if(getType(c) > getType(ctop())){
                            pushc(c);
                        }else{
                            while((1 - empytc()) && getType(c) <= getType(ctop())){
                                poly_exp[num] = popc();
                                num++;
                            }
                            pushc(c);
                        }
                    }
                }
            }
        }
    }
    while(empytc() == 0){
        poly_exp[num] = ctop();
        num++;
        popc();
    }
}
Exemple #26
0
 float SVIVector4::getAngle(const SVIVector4 &kVector)
{
	float fdot = dot(kVector);
	float fMagnitude = (getlength() * kVector.getlength());
	return acosf(fdot / fMagnitude);
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif
	int nCase=1;
	while(scanf("%d",&n)==1 && n)
	{
		printf("Forest %d\n",nCase++);
		for(int i=0;i<n;i++)
		{
			scanf("%lf %lf %lf %lf",&p[i].x,&p[i].y,&v[i],&l[i]);
		}
		int pp=-1;
		int size=1<<(n);
		for(int i=1;i<size;i++)
		{
			cnt=0;
			double val=0;
			double len=0;
			int ncnt=0;
			bool flag=false;
			for(int j=0;j<n;j++)
			{
				if(!(i&(1<<j)))
					pt[cnt++]=p[j];
				else
				{
					ncnt++;
					len+=l[j];
					val+=v[j];
					if(pp!=-1 && val>dp[pp]+eps)
					{
					    flag=true;
					    break;
					}
				}
			}
			if(flag)    continue;
			if(cnt==0)  continue;

			int tmp;
			if(cnt==2 || cnt==1)
			{
			    for(int k=0;k<cnt;k++)
                    convex[k]=pt[k];
                tmp=cnt;
			}
			else
                tmp=graham(cnt,pt,convex);
			double dist=getlength(tmp,convex);
			dp[i]=val;
			extra[i]=len-dist;
			num[i]=ncnt;
			if(pp==-1 && extra[i]>eps)
			{
			    pp=i;
			    continue;
			}
			if(extra[i]<-eps)   continue;
			if(dp[pp]>dp[i]+eps)
			{
			    pp=i;
			}
			else if(dp[pp]<dp[i]-eps){}
			else
			{
			    if(num[pp]>num[i])
                    pp=i;
			}
		}

		printf("Cut these trees:");
		for(int i=0;i<n;i++)
		{
			if(pp&(1<<i))
				printf(" %d",i+1);
		}
		printf("\nExtra wood: %.2f\n\n",extra[pp]);

	}

	return 0;
}
Exemple #28
0
int
clienthead(int clientsocket_fd, char *info[18], ReqInfo *req_info, char *pathname)
{
  char *gmt_str;
  char *mtime_str;
  char *file_type;
  char *file_length;
  char hostname[128];

  if (req_info->type != SIMPLE)
    if (write(clientsocket_fd, "HTTP/1.0 ", strlen("HTTP/1.0 ")) != 
        strlen("HTTP/1.0 ")) {
      fprintf(stderr, "Unable to write: %s\n", strerror(errno));
      return 1;
    }
  if (write(clientsocket_fd, info[req_info->status], 
            strlen(info[req_info->status])) != strlen(info[req_info->status])) {
    fprintf(stderr, "Unable to write %s: %s\n",
            info[req_info->status], strerror(errno));
    return 1;
  }
  if (write(clientsocket_fd, "\n", 1) != 1) {
    fprintf(stderr, "Unable to write: %s\n", strerror(errno));
    return 1;
  }
  if (getgmttime(& gmt_str)) {
    fprintf(stderr, "Unable to get gmt time.\n");
    return 1;
  }
  if ((req_time = (char*)malloc((strlen(gmt_str)+1)*sizeof(char))) == NULL) {
      fprintf(stderr, "Unable to allocate memory: %s\n",
              strerror(errno));
      return 1;
  }
  strcpy(req_time, gmt_str);
  req_time[GMT_LENGTH] = '\0';
  if (req_info->type != SIMPLE) {
    if (write(clientsocket_fd, "Date: ", strlen("Date: ")) != strlen("Date: ")) {
      fprintf(stderr, "Unable to write: %s\n", strerror(errno));
      return 1;
    }
    if (write(clientsocket_fd, gmt_str, strlen(gmt_str)) != strlen(gmt_str)) {
      fprintf(stderr, "Unable to write: %s\n", strerror(errno));
      return 1;
    }
    if (gethostname(hostname, sizeof(hostname)) < 0) {
      fprintf(stderr, "Unable to get hostname: %s\n", strerror(errno));
      return 1;
    }
    if (write(clientsocket_fd, "Server: ", strlen("Server: ")) != 
        strlen("Server: ")) {
      fprintf(stderr, "Unable to write: %s\n", strerror(errno));
      return 1;
    }
    if (write(clientsocket_fd, hostname, strlen(hostname)) != 
        strlen(hostname)) {
      fprintf(stderr, "Unable to write: %s\n", strerror(errno));
      return 1;
    }
    if (write(clientsocket_fd, "\n", 1) != 1) {
      fprintf(stderr, "Unable to write: %s\n", strerror(errno));
      return 1;
    }
  }
  if (req_info->status != OK && req_info->status != SIMPLE_RESPONSE && 
      req_info->status != CREATED)
    return 0;
  if (req_info->type != SIMPLE) {
    if (getmtime(pathname, &mtime_str)) {
      fprintf(stderr, "Unable to get modify time.\n");
      return 1;
    }
    if (write(clientsocket_fd, "Last-Modified: ", strlen("Last-Modified: ")) != 
              strlen("Last-Modified: ")) {
      fprintf(stderr, "Unable to write: %s\n", strerror(errno));
      return 1;
    }
    if (write(clientsocket_fd, mtime_str, strlen(mtime_str)) != 
        strlen(mtime_str)) {
      fprintf(stderr, "Unable to write: %s\n", strerror(errno));
      return 1;
    }
    if (gettype(& file_type, pathname)) {
      fprintf(stderr, "Unable to get type.\n");
      return 1;
    }
    if (write(clientsocket_fd, "Content-Type: ", strlen("Content-Type: ")) != 
        strlen("Content-Type: ")) {
      fprintf(stderr, "Unable to write: %s\n", strerror(errno));
      return 1;
    }
    if (write(clientsocket_fd, file_type, strlen(file_type)) != 
        strlen(file_type)) {
      fprintf(stderr, "Unable to write: %s\n", strerror(errno));
      return 1;
    }
    if (write(clientsocket_fd, "\n", 1) != 1) {
      fprintf(stderr, "Unable to write: %s\n", strerror(errno));
      return 1;
    }
  }
  if ((file_length = getlength(pathname)) == NULL) {
    fprintf(stderr, "Unable to get type.\n");
    return 1;
  }
  if ((log_length = (char*)malloc((strlen(file_length)+1)*sizeof(char))) == NULL) {
      fprintf(stderr, "Unable to allocate memory: %s\n",
              strerror(errno));
      return 1;
  }
  strcpy(log_length, file_length);
  if (req_info->type != SIMPLE) {
    if (write(clientsocket_fd, "Content-Length: ", strlen("Content-Length: ")) != 
        strlen("Content-Length: ")) {
      fprintf(stderr, "Unable to write: %s\n", strerror(errno));
      return 1;
    }
    if (write(clientsocket_fd, file_length, strlen(file_length)) != 
        strlen(file_length)) {
      fprintf(stderr, "Unable to write: %s\n", strerror(errno));
      return 1;
    }
    if (write(clientsocket_fd, "\n", 1) != 1) {
      fprintf(stderr, "Unable to write: %s\n", strerror(errno));
      return 1;
    }
    if (write(clientsocket_fd, "\n", 1) != 1) {
      fprintf(stderr, "Unable to write: %s\n", strerror(errno));
      return 1;
    }
  }
  return 0;
}
void handlepencilmsg(cJSON *json, struct user *usr) {
	struct pencil *p = &usr->pencil;
	struct buffer buf;
	char buffer_empty = 1;
	char mousedown;

	json = jsongetjson(json, "data");
	if (!json)
		return;
	json = json->child;

	p->ticksolid = max(p->ticksolid + 1, usr->gm->tick + SERVER_DELAY / TICK_LENGTH + usr->gm->inkdelay / TICK_LENGTH);

	/* this will be sent to other players */
	buf.start = 0;
	appendheader(&buf, MODE_PENCIL, usr->index);
	appendtick(&buf, p->ticksolid);

	if ((mousedown = json->valueint == -1))
		json = json->next;

	appendchar(&buf, mousedown);
	assert(!pthread_mutex_lock(&usr->gm->lock));

	while (json) {
		int x, y, tick;

		if (!readpencilmsg(&json, &x, &y, &tick))
			break;

		if (tick < p->tick || x < 0 || y < 0 || x > usr->gm->w || y > usr->gm->h)
			break;

		if (abs(usr->gm->tick + SERVER_DELAY / TICK_LENGTH - tick) > MAX_LAG_SPIKE / TICK_LENGTH) {
			warningplayer(usr, "error: tick of pencil msg not valid\n");
			break;
		}

		regenink(p, tick);

		if (mousedown) {
			if (p->ink < MOUSEDOWN_INK) {
				warningplayer(usr, "error: not enough ink for pencil down. %d required, %f left\n", MOUSEDOWN_INK, p->ink);
				break;
			}

			p->ink -= MOUSEDOWN_INK;
			p->down = 1;
			mousedown = 0;
		}
		else {
			double d = getlength(p->x - x, p->y - y);

			if (!p->down) {
				warningplayer(usr, "error: pencil move: pencil not down\n");
				break;
			}

			if (p->ink < d) {
				warningplayer(usr, "error: pencil move: not enough ink. %f required, %f left\n", d, p->ink);
				break;
			}

			if (d < INK_MIN_DISTANCE)
				p->down = 0;

			if (p->x == x && p->y == y)
				break;

			p->ink -= d;
			queuepencilseg(p, x, y);
		}

		appendpos(&buf, x, y);
		p->x = x;
		p->y = y;
		buffer_empty = 0;
	}

	if (!buffer_empty)
		airstr(buf.start, buf.at - buf.start, usr->gm, 0);

	pthread_mutex_unlock(&usr->gm->lock);
	free(buf.start);
}
Exemple #30
0
 SVIVector4 SVIVector4::normal(void)
{
	float flength = getlength();
	return (*this * (1.0f / flength));
}