Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1960 lines
60 KiB

  1. /************************************************************/
  2. /* Windows Write, Copyright 1985-1992 Microsoft Corporation */
  3. /************************************************************/
  4. /* trans2.c -- Save routines for WRITE (also see trans4.c; routines were
  5. moved because of compiler heap overflows) */
  6. #define NOWINMESSAGES
  7. #define NOVIRTUALKEYCODES
  8. #define NOWINSTYLES
  9. #define NOCLIPBOARD
  10. #define NOGDICAPMASKS
  11. #define NOSYSMETRICS
  12. #define NOMENUS
  13. #define NOKEYSTATE
  14. #define NOSYSCOMMANDS
  15. #define NORASTEROPS
  16. #define NOSHOWWINDOW
  17. //#define NOATOM
  18. #define NOCREATESTRUCT
  19. #define NODRAWTEXT
  20. //#define NOGDI
  21. //#define NOMETAFILE
  22. #define NOBRUSH
  23. #define NOPEN
  24. #define NOFONT
  25. #define NOWNDCLASS
  26. #define NOWH
  27. #define NOWINOFFSETS
  28. #define NOICON
  29. #define NOCOMM
  30. #define NOSOUND
  31. #include <windows.h>
  32. #include "mw.h"
  33. #include "doslib.h"
  34. #include "propdefs.h"
  35. #define NOUAC
  36. #include "cmddefs.h"
  37. #include "docdefs.h"
  38. #include "filedefs.h"
  39. #include "fkpdefs.h"
  40. #include "printdef.h"
  41. #include "code.h"
  42. #include "heapdefs.h"
  43. #include "heapdata.h"
  44. #define NOSTRUNDO
  45. #include "str.h"
  46. #include "debug.h"
  47. #include "fontdefs.h"
  48. #include "obj.h"
  49. #include "winddefs.h"
  50. CHAR *PchGetPn();
  51. CHAR *PchFromFc();
  52. typePN PnAlloc();
  53. struct PGTB **HpgtbGet();
  54. #ifdef DEBUG
  55. /* Make these variables during debug so testers can force limits */
  56. typeFC fcBound = (pnMaxScratch >> 2) * ((typeFC)cbSector);
  57. int cpBound = 512;
  58. #else
  59. #define fcBound ((typeFC) ((pnMaxScratch >> 2) * ((typeFC)cbSector)))
  60. #define cpBound (512)
  61. #endif
  62. extern CHAR *PchFillPchId();
  63. extern int vfOldWriteFmt; /* delete objects before saving */
  64. extern HANDLE hParentWw;
  65. extern int vfnWriting;
  66. extern struct BPS *mpibpbps;
  67. extern typeTS tsMruBps;
  68. extern int vibpWriting;
  69. extern CHAR (**vhrgbSave)[];
  70. extern struct DOD (**hpdocdod)[];
  71. extern int docCur;
  72. extern int docMac;
  73. extern int docScrap;
  74. extern int docUndo;
  75. extern struct FCB (**hpfnfcb)[];
  76. extern int fnMac;
  77. extern int wwMac;
  78. /* extern ENV vEnvMainLoop; */
  79. extern int vfBuffersDirty;
  80. extern int vfDiskFull;
  81. extern int vfDiskError;
  82. extern typeCP vcpFetch;
  83. extern CHAR *vpchFetch;
  84. extern int vccpFetch;
  85. extern typeFC fcMacPapIns;
  86. extern typeFC fcMacChpIns;
  87. extern typeCP vcpLimParaCache;
  88. extern struct FKPD vfkpdCharIns;
  89. extern struct FKPD vfkpdParaIns;
  90. extern struct PAP vpapPrevIns;
  91. extern struct PAP vpapAbs;
  92. extern struct PAP *vppapNormal;
  93. extern struct CHP vchpNormal;
  94. extern struct CHP vchpInsert;
  95. extern struct CHP vchpFetch;
  96. extern struct FPRM fprmCache;
  97. extern HCURSOR vhcIBeam;
  98. extern int ferror;
  99. extern CHAR szExtBackup[]; /* extension for Write backup files */
  100. extern CHAR szExtWordBak[]; /* extension for Word backup files */
  101. extern CHAR (**hszTemp)[];
  102. extern CHAR szExtDoc[];
  103. #ifdef INTL /* International version */
  104. extern int vfTextOnlySave;
  105. extern int vWordFmtMode; /* used during saves. If false, no conversion is
  106. done. True is convert to Word format,CVTFROMWORD
  107. is translate chars from Word character set at
  108. save */
  109. #endif /* International version */
  110. /*** CmdXfSave - Save document to passed filename (TRANSFER SAVE)
  111. *
  112. * ENTRY: szFile - a normalized filename
  113. * fFormatted - TRUE = save as formatted file
  114. * FALSE = save as unformatted file
  115. * fBackup - TRUE = keep a backup copy of the file
  116. * FALSE = don't (but see below)
  117. *
  118. * EXIT:
  119. *
  120. * NOTE: A backup file may be kept even if fBackup is FALSE.
  121. * This is because piece tables in documents other than docCur
  122. * may refer to the information. If a backup file is kept for
  123. * this reason, the following holds: (1) The file has an fn,
  124. * (2) the file's hpfnfcb entry has its fDelete field set to TRUE,
  125. * indicating that the file should be deleted when WORD exits,
  126. * (3) the file is, in fact, referenced in some document
  127. * (PurgeTemps assures that unreferenced files are deleted)
  128. *
  129. * Note 2: Non-formatted save code modified (bz 12/2/85) to allow
  130. * similar saving in Word format. The calls to FnCreate and
  131. * FWriteFn were changed to allow for formatted saving, but
  132. * FWriteFn is changed to convert text from ANSI to OEM character
  133. * sets, to not put out the font table and to mark
  134. * the file header (fib) so the file will be treated as a Word
  135. * file, but, like a non-formatted save, the piece table is not
  136. * cleaned up. Note that the backup file is saved in the original,
  137. * Write format.
  138. *
  139. */
  140. CmdXfSave(szFile, fFormatted, fBackup, hcAfterward)
  141. CHAR szFile[];
  142. int fFormatted;
  143. int fBackup;
  144. HCURSOR hcAfterward; /* handle to cursor to display after hourglass */
  145. {
  146. extern int vfnSaving; /* Set this so we prompt for "Write Save File"
  147. if disk change is necessary */
  148. extern int vfDiskError;
  149. extern int vfSysFull;
  150. extern int vfOutOfMemory;
  151. int fSave = vfDiskError;
  152. int fDidBackup;
  153. CHAR szFileT [cchMaxFile];
  154. int fOutOfMemory=vfOutOfMemory;
  155. int docTemp;
  156. /* Make a local copy of string parm in case it is in the heap */
  157. #ifdef DFILE
  158. CommSzSz("CmdXfSave----szFile (presumed ANSI) ", szFile);
  159. #endif
  160. StartLongOp();
  161. bltsz( szFile, szFileT );
  162. szFileT[ cchMaxFile - 1] = '\0';
  163. /* Reset error conditions to give us a chance */
  164. vfOutOfMemory = vfDiskFull = vfSysFull = vfDiskError = ferror = FALSE;
  165. SetRfnMac( rfnMacSave ); /* Increase # of DOS handles used to speed save */
  166. /* Memory kludge: To assure that we can actually save this file under low
  167. memory conditions, free the bogus heap block */
  168. if (vhrgbSave != 0)
  169. {
  170. FreeH(vhrgbSave); /* Return this memory to free pool. */
  171. vhrgbSave = 0;
  172. }
  173. /* In-Line version of FreeBitmapCache (so we don't have to swap in
  174. picture.c) */
  175. /*FreeBitmapCache(); */ /* Give us even more memory */
  176. {
  177. extern int vdocBitmapCache;
  178. extern HBITMAP vhbmBitmapCache;
  179. vdocBitmapCache = docNil;
  180. if (vhbmBitmapCache != NULL)
  181. {
  182. DeleteObject( vhbmBitmapCache );
  183. vhbmBitmapCache = NULL;
  184. }
  185. }
  186. /* Can't undo save -- set "Can't Undo"; also clear out docUndo
  187. for heap space reclamation and to aid PurgeTemps */
  188. ClobberDoc(docUndo, docNil, cp0, cp0);
  189. NoUndo();
  190. (**hpdocdod)[docCur].fBackup = fBackup;
  191. /* Note: Non-formatted save code modified (bz 12/2/85) to allow
  192. * similar saving in Word format. The calls to FnCreate and
  193. * FWriteFn were changed to allow for formatted saving, but
  194. * FWriteFn is changed to convert text from ANSI to OEM character
  195. * sets, to not put out the font table and to mark
  196. * the file header (fib) so the file will be treated as a Word
  197. * file, but, like a non-formatted save, the piece table is not
  198. * cleaned up. Note that the backup file is saved in the original,
  199. * Write format.
  200. * Note that a file CAN be saved both unformatted and in Word format
  201. * - in that case, Word format means convert to OEM character set.
  202. */
  203. #if defined(OLE)
  204. ObjSavingDoc(fFormatted);
  205. #endif
  206. if (((**hpdocdod)[docCur].fFormatted && !fFormatted)
  207. #ifdef INTL /* International version */
  208. || (vWordFmtMode == TRUE) /* convert To Word format? */
  209. #endif /* International version */
  210. || (vfOldWriteFmt))
  211. {
  212. int fn;
  213. CHAR (**hsz)[];
  214. CHAR szT [cchMaxFile];
  215. CHAR szWriteFile [cchMaxFile];
  216. CHAR szBak [cchMaxFile];
  217. /* Set szFileT's path name into szWriteFile, so the temp file
  218. gets created in the right place when we call FnCreateSz */
  219. SplitSzFilename( szFileT, szWriteFile, szT );
  220. /* Create szWrite: a new, uniquely named file */
  221. if ((fn=FnCreateSz( szWriteFile,
  222. fFormatted?(**hpdocdod)[docCur].cpMac:cpNil,
  223. dtyNetwork ))==fnNil)
  224. /* Couldn't create the write file */
  225. goto SaveDone;
  226. /* Make backup of szFileT (if it exists); purge all unneeded backups
  227. which were kept for their pieces but are no longer referenced */
  228. fDidBackup = FBackupSzFile( szFileT, fBackup, szBak );
  229. if (ferror)
  230. goto LXFRet; /* Backup failed */
  231. PurgeTemps();
  232. vfnSaving = fn;
  233. /* ForcePmt(IDPMTSaving);*/
  234. #ifdef INTL /* International version */
  235. if ((vWordFmtMode == TRUE) /* converting To Word format? */
  236. || (vfTextOnlySave == TRUE) /* converting To Text format? */
  237. || (vfOldWriteFmt))
  238. /* Delete all pictures. To do this, make a copy
  239. of docCur in docTemp, the go through docTemp, deleting all picture
  240. paragraphs. Write out this document, then kill it. */
  241. {
  242. extern typeCP vcpLimParaCache, vcpFirstParaCache;
  243. extern typeCP cpMinCur, cpMacCur, cpMinDocument;
  244. typeCP cpMinCurT = cpMinCur;
  245. typeCP cpMacCurT = cpMacCur;
  246. typeCP cpMinDocumentT = cpMinDocument;
  247. typeCP cpNow;
  248. typeCP cpLimPara, dcp;
  249. typeCP cpMac = (**hpdocdod) [docCur].cpMac;
  250. /* Create copy of document */
  251. docTemp = DocCreate(fnNil, HszCreate(""), dtyNormal);
  252. if (docTemp == docNil)
  253. goto SaveDone; /* Out of memory */
  254. ClobberDoc(docTemp, docCur, cp0, CpMacText(docCur));
  255. if (ferror)
  256. return TRUE;
  257. /* Expand range of interest to whole document (for CachePara) */
  258. cpMinCur = cp0;
  259. cpMacCur = cpMac;
  260. /* Loop on paras */
  261. for ( cpNow = cp0; cpNow < cpMac; cpNow = cpLimPara )
  262. {
  263. CachePara( docTemp, cpNow );
  264. if (!vpapAbs.fGraphics)
  265. /* update to next cplim only if not deleting. If deleting,
  266. next time will be at same cp */
  267. {
  268. cpLimPara = vcpLimParaCache;
  269. continue;
  270. }
  271. /* Now delete graphics paragraph */
  272. Replace(docTemp, vcpFirstParaCache,
  273. dcp = (vcpLimParaCache - vcpFirstParaCache),
  274. fnNil, fc0, fc0);
  275. cpMac -= dcp; /* size of doc has been reduced */
  276. }
  277. /* Restore cpMinCur, cpMacCur */
  278. cpMinCur = cpMinCurT;
  279. cpMacCur = cpMacCurT;
  280. /* destroyed possibly by DocCreate */
  281. cpMinDocument = cpMinDocumentT;
  282. vcObjects = 0; // OLE object count
  283. }
  284. else
  285. #endif /* International version */
  286. {
  287. docTemp = docCur; // note the else, above #endif (2.7.91) D. Kent
  288. }
  289. if (FWriteFn(fn, docTemp, fFormatted))
  290. {
  291. int fpe = FpeRenameFile( szWriteFile, szFileT );
  292. if ( FIsErrFpe( fpe ) )
  293. { /* Rename failed -- might be nonexistent path */
  294. Error( (fpe == fpeBadPathError) ? IDPMTNoPath : IDPMTSDE2 );
  295. }
  296. else
  297. OutSaved(docTemp);
  298. }
  299. else
  300. { /* Write failed */
  301. if (fDidBackup && !FIsErrFpe(FpeRenameFile(szBak, szFileT)))
  302. {
  303. int fn = FnFromSz(szFileT);
  304. struct FCB *pfcb;
  305. if (fn != fnNil)
  306. {
  307. (pfcb = &(**hpfnfcb)[fn])->fDelete = false;
  308. }
  309. }
  310. FDeleteFn( fn );
  311. }
  312. #ifdef INTL /* International version */
  313. if (vWordFmtMode == TRUE) /* converting To Word format? */
  314. KillDoc (docTemp);
  315. #endif /* International version */
  316. (**hpdocdod)[docCur].fDirty = false; /* document should not
  317. be dirty after T-S.*/
  318. FreeH((**hpdocdod)[docCur].hszFile);
  319. hsz = HszCreate((PCH)szFileT);
  320. (**hpdocdod)[docCur].hszFile = hsz;
  321. }
  322. else
  323. { /* Save Formatted document */
  324. CleanDoc( docCur, (**hpdocdod)[docCur].dty, szFileT, fFormatted, fBackup );
  325. }
  326. OpenEveryHardFn(); /* Reopen files on nonremoveable media so other net
  327. users can't steal them away */
  328. SaveDone:
  329. SetRfnMac( rfnMacEdit ); /* Reduce # of file handles used */
  330. vfnSaving = fnNil;
  331. #ifdef NEVER
  332. /* It doesn't do us much good to Assert here that an error didn't happen.
  333. After-the-fact checking doesn't make up for ignoring real-time errors.
  334. ..pault 10/31/89 */
  335. Assert( !vfOutOfMemory ); /* Our reserved space block was sufficient
  336. to get us through the save */
  337. #endif
  338. vfOutOfMemory = fOutOfMemory;
  339. /* vhrgbSave is a pointer a clump on the heap used during the save operation.
  340. By freeing it in the beginning of the save operation, we are assured
  341. that we will have enough memory to actually do the save.
  342. At the present point
  343. in the code, we are finished with the save and wish to reclaim vhrgbSave
  344. so the next Save operation may perform properly. The net memory usage
  345. caused by a save should be minimal. It only temporarily requires a
  346. significant chunk of memory. */
  347. vhrgbSave = (CHAR (**)[]) HAllocate( cwSaveAlloc +
  348. ( (wwMac-1) * cwHeapMinPerWindow ));
  349. /* Restore previous disk err state */
  350. #if WINVER >= 0x300
  351. /* We currently have no way to FORGET they had a disk error.
  352. So if this whole operation did not have an error and we
  353. felt there was one beforehand, we do it now ..pault */
  354. vfDiskError = (!vfDiskError && fSave) ? fFalse : fSave;
  355. #else
  356. vfDiskError = fSave;
  357. #endif
  358. LXFRet:
  359. EndLongOp(hcAfterward);
  360. }
  361. CleanDoc(doc, dty, szFile, fFormatted, fBackup )
  362. int doc, fFormatted;
  363. int dty;
  364. CHAR szFile[];
  365. int fBackup;
  366. { /* Write the contents of doc into szFile and clean up piece table */
  367. /* if dty == dtyNetwork, writes the doc to a unique file & returns the */
  368. /* filename through szFile */
  369. /* Returns the fn of the file it wrote to */
  370. /* *************************************
  371. In the normal backup processing, we rename the existing file. When
  372. saving in CONVFROMWORD mode, we want to keep the original Word file around,
  373. so we bypass the backup phase and then skip the renaming phase below.
  374. This leaves the original file around. At save time we may write over it
  375. or rename the saving file, as we wish, but this way we have the Word file
  376. around in case we don't save out of Write.
  377. ************************************** */
  378. extern int vdocParaCache;
  379. extern int vfnSaving; /* Set this so we prompt for "Save" disk if disk
  380. changes are necessary */
  381. int fDidBackup=FALSE;
  382. int fn;
  383. CHAR (**hsz)[];
  384. CHAR szBak [cchMaxFile];
  385. CHAR szWrite [cchMaxFile];
  386. int fDummy;
  387. #if WINVER >= 0x300
  388. /* I don't understand WHY the following resets to ROOT directory, but
  389. changing it causes problems -- so I'm leaving it! Obviously code
  390. somewhere else expects file to be there and I don't see it ..pault */
  391. #endif
  392. /* Set path name of szFile into szWrite so the temp file gets created
  393. in the right place */
  394. if (dty == dtyNetwork)
  395. {
  396. szWrite [0] = '\0'; /* Create temp file on Temp drive in the root */
  397. }
  398. else
  399. {
  400. CHAR szT [cchMaxFile];
  401. SplitSzFilename( szFile, szWrite, szT );
  402. }
  403. /* Create szWrite: a new, uniquely named file */
  404. if ((fn=FnCreateSz( szWrite, fFormatted ? (**hpdocdod)[doc].cpMac : cpNil,
  405. dtyNetwork )) == fnNil)
  406. /* Couldn't create the write file */
  407. return fnNil;
  408. vfnSaving = fn;
  409. /* *************************************
  410. In the normal backup processing, we rename the existing file. When
  411. saving in CONVFROMWORD mode, we want to keep the original Word file around,
  412. so we bypass the backup phase and then skip the renaming phase below.
  413. This leaves the original file around. At save time we may write over it
  414. or rename the saving file, as we wish, but this way we have the Word file
  415. around in case we don't save out of Write.
  416. ************************************** */
  417. if (doc != docScrap)
  418. {
  419. /* Make a backup of szFile (if szFile exists) */
  420. #ifdef INTL /* International version */
  421. if (vWordFmtMode == CONVFROMWORD) /* converting from a Word document */
  422. fDidBackup = false;
  423. else
  424. #endif /* International version */
  425. {
  426. fDidBackup = FBackupSzFile( szFile, fBackup, szBak );
  427. if (ferror)
  428. return; /* Backup failed */
  429. }
  430. PurgeTemps();
  431. }
  432. if ( dty == dtyNetwork )
  433. bltsz( szWrite, szFile );
  434. if (!FWriteFn(fn, doc, fFormatted))
  435. { /* Save failed; rename backup file back to original */
  436. /* note in intl CONVFROMWORD case, fDidBackup will be false
  437. and this renaming won't happen, which is ok */
  438. if (fDidBackup && !FIsErrFpe(FpeRenameFile(szBak, szFile)))
  439. {
  440. int fn = FnFromSz(szFile);
  441. struct FCB *pfcb;
  442. if (fn != fnNil)
  443. {
  444. (pfcb = &(**hpfnfcb)[fn])->fDelete = false;
  445. }
  446. }
  447. FDeleteFn( fn );
  448. return fnNil; /* Disk full or write error */
  449. }
  450. /* *************************************
  451. Here we rename our temp file szWrite to have the name of the "save as"
  452. file. In FBackupSzFile, the fdelete flag for the fn of szfile was set
  453. on so we don't have to explicitly delete the original file.
  454. When a CONVFROMWORD save is done, we do not rename and did not go
  455. through the backup procedure, so the original Word file is still
  456. out there with its original name. We do, however, set the delete bit to
  457. true, so the file will get deleted after the next save, when a true rename
  458. will be done.
  459. ************************************** */
  460. if ( dty != dtyNetwork )
  461. #ifdef INTL /* International version */
  462. if (vWordFmtMode == CONVFROMWORD) /* converting from a Word document */
  463. {
  464. (**hpfnfcb)[fn].fDelete = true;
  465. }
  466. else
  467. #endif /* International version */
  468. {
  469. int fpe=FpeRenameFile( szWrite, szFile );
  470. if (FIsErrFpe( fpe ))
  471. {
  472. Error( (fpe == fpeBadPathError) ? IDPMTNoPath : IDPMTSDE2 );
  473. return fnNil;
  474. }
  475. }
  476. if (doc == docScrap)
  477. (**hpfnfcb)[fn].fDelete = true;
  478. else
  479. OutSaved(doc);
  480. FreeH((**hpdocdod)[doc].hpctb); /* Free old piece table */
  481. FInitPctb(doc, fn);
  482. (**hpdocdod)[doc].fFormatted = fFormatted;
  483. FreeH((**hpdocdod)[doc].hszFile);
  484. hsz = HszCreate((PCH)szFile);
  485. (**hpdocdod)[doc].hszFile = hsz;
  486. if (fFormatted)
  487. {
  488. /* reload font table, which may have changed */
  489. SmashDocFce(doc);
  490. FreeFfntb(HffntbGet(doc));
  491. (**hpdocdod)[doc].hffntb = HffntbCreateForFn(fn, &fDummy);
  492. //SaveFontProfile(doc);
  493. ResetDefaultFonts(FALSE);
  494. }
  495. /* By diddling the document attributes on save, we have invalidated caches */
  496. InvalidateCaches( doc );
  497. vdocParaCache = docNil;
  498. if (!ferror && !vfBuffersDirty && doc != docScrap)
  499. ReduceFnScratchFn( fn );
  500. if (fFormatted)
  501. { /* Readjust the running head margins. We munged them back to the
  502. paper-relative measurements in FWriteFn, so we'll remunge them
  503. to margin-relative here. */
  504. /* With the test for doc != docScrap we fix a major bug, and knowingly at the
  505. last minute introduce a minor one. The old problem was that we would apply
  506. a sprm to docScrap, then mash the scratch file in ReduceFnScratch, our
  507. caller, rendering the bfprm bogus, causing rare crashes. The new problem
  508. will be that running head text in the scrap will have incorrect margins
  509. after a save. */
  510. if (doc != docScrap)
  511. ApplyRHMarginSprm( doc );
  512. InvalidateCaches( doc );
  513. vdocParaCache = docNil;
  514. }
  515. return fn;
  516. }
  517. /*** FWriteFn - write a file
  518. *
  519. * Note: Modified (bz 12/2/85) to allow saving in Word format.
  520. * FWriteFn is changed to convert text from ANSI to OEM character
  521. * sets, to not put out the font table and to mark
  522. * the file header (fib) so the file will be treated as a Word
  523. * file.
  524. *
  525. */
  526. int FWriteFn(fn, doc, fFormatted)
  527. int fn, doc, fFormatted;
  528. { /* Write characters from a doc to fn */
  529. /* Return true if successful */
  530. #ifdef CASHMERE
  531. extern int docBuffer;
  532. #endif
  533. typeCP cpMac;
  534. CHAR *pchFprop;
  535. struct RUN *prun;
  536. struct FIB *pfib;
  537. struct FCB *pfcb;
  538. int cchT;
  539. struct FNTB **hfntb;
  540. struct FFNTB **hffntb=(struct FFNTB **)NULL;
  541. #ifdef CASHMERE
  542. struct SETB **hsetb = 0, **hsetbT;
  543. #else
  544. struct SEP **hsep;
  545. #endif
  546. struct PGTB **hpgtb;
  547. int ised, csed;
  548. struct SED *psed, *psedT;
  549. int cw;
  550. int fFileFlushed;
  551. int rfn;
  552. CHAR mpftcftc[iffnMax];
  553. struct PAP pap;
  554. struct CHP chp;
  555. struct FKP fkp;
  556. vfnWriting = fn;
  557. vibpWriting = IbpEnsureValid(fn, (typePN)0);
  558. cpMac = (**hpdocdod)[doc].cpMac;
  559. /* FIB has already been written. */
  560. if (fFormatted)
  561. {
  562. /* KLUDGE: If the doc does not contain at least one complete para,
  563. and it has a nonnull tab table,
  564. add an Eol to its end to hold the tabs in a FPAP */
  565. CachePara( doc, cp0 );
  566. if (vcpLimParaCache > cpMac && (doc != docScrap) &&
  567. (**hpdocdod) [doc].hgtbd != NULL )
  568. {
  569. extern int vdocParaCache;
  570. InsertEolInsert( doc, cpMac );
  571. vdocParaCache = docNil;
  572. cpMac = (**hpdocdod) [doc].cpMac;
  573. }
  574. /* Write characters */
  575. /* Modified to handle ANSI to OEM conversion for Word docs */
  576. FetchCp(doc, cp0, 0, fcmChars + fcmNoExpand);
  577. {
  578. Scribble(4, 'T');
  579. while (vcpFetch < cpMac && !(vfDiskFull || vfDiskError))
  580. {
  581. #ifdef INTL /* International version */
  582. if (vWordFmtMode == FALSE) /* no conversion */
  583. #endif /* International version */
  584. WriteRgch(fn, vpchFetch, (int)vccpFetch);
  585. #ifdef INTL /* International version */
  586. else
  587. {
  588. /* bufT is a buffer for translating from ANSI to
  589. OEM chars. The amount of data from FetchCp
  590. must be no > than a disk page, which is
  591. cfcFetch, which is itself cbSector. We use
  592. bufT to hold the translated chars, then write
  593. them out with WriteRgch. */
  594. CHAR bufT[cbSector + 1];
  595. CHAR *pch;
  596. Assert ((int)vccpFetch <= cbSector);
  597. /* load chars into bufT and translate to OEM
  598. chars, and write out */
  599. pch = (CHAR *) bltbyte(vpchFetch, bufT,
  600. (int)vccpFetch);
  601. *pch = '\0';
  602. if (vWordFmtMode == TRUE)
  603. /* from Write/ANSI to Word/OEM */
  604. AnsiToOem((LPSTR)bufT, (LPSTR)bufT);
  605. else
  606. /* from Word/OEM to Write/ANSI */
  607. OemToAnsi((LPSTR)bufT, (LPSTR)bufT);
  608. WriteRgch(fn, bufT, (int)vccpFetch);
  609. }
  610. #endif /* International version */
  611. FetchCp(docNil, cpNil, 0, fcmChars + fcmNoExpand);
  612. }
  613. Scribble(4,' ');
  614. }
  615. if ((vfDiskFull || vfDiskError))
  616. goto AbortWrite;
  617. /* Go to beginning of next page */
  618. AlignFn(fn, cbSector, false);
  619. (**hpfnfcb)[fn].pnChar = (**hpfnfcb)[fn].pnMac;
  620. /* Now write char props */
  621. Scribble(4, 'C');
  622. fkp.fcFirst = cfcPage; /* first fkp starts with first valid fc */
  623. fkp.crun = 0;
  624. prun = (struct RUN *) fkp.rgb;
  625. pchFprop = &fkp.rgb[cbFkp];
  626. CachePara(doc, cp0);
  627. /* set up font mapping and new font table */
  628. if (!FInitMapSave(doc, &hffntb, mpftcftc))
  629. goto AbortWrite;
  630. FetchCp(doc, cp0, 0, fcmProps);
  631. if (!FMapFtcSave(doc, hffntb, &vchpFetch, mpftcftc))
  632. goto AbortWrite;
  633. blt(&vchpFetch, &chp, cwCHP);
  634. while (vcpFetch < cpMac && !(vfDiskFull || vfDiskError))
  635. { /* This could be optimized by allowing multiple runs to point */
  636. /* to one fchp. */
  637. if (CchDiffer(&vchpFetch, &chp, cchCHP) != 0)
  638. {
  639. FAddRun(fn, &fkp, &pchFprop, &prun, &chp,
  640. &vchpNormal, cchCHP, vcpFetch + cfcPage);
  641. blt(&vchpFetch, &chp, cwCHP);
  642. }
  643. FetchCp(docNil, cpNil, 0, fcmProps);
  644. if (!FMapFtcSave(doc, hffntb, &vchpFetch, mpftcftc))
  645. goto AbortWrite;
  646. }
  647. Scribble(4,' ');
  648. if ((vfDiskFull || vfDiskError))
  649. goto AbortWrite;
  650. /* Write out last char run. */
  651. FAddRun(fn, &fkp, &pchFprop, &prun, &chp, &vchpNormal,
  652. cchCHP, cpMac + cfcPage);
  653. WriteRgch(fn, &fkp, cbSector);
  654. if ((vfDiskFull || vfDiskError))
  655. goto AbortWrite;
  656. /* Now write para runs; one for every para */
  657. Scribble(4,'C');
  658. (**hpfnfcb)[fn].pnPara = (**hpfnfcb)[fn].pnMac;
  659. fkp.fcFirst = cfcPage; /* first fkp starts with first valid fc */
  660. fkp.crun = 0;
  661. prun = (struct RUN *) fkp.rgb;
  662. pchFprop = &fkp.rgb[cbFkp];
  663. CachePara(doc, cp0);
  664. /* KLUDGE: We have running head indents relative to the
  665. margins -- subtract out the margins now, because
  666. our (WORD-compatible) file format is PAPER-relative */
  667. if (vpapAbs.rhc)
  668. {
  669. struct SEP *psep = *(**hpdocdod)[ doc ].hsep;
  670. vpapAbs.dxaLeft += psep->xaLeft;
  671. vpapAbs.dxaRight += psep->xaMac -
  672. (psep->xaLeft + psep->dxaText);
  673. }
  674. #ifdef INTL
  675. /* Ensure no pictures in Word documents. This is necessary
  676. because Word 4.0 uses the fGraphics bit as part of the
  677. new border type code (btc) property. */
  678. if (vWordFmtMode == CONVFROMWORD)
  679. vpapAbs.fGraphics = FALSE;
  680. #endif
  681. FAddRun(fn, &fkp, &pchFprop, &prun, &vpapAbs, vppapNormal,
  682. cchPAP, vcpLimParaCache + cfcPage);
  683. blt(&vpapAbs, &pap, cwPAP);
  684. while (vcpLimParaCache <= cpMac && !(vfDiskFull || vfDiskError))
  685. {
  686. CachePara(doc, vcpLimParaCache);
  687. /* KLUDGE: We have running head indents relative to the
  688. margins -- subtract out the margins now, because
  689. our (WORD-compatible) file format is PAPER-relative */
  690. if (vpapAbs.rhc)
  691. {
  692. struct SEP *psep = *(**hpdocdod)[ doc ].hsep;
  693. vpapAbs.dxaLeft += psep->xaLeft;
  694. vpapAbs.dxaRight += psep->xaMac -
  695. (psep->xaLeft + psep->dxaText);
  696. }
  697. #ifdef INTL
  698. /* Ensure no pictures in Word documents. This is necessary
  699. because Word 4.0 uses the fGraphics bit as part of the
  700. new border type code (btc) property. */
  701. if (vWordFmtMode == CONVFROMWORD)
  702. vpapAbs.fGraphics = FALSE;
  703. #endif
  704. #ifdef BOGUS
  705. /* this would have erased all tab setting if saving back a Word document */
  706. /* For MEMO: the only tabs we write are in the first para run;
  707. override all other tab tables to keep files compact */
  708. if (vpapAbs.rgtbd [0].dxa != 0)
  709. bltc( vpapAbs.rgtbd, 0, cwTBD * itbdMax );
  710. #endif
  711. FAddRun(fn, &fkp, &pchFprop, &prun, &vpapAbs, vppapNormal,
  712. FParaEq( &vpapAbs, &pap ) ? -cchPAP : cchPAP,
  713. vcpLimParaCache + cfcPage);
  714. blt(&vpapAbs, &pap, cwPAP);
  715. }
  716. WriteRgch(fn, &fkp, cbSector);
  717. Scribble(4,' ');
  718. if ((vfDiskFull || vfDiskError))
  719. goto AbortWrite;
  720. /* Output footnote table */
  721. Scribble(4,'F');
  722. (**hpfnfcb)[fn].pnFntb = (**hpfnfcb)[fn].pnMac;
  723. #ifdef FOOTNOTES /* In MEMO, we NEVER write a footnote table */
  724. if ((hfntb = HfntbGet(doc)) != 0)
  725. {
  726. WriteRgch(fn, *hfntb,
  727. ((**hfntb).cfnd * cwFND + cwFNTBBase) * sizeof (int));
  728. AlignFn(fn, cbSector, false);
  729. }
  730. Scribble(4,' ');
  731. if ((vfDiskFull || vfDiskError))
  732. goto AbortWrite;
  733. #endif /* FOOTNOTES */
  734. #ifdef CASHMERE /* Output section properties, table from hsetb */
  735. /* Output section properties */
  736. Scribble(4,'S');
  737. (**hpfnfcb)[fn].pnSep = (**hpfnfcb)[fn].pnMac;
  738. if ((hsetb = HsetbGet(doc)) != 0)
  739. { /* Write out section props */
  740. cw = cwSETBBase + (**hsetb).csedMax * cwSED;
  741. csed = (**hsetb).csed;
  742. hsetbT = (struct SETB **) HAllocate(cw);
  743. if (FNoHeap(hsetbT))
  744. return false; /* SHOULD REALLY GOTO ABORTWRITE */
  745. blt(*hsetb, *hsetbT, cw);
  746. FreezeHp();
  747. for (psed = &(**hsetb).rgsed[0], psedT = &(**hsetbT).rgsed[0],
  748. ised = 0;
  749. ised < csed; psed++, psedT++, ised++)
  750. if (psed->fc != fcNil)
  751. { /* Copy props to file and update setb */
  752. int cch;
  753. pchFprop = PchFromFc(psed->fn, psed->fc, &cch);
  754. Assert(cch >= *pchFprop + 1);
  755. psedT->fn = fn;
  756. AlignFn(fn, cch = *pchFprop + 1, false);
  757. psedT->fc = (**hpfnfcb)[fn].fcMac;
  758. WriteRgch(fn, pchFprop, cch);
  759. }
  760. MeltHp();
  761. AlignFn(fn, cbSector, false);
  762. }
  763. Scribble(4,' ');
  764. if ((vfDiskFull || vfDiskError))
  765. goto AbortWrite;
  766. /* Output section table */
  767. (**hpfnfcb)[fn].pnSetb = (**hpfnfcb)[fn].pnMac;
  768. if (hsetb != 0)
  769. {
  770. if (csed != 1 || (**hsetb).rgsed[0].fc != fcNil)
  771. {
  772. WriteRgch(fn, *hsetbT,
  773. ((**hsetb).csed * cwSED + cwSETBBase) * sizeof (int));
  774. AlignFn(fn, cbSector, false);
  775. }
  776. }
  777. #else /* MEMO VERSION: Write out a section table, 1 element long,
  778. if we had nonstandard section properties */
  779. {
  780. typeFC fcSect;
  781. /* Output section properties */
  782. fcSect = (long)( cfcPage *
  783. ((**hpfnfcb)[fn].pnSep = (**hpfnfcb)[fn].pnMac));
  784. if ((hsep = (**hpdocdod)[doc].hsep) != 0)
  785. {
  786. struct {
  787. CHAR cch;
  788. struct SEP sep;
  789. } fsep;
  790. fsep.cch = cchSEP;
  791. blt( *hsep, &fsep.sep, cwSEP );
  792. WriteRgch( fn, &fsep, sizeof( fsep ) );
  793. AlignFn( fn, cbSector, false );
  794. }
  795. if ((vfDiskFull || vfDiskError))
  796. goto AbortWrite;
  797. /* Output section table */
  798. (**hpfnfcb)[fn].pnSetb = (**hpfnfcb)[fn].pnMac;
  799. if (hsep != 0)
  800. { /* Section table has 1 real + 1 dummy entry with cp==cpMac+1
  801. This duplicates the output of PC Word 1.15 */
  802. struct {
  803. int csed;
  804. int csedMax;
  805. struct SED rgsed [2];
  806. } setb;
  807. setb.csed = setb.csedMax = 2;
  808. setb.rgsed [1].cp = 1 +
  809. (setb.rgsed [0].cp = (**hpdocdod)[doc].cpMac);
  810. setb.rgsed [0].fn = fn;
  811. setb.rgsed [0].fc = fcSect;
  812. setb.rgsed [1].fn = fnNil;
  813. setb.rgsed [1].fc = fcNil;
  814. WriteRgch( fn, &setb, sizeof( setb ) );
  815. AlignFn( fn, cbSector, false );
  816. }
  817. }
  818. #endif /* not CASHMERE */
  819. if ((vfDiskFull || vfDiskError))
  820. goto AbortWrite;
  821. /* Output buffer or page table */
  822. (**hpfnfcb)[fn].pnBftb = (**hpfnfcb)[fn].pnMac;
  823. #ifdef CASHMERE /* No docBuffer in WRITE */
  824. if(doc == docBuffer)
  825. {
  826. WriteBftb(fn);
  827. AlignFn(fn, cbSector, false);
  828. }
  829. else
  830. #endif
  831. if ((hpgtb = HpgtbGet(doc)) != 0)
  832. {
  833. WriteRgch(fn, *hpgtb,
  834. ((**hpgtb).cpgd * cwPGD + cwPgtbBase) * sizeof (int));
  835. AlignFn(fn, cbSector, false);
  836. }
  837. if ((vfDiskFull || vfDiskError))
  838. goto AbortWrite;
  839. /* Output font table */
  840. #ifdef INTL /* International version */
  841. /* no font table if saving in Word format */
  842. if (vWordFmtMode != TRUE) /* no conv or conv from word */
  843. #endif /* International version */
  844. {
  845. Scribble(4,'N');
  846. (**hpfnfcb)[fn].pnFfntb = (**hpfnfcb)[fn].pnMac;
  847. WriteFfntb(fn, hffntb); /* hffntb gets freed below */
  848. AlignFn(fn, cbSector, false);
  849. Scribble(4,' ');
  850. if ((vfDiskFull || vfDiskError))
  851. goto AbortWrite;
  852. }
  853. /* Now update FIB at beginning of file */
  854. pfib = (struct FIB *) PchGetPn(fn, pn0, &cchT, true);
  855. pfib->pnPara = (pfcb = &(**hpfnfcb)[fn])->pnPara;
  856. pfib->pnFntb = pfcb->pnFntb;
  857. pfib->pnSep = pfcb->pnSep;
  858. pfib->pnSetb = pfcb->pnSetb;
  859. pfib->pnBftb = pfcb->pnBftb;
  860. /* mark file type if objects are in there */
  861. if (vcObjects)
  862. pfib->wIdent = wOleMagic;
  863. else
  864. pfib->wIdent = wMagic;
  865. #ifdef INTL /* International version */
  866. if (vWordFmtMode != TRUE) /* saving in Write format */
  867. #endif /* International version */
  868. {
  869. pfib->pnFfntb = pfcb->pnFfntb;
  870. pfib->pnMac = pfcb->pnMac;
  871. }
  872. #ifdef INTL /* International version */
  873. else
  874. {
  875. /* in Word format there is no font table. pnFfntb is the
  876. end of the file,so is set to pnMac. The Write pnMac field
  877. is not used in Word and is set to 0. */
  878. pfib->pnFfntb = pfcb->pnMac;
  879. pfib->pnMac = 0;
  880. }
  881. #endif /* International version */
  882. pfib->fcMac = pfcb->fcMac = cpMac + cfcPage;
  883. if ((**hpdocdod)[doc].dty == dtyNormal)
  884. {
  885. CHAR (**hszSsht)[];
  886. #ifdef STYLES
  887. #ifdef DEBUG
  888. Assert((**hpdocdod)[doc].docSsht != docNil);
  889. #endif
  890. hszSsht = (**hpdocdod)[(**hpdocdod)[doc].docSsht].hszFile;
  891. #else
  892. hszSsht = HszCreate((PCH)"");
  893. #endif /* STYLES */
  894. if (!FNoHeap(hszSsht))
  895. {
  896. bltbyte(**hszSsht, pfib->szSsht, CchSz(**hszSsht));
  897. #ifndef STYLES
  898. #if WINVER >= 0x300
  899. /* Here we WERE allowing the hszSsht from just above
  900. to be overwritten by another alloc! This code
  901. really shouldn't even be here because Write knows
  902. nothing about style sheets but we want to make as
  903. few changes now as possible ..pault 2/12/90 */
  904. FreeH(hszSsht);
  905. #endif
  906. #endif
  907. hszSsht = HszCreate((PCH)pfib->szSsht); /* HEAP MOVES */
  908. #if WINVER >= 0x300
  909. /* Just in case hszSsht has already been assigned
  910. here, we'll free it up ..pault 2/12/90 */
  911. if ((**hpfnfcb)[fn].hszSsht != NULL)
  912. FreeH((**hpfnfcb)[fn].hszSsht);
  913. #endif
  914. (**hpfnfcb)[fn].hszSsht = hszSsht;
  915. }
  916. else
  917. (**hpfnfcb)[fn].hszSsht = 0;
  918. }
  919. AbortWrite:
  920. vfnWriting = fnNil;
  921. FreeFfntb(hffntb);
  922. if (vfDiskFull || vfDiskError)
  923. fFileFlushed = FALSE;
  924. else
  925. fFileFlushed = FFlushFn(fn);
  926. CloseEveryRfn( TRUE );
  927. if (!fFileFlushed) /* Writing the file has failed due to disk full */
  928. {
  929. #ifdef CASHMERE
  930. if (hsetb != 0)
  931. FreeH(hsetbT);
  932. #endif
  933. LFlushFailed:
  934. FUndirtyFn(fn); /* Undirty all of the buffer pages
  935. holding parts of the unsuccessfully
  936. written file. */
  937. return false;
  938. }
  939. if (!FMakeRunTables(fn)) /* HM */
  940. (**hpfnfcb)[fn].fFormatted = false;
  941. /* Success! */
  942. #ifdef CASHMERE
  943. if (hsetb != 0)
  944. { /* HM */
  945. FreeH(hsetb);
  946. (**hpdocdod)[doc].hsetb = hsetbT;
  947. }
  948. #endif
  949. }
  950. else
  951. {
  952. WriteUnformatted(fn, doc);
  953. vfnWriting = fnNil;
  954. if (vfDiskFull || vfDiskError)
  955. fFileFlushed = FALSE;
  956. else
  957. fFileFlushed = FFlushFn(fn);
  958. CloseEveryRfn( TRUE );
  959. if (!fFileFlushed)
  960. goto LFlushFailed;
  961. }
  962. CloseEveryRfn( TRUE ); /* Be real sure the save file is closed */
  963. /* This fixes the "pasting between Write
  964. instances with the sharer loaded" bug */
  965. return true;
  966. }
  967. /*----------------------------------------------------------------------------
  968. -- Routine: ReduceFnScratchFn
  969. -- Description: this routine is called after successful TS, TGS, GTS and
  970. !vfBuffersDirty. Its purpose is to make sure that no doc has pointers
  971. to fnScratch and that we can empty fnScratch so as to reduce program
  972. disk space
  973. -- Arguments: none
  974. -- Returns: none
  975. -- Side Effects: docScrap is cleaned up and becomes a new doc that has no
  976. pointers into fnScratch. FnScratch is emptied, all global variables
  977. associated with this new fnScratch are initialized.
  978. -- Bugs:
  979. -- History:
  980. Apr 16 '84 -- created (chic)
  981. Aug 9 '85 -- modified it so it puts the scratch file on the same disk
  982. as the passed fn to reduce disk swapping on floppy
  983. systems.
  984. ----------------------------------------------------------------------------*/
  985. ReduceFnScratchFn( fn )
  986. {
  987. int doc;
  988. struct DOD *pdod;
  989. CHAR sz[cchMaxFile];
  990. int cchT;
  991. struct FCB *pfcb;
  992. #ifdef BOGUSBL /* Because of disk switching, always worth doing */
  993. if ((**hpfnfcb)[fnScratch].fcMac > fcBound) /* worth doing */
  994. #endif
  995. {
  996. for (pdod = &(**hpdocdod)[0],doc = 0; doc < docMac; pdod++,doc++)
  997. {
  998. /* don't do anything if any of the allocated doc (hpctb != 0)
  999. except docScrap is dirty */
  1000. /* docUndo may be dirty, but it also should be empty */
  1001. if (doc != docScrap && pdod->hpctb != 0 && pdod->fDirty &&
  1002. doc != docUndo)
  1003. {
  1004. Assert(0);
  1005. return;
  1006. }
  1007. } /* end of for loop */
  1008. Assert( (**hpdocdod) [docUndo].cpMac == cp0 );
  1009. /* now no doc can possibly has pointers to fnScratch except docScrap */
  1010. pdod = &(**hpdocdod)[docScrap];
  1011. #if 0
  1012. This check was only for speed considerations. With the 64K
  1013. transcendence we *must* clean docScrap no matter what.
  1014. (7.10.91) v-dougk
  1015. if (pdod->cpMac > cpBound) /* too big to be cleaned up */
  1016. {
  1017. Assert(0);
  1018. /**
  1019. But we don't know if docScrap points into fnScratch. We only need
  1020. to clear docScrap if it points into fnScratch. We shouldn't
  1021. abort here unless we know that docScrap does indeed point into
  1022. fnScratch. If it doesn't then we needn't clear it and can proceed
  1023. to clear fnScratch.
  1024. We know that docScratch will not point to any large
  1025. OLE object data in fnScratch because object data is never put
  1026. into fnScratch at a time that the user could select it.
  1027. That is, it is unlikely that the size of docScratch will
  1028. exceed cpBound on account of object data alone.
  1029. Only the presence of large textual data in docScratch
  1030. is likely to cause this failure.
  1031. (7.10.91) v-dougk
  1032. **/
  1033. return;
  1034. }
  1035. else /* small enough to be cleaned up */
  1036. #endif
  1037. {
  1038. if (pdod->cpMac > cp0)
  1039. {
  1040. #ifdef STYLES
  1041. /* doc has to have a valid style sheet before CleanDoc */
  1042. pdod->docSsht = (**hpdocdod)[docCur].docSsht;
  1043. #endif /* STYLES */
  1044. CachePara(docScrap,cp0);
  1045. /* Save docScrap to new, unique file; name in sz */
  1046. CleanDoc( docScrap, dtyNetwork, sz, true, false );
  1047. #ifdef STYLES
  1048. (**hpdocdod)[docScrap].docSsht = docNil;
  1049. #endif /* STYLES */
  1050. }
  1051. if (!ferror) /* in case something went wrong in rename or make backup file in CleanDoc */
  1052. {
  1053. typePN pnMacScratch;
  1054. pfcb = &(**hpfnfcb)[fnScratch];
  1055. pnMacScratch = pfcb->pnMac;
  1056. ResetFn(fnScratch); /* empty FnScratch */
  1057. #ifdef DEBUG
  1058. OutputDebugString("*** Reduced scratch file ***\n\r");
  1059. #endif
  1060. /* Put the scratch file on the same disk as the save file,
  1061. to reduce disk swapping in floppy environments */
  1062. if ( (fn != fnNil) &&
  1063. !((POFSTRUCT)(**hpfnfcb) [fnScratch].rgbOpenFileBuf)->fFixedDisk )
  1064. { /* fnScratch is on removable media */
  1065. extern CHAR szExtDoc[];
  1066. CHAR szNewScratch[ cchMaxFile ];
  1067. CHAR (**hszScratch)[] = (**hpfnfcb) [fnScratch].hszFile;
  1068. CHAR chDrive = (**(**hpfnfcb) [fn].hszFile) [0];
  1069. Assert( fn != fnScratch );
  1070. if (FEnsureOnLineFn( fn ))
  1071. if (GetTempFileName( TF_FORCEDRIVE | chDrive,
  1072. (LPSTR)(szExtDoc+1), 0, (LPSTR) szNewScratch))
  1073. { /* Created new file on same disk as fn */
  1074. CHAR (**hsz)[];
  1075. CHAR szNew [cchMaxFile];
  1076. #if WINVER >= 0x300
  1077. /* Currently: FNormSzFile *TAKES* an OEM sz, and
  1078. *RETURNS* an ANSI sz ..pault */
  1079. #endif
  1080. FNormSzFile( szNew, szNewScratch, dtyNormal );
  1081. if (!FNoHeap( hsz = HszCreate(szNew)))
  1082. {
  1083. struct FCB *pfcb = &(**hpfnfcb)[fnScratch];
  1084. /* Delete old scratch file */
  1085. if (FEnsureOnLineFn( fnScratch ))
  1086. FDeleteFile( &(**hszScratch)[0] );
  1087. /* Put new scratch file back on line and
  1088. open it, so OpenFile gets its buffer info */
  1089. pfcb->hszFile = hsz;
  1090. pfcb->fOpened = FALSE;
  1091. FEnsureOnLineFn( fn );
  1092. FAccessFn( fnScratch, dtyNormal);
  1093. }
  1094. }
  1095. }
  1096. pfcb->pnMac = pnMacScratch;
  1097. /* reset all global varibales associated with an empty fnScratch */
  1098. fprmCache.cch = 0;
  1099. fcMacPapIns = 0;
  1100. fcMacChpIns = 0;
  1101. /* just in case */
  1102. pfcb->pnChar = pfcb->pnPara = pfcb->pnFntb = pfcb->pnSep = pfcb->pnSetb = pfcb->pnBftb = pn0;
  1103. vfkpdParaIns.brun = vfkpdCharIns.brun = 0;
  1104. vfkpdParaIns.bchFprop = vfkpdCharIns.bchFprop = cbFkp;
  1105. vfkpdParaIns.pn = PnAlloc(fnScratch);
  1106. ((struct FKP *)PchGetPn(fnScratch, vfkpdParaIns.pn,
  1107. &cchT, true))->fcFirst = fc0;
  1108. vfkpdCharIns.pn = PnAlloc(fnScratch);
  1109. ((struct FKP *)PchGetPn(fnScratch, vfkpdCharIns.pn,
  1110. &cchT, true))->fcFirst = fc0;
  1111. vfkpdParaIns.ibteMac = vfkpdCharIns.ibteMac = 0;
  1112. blt(&vchpNormal, &vchpInsert, cwCHP);
  1113. blt(vppapNormal, &vpapPrevIns, cwPAPBase + cwTBD);
  1114. } /* end of ferror */
  1115. else
  1116. Assert(0);
  1117. } /* end of small enough to be cleaned up */
  1118. } /* end of worth doing */
  1119. } /* end of ReduceFnScratchFn */
  1120. ResetFn(fn)
  1121. { /* make fn look as if no characters have been written */
  1122. /* but don't try to reuse fn (in that case, must rehash) */
  1123. int ibp;
  1124. register struct BPS *pbps;
  1125. struct FCB *pfcb;
  1126. Assert( fn != fnNil );
  1127. (pfcb = &(**hpfnfcb)[fn])->fcMac = fc0;
  1128. pfcb->pnMac = pn0;
  1129. for (ibp = 0, pbps = &mpibpbps [0]; ibp < ibpMax; ++ibp, ++pbps)
  1130. { /* find all buffer pages and "clear" them */
  1131. if (pbps->fn == fn)
  1132. {
  1133. #ifdef CKSM
  1134. #ifdef DEBUG
  1135. extern unsigned (**hpibpcksm) [];
  1136. if (!pbps->fDirty)
  1137. Assert( (**hpibpcksm) [ibp] == CksmFromIbp( ibp ) );
  1138. #endif
  1139. #endif
  1140. pbps->fDirty = false;
  1141. pbps->cch = 0;
  1142. #ifdef CKSM
  1143. #ifdef DEBUG
  1144. /* Recompute checksum to account for cch change */
  1145. (**hpibpcksm) [ibp] = CksmFromIbp( ibp );
  1146. #endif
  1147. #endif
  1148. }
  1149. }
  1150. }
  1151. /*** FBackupSzFile - Make a backup copy of the passed szFile
  1152. *
  1153. * ENTRY: szFile - the (assumed normalized) name of the
  1154. * file to back up
  1155. * fBackup - whether the user is interested in seeing
  1156. * a backup copy of szFile
  1157. * EXIT: szBak - the (normalized) name of the backup
  1158. * file is returned through here
  1159. * RETURNS: TRUE=We made a backup copy, szBak of szFile
  1160. * FALSE=We didn't/couldn't make a backup copy
  1161. *
  1162. * NOTE: We try to put the backup file into the directory used
  1163. * as the default by GetTempFileName; in general, this
  1164. * effort will succeed if the directory is on the same
  1165. * physical drive as the original.
  1166. * NOTE: we may keep around a backup copy regardless of the
  1167. * setting of fBackup, to have access to pieces in it.
  1168. * However, the file's fDelete flag will be set to TRUE, marking
  1169. * it for eventual deletion
  1170. */
  1171. FBackupSzFile( szFile, fBackup, szBak ) /* filenames taken as ANSI */
  1172. CHAR szFile[];
  1173. int fBackup;
  1174. CHAR szBak[];
  1175. { /* Copy szFile into a backup copy, and give backup old fn. */
  1176. /* Also, return name of backup file */
  1177. int fnOld;
  1178. int f;
  1179. int fDelete = false;
  1180. int rfn;
  1181. int fTryAgain=FALSE;
  1182. CHAR chDriveTempFile;
  1183. int fn;
  1184. CHAR rgbBuf[ cbOpenFileBuf ];
  1185. #ifdef ENABLE
  1186. if ((fn = FnFromSz( szFile )) != fnNil)
  1187. /* Avoid share violations while checking for existence */
  1188. CloseFn( fn );
  1189. if (OpenFile( (LPSTR) szFile, (LPOFSTRUCT) rgbBuf, OF_EXIST ) != -1)
  1190. #endif
  1191. if (FExistsSzFile( dtyAny, szFile ))
  1192. { /* File exists; make backup (even if it's on another floppy) */
  1193. int fSame;
  1194. /* szBak <-- backup file name (it'll be normalized since szFile is) */
  1195. bltsz( szFile, szBak );
  1196. #ifdef INTL /* International version */
  1197. /* if file has .WRI extension, put Write .BKP extension on,
  1198. otherwise put Word backup extension (.BAK) on instead. */
  1199. AppendSzExt( szBak, szExtDoc, TRUE );
  1200. fSame = FSzSame( szFile, szBak ); /* Whether file is .WRI */
  1201. AppendSzExt( szBak, (fSame ? szExtBackup : szExtWordBak),
  1202. TRUE );
  1203. #else
  1204. AppendSzExt( szBak, szExtBackup, TRUE );
  1205. #endif /* International version */
  1206. fSame = FSzSame( szFile, szBak ); /* Whether file is .BAK already */
  1207. Assert( szBak [1] == ':' );
  1208. chDriveTempFile = szBak[0]; /* Drive on which to create temp file */
  1209. for( ;; )
  1210. {
  1211. if (!fBackup || fSame )
  1212. { /* This is just being kept for its pieces OR the file
  1213. happens to be .BAK already: give backup a unique name */
  1214. CHAR szBakT [cchMaxFile];
  1215. if (!fTryAgain)
  1216. { /* First time through; try not forcing the drive
  1217. letter to see how we fare. The advantage is that
  1218. if we succeed, the temp file is in a more standard
  1219. place. If the rename fails, we end up at the
  1220. branch below. */
  1221. fTryAgain = TRUE; /* Try a second time if we fail */
  1222. if (!GetTempFileName( chDriveTempFile,
  1223. (LPSTR)(szExtDoc+1), 0, (LPSTR)szBakT))
  1224. continue;
  1225. }
  1226. else
  1227. { /* Second time through -- try forcing the drive */
  1228. /* Grab a temp file on the same drive as the original */
  1229. fTryAgain = FALSE; /* No more tries */
  1230. if (!GetTempFileName( chDriveTempFile | TF_FORCEDRIVE,
  1231. (LPSTR)(szExtDoc+1), 0, (LPSTR) szBakT))
  1232. {
  1233. Error( IDPMTSDE2 ); /* should probably GOTO HARDCORE instead */
  1234. return FALSE;
  1235. }
  1236. }
  1237. /* szBakT <-- Temp name in OEM */
  1238. /* szBak <-- Normalized temporary name in ANSI */
  1239. FNormSzFile( szBak, szBakT, dtyNormal );
  1240. fDelete = TRUE;
  1241. }
  1242. if ((fnOld = FnFromSz(szBak)) != fnNil)
  1243. { /* We have backup open */
  1244. CHAR szT[cchMaxFile];
  1245. FBackupSzFile( szBak, false, szT );
  1246. }
  1247. if ((fnOld = FnFromSz(szFile)) != fnNil)
  1248. { /* We have file open */
  1249. /* ? */ FFlushFn(fnOld);
  1250. CloseFn( fnOld );
  1251. }
  1252. /* Rename szFile to be temp name (must delete the empty temp file) */
  1253. if (!FDeleteFile( szBak ) || FIsErrFpe(FpeRenameFile( szFile, szBak )))
  1254. {
  1255. extern HWND vhWnd;
  1256. CHAR szT [cchMaxSz];
  1257. CHAR *pchSrc;
  1258. if (fTryAgain)
  1259. /* Failed with temp name on the default drive; try again */
  1260. continue;
  1261. /* HARDCORE FAILURE: Could not rename file */
  1262. HardCore:
  1263. /* pchSrc <-- ptr to start of filename (sans path) */
  1264. #ifdef DBCS //T-HIROYN 1992.07.13
  1265. pchSrc = &szFile [CchSz( szFile )];
  1266. while (pchSrc > szFile) {
  1267. pchSrc = AnsiPrev(szFile,pchSrc);
  1268. if (*pchSrc == '\\')
  1269. {
  1270. pchSrc++;
  1271. break;
  1272. }
  1273. }
  1274. #else
  1275. pchSrc = &szFile [CchSz( szFile ) - 1];
  1276. while (pchSrc > szFile)
  1277. if (*(--pchSrc) == '\\')
  1278. {
  1279. pchSrc++;
  1280. break;
  1281. }
  1282. #endif
  1283. Assert( pchSrc > szFile ); /* Always "X:\" in normalized name */
  1284. MergeStrings (IDPMTRenameFail, pchSrc, szT);
  1285. IdPromptBoxSz( vhWnd, szT, ErrorLevel( IDPMTRenameFail ) );
  1286. return FALSE; /* We couldn't rename this file to the backup name */
  1287. }
  1288. if (fnOld != fnNil)
  1289. { /* We had an fn for the renamed file; must update fcb */
  1290. CHAR (**hszBak)[];
  1291. struct FCB *pfcb;
  1292. FreeH((**hpfnfcb)[fnOld].hszFile); /* HM */
  1293. hszBak = HszCreate((PCH)szBak);
  1294. pfcb = &(**hpfnfcb)[fnOld];
  1295. pfcb->hszFile = hszBak;
  1296. pfcb->fDelete = fDelete;
  1297. }
  1298. else if (!fBackup)
  1299. { /* Delete this NOW. (!fBackup indicates that the user
  1300. doesn't care about the backup copy, and Word can't care
  1301. about its pieces since it doesn't have an fn. So nobody
  1302. wants it, so we get rid of it) */
  1303. FDeleteFile( szBak );
  1304. return false;
  1305. }
  1306. return true;
  1307. } /* end for( ;; ) */
  1308. } /* end if (FExists... */
  1309. return false; /* file did not exist; make no backup */
  1310. }
  1311. int FExistsSzFile(dty, szFile)
  1312. int dty;
  1313. CHAR szFile[];
  1314. { /* Return true iff file exists */
  1315. CHAR rgbBuf[ 128 ]; /* Buffer used by OpenFile */
  1316. int bRetval;
  1317. #ifdef DEBUG
  1318. {
  1319. int junk;
  1320. Assert(FValidFile(szFile, CchSz(szFile)-1, &junk));
  1321. }
  1322. #endif /*DEBUG*/
  1323. /* Use FnFromSz to avoid share violations on files we have open */
  1324. if (FnFromSz( szFile ) != fnNil)
  1325. return TRUE;
  1326. SetErrorMode(1);
  1327. bRetval = OpenFile( (LPSTR) szFile, (LPOFSTRUCT) rgbBuf, OF_EXIST ) != -1;
  1328. SetErrorMode(0);
  1329. return bRetval;
  1330. }
  1331. struct PGTB **HpgtbGet(doc)
  1332. int doc;
  1333. { /* Return hpgtb if doc has one, 0 if none */
  1334. struct DOD *pdod;
  1335. if ((pdod = &(**hpdocdod)[doc])->dty != dtyNormal)
  1336. return 0;
  1337. else
  1338. return pdod->hpgtb;
  1339. }
  1340. FreeFn( fn )
  1341. int fn;
  1342. { /* Forget about the existence of file fn. Assumes no document holds
  1343. pieces from fn. Frees all heap items in (**hpfnfcb) [fn] and
  1344. marks the fcb as free (rfn == rfnNil).
  1345. */
  1346. #define IibpHash(fn,pn) ((int) ((fn + 1) * (pn + 1)) & 077777) % iibpHashMax
  1347. extern int iibpHashMax;
  1348. extern CHAR *rgibpHash;
  1349. extern struct BPS *mpibpbps;
  1350. register struct BPS *pbps;
  1351. int pn;
  1352. extern CHAR (**hszTemp)[];
  1353. register struct FCB *pfcb = &(**hpfnfcb)[fn];
  1354. CHAR (**hsz)[] = pfcb->hszFile;
  1355. CHAR (**hszSsht)[] = pfcb->hszSsht;
  1356. typeFC (**hgfcChp)[]=pfcb->hgfcChp;
  1357. typeFC (**hgfcPap)[]=pfcb->hgfcPap;
  1358. FreezeHp();
  1359. CloseFn( fn );
  1360. /* Purge buffer slots holding pages of fn; maintain integrity of hash chains */
  1361. for ( pn = 0; pn < pfcb->pnMac; pn++ )
  1362. FreeBufferPage( fn, pn );
  1363. #ifdef DEBUG
  1364. CheckIbp();
  1365. #endif
  1366. pfcb->fDelete = FALSE;
  1367. pfcb->hszFile = hszTemp;
  1368. pfcb->rfn = rfnFree;
  1369. MeltHp();
  1370. if ( hsz != hszTemp )
  1371. FreeH( hsz );
  1372. #if WINVER >= 0x300
  1373. /* Previously we allocated a small block ("") for the
  1374. style sheet but never freed it! ..pault 2/12/90 */
  1375. if (hszSsht != NULL)
  1376. FreeH(hszSsht);
  1377. #endif
  1378. if (hgfcChp)
  1379. FreeH( hgfcChp );
  1380. if (hgfcPap)
  1381. FreeH( hgfcPap );
  1382. }
  1383. /*** FUndirtyFn
  1384. *
  1385. *
  1386. */
  1387. FUndirtyFn(fn)
  1388. int fn;
  1389. /*
  1390. Description: Mark all buffer pages holding parts of this file
  1391. as non-dirty.
  1392. Called after a disk full caused writing a file
  1393. to fail (in FWriteFn).
  1394. Returns: nothing.
  1395. */
  1396. {
  1397. #ifdef CKSM
  1398. #ifdef DEBUG
  1399. extern unsigned (**hpibpcksm) [];
  1400. #endif
  1401. #endif
  1402. int ibp;
  1403. struct BPS *pbps;
  1404. for (ibp = 0, pbps = mpibpbps; ibp < ibpMax; ibp++,
  1405. pbps++)
  1406. {
  1407. if (pbps->fn == fn)
  1408. {
  1409. pbps->fDirty = FALSE;
  1410. #ifdef CKSM
  1411. #ifdef DEBUG
  1412. /* Update checksum */
  1413. (**hpibpcksm) [ibp] = CksmFromIbp( ibp );
  1414. #endif
  1415. #endif
  1416. }
  1417. }
  1418. }
  1419. /*** IbpWriting - Find buffer page while writing file
  1420. *
  1421. *
  1422. *
  1423. */
  1424. IbpWriting(fn)
  1425. int fn;
  1426. /* called when trying to find a slot in the file page "cache" buffer and */
  1427. /* vfnWriting != fnNil (currently in the process of writing some file) */
  1428. {
  1429. typeTS dTs;
  1430. int ibp;
  1431. if (fn == vfnWriting)
  1432. { /* writing a piece of the vfnWriting file */
  1433. /* vibpWriting is the previous slot used to hold file */
  1434. /* contents. Keep slots contiguous and in the first */
  1435. /* (upper) half of the buffer area. */
  1436. if (vibpWriting > 0)
  1437. { /* We may have read multiple pages last time */
  1438. /* Advance past all slots holding contiguous pages */
  1439. struct BPS *pbps=&mpibpbps[ vibpWriting ];
  1440. int pn=(pbps-1)->pn;
  1441. while ( pbps->fn == fn && pbps->pn == ++pn )
  1442. {
  1443. pbps++;
  1444. vibpWriting++;
  1445. }
  1446. vibpWriting--;
  1447. }
  1448. if (++vibpWriting >= (ibpMax >> 1))
  1449. vibpWriting = 0;
  1450. /* We must abide by the restriction that */
  1451. /* we do not clobber the cbpMustKeep most recently used */
  1452. /* slots in the process. */
  1453. dTs = tsMruBps - mpibpbps[vibpWriting].ts;
  1454. dTs = ((dTs & 0x8000) ? (~dTs + 1) : dTs);/* absolute value */
  1455. if (dTs < cbpMustKeep)
  1456. vibpWriting = ibp = IbpLru(0);
  1457. else /* adjacent slot is o.k. - not too recently used */
  1458. ibp = vibpWriting;
  1459. }
  1460. else
  1461. /* If currently writing a file (but the current page is */
  1462. /* not part of it), try to find a slot in the lower half */
  1463. /* of the buffer. This decreases the possibility that the */
  1464. /* adjacent slot to vibpWriting will be too recently used. */
  1465. ibp = IbpLru(ibpMax >> 1);
  1466. return(ibp);
  1467. }
  1468. WriteFfntb(fn, hffntb)
  1469. int fn;
  1470. struct FFNTB **hffntb;
  1471. { /* Append a font table (ffntb) to fn */
  1472. struct FFNTB *pffntb;
  1473. struct FFN *pffn;
  1474. int iffn, cbffn;
  1475. int cchPageSpace = cbSector;
  1476. int cchWrite;
  1477. int wEndOfPage = -1;
  1478. int wEndOfTable = 0;
  1479. int cbT;
  1480. pffntb = *hffntb;
  1481. cbT = pffntb->iffnMac;
  1482. WriteRgch( fn, &cbT, sizeof (int) );
  1483. cchPageSpace = cbSector - sizeof (int);
  1484. for (iffn = 0; iffn < pffntb->iffnMac; iffn++)
  1485. {
  1486. pffn = *pffntb->mpftchffn[iffn];
  1487. cchWrite = (cbffn = CbFfn(CchSz(pffn->szFfn))) + (2 * sizeof(int));
  1488. if (cchWrite > cchPageSpace)
  1489. { /* This entry will not fit on the page; start new page */
  1490. Assert( cchPageSpace >= sizeof (int ));
  1491. WriteRgch( fn, &wEndOfPage, sizeof (int) );
  1492. AlignFn( fn, cbSector, false );
  1493. cchPageSpace = cbSector;
  1494. }
  1495. Assert( cchWrite <= cchPageSpace );
  1496. #ifdef NEWFONTENUM
  1497. /* let's just pretend we never added a charset field... pault */
  1498. cbffn -= sizeof(BYTE);
  1499. WriteRgch( fn, &cbffn, sizeof (int) ); /* Write entry size in bytes */
  1500. WriteRgch( fn, &pffn->ffid, cbffn ); /* Write the entry */
  1501. #else
  1502. WriteRgch( fn, &cbffn, sizeof (int) ); /* Write entry size in bytes */
  1503. WriteRgch( fn, pffn, cbffn ); /* Write the entry */
  1504. #endif
  1505. cchPageSpace -= cbffn + sizeof (int);
  1506. }
  1507. Assert( cchPageSpace >= sizeof (int) );
  1508. WriteRgch( fn, &wEndOfTable, sizeof(int) ); /* Table is terminated with 0000 */
  1509. }
  1510. FMapFtcSave(doc, hffntb, pchp, mpftcftc)
  1511. /* attempt to map the ftc for this chp into it's new ftc in hffntb, according
  1512. to the mapping in mpftcftc. If there's no entry yet for this ftc, then
  1513. add it to the table. Returns FALSE if there was some problem */
  1514. int doc;
  1515. struct FFNTB **hffntb;
  1516. struct CHP *pchp;
  1517. CHAR *mpftcftc;
  1518. {
  1519. int ftc, ftcNew;
  1520. struct FFN *pffn;
  1521. CHAR rgbFfn[ibFfnMax];
  1522. ftc = pchp->ftc + (pchp->ftcXtra << 6);
  1523. ftcNew = mpftcftc[ftc];
  1524. if (ftcNew == ftcNil)
  1525. {
  1526. /* haven't encountered this font yet - add it to hffntb and mpftcftc */
  1527. pffn = *(*HffntbGet(doc))->mpftchffn[ftc];
  1528. bltbyte(pffn, rgbFfn, CbFromPffn(pffn));
  1529. ftcNew = FtcAddFfn(hffntb, pffn);
  1530. if (ftcNew == ftcNil)
  1531. /* some problem adding the font */
  1532. return(FALSE);
  1533. mpftcftc[ftc] = ftcNew;
  1534. }
  1535. pchp->ftc = ftcNew & 0x003f;
  1536. pchp->ftcXtra = (ftcNew & 0x00c0) >> 6;
  1537. return(TRUE);
  1538. }
  1539. FInitMapSave(doc, phffntb, mpftcftc)
  1540. /* sets up for ftc mapping */
  1541. int doc;
  1542. struct FFNTB ***phffntb;
  1543. CHAR *mpftcftc;
  1544. {
  1545. bltbc( mpftcftc, ftcNil, iffnMax );
  1546. return(FNoHeap(*phffntb = HffntbAlloc()) == FALSE);
  1547. }
  1548. /* O U T S A V E D */
  1549. OutSaved(doc)
  1550. int doc;
  1551. {
  1552. extern int docMode;
  1553. extern CHAR szMode[];
  1554. int NEAR CchExpCp( CHAR *, typeCP );
  1555. char szTmp[cchMaxSz];
  1556. LoadString(hINSTANCE, IDSTRChars, szTmp, sizeof(szTmp));
  1557. wsprintf(szMode,szTmp,(DWORD)(**hpdocdod)[doc].cpMac);
  1558. docMode = docNil;
  1559. DrawMode();
  1560. }
  1561. /* C C H E X P C P */
  1562. int NEAR CchExpCp(pch, cp)
  1563. CHAR *pch;
  1564. typeCP cp;
  1565. {
  1566. int cch = 0;
  1567. if (cp >= 10)
  1568. {
  1569. cch = CchExpCp(pch, cp / 10);
  1570. pch += cch;
  1571. cp %= 10;
  1572. }
  1573. *pch = '0' + cp;
  1574. return cch + 1;
  1575. }
  1576. #if 0
  1577. SaveFontProfile(doc)
  1578. /* updates our mru font entries in win.ini */
  1579. int doc;
  1580. {
  1581. extern CHAR szWriteProduct[];
  1582. extern CHAR szFontEntry[];
  1583. int iffn;
  1584. struct FFN *pffn;
  1585. CHAR *pchFontNumber, *pchT;
  1586. CHAR rgbProf[LF_FACESIZE + 10]; /* for good measure */
  1587. CHAR rgbFfn[ibFfnMax];
  1588. if (FInitFontEnum(doc, iffnProfMax, FALSE))
  1589. {
  1590. pffn = (struct FFN *)rgbFfn;
  1591. pchFontNumber = szFontEntry + CchSz(szFontEntry) - 2;
  1592. for (iffn = 0; iffn < iffnProfMax; iffn++)
  1593. {
  1594. if (!FEnumFont(pffn))
  1595. break;
  1596. #ifdef NEWFONTENUM
  1597. #endif
  1598. pchT = (CHAR *)bltbyte(pffn->szFfn, rgbProf, CchSz(pffn->szFfn))-1;
  1599. *pchT++ = ',';
  1600. ncvtu(pffn->ffid, &pchT);
  1601. #ifdef NEWFONTENUM
  1602. /* Save the font's charset value as well */
  1603. *pchT++ = ',';
  1604. ncvtu(pffn->chs, &pchT);
  1605. #endif
  1606. *pchT = '\0';
  1607. *pchFontNumber = '1' + iffn;
  1608. WriteProfileString((LPSTR)szWriteProduct, (LPSTR)szFontEntry,
  1609. (LPSTR)rgbProf);
  1610. }
  1611. EndFontEnum();
  1612. }
  1613. }
  1614. #endif
  1615.