"Fossies" - the Fresh Open Source Software Archive 
Member "muscle/html/muscle-by-example/examples/tarfilewriter/example_1_basic_usage.cpp" (21 Nov 2020, 2102 Bytes) of package /linux/privat/muscle7.62.zip:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the latest
Fossies "Diffs" side-by-side code changes report for "example_1_basic_usage.cpp":
7.61_vs_7.62.
1 #include <fcntl.h> // for the S_IR* macros
2 #include "system/SetupSystem.h" // for CompleteSetupSystem
3 #include "zlib/TarFileWriter.h"
4
5 using namespace muscle;
6
7 static void PrintExampleDescription()
8 {
9 printf("\n");
10 printf("This demonstrates basic usage of the muscle::TarFileWriter class by writing out a .tar file\n");
11 printf("\n");
12 }
13
14 static status_t WriteFakeFileDataToTarFile(TarFileWriter & writer, const char * fakeFileName)
15 {
16 #ifdef WIN32
17 const uint32 fileMode = 0777; // Windows doesn't have the S_* macros defined :(
18 #else
19 const uint32 fileMode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH;
20 #endif
21
22 status_t ret;
23 if (writer.WriteFileHeader(fakeFileName, fileMode, 0, 0, GetCurrentTime64(), TarFileWriter::TAR_LINK_INDICATOR_NORMAL_FILE, NULL, 0).IsError(ret)) return ret;
24
25 // Generate some fake data for our fake file to contain
26 uint8 fakeDataBuf[1024];
27 const uint8 dummyBuf[] = "All work and no play make Jack a dull boy. ";
28 for (uint32 i=0; i<sizeof(fakeDataBuf); i++) fakeDataBuf[i] = dummyBuf[i%(sizeof(dummyBuf)-1)];
29
30 return writer.WriteFileData(fakeDataBuf, sizeof(fakeDataBuf));
31 }
32
33 int main(int argc, char ** argv)
34 {
35 CompleteSetupSystem css;
36
37 PrintExampleDescription();
38
39 status_t ret;
40
41 const char * outputFileName = "./example_output.tar";
42 TarFileWriter writer(outputFileName, false);
43
44 const char * fakeFileNames[] = {
45 "file1.bin",
46 "file2.bin",
47 "file3.bin"
48 };
49 for (uint32 i=0; i<ARRAYITEMS(fakeFileNames); i++)
50 {
51 if (WriteFakeFileDataToTarFile(writer, fakeFileNames[i]).IsError(ret))
52 {
53 LogTime(MUSCLE_LOG_ERROR, "Error writing fake file data for [%s] to .tar file [%s], aborting! [%s]\n", fakeFileNames[i], outputFileName, ret());
54 return 10;
55 }
56 }
57
58 LogTime(MUSCLE_LOG_INFO, "Output file [%s] created.\n", outputFileName);
59 LogTime(MUSCLE_LOG_INFO, "Run \"tar tvf %s\" list its contents.\n", outputFileName);
60 LogTime(MUSCLE_LOG_INFO, "Run \"tar xvf %s\" un-tar its contents.\n", outputFileName);
61
62 printf("\n");
63 return 0;
64 }