summaryrefslogtreecommitdiff
path: root/rhtvision/examples/dlgdsn/strmoper.cc
blob: 1cc36e24b13e55ab3f6b88ed1a10ad0f154c0e0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/***************************************************************************

    strmoper.cc - Stream operations
    ---------------------------------------------------------------------
    May, 2000
    Copyright (C) 2000 by Warlei Alves
    walves@usa.net
    
    Modified by Salvador E. Tropea to compile without warnings.
    For gcc 2.95.x and then 3.0.1.
    
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
 
#define Uses_stdio
#define Uses_string
// didn't find std::ios (AP)
#define Uses_iostream

#define Uses_MsgBox
#define Uses_TFileDialog
#define Uses_TProgram
#define Uses_TApplication
#define Uses_TDeskTop
#define Uses_ofpstream
#define Uses_ifpstream

#include <tv.h>
#include "strmoper.h"

UsingNamespaceStd

bool fileExists(const char * FileName)
{
   FILE * test;
   
   bool rst = ((test = fopen(FileName, "r")) != 0);
   if (rst) fclose(test);
   return rst;
}

// SET: This function was originaly declared as const char * and returned a
// pointer to a temporal variable in the stack. Nicola Asuni found it and
// suggested a partial workaround. I then modified the function to return
// a char * using newStr to allocate it. I also modified all the points
// where this function is used (at least all that I found) to release the
// returned value.
char * getFileName(const char * aTitle, const char * ext, int Mode)
{
   TFileDialog *d = 0;
   int cmd = cmCancel;
   
   if (Mode == 0)
     d = (TFileDialog *) TProgram::application->validView( new
       TFileDialog(ext, aTitle, _("File"), fdOpenButton, 100) );
   else
     d = (TFileDialog *) TProgram::application->validView( new
       TFileDialog(ext, aTitle, _("File"), fdOKButton, 100) );
   if (d != 0) cmd = TProgram::deskTop->execView( d );
   if ( cmd != cmCancel )
   {
      char fileName[PATH_MAX];
      d->getFileName( fileName );
      delete d;
      return newStr( fileName );
   }
   else return NULL;
}


ifpstream * openFile(const char * FileName, char * Signature)
{
   if (!fileExists(FileName)) return 0;
   ifpstream * rst = new ifpstream( FileName, ios::in|ios::binary );
   char buf[50];
   if (rst)
   {
      rst->readBytes(&buf, strlen(Signature) + 1);
      if (strcmp(buf, Signature) != 0)
      {
          delete rst;
          return 0;
      }
   }
   return rst;
}

ofpstream * initFile(const char * FileName, char * current, char * Signature)
{
   if ( fileExists(FileName) &&
      ( current == 0 || strcmp(FileName, current) != 0 ) )
   {
      if ( messageBox(__("The file already exists. Overwrite it?"),
                    mfYesNoCancel) != cmYes ) return 0;
   }
   ofpstream * rst = new ofpstream( FileName, ios::in|ios::binary );
   if (rst) rst->writeBytes(Signature, strlen(Signature) + 1);
   return rst;
}