~diesch/ubuntu/trusty/scid/fix-for-1277520

« back to all changes in this revision

Viewing changes to src/misc.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Oliver Korff
  • Date: 2010-01-12 12:46:24 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20100112124624-d6cagy40mtvfu7vk
Tags: 1:4.2.0.cvs20100120-1
* New upstream version. The "Help/About Scid" menu states, that we 
  work with a 4.2 developmet version.
* Added a misc:Depends to the depends section in control
* Changed section from optional to extra to avoid warnings from debcheck,
  because of a depends on oss-compat, which is extra on other architectures
* Reworked the suggests in control. At first I wanted to avoid the suggest on 
  crafty (debcheck reports it is unavailable on some archs). For me this is 
  a suggestion and I do want the users to know that there is a crafty around.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
#include "error.h"
17
17
#include "misc.h"
18
18
 
 
19
#include "tkscid.h"
 
20
 
19
21
#include <stdlib.h>
20
22
#include <stdio.h>
 
23
#include <string.h>
21
24
#include <ctype.h>     // For isspace() function.
22
25
#include <sys/stat.h>  // Needed for fileSize() function.
 
26
#include <tcl.h>
23
27
 
24
28
#ifdef WINCE
25
 
#include <tcl.h>
 
29
  #include <tcl.h>
26
30
extern "C" int my_stat(const char *path, struct stat *buf);
27
31
#endif
28
32
 
30
34
// in scid_Init():
31
35
directionT sqDir[66][66];
32
36
 
 
37
// Keep track of current TCL interpreter for Tcl calls (like Tcl_Eval)
 
38
Tcl_Interp * currentTclInterp;
 
39
 
 
40
int logMemory = 0;
33
41
 
34
42
//////////////////////////////////////////////////////////////////////
35
43
//   Scid Initialisation Routine
72
80
    }
73
81
}
74
82
 
75
 
#ifndef POCKETENGINE
 
83
//////////////////////////////////////////////////////////////////////
 
84
//   my_ functions for I/O debugging and memory allocation
 
85
//
 
86
// =======================================================
 
87
 
 
88
#include <tcl.h>
 
89
 
 
90
#ifdef WINCE
 
91
// =======================================================
 
92
char * my_Tcl_Alloc(int size) {
 
93
 
 
94
  char * buf = Tcl_AttemptAlloc(size);
 
95
#ifndef POCKET
 
96
  if (logMemory)
 
97
    printf("Alloc %u %d\n", (unsigned int) buf, size );
 
98
#endif
 
99
  if (buf == NULL) {
 
100
    Tcl_Eval(currentTclInterp, "tk_messageBox -type ok -icon error -parent . -title \"Scid\" -message \"Out of memory\nScid should crash rather quickly\"");
 
101
  }
 
102
  return buf;
 
103
}
 
104
// =======================================================
 
105
char *  my_Tcl_AttemptAlloc(int size){
 
106
 
 
107
  char * buf = Tcl_AttemptAlloc(size);
 
108
#ifndef POCKET
 
109
  if (logMemory)
 
110
    printf("Alloc %u %d\n", (unsigned int) buf, size );
 
111
#endif
 
112
  return buf;
 
113
}
 
114
// =======================================================
 
115
char * my_Tcl_Realloc(char * ptr, int size) {
 
116
  char * buf = Tcl_AttemptRealloc(ptr, size);
 
117
#ifndef POCKET
 
118
  if (logMemory)
 
119
    printf("Realloc %u -> %u %d\n", (unsigned int) ptr, (unsigned int) buf, size );
 
120
#endif
 
121
  if (buf == NULL) {
 
122
    Tcl_Eval(currentTclInterp, "tk_messageBox -type ok -icon error -parent . -title \"Scid\" -message \"Out of memory\nScid could crash rather quickly\"");
 
123
  }
 
124
  return buf;
 
125
}
 
126
// =======================================================
 
127
void my_Tcl_Free(char * ptr) {
 
128
#ifndef POCKET
 
129
  if (logMemory)
 
130
    printf("Free %u\n", (unsigned int) ptr );
 
131
#endif
 
132
 
 
133
  Tcl_Free(ptr);
 
134
}
 
135
#endif // WINCE
 
136
 
 
137
// =======================================================
 
138
Tcl_Channel  my_Tcl_OpenFileChannel (Tcl_Interp * interp, CONST char * fileName, CONST char * modeString, int permissions){
 
139
  Tcl_Channel chan = Tcl_OpenFileChannel (currentTclInterp, fileName, modeString, permissions);
 
140
  if (chan == NULL) {
 
141
    char buf[200];
 
142
    sprintf(buf, "tk_messageBox -type ok -icon error -parent . -title \"Error\" -message \"Tcl_OpenFileChannel error %s\"", Tcl_ErrnoMsg(Tcl_GetErrno()));
 
143
    Tcl_Eval(currentTclInterp, buf);
 
144
  }
 
145
  return chan;
 
146
}
 
147
// =======================================================
 
148
// interpreter is set to NULL, so in case of any error, it is not appended to current result
 
149
Tcl_Channel  mySilent_Tcl_OpenFileChannel (Tcl_Interp * interp, CONST char * fileName, CONST char * modeString, int permissions){
 
150
  Tcl_Channel chan = Tcl_OpenFileChannel (NULL, fileName, modeString, permissions);
 
151
  return chan;
 
152
}
 
153
// =======================================================
 
154
int my_Tcl_Close (Tcl_Interp * interp, Tcl_Channel chan) {
 
155
  int res = Tcl_Close (currentTclInterp, chan);
 
156
  if (res != TCL_OK) {
 
157
      char buf[200];
 
158
    sprintf(buf, "tk_messageBox -type ok -icon error -parent . -title \"Error\" -message \"Tcl_Close error %s\"", Tcl_ErrnoMsg(Tcl_GetErrno()));
 
159
    Tcl_Eval(currentTclInterp, buf);
 
160
  }
 
161
  return res;
 
162
}
 
163
// =======================================================
 
164
int my_Tcl_Read (Tcl_Channel chan, char * bufPtr, int toRead) {
 
165
  int res = Tcl_Read (chan, bufPtr, toRead);
 
166
  if (res == -1) {
 
167
    char buf[200];
 
168
    sprintf(buf, "tk_messageBox -type ok -icon error -parent . -title \"Error\" -message \"Tcl_Read error %s\"", Tcl_ErrnoMsg(Tcl_GetErrno()));
 
169
    Tcl_Eval(currentTclInterp, buf);
 
170
  }
 
171
  return res;
 
172
}
 
173
// =======================================================
 
174
int my_Tcl_Write (Tcl_Channel chan, CONST char * s, int slen){
 
175
  int res;
 
176
  res = Tcl_Write ( chan, s, slen);
 
177
  if (res == -1) {
 
178
    char buf[200];
 
179
    sprintf(buf, "tk_messageBox -type ok -icon error -parent . -title \"Error\" -message \"Tcl_Write error %s\"", Tcl_ErrnoMsg(Tcl_GetErrno()));
 
180
    Tcl_Eval(currentTclInterp, buf);
 
181
  }
 
182
  return res;
 
183
}
 
184
// =======================================================
 
185
int my_Tcl_Flush (Tcl_Channel chan){
 
186
  int res = Tcl_Flush (chan);
 
187
  if (res != TCL_OK) {
 
188
    char buf[200];
 
189
    sprintf(buf, "tk_messageBox -type ok -icon error -parent . -title \"Error\" -message \"Tcl_Flush error %s\"", Tcl_ErrnoMsg(Tcl_GetErrno()));
 
190
    Tcl_Eval(currentTclInterp, buf);
 
191
  }
 
192
  return res;
 
193
}
 
194
// =======================================================
 
195
Tcl_WideInt my_Tcl_Seek (Tcl_Channel chan, Tcl_WideInt offset, int mode){
 
196
  Tcl_WideInt res = Tcl_Seek ( chan, offset, mode);
 
197
  if (res == -1) {
 
198
    char buf[200];
 
199
    sprintf(buf, "tk_messageBox -type ok -icon error -parent . -title \"Error\" -message \"Tcl_Seek error %s\"", Tcl_ErrnoMsg(Tcl_GetErrno()));
 
200
    Tcl_Eval(currentTclInterp, buf);
 
201
  }
 
202
  return res;
 
203
}
 
204
// =======================================================
 
205
Tcl_WideInt my_Tcl_Tell (Tcl_Channel chan){
 
206
  Tcl_WideInt res = Tcl_Tell (chan);
 
207
  if (res == -1) {
 
208
    Tcl_Eval(currentTclInterp, "tk_messageBox -type ok -icon error -parent . -title \"Error\" -message \"Tcl_Tell error\"");
 
209
  }
 
210
  return res;
 
211
}
 
212
// =======================================================
 
213
int my_Tcl_SetChannelOption ( Tcl_Interp * interp, Tcl_Channel chan, CONST char * optionName, CONST char * newValue){
 
214
  int res = Tcl_SetChannelOption ( currentTclInterp, chan, optionName, newValue);
 
215
  if (res == TCL_ERROR) {
 
216
    char buf[200];
 
217
    sprintf(buf, "tk_messageBox -type ok -icon error -parent . -title \"Error\" -message \"Tcl_SetChannelOption %s %s error\"", optionName, newValue);
 
218
    Tcl_Eval(currentTclInterp, buf);
 
219
 
 
220
  }
 
221
  return res;
 
222
}
 
223
// =======================================================
 
224
int my_Tcl_Eof (Tcl_Channel chan) {
 
225
  return Tcl_Eof (chan);
 
226
}
 
227
 
76
228
//////////////////////////////////////////////////////////////////////
77
229
//   ECO Code Routines
78
230
 
189
341
    if (((eco % 131) % 5) == 1) { eco += 4; }
190
342
    return eco + 1;
191
343
}
192
 
#endif
193
344
 
194
345
//////////////////////////////////////////////////////////////////////
195
346
//   String Routines
1091
1242
    return square_Make (fyle_FromChar(chFyle), rank_FromChar(chRank));
1092
1243
}
1093
1244
 
1094
 
#ifndef POCKETENGINE
1095
1245
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1096
1246
// strUniqueExactMatch():
1097
1247
//      Given a string <keyStr> and a null-terminated array of strings
1170
1320
// rawFileSize():
1171
1321
//    Uses the stat() system call to get the size of the file.
1172
1322
//    Returns 0 if any error occurs.
 
1323
 
1173
1324
uint
1174
1325
rawFileSize (const char * name)
1175
1326
{
1176
 
    struct stat statBuf;    // Defined in <sys/stat.h>
1177
 
    if (stat (name, &statBuf) != 0) {
1178
 
        return 0;
1179
 
    }
1180
 
    return (uint) statBuf.st_size;
 
1327
  uint size = 0;
 
1328
  // Avoid C stat functions for portability
 
1329
  Tcl_StatBuf * statbuf = Tcl_AllocStatBuf();
 
1330
  Tcl_Obj * obj = Tcl_NewStringObj( name, strlen(name) );
 
1331
  Tcl_IncrRefCount(obj);
 
1332
  int res = Tcl_FSStat( obj, statbuf );
 
1333
  if ( res != 0) return 0;
 
1334
 
 
1335
  size = (uint) statbuf->st_size;
 
1336
  ckfree( (char*) statbuf);
 
1337
  Tcl_DecrRefCount(obj);
 
1338
   
 
1339
  return size;
1181
1340
}
1182
1341
 
1183
1342
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1306
1465
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1307
1466
// fileExists():
1308
1467
//      Returns true if the file exists, false otherwise.
1309
 
bool
1310
 
fileExists (const char * name, const char * suffix)
1311
 
{
1312
 
#ifdef WINCE
1313
 
    Tcl_StatBuf statBuf;
1314
 
    fileNameT fname;
1315
 
    int res;
1316
 
    strCopy (fname, name);
1317
 
    strAppend (fname, suffix);
1318
 
//    if (my_stat (fname, &statBuf) != 0) {
1319
 
    Tcl_Obj * obj = Tcl_NewStringObj(fname, strlen(fname));
1320
 
    res = Tcl_FSLstat(obj, &statBuf );
1321
 
    Tcl_DecrRefCount(obj); 
1322
 
    if ( res != 0) {
1323
 
        return false;
1324
 
    }
1325
 
#else
1326
 
    struct stat statBuf;    // Defined in <sys/stat.h>
1327
 
    fileNameT fname;
1328
 
    strCopy (fname, name);
1329
 
    strAppend (fname, suffix);
1330
 
    if (stat (fname, &statBuf) != 0) {
1331
 
        return false;
1332
 
    }
1333
 
#endif
1334
 
    return true;
1335
 
}
 
1468
// bool
 
1469
// fileExists (const char * name, const char * suffix)
 
1470
// {
 
1471
// #ifdef WINCE
 
1472
//     Tcl_StatBuf statBuf;
 
1473
//     fileNameT fname;
 
1474
//     int res;
 
1475
//     strCopy (fname, name);
 
1476
//     strAppend (fname, suffix);
 
1477
//     Tcl_Obj * obj = Tcl_NewStringObj(fname, strlen(fname));
 
1478
//     res = Tcl_FSLstat(obj, &statBuf );
 
1479
//     Tcl_DecrRefCount(obj); 
 
1480
//     if ( res != 0) {
 
1481
//         return false;
 
1482
//     }
 
1483
// #else
 
1484
//     struct stat statBuf;    // Defined in <sys/stat.h>
 
1485
//     fileNameT fname;
 
1486
//     strCopy (fname, name);
 
1487
//     strAppend (fname, suffix);
 
1488
//     if (stat (fname, &statBuf) != 0) {
 
1489
//         return false;
 
1490
//     }
 
1491
// #endif
 
1492
//     return true;
 
1493
// }
1336
1494
 
1337
1495
 
1338
1496
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1395
1553
}
1396
1554
#endif
1397
1555
 
1398
 
#endif
1399
1556
 
1400
1557
//////////////////////////////////////////////////////////////////////
1401
1558
//  EOF: misc.cpp