X11 Work Bench Toolkit  1.0
file_help.h
1 
2 // __ _ _ _ _ _ //
3 // / _|(_)| | ___ | |__ ___ | | _ __ | |__ //
4 // | |_ | || | / _ \ | '_ \ / _ \| || '_ \ | '_ \ //
5 // | _|| || || __/ | | | || __/| || |_) |_| | | | //
6 // |_| |_||_| \___|_____|_| |_| \___||_|| .__/(_)_| |_| //
7 // |_____| |_| //
8 // //
9 // helper utilities for file I/O //
10 // //
12 
13 /*****************************************************************************
14 
15  X11workbench - X11 programmer's 'work bench' application and toolkit
16  Copyright (c) 2010-2016 by Bob Frazier (aka 'Big Bad Bombastic Bob')
17  all rights reserved
18 
19  DISCLAIMER: The X11workbench application and toolkit software are supplied
20  'as-is', with no warranties, either implied or explicit.
21  Any claims to alleged functionality or features should be
22  considered 'preliminary', and might not function as advertised.
23 
24  BSD-like license:
25 
26  There is no restriction as to what you can do with this software, so long
27  as you include the above copyright notice and DISCLAIMER for any distributed
28  work that is equal to or derived from this one, along with this paragraph
29  that explains the terms of the license if the source is also being made
30  available. A "derived work" describes a work that uses a significant portion
31  of the source files or algorithms that are included with this one.
32  Specifically excluded from this are files that were generated by the software,
33  or anything that is included with the software that is part of another package
34  (such as files that were created or added during the 'configure' process).
35  Specifically included is the use of part or all of any of the X11 workbench
36  toolkit source or header files in your distributed application. If you do not
37  ship the source, the above copyright statement is still required to be placed
38  in a reasonably prominent place, such as documentation, splash screens, and/or
39  'about the application' dialog boxes.
40 
41  Use and distribution are in accordance with GPL, LGPL, and/or the above
42  BSD-like license. See COPYING and README files for more information.
43 
44 
45  Additional information at http://sourceforge.net/projects/X11workbench
46 
47 ******************************************************************************/
48 
49 
50 #ifndef _FILE_HELP_H_INCLUDED_
51 #define _FILE_HELP_H_INCLUDED_
52 
53 // read a file into a buffer, returning the buffer and its size
54 
55 #include <limits.h>
56 
57 #ifdef __cplusplus
58 extern "C" {
59 #endif // __cplusplus
60 
61 
62 // use NAME_MAX to determine max buffer sizes for things that use a file name
63 // maximum path name limits are OS dependent.
64 
65 #ifdef WIN32
66 #ifndef NAME_MAX
67 #define NAME_MAX MAX_PATH
68 #endif // NAME_MAX
69 #else // !WIN32
70 // POSIX systems normally define NAME_MAX in limits.h (or something it includes)
71 #ifndef NAME_MAX
72 #ifdef MAXNAMLEN
73 #define NAME_MAX MAXNAMLEN /* BSD defines MAXNAMLEN */
74 #else
75 #define NAME_MAX 255 /* standard value for BSD and Linux */
76 #endif // MAXNAMLEN
77 #endif // NAME_MAX
78 #endif // WIN32, !WIN32
79 
80 
81 // sub-groups for documentation
82 
140 typedef struct __file_help_buf__
141 {
144  long lBufferSize;
146  long lLineCount;
148  int iFlags;
149  char **ppLineBuf;
150  char cData[sizeof(char *)];
152 
161 {
162  file_help_buf_dirty = 1,
163  file_help_buf_reserved01 = 2,
164  file_help_buf_reserved02 = 4,
165  file_help_buf_reserved03 = 8,
166  file_help_buf_reserved04 = 16,
167  file_help_buf_reserved05 = 32,
168  file_help_buf_reserved06 = 64,
169  file_help_buf_reserved07 = 128,
170  file_help_buf_reserved08 = 256,
171 
172  file_help_buf_last = 0x80000000L
173 };
174 
184 static __inline__ int FBIsFileBufDirty(const file_help_buf_t *pBuf)
185 {
186  return pBuf && (pBuf->iFlags & file_help_buf_dirty);
187 }
188 
201 
215 file_help_buf_t *FBGetFileBufFromBuffer(const char *pBuf, long cbBuf);
216 
228 file_help_buf_t *FBGetFileBuf(const char *szFileName);
229 
230 
241 
242 
255  // parses or re-parses buffer, populating ppLineBuf
256 
268 int FBWriteFileBuf(const char *szFileName, const file_help_buf_t *pFB);
269 
281 int FBWriteFileBufHandle(int iHandle, const file_help_buf_t *pFB);
282 
283 
297 void FBInsertIntoFileBuf(file_help_buf_t **ppBuf, long cbOffset, const void *pData, long cbData);
298 
310 void FBDeleteFromFileBuf(file_help_buf_t *pBuf, long cbOffset, long cbDelFrom);
311 
312 
327 void FBInsertLineIntoFileBuf(file_help_buf_t **ppBuf, long lLineNum, const char *szLine);
328 
329 
342 void FBDeleteLineFromFileBuf(file_help_buf_t *pBuf, long lLineNum);
343 
344 
360 void FBReplaceLineInFileBuf(file_help_buf_t **ppBuf, long lLineNum, const char *szLine);
361 
362 
371 static __inline__ int FBWriteFileFromBuffer(const char *szFileName, const char *pBuf, unsigned int cbBuf)
372 {
373 int iRval;
374 file_help_buf_t *pFB;
375 
376 
377  pFB = FBGetFileBufFromBuffer(pBuf, cbBuf);
378 
379  if(!pFB)
380  {
381 // WB_ERROR_PRINT("ERROR - %s - unable to create file buf\n", __FUNCTION__);
382  return -1;
383  }
384 
385  iRval = FBWriteFileBuf(szFileName, pFB);
386 
387  FBDestroyFileBuf(pFB);
388 
389 // if(iRval < 0)
390 // {
391 // WB_ERROR_PRINT("ERROR - %s - unable to create file \"%s\" from file buf\n", __FUNCTION__, szFileName);
392 // }
393 
394  return iRval;
395 }
396 
397 
398 
399 
400 // SYSTEM INDEPENDENT FILE STATUS, LISTINGS, AND INFORMATION
401 
414 int WBReplicateFilePermissions(const char *szProto, const char *szTarget);
415 
424 char *WBGetCurrentDirectory(void);
425 
435 int WBIsDirectory(const char *szName);
436 
450 char *WBGetCanonicalPath(const char *szFileName);
451 
470 void *WBAllocDirectoryList(const char *szDirSpec);
471 
479 void WBDestroyDirectoryList(void *pDirectoryList);
480 
493 int WBNextDirectoryEntry(void *pDirectoryList, char *szNameReturn,
494  int cbNameReturn, unsigned long *pdwModeAttrReturn);
495 
507 char *WBGetDirectoryListFileFullPath(const void *pDirectoryList, const char *szFileName);
508 
520 char *WBGetSymLinkTarget(const char *szFileName);
521 
535 char *WBGetDirectoryListSymLinkTarget(const void *pDirectoryList, const char *szFileName);
536 
546 int WBStat(const char *szFileName, unsigned long *pdwModeAttrReturn);
547 
559 int WBGetDirectoryListFileStat(const void *pDirectoryList, const char *szFileName,
560  unsigned long *pdwModeAttrReturn);
561 
562 
563 #ifdef __cplusplus
564 };
565 #endif // __cplusplus
566 
567 
568 #endif // _FILE_HELP_H_INCLUDED_
569