void PDFGenerator::pdfDoc_AddPage(mlPDF::PAGE_SIZES pageSize, mlPDF::PAGE_DIRECTIONS pageDirection)
{
  if (pdfDocument)
  {
    HPDF_Page newPage = HPDF_AddPage(pdfDocument);

    if (newPage)
    {
      pdfDocCurrentPage = newPage;
      pdfDocPages.push_back(newPage);

      if (pageSize == mlPDF::PAGESIZE_USER) // It is illegal to set this value manually...
      {
        pageSize = mlPDF::PAGESIZE_A4; // ... therefore set it to default value.
      }

      HPDF_Page_SetSize(newPage, (HPDF_PageSizes)pageSize, (HPDF_PageDirection)pageDirection);
      _currentPageSize = pageSize;

      HPDF_Page_SetFontAndSize(pdfDocCurrentPage, _currentFontSettings.font, _currentFontSettings.fontSize);

      HPDF_Page_SetLineWidth(pdfDocCurrentPage, 1);
      pdfDoc_SetStrokeColor(0, 0, 0);
      pdfDoc_SetFillColor(0, 0, 0);

      pdfDoc_CurrentXPos = 0;
      pdfDoc_CurrentYPos = 0;
    }
    else
    {
      _handleError("pdfDoc_AddPage(mlPDF::PAGE_SIZES pageSize, mlPDF::PAGE_DIRECTIONS pageDirection)");
    }
  }
}
void PDFGenerator::pdfDoc_AddPage(float width, float height)
{
  if (pdfDocument)
  {
    HPDF_Page newPage = HPDF_AddPage(pdfDocument);

    if (newPage)
    {
      pdfDocCurrentPage = newPage;
      pdfDocPages.push_back(newPage);

      HPDF_Page_SetWidth(newPage, width);
      HPDF_Page_SetHeight(newPage, height);
      _currentPageSize = mlPDF::PAGESIZE_USER;

      HPDF_Page_SetFontAndSize(pdfDocCurrentPage, _currentFontSettings.font, _currentFontSettings.fontSize);

      HPDF_Page_SetLineWidth(pdfDocCurrentPage, 1);
      pdfDoc_SetStrokeColor(0, 0, 0);
      pdfDoc_SetFillColor(0, 0, 0);

      pdfDoc_CurrentXPos = 0;
      pdfDoc_CurrentYPos = 0;
    }
    else
    {
      _handleError("pdfDoc_AddPage(float width, float height)");
    }

  }
}
void PDFGenerator::pdfDoc_AddImage(float x, float y, float width, float height, mlPDF::IMAGE image, bool ignoreMargins)
{
  if ((pdfDocCurrentPage) && (image))
  {
    HPDF_Rect imageRect = _getPageRect(x, y, width, height, ignoreMargins);

    HPDF_STATUS result = HPDF_Page_DrawImage(pdfDocCurrentPage, image, imageRect.left, imageRect.bottom, width, height);
  
    if (result != HPDF_OK)
    {
      _handleError("pdfDoc_AddImage(float x, float y, float width, float height, mlPDF::IMAGE image, bool ignoreMargins)");
    }
  }
}
////////////////////// _genieEnqueueEvent ///////////////////
//
// Copy the bytes from a buffer supplied by the caller 
// to the input queue 
//
// Parms:	uint8_t * data, a pointer to the user's data
//
// Returns:	TRUE if there was an empty location in the queue
//				to copy the data into
//			FALSE if not
// Sets:	ERROR_REPLY_OVR if there was no room in the queue
//
bool _genieEnqueueEvent (uint8_t * data) {

	if (_genieEventQueue.n_events < MAX_GENIE_EVENTS-2) {
		memcpy (&_genieEventQueue.frames[_genieEventQueue.wr_index], data, 
				GENIE_FRAME_SIZE);
		_genieEventQueue.wr_index++;
		_genieEventQueue.wr_index &= MAX_GENIE_EVENTS -1;
		_genieEventQueue.n_events++;
		return TRUE;
	} else {
		_genieError = ERROR_REPLY_OVR;
		_handleError();
		return FALSE;
	}
}
ML_START_NAMESPACE


//----------------------------------------------------------------------------------

mlPDF::IMAGE PDFGenerator::pdfDoc_LoadImageFromFile(std::string filename)
{
  mlPDF::IMAGE newImage = NULL;

  if (pdfDocument)
  {
    const unsigned int filenameLength = static_cast<unsigned int>(filename.length());

    std::string last4 = "";

    if ((filenameLength > 4) && (mlPDF::fileExists(filename)))
    {
      last4 = filename.substr(filenameLength - 4, 4);
      mlPDF::PDFTools::stringLower(last4);

      if (last4 == ".png")
      {
        newImage = HPDF_LoadPngImageFromFile(pdfDocument, filename.c_str());
      }
      else if ((last4 == ".jpg") || (last4 == ".jpeg"))
      {
        newImage = HPDF_LoadJpegImageFromFile(pdfDocument, filename.c_str());
      }

      if (newImage)
      {
        pdfDocImages.push_back(newImage);
      }
      else
      {
        _handleError("pdfDoc_LoadImageFromFile(std::string filename)");
      }

    }  // if ( (filenameLength > 4) && (fileExists(filename)) )

  } // if (pdfDocument)

  return newImage;
}
void PDFGenerator::pdfDoc_AddImage(float x, float y, float width, float height, std::string imageFilename, bool ignoreMargins)
{
  if (pdfDocCurrentPage)
  {
    HPDF_Rect imageRect = _getPageRect(x, y, width, height, ignoreMargins);

    HPDF_Image image = pdfDoc_LoadImageFromFile(imageFilename);

    if (image)
    {
      HPDF_STATUS result = HPDF_Page_DrawImage(pdfDocCurrentPage, image, imageRect.left, imageRect.bottom, width, height);

      if (result != HPDF_OK)
      {
        _handleError("pdfDoc_AddImage(float x, float y, float width, float height, std::string imageFilename, bool ignoreMargins)");
      }
    }
  }
}
Exemplo n.º 7
0
void ESP_RestClient::m_resolveHostname() {
	LOG("About to resolve hostname of: %s\n", m_hostname.c_str());
	if (m_serverIP.addr != 0) {
		_connect();
		return;
	}
	int rc = espconn_gethostbyname(&m_conn, m_hostname.c_str(), &m_serverIP,
			dnsFoundCallback);
	if (rc == ESPCONN_OK) {
		LOG("We immediately have an IP address\n");
		_connect();
		return;
	}
	if (rc != ESPCONN_INPROGRESS) {
		_handleError();
		return;
	}
	return;
} // End of m_resolveHostname
Exemplo n.º 8
0
void TActionSevenZip::_prepAndLaunchRedirectedChild(String drive,
        HANDLE hChildStdOut, HANDLE hChildStdErr)
{
	f_Action->log(_("COMMAND:"));
	f_Action->log( ecc::QuoteStr(PGlobals->ExePath7Zip) + " " + _commandLineGui );
	String cmdLine = ecc::QuoteStr(PGlobals->ExePath7Zip) + " " + _commandLine;

    PROCESS_INFORMATION pi;
    STARTUPINFO si;

    ZeroMemory(&si, sizeof(STARTUPINFO));
    si.cb = sizeof(STARTUPINFO);
    si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE; //SW_SHOW;
    si.hStdOutput = hChildStdOut;
    si.hStdError  = hChildStdErr;

	// Create the child process:
	BOOL bFuncRetn = CreateProcess(
        NULL,
        cmdLine.c_str(),    // command line
        NULL,          	    // process security attributes
        NULL,          	    // primary thread security attributes
        TRUE,         	    // fInheritHandles: handles are inherited
        CREATE_NEW_CONSOLE, // creation flags
        NULL,          	    // use parent's environment
        drive.c_str(),	    // lpCurrentDirectory - Required for 7z(a).exe.
        &si,	            // STARTUPINFO pointer
        &pi	  	            // receives PROCESS_INFORMATION
		);

	if (bFuncRetn == NULL)
	{
		f_Action->logError(__FUNC__ ": CreateProcess() failed: " + abtGetLastErrorStr());
	}

    // Set global child process handle to cause threads to exit.
    hChildProcess = pi.hProcess;

    // Close any unnecessary handles.
    if (!CloseHandle(pi.hThread)) _handleError(__FUNC__ " [CloseHandle9]");
}
////////////////////// _genieWaitForIdle ////////////////////////
//
// Wait for the link to become idle or for the timeout period, 
// whichever comes first.
//
void _genieWaitForIdle (void) {
	uint16_t do_event_result;
	long timeout = millis() + _genieTimeout;

	for ( ; millis() < timeout;) {
		do_event_result = genieDoEvents();

		// if there was a character received from the 
		// display restart the timeout because doEvents
		// is in the process of receiving something
		if (do_event_result == GENIE_EVENT_RXCHAR) {
			timeout = millis() + _genieTimeout;
		}
		
		if (_genieGetLinkState() == GENIE_LINK_IDLE) {
			return;
		}
	}
	_genieError = ERROR_TIMEOUT;
	_handleError();
	return;
}
Exemplo n.º 10
0
void TActionSevenZip::_call7ZipExe(String drive)
{
    FORMACTION = f_Action;

	if (_error || _aborted) return;

	// Requires _destFileName(?) and _fileListFile.
 	if (!FileExists(_fileListFile))
	{
		_error = true;
		f_Action->logError(__FUNC__ ": "
            + ASPF(_("Could not find temporary filelist: %s"),
                ecc::QuoteStr(_fileListFile)) );
		return;
	}

    f_Action->logB( ASPF(_("Calling 7-Zip executable for drive: %s"), ecc::QuoteStr(drive)), 1);
    f_Action->log( ASPF(_("Using temporary filelist (%s): %s"),
        ( PGlobals->OEMlistfile7Zip ? String("OEM") : String("UTF-8") ), ecc::QuoteStr(_fileListFile)) );
    hChildProcess = NULL;

    HANDLE hOutputReadTmp, hOutputRead, hOutputWrite;
    HANDLE hErrorWrite;
    SECURITY_ATTRIBUTES sa;

    // Set up the security attributes struct.
    sa.nLength = sizeof(SECURITY_ATTRIBUTES);
    sa.lpSecurityDescriptor = NULL;
    sa.bInheritHandle = TRUE;

    // Create the child output pipe.
    if (!CreatePipe(&hOutputReadTmp, &hOutputWrite, &sa, 0))
        _handleError(__FUNC__ " [CreatePipe1]");

    // Create a duplicate of the output write handle for the std error
    // write handle. This is necessary in case the child application
    // closes one of its std output handles.
    if (!DuplicateHandle(GetCurrentProcess(), hOutputWrite,
            GetCurrentProcess(), &hErrorWrite, 0,
            TRUE, DUPLICATE_SAME_ACCESS)
        ) _handleError(__FUNC__ " [DuplicateHandle0]");

    // Create new output read handle and the input write handles. Set
    // the Properties to FALSE. Otherwise, the child inherits the
    // properties and, as a result, non-closeable handles to the pipes
    // are created.
    if (!DuplicateHandle(GetCurrentProcess(), hOutputReadTmp,
            GetCurrentProcess(),
            &hOutputRead, // Address of new handle.
            0, FALSE, // Make it uninheritable.
            DUPLICATE_SAME_ACCESS)
        ) _handleError(__FUNC__ " [DuplicateHandle1]");

    // Close inheritable copies of the handles you do not want to be inherited:
    if (!CloseHandle(hOutputReadTmp)) _handleError(__FUNC__ " [CloseHandle1]");

	// Start 7-Zip console process:
    _prepAndLaunchRedirectedChild(drive, hOutputWrite, hErrorWrite);

    // Close pipe handles (do not continue to modify the parent).
    // You need to make sure that no handles to the write end of the
    // output pipe are maintained in this process or else the pipe will
    // not close when the child process exits and the ReadFile will hang.
    if (!CloseHandle(hOutputWrite)) _handleError(__FUNC__ " [CloseHandle3]");
    if (!CloseHandle(hErrorWrite)) _handleError(__FUNC__ " [CloseHandle5]");

    // Launch the thread that reads the child's output:
    HANDLE hThread;
    DWORD ThreadId;
    __threadIsRunning = true;
    hThread = CreateThread(NULL, 0, _readOutputThread, (LPVOID)hOutputRead, 0, &ThreadId);
    if (hThread == NULL)
    {
        __threadIsRunning = false;
        _handleError(__FUNC__ " [Could not create thread]");
        _error = true;
    }

    while (__threadIsRunning)
    {
        if (_aborted)
        {
            f_Action->logWarn(_("Terminating 7-Zip process..."), 1);
            Application->ProcessMessages();
            if (TerminateProcess(hChildProcess, 0) != 0)
                f_Action->logWarn(_("Ok."), -1, 1);
            else
            {
                f_Action->logWarn(_("Failed.") + " "
                    + abtGetLastErrorStr(), -1, 1);
            }
            break;
        }

        // Prevent Abakt from using all the CPU cycles by calling Sleep():
        Sleep(2); // Sleep for 0.002 sec, this will give the 7z.exe process more CPU cycles.
        // Notify user that Abakt is busy:
        f_Action->lbl_ProgressInfo->Caption =
            ASPF(_("File %d/%d [%s]"),
                __compressingFileCount, FORMACTION->abtFileList->incFilesTotalCount,
                f_Action->timeLapse.AsStr() );
        Application->ProcessMessages();
    }

    if (!CloseHandle(hOutputRead))
        _handleError(__FUNC__ " [CloseHandle7]");

    // Tell the thread to exit and wait for thread to die:
    if (WaitForSingleObject(hThread, 1000) == WAIT_FAILED)
        _handleError(__FUNC__ " [WaitForSingleObject]");

    _deleteFileListFile();

	f_Action->logNotif( ASPF(_("Elapsed Time: %s"), TranslateHMS(f_Action->timeLapse.AsString())) );
}
///////////////////////// genieDoEvents /////////////////////////
//
// This is the heart of the Genie comms state machine.
//
uint16_t genieDoEvents (void) {
	uint8_t c;
	static uint8_t	rx_data[6];
	static uint8_t	checksum = 0;

	c = _genieGetchar();

	////////////////////////////////////////////
	//
	// If there are no characters to process and we have 
	// queued events call the user's handler function.
	//
	if (_genieError == ERROR_NOCHAR) {
		if (_genieEventQueue.n_events > 0) (_genieUserHandler)();
		return GENIE_EVENT_NONE;
	}
	
	///////////////////////////////////////////
	//
	// Main state machine
	//
	switch (_genieGetLinkState()) {
		case GENIE_LINK_IDLE:
			switch (c) {
				case GENIE_REPORT_EVENT:
				// event frame out of the blue, set the link state
				// and fall through to the frame-accumulate code
				// at the end of this function
				_geniePushLinkState(GENIE_LINK_RXEVENT);
				break;
					
				default:
				// error, bad character, no other character 
				// is acceptable in this state
				return GENIE_EVENT_RXCHAR;
				
			}
			break;
				
		case GENIE_LINK_WFAN:
			switch (c) {

				case GENIE_ACK:
					_geniePopLinkState();
					return GENIE_EVENT_RXCHAR;

				case GENIE_NAK:
					_geniePopLinkState();
					_genieError = ERROR_NAK;
					_handleError();
					return GENIE_EVENT_RXCHAR;
			
				case GENIE_REPORT_EVENT:
					// event frame out of the blue while waiting for an ACK
					// save/set the link state and fall through to the 
					// frame-accumulate code at the end of this function
					_geniePushLinkState(GENIE_LINK_RXEVENT);
					break;

				case GENIE_REPORT_OBJ:
				default:
					// error, bad character
					return GENIE_EVENT_RXCHAR;	
			}
			break;

		case GENIE_LINK_WF_RXREPORT: // waiting for the first byte of a report
			switch (c) {
			
				case GENIE_REPORT_EVENT:
				// event frame out of the blue while waiting for the first
				// byte of a report frame
				// save/set the link state and fall through to the
				// frame-accumulate code at the end of this function
				_geniePushLinkState(GENIE_LINK_RXEVENT);
				break;

				case GENIE_REPORT_OBJ:
				// first byte of a report frame
				// replace the GENIE_LINK_WF_RXREPORT link state 
				// with GENIE_LINK_RXREPORT to indicate that we
				// are now receiving a report frame
				_geniePopLinkState();
				_geniePushLinkState(GENIE_LINK_RXREPORT);
				break;

				case GENIE_ACK:
				case GENIE_NAK:
				default:
				// error, bad character
				return GENIE_EVENT_RXCHAR;
//				break;
			}

		case GENIE_LINK_RXREPORT:		// already receiving report
		case GENIE_LINK_RXEVENT:		// already receiving event
		default:
			break;
		
	}

	///////////////////////////////////////////////////////
	// We get here if we are in the process of receiving 
	// a report or event frame. Accumulate GENIE_FRAME_SIZE 
	// bytes into a local buffer then queue them as a frame
	// into the event queue
	//
	if (_genieGetLinkState() == GENIE_LINK_RXREPORT || \
		_genieGetLinkState() == GENIE_LINK_RXEVENT) {
			
		checksum = (rxframe_count == 0) ? c : checksum ^ c;

		rx_data[rxframe_count] = c;

		if (rxframe_count == GENIE_FRAME_SIZE -1) {
			// all bytes received, if the CS is good 
			// queue the frame and restore the link state
			if (checksum == 0) {
				_genieEnqueueEvent(rx_data);
				rxframe_count = 0;
				// revert the link state to whatever it was before
				// we started accumulating this frame
				_geniePopLinkState();
				return GENIE_EVENT_RXCHAR;
			} else {
				_genieError = ERROR_BAD_CS;
				_handleError();
			}	
		}
		rxframe_count++;
		return GENIE_EVENT_RXCHAR;
	}
}