Пример #1
0
int checkColumn(int table[7][20], int column, int stackDepth)
{
	int i;
	for(i=stackDepth;i>0;i--)
	{
		if(!checkSwap(table[column][i-1],table[column][i])) return 0;
	}
	return 1;
}
Пример #2
0
void checkTable(int table[7][20], int founds[4][13]){ //check for valid table->table / table->foundations move, do it
	int i,j,k,i0,i1,y;
	//check for table -> foundation moves
	for(i=0;i<7;i++) 
	{
		for(j=0;j<4;j++)
		{
			if(checkFoundMove(table[i][0], founds[j][0])){
				if(DEBUG >= 30) printf("T->F: %.2d, %.2d\n",table[i][0], founds[j][0]);
				for(k=12;k>0;k--){
					founds[j][k] = founds[j][k-1];
				}
				founds[j][0] = table[i][0];
				for(k=0;k<(19);k++){//move down cards at source
					table[i][k] = table[i][k+1];
				}
			}
		}
	}
	//check for table -> table moves
	for(y=19;y>=0;y--)//go down the rows
	{
		for(i0=0;i0<7;i0++)//source card
		{
			for(i1=0;i1<7;i1++)//dest card
			{
				if(i0!=i1)//no move if same row
				{
					if(checkSwap(table[i0][y],table[i1][0])) //if top card matches dest
					{
						if(checkColumn(table,i0,y)){
							if(DEBUG >= 30) printf("T->T: %d:  %.2d, %.2d\n",y+1,table[i0][y]%13,table[i1][0]%13);
							doMove(table, i0,i1,y+1);
						}
					}
				}
			}
		}
	}
}
Пример #3
0
int checkWaste(int table[7][20], int deck[52], int wastePos, int founds[4][13]) //checks for waste->table move
{
	//printCard(deck[wastePos]);
	//printf("\n");
	int i,j;
	for(i=0;i<4;i++) //check each foundation column
	{
		if(checkFoundMove(deck[wastePos],founds[i][0]))
		{
			if(DEBUG >= 30) printf("W->F: %.2d, %.2d\n",deck[wastePos]%13,founds[i][j]%13);
			for(j=12;j>0;j--){
				founds[i][j] = founds[i][j-1];
			}
			founds[i][0] = deck[wastePos];
			deck[wastePos] = 99;
			
			return wastePos+1;
		}
	
	}
	for(i=0;i<7;i++) //check each table column
	{
		
		if(checkSwap(deck[wastePos], table[i][0])) 
		{	
			if(DEBUG >= 30) printf("W->T: %.2d, %.2d\n", deck[wastePos]%13,table[i][0]%13);
			for(j=19;j>=0;j--){//move up cards at dest
				table[i][j] = table[i][j-1];
			}
			table[i][0] = deck[wastePos];
			deck[wastePos] = 99;
			
			break;
		}
	}			
	
	return wastePos+1;
}
Пример #4
0
void
BaseShadow::baseInit( ClassAd *job_ad, const char* schedd_addr, const char *xfer_queue_contact_info )
{
	int pending = FALSE;

	if( ! job_ad ) {
		EXCEPT("baseInit() called with NULL job_ad!");
	}
	jobAd = job_ad;

	if (sendUpdatesToSchedd && ! is_valid_sinful(schedd_addr)) {
		EXCEPT("schedd_addr not specified with valid address");
	}
	scheddAddr = sendUpdatesToSchedd ? strdup( schedd_addr ) : strdup("noschedd");

	m_xfer_queue_contact_info = xfer_queue_contact_info;

	if ( !jobAd->LookupString(ATTR_OWNER, owner)) {
		EXCEPT("Job ad doesn't contain an %s attribute.", ATTR_OWNER);
	}

	if( !jobAd->LookupInteger(ATTR_CLUSTER_ID, cluster)) {
		EXCEPT("Job ad doesn't contain a %s attribute.", ATTR_CLUSTER_ID);
	}

	if( !jobAd->LookupInteger(ATTR_PROC_ID, proc)) {
		EXCEPT("Job ad doesn't contain a %s attribute.", ATTR_PROC_ID);
	}


		// Grab the GlobalJobId if we've got it.
	if( ! jobAd->LookupString(ATTR_GLOBAL_JOB_ID, &gjid) ) {
		gjid = NULL;
	}

	// grab the NT domain if we've got it
	jobAd->LookupString(ATTR_NT_DOMAIN, domain);
	if ( !jobAd->LookupString(ATTR_JOB_IWD, iwd)) {
		EXCEPT("Job ad doesn't contain an %s attribute.", ATTR_JOB_IWD);
	}

	if( !jobAd->LookupFloat(ATTR_BYTES_SENT, prev_run_bytes_sent) ) {
		prev_run_bytes_sent = 0;
	}
	if( !jobAd->LookupFloat(ATTR_BYTES_RECVD, prev_run_bytes_recvd) ) {
		prev_run_bytes_recvd = 0;
	}

		// construct the core file name we'd get if we had one.
	MyString tmp_name = iwd;
	tmp_name += DIR_DELIM_CHAR;
	tmp_name += "core.";
	tmp_name += cluster;
	tmp_name += '.';
	tmp_name += proc;
	core_file_name = strdup( tmp_name.Value() );

        // put the shadow's sinful string into the jobAd.  Helpful for
        // the mpi shadow, at least...and a good idea in general.
	MyString tmp_addr = ATTR_MY_ADDRESS;
	tmp_addr += "=\"";
	tmp_addr += daemonCore->InfoCommandSinfulString();
	tmp_addr += '"';
    if ( !jobAd->Insert( tmp_addr.Value() )) {
        EXCEPT( "Failed to insert %s!", ATTR_MY_ADDRESS );
    }

	DebugId = display_dprintf_header;
	
	config();

		// Make sure we've got enough swap space to run
	checkSwap();

	// handle system calls with Owner's privilege
// XXX this belong here?  We'll see...
	// Calling init_user_ids() while in user priv causes badness.
	// Make sure we're in another priv state.
	set_condor_priv();
	if ( !init_user_ids(owner.Value(), domain.Value())) {
		dprintf(D_ALWAYS, "init_user_ids() failed as user %s\n",owner.Value() );
		// uids.C will EXCEPT when we set_user_priv() now
		// so there's not much we can do at this point
		
#if ! defined(WIN32)
		if ( param_boolean( "SHADOW_RUN_UNKNOWN_USER_JOBS", false ) )
		{
			dprintf(D_ALWAYS, "trying init_user_ids() as user nobody\n" );
			
			owner="nobody";
			domain=NULL;
			if (!init_user_ids(owner.Value(), domain.Value()))
			{
				dprintf(D_ALWAYS, "init_user_ids() failed!\n");
			}
			else
			{
				jobAd->Assign( ATTR_JOB_RUNAS_OWNER, "FALSE" );
				m_RunAsNobody=true;
				dprintf(D_ALWAYS, "init_user_ids() now running as user nobody\n");
			}
		}
#endif

	}
	set_user_priv();
	daemonCore->Register_Priv_State( PRIV_USER );

	dumpClassad( "BaseShadow::baseInit()", this->jobAd, D_JOB );

		// initialize the UserPolicy object
	shadow_user_policy.init( jobAd, this );

		// setup an object to keep our job ad updated to the schedd's
		// permanent job queue.  this clears all the dirty bits on our
		// copy of the classad, so anything we touch after this will
		// be updated to the schedd when appropriate.

		// Unless we got a command line arg asking us not to
	if (sendUpdatesToSchedd) {
		// the usual case
		job_updater = new QmgrJobUpdater( jobAd, scheddAddr, CondorVersion() );
	} else {
		job_updater = new NullQmgrJobUpdater( jobAd, scheddAddr, CondorVersion() );
	}

		// init user log; hold on failure
		// NOTE: job_updater must be initialized _before_ initUserLog(),
		// in order to handle the case of the job going on hold as a
		// result of failure in initUserLog().
	initUserLog();

		// change directory; hold on failure
	if ( cdToIwd() == -1 ) {
		EXCEPT("Could not cd to initial working directory");
	}

		// check to see if this invocation of the shadow is just to write
		// a terminate event and exit since this job had been recorded as
		// pending termination, but somehow the job didn't leave the queue
		// and the schedd is trying to restart it again..
	if( jobAd->LookupInteger(ATTR_TERMINATION_PENDING, pending)) {
		if (pending == TRUE) {
			// If the classad of this job "thinks" that this job should be
			// finished already, let's enact that belief.
			// This function does not return.
			this->terminateJob(US_TERMINATE_PENDING);
		}
	}

		// If we need to claim the startd before activating the claim
	int wantClaiming = 0;
	jobAd->LookupBool(ATTR_CLAIM_STARTD, wantClaiming);
	if (wantClaiming) {
		MyString startdSinful;
		MyString claimid;

			// Pull startd addr and claimid out of the jobad
		jobAd->LookupString(ATTR_STARTD_IP_ADDR, startdSinful);
		jobAd->LookupString(ATTR_CLAIM_ID, claimid);

		dprintf(D_ALWAYS, "%s is true, trying to claim startd %s\n", ATTR_CLAIM_STARTD, startdSinful.Value());

		classy_counted_ptr<DCStartd> startd = new DCStartd("description", NULL, startdSinful.Value(), claimid.Value());
	
		classy_counted_ptr<DCMsgCallback> cb = 
			new DCMsgCallback((DCMsgCallback::CppFunction)&BaseShadow::startdClaimedCB,
			this, jobAd);
																 
			// this can't fail, will always call the callback
		startd->asyncRequestOpportunisticClaim(jobAd, 
											   "description", 
											   daemonCore->InfoCommandSinfulString(), 
											   1200 /*alive interval*/, 
											   20 /* net timeout*/, 
											   100 /*total timeout*/, 
											   cb);
	}
}
Пример #5
0
//------------------------------------------------------------------------------
// Read a bitmap (BMP) format file
//------------------------------------------------------------------------------
bool Image::readFileBMP(const char* const filename, const char* const path)
{
   static const unsigned int BITMAPFILE_SIZE = (MAX_PATH_LEN + MAX_FILENAME_LEN);
   char bitmapFile[BITMAPFILE_SIZE];
   bitmapFile[0] = '\0';

   // append path name
   const char* p1 = path;
   if (p1 != nullptr && std::strlen(p1) > 0) {
      base::utStrcat(bitmapFile, sizeof(bitmapFile), p1);
      base::utStrcat(bitmapFile, sizeof(bitmapFile), "/");
   }

   // append file name
   const char* p2 = filename;
   if (p2 != nullptr && std::strlen(p2) > 0) {
      base::utStrcat(bitmapFile, sizeof(bitmapFile), p2);
   }

   // Do we have a full path name?
   if (std::strlen(bitmapFile) <= 1) {
      if (isMessageEnabled(MSG_ERROR)) {
         std::cerr << "Image::readFileBMP(): invalid file name: " << bitmapFile << std::endl;
      }
      return false;
   }

   if (isMessageEnabled(MSG_INFO)) {
      std::cout << "Image: Loading file: " << filename << std::endl;
   }

   FILE* fp = std::fopen(bitmapFile,"rb");
   if (fp == nullptr) {
      if (isMessageEnabled(MSG_ERROR)) {
         std::cerr << "Image::readFileBMP: unable to open bitmap file: " << bitmapFile << std::endl;
      }
      return false;
   }

   // Big or little ending?  Swap if we're a big endian arch
   bool swap = checkSwap();

   // Read the bitmap file header (BITMAPFILEHEADER)
   char bfType[2];
   uint32_t  bfSize(0);
   uint16_t  bfReserved1(0);
   uint16_t  bfReserved2(0);
   uint32_t  bfOffBits(0);

   //unsigned int bitmapFileHdrSize =
   //   sizeof(bfType) + sizeof(bfSize) + sizeof(bfReserved1) + sizeof(bfReserved2) + sizeof(bfOffBits);

   size_t nItemsRead(0);
   nItemsRead = std::fread(&bfType, sizeof(char), 2, fp);

   nItemsRead = std::fread(&bfSize, sizeof(bfSize), 1, fp);
   if (swap) bfSize = convertUInt32(bfSize);

   nItemsRead = std::fread(&bfReserved1, sizeof(bfReserved1), 1, fp);
   if (swap) bfReserved1 = convertUInt16(bfReserved1);

   nItemsRead = std::fread(&bfReserved2, sizeof(bfReserved2), 1, fp);
   if (swap) bfReserved2 = convertUInt16(bfReserved2);

   nItemsRead = std::fread(&bfOffBits, sizeof(bfOffBits), 1, fp);
   if (swap) bfOffBits = convertUInt32(bfOffBits);

   if (bfType[0] != 'B' || bfType[1] != 'M') {
      // Not a bitmap file
      if (isMessageEnabled(MSG_ERROR)) {
         std::cerr << "Image::readFileBMP(1): invalid bitmap file: " << bitmapFile << std::endl;
      }
      std::fclose(fp);
      return false;
   }

   // Read the bitmap file info
   BITMAPINFOHEADER_X bmfi;
   nItemsRead = std::fread(&bmfi, sizeof(BITMAPINFOHEADER_X), 1, fp);

   if (swap) {
      bmfi.biSize = convertUInt32(bmfi.biSize);
      bmfi.biWidth = convertInt32(bmfi.biWidth);
      bmfi.biHeight = convertInt32(bmfi.biHeight);
      bmfi.biPlanes = convertUInt16(bmfi.biPlanes);
      bmfi.biBitCount = convertUInt16(bmfi.biBitCount);
      bmfi.biCompression = convertUInt32(bmfi.biCompression);
      bmfi.biSizeImage = convertUInt32(bmfi.biSizeImage);
      bmfi.biXPelsPerMeter = convertInt32(bmfi.biXPelsPerMeter);
      bmfi.biYPelsPerMeter = convertInt32(bmfi.biYPelsPerMeter);
      bmfi.biClrUsed = convertUInt32(bmfi.biClrUsed);
      bmfi.biClrImportant = convertUInt32(bmfi.biClrImportant);
   }

   if (bmfi.biSize != sizeof(BITMAPINFOHEADER_X) || bmfi.biPlanes != 1) {
      // Not a bitmap file
      if (isMessageEnabled(MSG_ERROR)) {
         std::cerr << "Image::readFileBMP(2): invalid bitmap file: " << bitmapFile << std::endl;
      }
      std::fclose(fp);
      return false;
   }

   // set width & height (truncates to powers of two)
   setWidth(bmfi.biWidth);
   setHeight(bmfi.biHeight);

   // set resolution
   setXResolutionPPM(bmfi.biXPelsPerMeter);
   setYResolutionPPM(bmfi.biYPelsPerMeter);

   // Read the colors
   GLubyte* bmap = nullptr;
   if (bmfi.biBitCount == 24) {
      setNumComponents(3);
      setFormat(GL_BGR_EXT);
      bmap = readRgbValuesBMP(fp, bfOffBits, &bmfi);
   }
   else if (bmfi.biBitCount == 8){
      setNumComponents(3);
      setFormat(GL_BGR_EXT);
      bmap = readColorValuesBMP(fp, bfOffBits, &bmfi);
   }
   else if (bmfi.biBitCount == 4){
      if (isMessageEnabled(MSG_WARNING)) {
         std::cerr << "Image::readFileBMP: can not load a 16 color bitmap!" << std::endl;
      }
   }

   if (bmap == nullptr) {
      if (isMessageEnabled(MSG_WARNING)) {
         std::cerr << "Image::readFileBMP(3): invalid bitmap file: " << bitmapFile << std::endl;
      }
   }

   // close the file
   std::fclose(fp);

   // Set the pixel bit map
   if (bmap != nullptr) setPixels(bmap);

   return (bmap != nullptr);
}
bool Solution::checkSwap(int p1, int p2)
{

	return checkSwap(processes_[p1], processes_[p2]);

}