SNAP Library 4.0, Developer Reference  2017-07-27 13:18:06
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TZipOut Class Reference

#include <zipfl.h>

Inheritance diagram for TZipOut:
Collaboration diagram for TZipOut:

Public Member Functions

 TZipOut (const TStr &_FNm)
 
 ~TZipOut ()
 
int PutCh (const char &Ch)
 
int PutBf (const void *LBf, const TSize &LBfL)
 
void Flush ()
 
- Public Member Functions inherited from TSOut
 TSOut ()
 
 TSOut (const TStr &Str)
 
virtual ~TSOut ()
 
void EnableLnTrunc (const int &_MxLnLen)
 
void DisableLnTrunc ()
 
virtual TFileId GetFileId () const
 
int PutMem (const TMem &Mem)
 
int PutCh (const char &Ch, const int &Chs)
 
int PutBool (const bool &Bool)
 
int PutInt (const int &Int)
 
int PutInt (const int &Int, const char *FmtStr)
 
int PutUInt (const uint &Int)
 
int PutUInt (const uint &Int, const char *FmtStr)
 
int PutFlt (const double &Flt)
 
int PutFlt (const double &Flt, const char *FmtStr)
 
int PutStr (const char *CStr)
 
int PutStr (const TChA &ChA)
 
int PutStr (const TStr &Str, const char *FmtStr)
 
int PutStr (const TStr &Str, const bool &ForceInLn=false)
 
int PutStrLn (const TStr &Str, const bool &ForceInLn=false)
 
int PutStrFmt (const char *FmtStr,...)
 
int PutStrFmtLn (const char *FmtStr,...)
 
int PutIndent (const int &IndentLev=1)
 
int PutLn (const int &Lns=1)
 
int PutDosLn (const int &Lns=1)
 
int PutSep (const int &NextStrLen=0)
 
int PutSepLn (const int &Lns=0)
 
void SaveCs ()
 
void SaveBf (const void *Bf, const TSize &BfL)
 
void Save (const bool &Bool)
 
void Save (const char &Ch)
 
void Save (const uchar &UCh)
 
void Save (const short &Short)
 
void Save (const ushort &UShort)
 
void Save (const int &Int)
 
void Save (const uint &UInt)
 
void Save (const int64 &Int)
 
void Save (const uint64 &UInt)
 
void Save (const double &Flt)
 
void Save (const sdouble &SFlt)
 
void Save (const ldouble &LFlt)
 
void Save (const char *CStr, const TSize &CStrLen)
 
void Save (const char *CStr)
 
void Save (TSIn &SIn, const TSize &BfL=-1)
 
void Save (const PSIn &SIn, const TSize &BfL=-1)
 
void Save (const void *Bf, const TSize &BfL)
 
TSOutoperator<< (const bool &Bool)
 
TSOutoperator<< (const uchar &UCh)
 
TSOutoperator<< (const char &Ch)
 
TSOutoperator<< (const short &Sh)
 
TSOutoperator<< (const ushort &USh)
 
TSOutoperator<< (const int &Int)
 
TSOutoperator<< (const uint &Int)
 
TSOutoperator<< (const int64 &Int)
 
TSOutoperator<< (const uint64 &UInt)
 
TSOutoperator<< (const float &Flt)
 
TSOutoperator<< (const double &Double)
 
TSOutoperator<< (const long double &LDouble)
 
TSOutoperator<< (const TSOutMnp &Mnp)
 
TSOutoperator<< (TSOut &(*FuncPt)(TSOut &))
 
TSOutoperator<< (TSIn &SIn)
 
TSOutoperator<< (PSIn &SIn)
 
- Public Member Functions inherited from TSBase
 TSBase (const TSStr &Nm)
 
virtual ~TSBase ()
 
virtual TStr GetSNm () const
 

Static Public Member Functions

static PSOut New (const TStr &FNm)
 
static bool IsZipFNm (const TStr &FNm)
 Check whether the file extension of FNm is that of a compressed file (.gz, .7z, .rar, .zip, .cab, .arj. bzip2). More...
 
static bool IsZipExt (const TStr &FNmExt)
 Check whether the file extension FNmExt is that of a compressed file (.gz, .7z, .rar, .zip, .cab, .arj. bzip2). More...
 
static TStr GetCmd (const TStr &ZipFNm)
 Return a command-line string that is executed in order to decompress a file to standard output. More...
 
static PSOut NewIfZip (const TStr &FNm)
 

Private Member Functions

void FlushBf ()
 
void CreateZipProcess (const TStr &Cmd, const TStr &ZipFNm)
 
 TZipOut ()
 
 TZipOut (const TZipOut &)
 
TZipOutoperator= (const TZipOut &)
 

Static Private Member Functions

static void FillFExtToCmdH ()
 

Private Attributes

FILE * ZipStdinRd
 
FILE * ZipStdinWr
 
char * Bf
 
TSize BfL
 

Static Private Attributes

static const TSize MxBfL =4*1024
 
static TStrStrH FExtToCmdH
 

Additional Inherited Members

- Static Public Attributes inherited from TSOut
static const TPt< TSOutStdOut =PSOut(new TStdOut())
 
- Protected Attributes inherited from TSBase
TCRef CRef
 
TSStr SNm
 
TCs Cs
 

Detailed Description

Compressed File Output Stream. The class directly writes to a compressed file. This is eachieved by TZipFl outputing into a pipe from which 7ZIP then reads and compresses. The class requires 7ZIP to be installed on the machine. Go to http://www.7-zip.org to install the software. 7z (7z.exe) is an executable and can decompress the following formats: .gz, .7z, .rar, .zip, .cab, .arj. bzip2. The class TZIpOut expects that '7z' ('7z.exe') is in the working path. Note2: For 7z to work properly you need both the 7z executable and the directory 'Codecs'. Note3: Use TZipIn::SevenZipPath to set the path to 7z executable.

Definition at line 74 of file zipfl.h.

Constructor & Destructor Documentation

TZipOut::TZipOut ( )
private

Referenced by New().

Here is the caller graph for this function:

TZipOut::TZipOut ( const TZipOut )
private
TZipOut::TZipOut ( const TStr _FNm)

Definition at line 337 of file zipfl.cpp.

References Bf, BfL, CreateZipProcess(), EAssertR, TStr::Empty(), GetCmd(), MxBfL, ZipStdinRd, and ZipStdinWr.

337  : TSBase(FNm.CStr()), TSOut(FNm), ZipStdinRd(NULL), ZipStdinWr(NULL), Bf(NULL), BfL(0){
338  EAssertR(! FNm.Empty(), "Empty file-name.");
339  #ifdef GLib_WIN
340  // create pipes
341  SECURITY_ATTRIBUTES saAttr;
342  saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
343  saAttr.bInheritHandle = TRUE;
344  saAttr.lpSecurityDescriptor = NULL;
345  // Create a pipe for the child process's STDOUT.
346  EAssertR(CreatePipe(&ZipStdinRd, &ZipStdinWr, &saAttr, 0), "Stdout pipe creation failed");
347  // Ensure the read handle to the pipe for STDOUT is not inherited.
348  SetHandleInformation(ZipStdinWr, HANDLE_FLAG_INHERIT, 0);
349  #else
350  // no implementation necessary
351  #endif
352  CreateZipProcess(GetCmd(FNm), FNm);
353  Bf=new char[MxBfL]; BfL=0;
354 }
static TStr GetCmd(const TStr &ZipFNm)
Return a command-line string that is executed in order to decompress a file to standard output...
Definition: zipfl.cpp:421
TSBase(const TSStr &Nm)
Definition: fl.h:50
char * Bf
Definition: zipfl.h:83
TSize BfL
Definition: zipfl.h:84
static const TSize MxBfL
Definition: zipfl.h:76
FILE * ZipStdinRd
Definition: zipfl.h:81
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
TSOut()
Definition: fl.h:136
FILE * ZipStdinWr
Definition: zipfl.h:81
void CreateZipProcess(const TStr &Cmd, const TStr &ZipFNm)
Definition: zipfl.cpp:307

Here is the call graph for this function:

TZipOut::~TZipOut ( )

Definition at line 360 of file zipfl.cpp.

References Bf, BfL, EAssertR, FlushBf(), ZipStdinRd, and ZipStdinWr.

360  {
361  if (BfL!=0) { FlushBf(); }
362  #ifdef GLib_WIN
363  if (ZipStdinWr != NULL) { EAssertR(CloseHandle(ZipStdinWr), "Closing write-end of pipe failed"); }
364  if (ZipStdinRd != NULL) { EAssertR(CloseHandle(ZipStdinRd), "Closing read-end of pipe failed"); }
365  #else
366  if (ZipStdinWr != NULL) { EAssertR(pclose(ZipStdinWr) != -1, "Closing of the process failed"); }
367  #endif
368  if (Bf!=NULL) { delete[] Bf; }
369 }
char * Bf
Definition: zipfl.h:83
TSize BfL
Definition: zipfl.h:84
FILE * ZipStdinRd
Definition: zipfl.h:81
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
FILE * ZipStdinWr
Definition: zipfl.h:81
void FlushBf()
Definition: zipfl.cpp:296

Here is the call graph for this function:

Member Function Documentation

void TZipOut::CreateZipProcess ( const TStr Cmd,
const TStr ZipFNm 
)
private

Definition at line 307 of file zipfl.cpp.

References TStr::CStr(), EAssertR, TStr::Fmt(), ZipStdinRd, and ZipStdinWr.

Referenced by TZipOut().

307  {
308  const TStr CmdLine = TStr::Fmt("%s %s", Cmd.CStr(), ZipFNm.CStr());
309  #ifdef GLib_WIN
310  PROCESS_INFORMATION piProcInfo;
311  STARTUPINFO siStartInfo;
312  ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION));
313  ZeroMemory( &siStartInfo, sizeof(STARTUPINFO));
314  siStartInfo.cb = sizeof(STARTUPINFO);
315  siStartInfo.hStdInput = ZipStdinRd;
316  siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
317  // Create the child process.
318  const BOOL FuncRetn = CreateProcess(NULL,
319  (LPSTR) CmdLine.CStr(), // command line
320  NULL, // process security attributes
321  NULL, // primary thread security attributes
322  TRUE, // handles are inherited
323  0, // creation flags
324  NULL, // use parent's environment
325  NULL, // use parent's current directory
326  &siStartInfo, // STARTUPINFO pointer
327  &piProcInfo); // receives PROCESS_INFORMATION
328  EAssertR(FuncRetn!=0, TStr::Fmt("Can not execute '%s' (Set the TZipIn::SevenZipPath)", CmdLine.CStr()).CStr());
329  CloseHandle(piProcInfo.hProcess);
330  CloseHandle(piProcInfo.hThread);
331  #else
332  ZipStdinWr = popen((TZipIn::SevenZipPath+"/"+CmdLine).CStr(), "r");
333  EAssertR(ZipStdinWr, TStr::Fmt("Can not execute '%s' (Set the TZipIn::SevenZipPath)", CmdLine.CStr()).CStr());
334  #endif
335 }
static TStr SevenZipPath
Definition: zipfl.h:17
Definition: dt.h:412
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
FILE * ZipStdinRd
Definition: zipfl.h:81
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
FILE * ZipStdinWr
Definition: zipfl.h:81
char * CStr()
Definition: dt.h:476

Here is the call graph for this function:

Here is the caller graph for this function:

void TZipOut::FillFExtToCmdH ( )
staticprivate

Definition at line 402 of file zipfl.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::Empty(), and FExtToCmdH.

Referenced by GetCmd(), and IsZipExt().

402  {
403  // 7za compress: "a -y -bd -si{CompressedFNm}"
404  #ifdef GLib_WIN
405  const char* ZipCmd = "7z.exe a -y -bd -si";
406  #else
407  const char* ZipCmd = "7za a -y -bd -si";
408  #endif
409  if (FExtToCmdH.Empty()) {
410  FExtToCmdH.AddDat(".gz", ZipCmd);
411  FExtToCmdH.AddDat(".7z", ZipCmd);
412  FExtToCmdH.AddDat(".rar", ZipCmd);
413  FExtToCmdH.AddDat(".zip", ZipCmd);
414  FExtToCmdH.AddDat(".cab", ZipCmd);
415  FExtToCmdH.AddDat(".arj", ZipCmd);
416  FExtToCmdH.AddDat(".bzip2", ZipCmd);
417  FExtToCmdH.AddDat(".bz2", ZipCmd);
418  }
419 }
bool Empty() const
Definition: hash.h:227
static TStrStrH FExtToCmdH
Definition: zipfl.h:77
TDat & AddDat(const TKey &Key)
Definition: hash.h:238

Here is the call graph for this function:

Here is the caller graph for this function:

void TZipOut::Flush ( )
virtual

Implements TSOut.

Definition at line 388 of file zipfl.cpp.

References EAssertR, FlushBf(), TSBase::GetSNm(), and ZipStdinWr.

388  {
389  FlushBf();
390  #ifdef GLib_WIN
391  EAssertR(FlushFileBuffers(ZipStdinWr)!=0, "Can not flush file '"+GetSNm()+"'.");
392  #else
393  EAssertR(fflush(ZipStdinWr)==0, "Can not flush file '"+GetSNm()+"'.");
394  #endif
395 }
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
virtual TStr GetSNm() const
Definition: fl.cpp:20
FILE * ZipStdinWr
Definition: zipfl.h:81
void FlushBf()
Definition: zipfl.cpp:296

Here is the call graph for this function:

void TZipOut::FlushBf ( )
private

Definition at line 296 of file zipfl.cpp.

References Bf, BfL, EAssert, EAssertR, TSBase::GetSNm(), and ZipStdinWr.

Referenced by Flush(), PutCh(), and ~TZipOut().

296  {
297  #ifdef GLib_WIN
298  DWORD BytesOut;
299  EAssertR(WriteFile(ZipStdinWr, Bf, DWORD(BfL), &BytesOut, NULL)!=0, "Error writting to the file '"+GetSNm()+"'.");
300  #else
301  size_t BytesOut = fwrite(Bf, 1, BfL, ZipStdinWr);
302  #endif
303  EAssert(BytesOut == BfL);
304  BfL = 0;
305 }
char * Bf
Definition: zipfl.h:83
TSize BfL
Definition: zipfl.h:84
#define EAssert(Cond)
Definition: bd.h:280
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
virtual TStr GetSNm() const
Definition: fl.cpp:20
FILE * ZipStdinWr
Definition: zipfl.h:81

Here is the call graph for this function:

Here is the caller graph for this function:

TStr TZipOut::GetCmd ( const TStr ZipFNm)
static

Return a command-line string that is executed in order to decompress a file to standard output.

Definition at line 421 of file zipfl.cpp.

References TStr::CStr(), EAssertR, THash< TKey, TDat, THashFunc >::Empty(), FExtToCmdH, FillFExtToCmdH(), TStr::Fmt(), THash< TKey, TDat, THashFunc >::GetDat(), TStr::GetFExt(), TStr::GetFMid(), TStr::GetLc(), and THash< TKey, TDat, THashFunc >::IsKey().

Referenced by TZipOut().

421  {
423  const TStr Ext = ZipFNm.GetFExt().GetLc();
424  EAssertR(FExtToCmdH.IsKey(Ext), TStr::Fmt("Unsupported file extension '%s'", Ext.CStr()));
425  return FExtToCmdH.GetDat(Ext)+ZipFNm.GetFMid();
426 }
TStr GetFMid() const
Definition: dt.cpp:1403
bool Empty() const
Definition: hash.h:227
TStr GetFExt() const
Definition: dt.cpp:1421
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
TStr GetLc() const
Definition: dt.h:499
static TStrStrH FExtToCmdH
Definition: zipfl.h:77
static void FillFExtToCmdH()
Definition: zipfl.cpp:402
Definition: dt.h:412
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
#define EAssertR(Cond, MsgStr)
Definition: bd.h:283
char * CStr()
Definition: dt.h:476
bool IsKey(const TKey &Key) const
Definition: hash.h:258

Here is the call graph for this function:

Here is the caller graph for this function:

bool TZipOut::IsZipExt ( const TStr FNmExt)
static

Check whether the file extension FNmExt is that of a compressed file (.gz, .7z, .rar, .zip, .cab, .arj. bzip2).

Definition at line 397 of file zipfl.cpp.

References THash< TKey, TDat, THashFunc >::Empty(), FExtToCmdH, FillFExtToCmdH(), and THash< TKey, TDat, THashFunc >::IsKey().

Referenced by IsZipFNm().

397  {
399  return FExtToCmdH.IsKey(FNmExt);
400 }
bool Empty() const
Definition: hash.h:227
static TStrStrH FExtToCmdH
Definition: zipfl.h:77
static void FillFExtToCmdH()
Definition: zipfl.cpp:402
bool IsKey(const TKey &Key) const
Definition: hash.h:258

Here is the call graph for this function:

Here is the caller graph for this function:

static bool TZipOut::IsZipFNm ( const TStr FNm)
inlinestatic

Check whether the file extension of FNm is that of a compressed file (.gz, .7z, .rar, .zip, .cab, .arj. bzip2).

Definition at line 103 of file zipfl.h.

References TStr::GetFExt(), and IsZipExt().

Referenced by NewIfZip().

103 { return IsZipExt(FNm.GetFExt()); }
TStr GetFExt() const
Definition: dt.cpp:1421
static bool IsZipExt(const TStr &FNmExt)
Check whether the file extension FNmExt is that of a compressed file (.gz, .7z, .rar, .zip, .cab, .arj. bzip2).
Definition: zipfl.cpp:397

Here is the call graph for this function:

Here is the caller graph for this function:

PSOut TZipOut::New ( const TStr FNm)
static

Definition at line 356 of file zipfl.cpp.

References TZipOut().

Referenced by NewIfZip().

356  {
357  return PSOut(new TZipOut(FNm));
358 }
TPt< TSOut > PSOut
Definition: fl.h:211

Here is the call graph for this function:

Here is the caller graph for this function:

static PSOut TZipOut::NewIfZip ( const TStr FNm)
inlinestatic

Definition at line 108 of file zipfl.h.

References IsZipFNm(), New(), and TFOut::New().

108 { return IsZipFNm(FNm) ? New(FNm) : TFOut::New(FNm); }
Definition: fl.h:319
static PSOut New(const TStr &FNm)
Definition: zipfl.cpp:356
static bool IsZipFNm(const TStr &FNm)
Check whether the file extension of FNm is that of a compressed file (.gz, .7z, .rar, .zip, .cab, .arj. bzip2).
Definition: zipfl.h:103

Here is the call graph for this function:

TZipOut& TZipOut::operator= ( const TZipOut )
private
int TZipOut::PutBf ( const void *  LBf,
const TSize LBfL 
)
virtual

Implements TSOut.

Definition at line 376 of file zipfl.cpp.

References Bf, BfL, MxBfL, and PutCh().

376  {
377  int LBfS=0;
378  if (BfL+LBfL>MxBfL){
379  for (TSize LBfC=0; LBfC<LBfL; LBfC++){
380  LBfS+=PutCh(((char*)LBf)[LBfC]);}
381  } else {
382  for (TSize LBfC=0; LBfC<LBfL; LBfC++){
383  LBfS+=(Bf[BfL++]=((char*)LBf)[LBfC]);}
384  }
385  return LBfS;
386 }
int PutCh(const char &Ch)
Definition: zipfl.cpp:371
char * Bf
Definition: zipfl.h:83
TSize BfL
Definition: zipfl.h:84
size_t TSize
Definition: bd.h:58
static const TSize MxBfL
Definition: zipfl.h:76

Here is the call graph for this function:

int TZipOut::PutCh ( const char &  Ch)
virtual

Implements TSOut.

Definition at line 371 of file zipfl.cpp.

References Bf, BfL, FlushBf(), and MxBfL.

Referenced by PutBf().

371  {
372  if (BfL==MxBfL) {FlushBf();}
373  return Bf[BfL++]=Ch;
374 }
char * Bf
Definition: zipfl.h:83
TSize BfL
Definition: zipfl.h:84
static const TSize MxBfL
Definition: zipfl.h:76
void FlushBf()
Definition: zipfl.cpp:296

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

char* TZipOut::Bf
private

Definition at line 83 of file zipfl.h.

Referenced by FlushBf(), PutBf(), PutCh(), TZipOut(), and ~TZipOut().

TSize TZipOut::BfL
private

Definition at line 84 of file zipfl.h.

Referenced by FlushBf(), PutBf(), PutCh(), TZipOut(), and ~TZipOut().

TStrStrH TZipOut::FExtToCmdH
staticprivate

Definition at line 77 of file zipfl.h.

Referenced by FillFExtToCmdH(), GetCmd(), and IsZipExt().

const TSize TZipOut::MxBfL =4*1024
staticprivate

Definition at line 76 of file zipfl.h.

Referenced by PutBf(), PutCh(), and TZipOut().

FILE* TZipOut::ZipStdinRd
private

Definition at line 81 of file zipfl.h.

Referenced by CreateZipProcess(), TZipOut(), and ~TZipOut().

FILE * TZipOut::ZipStdinWr
private

Definition at line 81 of file zipfl.h.

Referenced by CreateZipProcess(), Flush(), FlushBf(), TZipOut(), and ~TZipOut().


The documentation for this class was generated from the following files: