コード例 #1
0
ファイル: cache1d.c プロジェクト: Daedolon/erampage
void suckcache(intptr_t *suckptr)
{
    int32_t i;

    //Can't exit early, because invalid pointer might be same even though lock = 0
    for (i=0; i<cacnum; i++)
        if ((intptr_t)(*cac[i].hand) == (intptr_t)suckptr)
        {
            if (*cac[i].lock) *cac[i].hand = 0;
            cac[i].lock = &zerochar;
            cac[i].hand = 0;

            //Combine empty blocks
            if ((i > 0) && (*cac[i-1].lock == 0))
            {
                cac[i-1].leng += cac[i].leng;
                cacnum--; copybuf(&cac[i+1],&cac[i],(cacnum-i)*sizeof(cactype));
            }
            else if ((i < cacnum-1) && (*cac[i+1].lock == 0))
            {
                cac[i+1].leng += cac[i].leng;
                cacnum--; copybuf(&cac[i+1],&cac[i],(cacnum-i)*sizeof(cactype));
            }
        }
}
コード例 #2
0
ファイル: flashdemo.c プロジェクト: MSFeng/PurdueCourses
int main()
{   
    unsigned int* memspot = (unsigned int*)SECTOR1;		//We need a means of addressing our Flash
    char stringVal[3] = "HI";   //Lets store "HI"
    
    flash_setup();     //set flash divider (This is for a 24MHz Bus clock, and a 8MHz Oscillator)
    
    int result = 0;				//Lets get the size of the function to copy		
    char* p2 = (char*)copybuf;			//pointer to the memory base of cmpbuf
    char* p1 = (char*)program_flash;		//pointer to the memory base of setflag
    int size = p2 - p1;			//subtract the two addresses

    unsigned char code_array[size];		//reserve spot on the stack to copy the function to

    copybuf((unsigned char*)program_flash, code_array, size);	//cast function (cause its void)
    result = cmpbuf((unsigned char*)program_flash, code_array, size);  //again cast
    if(result == 0)
    {
      printf("Copy Success");
    }
    else
    {
      printf("Copy Failed!");
    }
    
    printf("Pre Function Call: %x", *memspot);

   typedef void (*PF)(unsigned int*, char*);	  	//this type defines PF as a pointer to a function that whose type is void and takes no parameters
   ((PF)code_array)(memspot, stringVal);		//call the function from RAM now
   
   printf("Post Function Call: %x", *memspot);

   return 0;
}
コード例 #3
0
ファイル: utformat.c プロジェクト: ScottDaniels/xfm
extern void UTformat( char *inbuf, char **inlist, char *outbuf,
               char *control, char *delim, char escsym)
{
 int fieldsize = 0; /* minimum field size to display a token in */
 int i;             /* loop indexes - token vars */
 int j;             /* index intro control buffer */
 int k;             /* index into output buffer */
 int idx;           /* index into toks array as specfied by $n in control */
 char **toks;       /* pointers to tokens to process */
 char *tok;         /* pointer at current tok from input buffer */
 char *sub;         /* pointer at token to substitute for the $n param */
 int count;         /* number of tokens to prevent writing bad data */
 int freeflag = 0;  /* flag indicating whether tok array is freed at end */


 if( inbuf != NULL )   /* if a string passed in for us to parse */
  {
   freeflag = 1;                             /* we must free it at the end */
   toks = (char **) malloc( (unsigned) (256 * sizeof (char *)) );

   /*tok = strtok( inbuf, delim ); */ /* get first token */

   tok = inbuf;
   i = 0;
   while( *tok && i < 255 )
    {
     toks[i] = tok;
     for( ; *tok && !strchr( delim, *tok ); tok++ )
      if( *tok == escsym && strchr( delim, *(tok+1)) )
       {
        tok++;
        *tok |= 0x80;
       }
     if( *tok )
      *tok++ = EOS;
     i++;
    }

#ifdef KEEP
   /* toks[0] = tok; */                /* and put it in the buffer */
   for( i = 1; (tok = strtok( NULL, delim )) != NULL && i < 29; i++ )
    toks[i] = tok;   /* capture pointers to each token in input buffer */
   toks[i] = NULL;   /* last one in the list is NULL */
#endif
   count = i;
  }                  /* end if string passed in to parse */
 else                /* a list of tokens passed in to use */
  {
   for( count = 0; inlist[count] != NULL;  count++ );
   toks = inlist;     /* point at the list for the rest to use */
  }

 for( j = 0, k = 0; control[j] != EOS; j++ )
  {
   if( control[j] == '#' )   /* field size specified */
    {
     fieldsize = atoi( &control[j+1] );  /* save it for next $ in control str*/
     for( j++; control[j+1] != EOS && control[j+1] >= '0' &&
               control[j+1] <= '9'; j++ );   /* skip #parm in control */
    }
   else
   if( control[j] == '$' )   /* substitute an input token? */
    {
     idx = atoi( &control[j+1] ) - 1;  /* get user's number & make 0 based */
     for( j++; control[j+1] != EOS && control[j+1] >= '0' &&
               control[j+1] <= '9'; j++ );         /* skip $parm in control */

     sub = toks[idx];          /* point at string to place into output buf */
     if( sub == NULL || idx > count )         /* bad meat - just use blank */
      sub = " ";                                  /* just write in a blank */


     if( fieldsize > 0 )   /* previous # parm encountered in control? */
      {
       i = fieldsize - strlen( sub ); /* calc spaces to skip */
       for( ; i > 0; i-- )
        outbuf[k++] = ' ';   /* add lead blanks if we can */
      }

     k += copybuf( sub, &outbuf[k], escsym );           /* copy token in */

     if( fieldsize < 0 )         /* left justify # encountered previously */
      {
       i = abs( fieldsize ) - strlen( sub ); /* calc spaces to skip */
       for( ; i > 0; i-- )
        outbuf[k++] = ' ';   /* add trailing blanks if we can */
      }
     fieldsize = 0;    /* reset */
    }    /* end if $ */
   else
    if( control[j] == '~' )   /* print all tokens from the number on out */
     {
      idx = atoi( &control[j+1] ) - 1;  /* get user's number & make 0 based */
      for( j++; control[j+1] != EOS && control[j+1] >= '0' &&
                control[j+1] <= '9'; j++ );   /* skip $parm in control */
      while( (sub = toks[idx]) != NULL )      /* put in remaining toks */
       {
        k += copybuf( sub, &outbuf[k], escsym );
/*
        for( i = 0; sub[i] != EOS; i++ )
         outbuf[k++] = sub[i];  
*/
        idx++;
        outbuf[k++] = ' ';          /* seperate with a blank */
       }                            /* end while */
      k--;                          /* dont leave last trailing blank */
     }                              /* end if & */
    else
     if( control[j] == '^' &&     /* escape only utformat special characters */
         (control[j+1] == '~' || control[j+1] == '$' || control[j+1] == '#') )
      {
       ++j;                        /* skip escape symbol */
       outbuf[k++] = control[j];   /* then copy in the escaped character */
      }
     else
      outbuf[k++] = control[j];   /* just copy next character */
  }                               /* end for j,k */

 if( freeflag && toks )
  free( toks );         /* free it if we allocated it */
 outbuf[k] = EOS;       /* terminate the buffer properly */
}                       /* UTformat */
コード例 #4
0
ファイル: serv_3.cpp プロジェクト: vint1024/Stream_music
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	switch (message)
	{
	case WM_CREATE:
		{
			hdc = BeginPaint(hWnd, &ps);// дескриптор л¤ отображени¤ элементов окна
			startButton = CreateWindowEx(0,TEXT("button"),TEXT("Start Server"),WS_VISIBLE|WS_CHILD,700, 20, 150, 30, hWnd, (HMENU)START_ID, hInst, 0);
			stopButton = CreateWindowEx(0,TEXT("button"),TEXT("Stop Server"),WS_VISIBLE|WS_CHILD,700, 60, 150, 30, hWnd, (HMENU)STOP_ID, hInst, 0);
			addButton = CreateWindowEx(0,TEXT("button"),TEXT("Add file"),WS_VISIBLE|WS_CHILD,700, 150, 150, 30, hWnd, (HMENU)ADD_ID, hInst, 0);
			exitButton = CreateWindowEx(0,TEXT("button"),TEXT("Exit"),WS_VISIBLE|WS_CHILD,700, 350, 150, 30, hWnd, (HMENU)EXIT_ID, hInst, 0);
			hEdit			= CreateWindowEx(	0,TEXT("edit"),TEXT("127.0.0.1"), WS_CHILD | WS_VISIBLE| ES_LEFT | ES_MULTILINE,60, 10, 400, 20, hWnd, (HMENU)EDIT_ID, hInst, 0);
			hEdit2			= CreateWindowEx(	0,TEXT("edit"),TEXT("12345"), WS_CHILD | WS_VISIBLE| ES_LEFT | ES_MULTILINE,60, 40, 400, 20, hWnd, (HMENU)EDIT2_ID, hInst, 0);
			hList			= CreateWindowEx(	0,TEXT("ListBox"),0,WS_CHILD | WS_VISIBLE| ES_LEFT|ES_NUMBER, 60, 130, 500, 400, hWnd, (HMENU)LIST_ID, hInst, 0);
			EndPaint(hWnd, &ps);
			break;
		}
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// –азобрать выбор:
		switch (wmId)
		{
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		case EXIT_ID:
			DestroyWindow(hWnd);
			break;
		case ADD_ID:
		{
			OPENFILENAME ofn;
			PSTR FileName  = new char [255];
			lstrcpy(FileName,"");
			ZeroMemory(&ofn,sizeof(ofn));										// очистим структуру
			ofn.lStructSize = sizeof(ofn);
			ofn.hwndOwner = hWnd;
			ofn.lpstrFile = FileName;
			ofn.lpstrFilter = "WAV\0*.wav";// маскафильтра добавлени¤ файла
			ofn.nFilterIndex = 1;//айдишник структуры
			ofn.lpstrFileTitle = NULL;
			ofn.nMaxFileTitle = 0;
			ofn.lpstrInitialDir = NULL;
			ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
			ofn.nMaxFile = 9999;
			ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_EXPLORER;
			bool ret = GetOpenFileName(&ofn); 
			DefWindowProc(hWnd, WM_PAINT, wParam, lParam);
			SendMessage(hList,LB_ADDSTRING,wParam,(LPARAM)ofn.lpstrFile);
			SendMessage(hList,LB_SETCURSEL,0,0);
			break;
		}
		case START_ID:
		{
			WSAStartup(MAKEWORD(2,2), &wsaData);										// Initialize Winsock
			SendRecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);					// Create a SOCKET for connecting to server
			// Setup the TCP listening socket
			ServerAddr.sin_family=AF_INET;												//семейство адресов 
			char S[255];
			SendMessage(hEdit, WM_GETTEXT, 255, (LPARAM)S);
			ServerAddr.sin_addr.s_addr = inet_addr( S );
			SendMessage(hEdit2, WM_GETTEXT, 255, (LPARAM)S);
			int tmp = atoi(S); // число
			ServerAddr.sin_port=htons(tmp);
			err = bind( SendRecvSocket, (sockaddr *) &ServerAddr, sizeof(ServerAddr));	// св¤зывание адреса с сокетом
			if (err == SOCKET_ERROR) 
			{
				char strerr[256];
				int tmp = WSAGetLastError();
				sprintf(strerr,"%d",tmp);
				std::string tmp_S;
				tmp_S="ERROR number: ";
				tmp_S+=strerr;
				MessageBox(hWnd,(LPCSTR)strerr,tmp_S.c_str(),  MB_ICONERROR);
				closesocket(SendRecvSocket);
				WSACleanup();
				break;
			}
			WIN32_FIND_DATA FindFileData;
			HANDLE hFind;
			int el=-100;
			el=SendMessage(hList,LB_GETCURSEL,0,0);
			if (el==-1)
			{
				MessageBox(hWnd,"Add element", "ERROR", MB_ICONERROR);
				closesocket(SendRecvSocket);
				WSACleanup();
				break;
			}
			SendMessage(hList,LB_GETTEXT, el, (LPARAM)TransFileName);
			hFind = FindFirstFile((LPCSTR)TransFileName, &FindFileData);
			FindClose(hFind);
			ifs.open(TransFileName,std::ios_base::binary);
			SetTimer(hWnd,100500,50,NULL);
			break;
		}
		case STOP_ID:
		{
			KillTimer(hWnd,100500);
			if (ifs.is_open()) ifs.close();
			closesocket(SendRecvSocket);
			WSACleanup();
			break;
		}
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
	{
		hdc = BeginPaint(hWnd, &ps);
		// добавьть любой код отрисовки...
		EndPaint(hWnd, &ps);
		break;
	}
	case WM_TIMER:
		{
			if(wParam==100500)
			{
				DWORD val = 20; // ждем 20 мс
				setsockopt (SendRecvSocket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&val, sizeof DWORD);		//без этого вызова висим вечно
				err = recvfrom(SendRecvSocket,recvbuf,maxlen,0,(sockaddr *)&ClientAddr,&ClientAddrSize);
				if (err > 0) 
				{
					recvbuf[err]=0;
					std::string inS, FunctionS, ComandS;
					inS = (char* )recvbuf;
					int i =0;
					while ((i<inS.length()) && (inS[i] != ' '))
						ComandS +=inS[i++];
					int comand = atoi(ComandS.c_str());
					if (comand == 1)
					{
						const int NN=sizeof(sWaveHeader);
						char* buf=new char[NN];
						int k=0;
						if (ifs.peek()!=EOF)
						{
							for (int j =0; j<NN; ++j)
							{
								buf[k]=ifs.get();
								++k;
								if (ifs.peek()==EOF)
									break;
							}
						}
						sWaveHeader Hdr;
						copybuf(reinterpret_cast<char*>(&Hdr),buf, sizeof(sWaveHeader));
						sendto(SendRecvSocket,buf,k,0,(sockaddr *)&ClientAddr,sizeof(ClientAddr));		// отправл¤ем результат на сервер
						delete []buf;
					}
					if(comand>1)
					{
						++count_obr;
						const int NN=comand;
						char* buf=new char[NN];
						int k=0;
						if (ifs.peek()!=EOF)
						{
							for (int j =0; j<NN; ++j)
							{
								buf[k]=ifs.get();
								++k;
								if (ifs.peek()==EOF)
									break;
							}
						}
						sendto(SendRecvSocket,buf,k,0,(sockaddr *)&ClientAddr,sizeof(ClientAddr));			// отправл¤ем результат на сервер
						delete []buf;
					}

				}
				if (ifs.is_open())
					if (ifs.peek()==EOF)
					{
						KillTimer(hWnd,100500);
						if (ifs.is_open())ifs.close();
						closesocket(SendRecvSocket);
						WSACleanup();

						char tmp_count[256];
						sprintf(tmp_count,"%d",count_obr);
						std::string message_i;
						message_i = TransFileName;
						message_i+= "  file transfer is complete. count_obr = ";
						message_i+= tmp_count;
						MessageBox(hWnd, message_i.c_str(),"Information", MB_ICONINFORMATION);
					}
			}
			break;
		}
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}