00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "stdafx.h"
00028
00029 #include "iapi.h"
00030 #include <shellapi.h>
00031 #include <errno.h>
00032 #include <stdio.h>
00033 #include "Helpers.h"
00034 #include <io.h>
00035
00036 #ifdef CC_PDF_CONVERTER
00037 #define PRODUCT_NAME "CC PDF Converter"
00038 #elif EXCEL_TO_PDF
00039 #define PRODUCT_NAME "Excel to PDF Converter"
00040 #else
00041 #error "One of the printer types must be defined"
00042 #endif
00043
00044 #ifdef _DEBUG
00046 FILE* pSave = NULL;
00047 #endif
00049 FILE* fileInput;
00051 char cBuffer[MAX_PATH * 2 + 1];
00053 int nBuffer = 0;
00055 int nInBuffer = 0;
00057 #define MAX_ERR 1023
00059 char cErr[MAX_ERR + 1];
00060
00061 #ifdef _DEBUG
00062
00068 static void WriteOutput(const char* pBefore, const char* buf, int len)
00069 {
00070
00071 int n = len + strlen(pBefore);
00072 char* pStr = new char[n + 1];
00073
00074 sprintf_s(pStr, n + 1, "%s%.*s", pBefore, len, buf);
00075
00076 ::OutputDebugString(pStr);
00077
00078 delete [] pStr;
00079 }
00080 #endif
00081
00083
00087 bool ExistsAsFolder(LPCTSTR pPath)
00088 {
00089 WIN32_FILE_ATTRIBUTE_DATA data;
00090 if (!::GetFileAttributesEx(pPath, GetFileExInfoStandard, &data))
00091 return false;
00092 if (data.dwFileAttributes & (FILE_ATTRIBUTE_OFFLINE|FILE_ATTRIBUTE_SPARSE_FILE|FILE_ATTRIBUTE_TEMPORARY))
00093 return false;
00094
00095 return ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
00096 }
00098
00099 #define TEMP_FILENAME "ccprint_"
00100 #define TEMP_EXTENSION "pdf"
00101
00102 void CleanTempFiles ()
00103 {
00104 char sTempFolder[MAX_PATH];
00105 GetTempPath(MAX_PATH, sTempFolder);
00106
00107 char sFileToFind[MAX_PATH];
00108 sprintf_s (sFileToFind, "%s%s*.%s", sTempFolder, TEMP_FILENAME, TEMP_EXTENSION);
00109
00110 struct _finddata64i32_t finddata;
00111 intptr_t hFind = _findfirst (sFileToFind, &finddata);
00112 int ret = (int)hFind;
00113 char sFullFilename[MAX_PATH];
00114
00115 while (ret != -1) {
00116 sprintf_s (sFullFilename, "%s%s", sTempFolder, finddata.name);
00117 DeleteFile (sFullFilename);
00118 ret = _findnext (hFind, &finddata);
00119 }
00120
00121 _findclose (hFind);
00122 }
00123
00124
00126
00134 static int GSDLLCALL my_in(void *instance, char *buf, int len)
00135 {
00136
00137 int ch;
00138 int count = 0;
00139 char* pStart = buf;
00140
00141 while (count < len)
00142 {
00143
00144 if (nBuffer > nInBuffer)
00145
00146 ch = cBuffer[nInBuffer++];
00147 else
00148
00149 ch = fgetc(fileInput);
00150 if (ch == EOF)
00151
00152 return 0;
00153
00154 *buf++ = ch;
00155 count++;
00156 if (ch == '\n')
00157
00158 break;
00159 }
00160 #ifdef _DEBUG
00161
00162 WriteOutput("", pStart, count);
00163 if (pSave != NULL)
00164 {
00165
00166 fwrite(pStart, 1, count, pSave);
00167 }
00168 #endif
00169
00170 return count;
00171 }
00172
00180 static int GSDLLCALL my_out(void *instance, const char *str, int len)
00181 {
00182 #ifdef _DEBUG
00183
00184 fwrite(str, 1, len, stdout);
00185 fflush(stdout);
00186
00187 WriteOutput("OUT: ", str, len);
00188 #endif
00189
00190
00191 return len;
00192 }
00193
00201 static int GSDLLCALL my_err(void *instance, const char *str, int len)
00202 {
00203 #ifdef _DEBUG
00204
00205 fwrite(str, 1, len, stderr);
00206 fflush(stderr);
00207
00208 WriteOutput("ERR: ", str, len);
00209 #endif
00210
00211 int nAdd = min(len, (int)(MAX_ERR - strlen(cErr)));
00212 strncat_s(cErr, str, MAX_ERR);
00213
00214 return len;
00215 }
00216
00221 void CleanInput()
00222 {
00223 char cBuffer[1024];
00224 while (fread(cBuffer, 1, 1024, fileInput) > 0)
00225 ;
00226 }
00227
00232 void CenterWindow(HWND hWnd)
00233 {
00234
00235 RECT rcWnd;
00236 ::GetWindowRect(hWnd, &rcWnd);
00237 RECT rcCenter, rcArea;
00238
00239 ::SystemParametersInfo(SPI_GETWORKAREA, NULL, &rcArea, NULL);
00240 rcCenter = rcArea;
00241
00242 int WndWidth = rcWnd.right - rcWnd.left;
00243 int WndHeight = rcWnd.bottom - rcWnd.top;
00244
00245
00246 int xLeft = (rcCenter.left + rcCenter.right) / 2 - WndWidth / 2;
00247 int yTop = (rcCenter.top + rcCenter.bottom) / 2 - WndHeight / 2;
00248
00249
00250 if (xLeft < rcArea.left)
00251 xLeft = rcArea.left;
00252 else if(xLeft + WndWidth > rcArea.right)
00253 xLeft = rcArea.right - WndWidth;
00254
00255 if(yTop < rcArea.top)
00256 yTop = rcArea.top;
00257 else if(yTop + WndHeight > rcArea.bottom)
00258 yTop = rcArea.bottom - WndHeight;
00259
00260
00261 ::SetWindowPos(hWnd, NULL, xLeft, yTop, -1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
00262 }
00263
00271 UINT_PTR CALLBACK SaveDlgCallback(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
00272 {
00273
00274 switch (uMsg)
00275 {
00276 case WM_NOTIFY:
00277
00278 {
00279 LPNMHDR pNotify = (LPNMHDR)lParam;
00280 if (pNotify->code == CDN_INITDONE)
00281 {
00282
00283 HWND hParent = ::GetParent(hDlg);
00284 ::BringWindowToTop(hParent);
00285 SetForegroundWindow(hParent);
00286 CenterWindow(hParent);
00287 }
00288 }
00289 break;
00290 }
00291 return FALSE;
00292 }
00293
00295 const char* ARGS[] =
00296 {
00297 "PS2PDF",
00298 "-dNOPAUSE",
00299 "-dBATCH",
00300 "-dSAFER",
00301 "-sDEVICE=pdfwrite",
00302 "-sOutputFile=c:\\test.pdf",
00303 "-I.\\",
00304 "-c",
00305 ".setpdfwrite",
00306 "-"
00307 };
00308
00317 int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
00318 {
00319
00320 char cPath[MAX_PATH + 1];
00321 char cFile[MAX_PATH + 128];
00322 char cInclude[3 * MAX_PATH + 7];
00323 cErr[0] = '\0';
00324
00325 #ifdef _DEBUG
00326
00327 errno_t file_err = fopen_s (&pSave, "c:\\test.ps", "w+b");
00328 #endif
00329
00330
00331 CleanTempFiles();
00332
00333
00334 if (::GetModuleFileName(NULL, cPath, MAX_PATH))
00335 {
00336
00337 char* pPos = strrchr(cPath, '\\');
00338 if (pPos != NULL)
00339 *(pPos) = '\0';
00340 else
00341 cPath[0] = '\0';
00342
00343 sprintf_s (cInclude, sizeof(cInclude), "-I%s\\urwfonts;%s\\lib", cPath, cPath);
00344 ARGS[6] = cInclude;
00345 }
00346
00347 #ifdef _DEBUG_CMD
00348
00349 fileInput = fopen("c:\\test1.ps", "rb");
00350 #else
00351
00352 fileInput = stdin;
00353 #endif
00354
00355
00356 cPath[0] = '\0';
00357 bool bAutoOpen = false;
00358 bool bMakeTemp = false;
00359
00360 nBuffer = fread(cBuffer, 1, MAX_PATH * 2, fileInput);
00361 cBuffer[nBuffer] = EOF;
00362
00363
00364 if ((nBuffer > 8) && (strncmp(cBuffer, "%%File: ", 8) == 0))
00365 {
00366
00367 char ch;
00368 int nCount = 0;
00369 nInBuffer += 8;
00370 do
00371 {
00372 ch = cBuffer[nInBuffer++];
00373 if (ch == EOF)
00374 break;
00375 if (ch == '\n')
00376 break;
00377 cPath[nCount++] = ch;
00378 } while (true);
00379
00380 if (ch == EOF)
00381 {
00382
00383 return 0;
00384 }
00385
00386
00387 cPath[nCount] = '\0';
00388
00389
00390 if (strcmp(cPath, ":dropfile:") == 0)
00391 {
00392
00393 CleanInput();
00394 return 0;
00395 }
00396
00397 sprintf_s(cFile, sizeof(cFile), "-sOutputFile=%s", cPath);
00398 ARGS[5] = cFile;
00399 #ifdef _DEBUG
00400
00401 WriteOutput("FILENAME: ", cPath, nCount);
00402 #endif
00403 }
00404
00405 if ((nBuffer - nInBuffer > 14) && ((!strncmp(cBuffer + nInBuffer, "%%FileAutoOpen", 14)) || (!strncmp(cBuffer + nInBuffer, "%%CreateAsTemp", 14))))
00406 {
00407
00408 if (!strncmp(cBuffer + nInBuffer, "%%CreateAsTemp", 14))
00409 bMakeTemp = true;
00410
00411 nInBuffer += 14;
00412 bAutoOpen = true;
00413 while ((cBuffer[nInBuffer] != EOF) && (cBuffer[nInBuffer] != '\n'))
00414 nBuffer++;
00415 if (cBuffer[nInBuffer] == EOF)
00416 {
00417
00418 return 0;
00419 }
00420 }
00421
00422
00423 if (cPath[0] == '\0')
00424 {
00425
00426 if (bMakeTemp) {
00427 char sTempFolder[MAX_PATH];
00428 GetTempPath(MAX_PATH, sTempFolder);
00429 sprintf_s (cPath, MAX_PATH, "%s%s%u.%s", sTempFolder, TEMP_FILENAME, GetTickCount(), TEMP_EXTENSION);
00430
00431 HANDLE test = CreateFile (cPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL);
00432 if (test == INVALID_HANDLE_VALUE) {
00433
00434 bMakeTemp = false;
00435 }
00436 else {
00437 CloseHandle (test);
00438 sprintf_s (cFile, sizeof(cFile), "-sOutputFile=%s", cPath);
00439 ARGS[5] = cFile;
00440 }
00441 }
00442
00443
00444
00445 if (!bMakeTemp) {
00446
00447 OPENFILENAME info;
00448 memset(&info, 0, sizeof(info));
00449 info.lStructSize = sizeof(info);
00450 info.hInstance = hInstance;
00451 info.lpstrFilter = "PDF Files (*.pdf)\0*.pdf\0All Files (*.*)\0*.*\0\0";
00452 info.lpstrFile = cPath;
00453 info.nMaxFile = MAX_PATH + 1;
00454 info.lpstrTitle = "Select a filename to write into";
00455 info.Flags = OFN_ENABLESIZING|OFN_EXPLORER|OFN_NOREADONLYRETURN|OFN_OVERWRITEPROMPT|OFN_PATHMUSTEXIST|OFN_ENABLEHOOK;
00456 info.lpstrDefExt = "pdf";
00457
00458 info.lpfnHook = SaveDlgCallback;
00459
00460 if (GetSaveFileName(&info))
00461 {
00462
00463 sprintf_s (cFile, sizeof(cFile), "-sOutputFile=%s", cPath);
00464 ARGS[5] = cFile;
00465 #ifdef _DEBUG
00466
00467 WriteOutput("FILENAME (USER): ", cPath, strlen(cPath));
00468 #endif
00469 }
00470 else
00471 {
00472
00473 CleanInput();
00474 return 0;
00475 }
00476 }
00477 }
00478
00479
00480 void* pGS;
00481 if (gsapi_new_instance(&pGS, NULL) < 0)
00482 {
00483
00484 return -1;
00485 }
00486
00487
00488 if (gsapi_set_stdio(pGS, my_in, my_out, my_err) < 0)
00489 {
00490
00491 gsapi_delete_instance(pGS);
00492 return -2;
00493 }
00494
00495
00496 int nRet = gsapi_init_with_args(pGS, sizeof(ARGS)/sizeof(char*), (char**)ARGS);
00497
00498 gsapi_exit(pGS);
00499 gsapi_delete_instance(pGS);
00500
00501 #ifdef _DEBUG
00502
00503 fclose(pSave);
00504 #endif
00505 #ifdef _DEBUG_CMD
00506
00507 fclose(fileInput);
00508 #endif
00509
00510
00511 if (strlen(cErr) > 0)
00512 {
00513
00514 MessageBox(NULL, cErr, PRODUCT_NAME, MB_ICONERROR|MB_OK);
00515 return 0;
00516 }
00517
00518
00519 if (bAutoOpen && CanOpenPDFFiles()) {
00520
00521 ShellExecute(NULL, NULL, cPath, NULL, NULL, SW_NORMAL);
00522 }
00523
00524
00525 return 0;
00526 }