home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlsrc.pak / EDITFILE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  8.4 KB  |  348 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1992, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of class TEditFile, a text edit which can find/replace
  6. //   and read/write from/to a file.
  7. //----------------------------------------------------------------------------
  8. #pragma hdrignore SECTION
  9. #include <owl/owlpch.h>
  10. #include <owl/editfile.h>
  11. #include <owl/applicat.h>
  12. #include <string.h>
  13. #include <dir.h>
  14. #include <limits.h>
  15.  
  16. #if !defined(SECTION) || SECTION == 1
  17.  
  18. DEFINE_RESPONSE_TABLE1(TEditFile, TEditSearch)
  19.   EV_COMMAND(CM_FILESAVE, CmFileSave),
  20.   EV_COMMAND(CM_FILESAVEAS, CmFileSaveAs),
  21.   EV_COMMAND_ENABLE(CM_FILESAVE, CmSaveEnable),
  22. END_RESPONSE_TABLE;
  23.  
  24. //
  25. // constructor for a TEditFile
  26. //
  27. // initializes its data members using passed parameters and default values
  28. //
  29. TEditFile::TEditFile(TWindow*        parent,
  30.                      int             id,
  31.                      const char far* text,
  32.                      int x, int y, int w, int h,
  33.                      const char far* fileName,
  34.                      TModule*        module)
  35. :
  36.   TEditSearch(parent, id, text, x, y, w, h, module)
  37. {
  38.   FileName = fileName ? strnewdup(fileName) : 0;
  39. }
  40.  
  41. //
  42. // dispose of the file name
  43. //
  44. TEditFile::~TEditFile()
  45. {
  46.   delete FileName;
  47. }
  48.  
  49. //
  50. // performs setup for a TEditFile
  51. //
  52. void
  53. TEditFile::SetupWindow()
  54. {
  55.   TEditSearch::SetupWindow();
  56.   FileData.Flags = OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT;
  57.   FileData.SetFilter(GetModule()->LoadString(IDS_FILEFILTER).c_str());
  58.  
  59.   SetFileName(FileName);
  60.   if (FileName && !Read()) {
  61.     string msgTemplate(GetModule()->LoadString(IDS_UNABLEREAD));
  62.     char*  msg = new char[MAXPATH + msgTemplate.length()];
  63.     wsprintf(msg, msgTemplate.c_str(), FileName);
  64.     MessageBox(msg, GetApplication()->GetName(), MB_ICONEXCLAMATION | MB_OK);
  65.     delete msg;
  66.     SetFileName(0);
  67.   }
  68. }
  69.  
  70. //
  71. // sets the file name of the window and updates the caption
  72. // replacing an empty name with 'Untitled' in its caption
  73. //
  74. void
  75. TEditFile::SetFileName(const char far* fileName)
  76. {
  77.   if (fileName != FileName) {
  78.     delete FileName;
  79.     FileName = fileName ? strnewdup(fileName) : 0;
  80.   }
  81.   string untitled(GetModule()->LoadString(IDS_UNTITLEDFILE));
  82.   SetDocTitle(FileName ? (const char far*)FileName : untitled.c_str(), 0);
  83. }
  84.  
  85. //
  86. // begins the edit of a new file, after determining that it is Ok to
  87. // clear the TEdit's text
  88. //
  89. void
  90. TEditFile::NewFile()
  91. {
  92.   if (CanClear()) {
  93.     Clear();
  94.     Invalidate();
  95.     ClearModify();
  96.     SetFileName(0);
  97.   }
  98. }
  99.  
  100. //
  101. // replaces the current file with the given file
  102. //
  103. void
  104. TEditFile::ReplaceWith(const char far* fileName)
  105. {
  106.   if (Read(fileName)) {
  107.     Invalidate();
  108.     SetFileName(fileName);
  109.   }
  110.   else {
  111.     string msgTemplate(GetModule()->LoadString(IDS_UNABLEREAD));
  112.     char*  msg = new char[MAXPATH + msgTemplate.length()];
  113.     wsprintf(msg, msgTemplate.c_str(), fileName);
  114.     MessageBox(msg, GetApplication()->GetName(), MB_ICONEXCLAMATION | MB_OK);
  115.     delete msg;
  116.   }
  117. }
  118.  
  119. //
  120. // brings up a dialog allowing the user to open a file into this
  121. // window
  122. //
  123. // same as selecting File|Open from the menus
  124. //
  125. void
  126. TEditFile::Open()
  127. {
  128.   if (CanClear()) {
  129.     *FileData.FileName = 0;
  130.     if (TFileOpenDialog(this, FileData).Execute() == IDOK)
  131.       ReplaceWith(FileData.FileName);
  132.   }
  133. }
  134.  
  135. //
  136. // reads the contents of a  specified file, or the previously-specified file
  137. // if no name passed, into the TEdit control
  138. // The caller is responsible for any error UI
  139. //
  140. bool
  141. TEditFile::Read(const char far* fileName)
  142. {
  143.   if (!fileName)
  144.     if (FileName)
  145.       fileName = FileName;
  146.     else
  147.       return false;
  148.  
  149.   bool   success = false;
  150.   HFILE  file = _lopen(fileName, OF_READ);
  151.  
  152.   if (file != HFILE_ERROR) {
  153.     long  charsToRead = _llseek(file, 0, SEEK_END);
  154.     _llseek(file, 0, SEEK_SET);
  155.  
  156.     if (charsToRead >= 0
  157.       #if !defined(BI_PLAT_WIN32)
  158.         && charsToRead < UINT_MAX     // limit 16bit edit ctrls to 1 segment
  159.       #endif
  160.       ) {
  161.       Clear();
  162.  
  163.       // Lock and resize Editor's buffer to the size of the file
  164.       // Then if OK, read the file into editBuffer
  165.       //
  166.       char far* editBuffer = LockBuffer(uint(charsToRead+1));
  167.       if (editBuffer) {
  168.         if (_lread(file, editBuffer, uint(charsToRead)) == charsToRead) {
  169.  
  170.           // 0 terminate Editor's buffer
  171.           //
  172.           editBuffer[int(charsToRead)] = 0;
  173.           success = true;
  174.           ClearModify();
  175.         }
  176.         UnlockBuffer(editBuffer, true);
  177.       }
  178.     }
  179.     _lclose(file);
  180.   }
  181.  
  182.   return success;
  183. }
  184.  
  185. //
  186. // saves the contents of the TEdit child control into the file currently
  187. // being editted
  188. //
  189. // returns true if the file was saved or IsModified returns false
  190. //(contents already saved)
  191. //
  192. bool
  193. TEditFile::Save()
  194. {
  195.   if (IsModified()) {
  196.     if (!FileName)
  197.       return SaveAs();
  198.  
  199.     if (!Write()) {
  200.       string msgTemplate(GetModule()->LoadString(IDS_UNABLEWRITE));
  201.       char*  msg = new char[MAXPATH + msgTemplate.length()];
  202.       wsprintf(msg, msgTemplate.c_str(), FileName);
  203.       MessageBox(msg, GetApplication()->GetName(), MB_ICONEXCLAMATION | MB_OK);
  204.       delete msg;
  205.       return false;
  206.     }
  207.   }
  208.   return true;  // editor's contents haven't been changed
  209. }
  210.  
  211. //
  212. // saves the contents of the TEdit child control into a file whose name
  213. // is retrieved from the user, through execution of a "Save" file dialog
  214. //
  215. // returns true if the file was saved
  216. //
  217. bool
  218. TEditFile::SaveAs()
  219. {
  220.   if (FileName)
  221.     strcpy(FileData.FileName, FileName);
  222.  
  223.   else
  224.     *FileData.FileName = 0;
  225.  
  226.   if (TFileSaveDialog(this, FileData).Execute() == IDOK) {
  227.     if (Write(FileData.FileName)) {
  228.       SetFileName(FileData.FileName);
  229.       return true;
  230.     }
  231.     string msgTemplate(GetModule()->LoadString(IDS_UNABLEWRITE));
  232.     char*  msg = new char[MAXPATH + msgTemplate.length()];
  233.     wsprintf(msg, msgTemplate.c_str(), FileName);
  234.     MessageBox(msg, GetApplication()->GetName(), MB_ICONEXCLAMATION | MB_OK);
  235.     delete msg;
  236.   }
  237.   return false;
  238. }
  239.  
  240. //
  241. // Enables save command only if text is modified
  242. //
  243. void
  244. TEditFile::CmSaveEnable(TCommandEnabler& commandHandler)
  245. {
  246.   commandHandler.Enable(IsModified());
  247. }
  248.  
  249. //
  250. // writes the contents of the TEdit child control to a specified file, or
  251. // the previously-specified file if none passed.
  252. // The caller is responsible for any error UI
  253. //
  254. bool
  255. TEditFile::Write(const char far* fileName)
  256. {
  257.   if (!fileName)
  258.     if (FileName)
  259.       fileName = FileName;
  260.     else
  261.       return false;
  262.  
  263.   int file = _lcreat(fileName, 0);
  264.   if (file == -1) {
  265.     return false;
  266.   }
  267.  
  268.   bool success = false;
  269.   char far* editBuffer = LockBuffer();
  270.   if (editBuffer) {
  271.     success = ToBool(_lwrite(file, editBuffer, strlen(editBuffer)) != (uint16)-1);
  272.     UnlockBuffer(editBuffer);
  273.     if (success)
  274.       ClearModify();
  275.   }
  276.   _lclose(file);
  277.  
  278.   return success;
  279. }
  280.  
  281. //
  282. // returns a bool value indicating whether or not it is Ok to clear
  283. // the TEdit's text
  284. //
  285. // returns true if the text has not been changed, or if the user Oks the
  286. // clearing of the text
  287. //
  288. bool
  289. TEditFile::CanClear()
  290. {
  291.   if (IsModified()) {
  292.     string msgTemplate(GetModule()->LoadString(IDS_FILECHANGED));
  293.     string untitled(GetModule()->LoadString(IDS_UNTITLEDFILE));
  294.     char*  msg = new char[MAXPATH+msgTemplate.length()];
  295.  
  296.     wsprintf(msg, msgTemplate.c_str(),
  297.              FileName ? (const char far*)FileName : untitled.c_str());
  298.  
  299.     int result = MessageBox(msg, GetApplication()->GetName(), MB_YESNOCANCEL|MB_ICONQUESTION);
  300.     delete msg;
  301.     return result==IDYES ? Save() : result != IDCANCEL;
  302.   }
  303.   return true;
  304. }
  305.  
  306. bool
  307. TEditFile::CanClose()
  308. {
  309.   return CanClear();
  310. }
  311.  
  312. #endif
  313. #if !defined(SECTION) || SECTION == 2
  314.  
  315.  
  316. IMPLEMENT_STREAMABLE1(TEditFile, TEditSearch);
  317.  
  318. //
  319. // reads an instance of TEditFile from the passed ipstream
  320. //
  321. void*
  322. TEditFile::Streamer::Read(ipstream& is, uint32 /*version*/) const
  323. {
  324.   TEditFile* o = GetObject();
  325.   ReadBaseObject((TEditSearch*)o, is);
  326.  
  327.   o->FileName = is.freadString();
  328.   if (!*o->FileName) {
  329.     delete o->FileName;
  330.     o->FileName = 0;
  331.   }
  332.   return o;
  333. }
  334.  
  335. //
  336. // writes the TEditFile to the passed opstream
  337. //
  338. void
  339. TEditFile::Streamer::Write(opstream& os) const
  340. {
  341.   TEditFile* o = GetObject();
  342.   WriteBaseObject((TEditSearch*)o, os);
  343.   os.fwriteString(o->FileName ? o->FileName : "");
  344. }
  345.  
  346. #endif
  347.  
  348.