NCCLOUD  0.1
common.h
Go to the documentation of this file.
00001 
00007 /* ===================================================================
00008 Copyright (c) 2013, Henry C. H. Chen
00009 All rights reserved.
00010 
00011 Redistribution and use in source and binary forms, with or without
00012 modification, are permitted provided that the following conditions are
00013 met:
00014 
00015   - Redistributions of source code must retain the above copyright
00016     notice, this list of conditions and the following disclaimer.
00017 
00018   - Redistributions in binary form must reproduce the above copyright
00019     notice, this list of conditions and the following disclaimer in
00020     the documentation and/or other materials provided with the
00021     distribution.
00022 
00023   - Neither the name of the Chinese University of Hong Kong nor the
00024     names of its contributors may be used to endorse or promote
00025     products derived from this software without specific prior written
00026     permission.
00027 
00028 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00029 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00030 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00031 A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
00032 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00033 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00034 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00035 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00036 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00037 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00038 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00039 =================================================================== */
00040 
00041 
00042 #ifndef NCCLOUD_COMMON_H
00043 #define NCCLOUD_COMMON_H
00044 
00045 #include <cstdio>
00046 #include <cstdlib>
00047 #include <errno.h>
00048 #include <iostream>
00049 #include <sstream>
00050 
00052 #define show_error(call) do { \
00053     fprintf(stderr, "%c[1;31;40m", 0x1B); \
00054     fprintf(stderr, "%s(%d) in %s:: ", __FILE__, __LINE__, __func__); \
00055     perror(call); \
00056     fprintf(stderr, "%c[0m", 0x1B); \
00057     exit(-1); } while (0)
00058 
00060 #define show_file_error(call, filename, fp) do { \
00061     fprintf(stderr, "%c[1;31;40m", 0x1B); \
00062     fprintf(stderr, "%s(%d) in %s:: ", __FILE__, __LINE__, __func__); \
00063     perror(call); \
00064     fprintf(stderr, "\twhen working with file: %s\n", filename); \
00065     fprintf(stderr, "%c[0m", 0x1B); \
00066     if (fp) { fclose(fp); } \
00067     exit(-1); } while (0)
00068 
00069 
00071 inline void print(std::ostream &s)
00072 {
00073   std::cout << s.rdbuf();
00074   std::cout.flush();
00075   s.clear();
00076 }
00077 
00078 
00080 inline void print_error(std::ostream &s)
00081 {
00082   std::cerr << s.rdbuf();
00083   std::cerr.flush();
00084   s.clear();
00085 }
00086 
00087 
00089 template <typename T>
00090 void reset_array(T **array, unsigned int size=0)
00091 {
00092   if (*array != NULL) {
00093     delete[] *array;
00094   }
00095   *array = size? (new T[size]) : NULL;
00096 }
00097 
00098 
00099 inline void write_file(std::string dst, char *data, size_t size)
00100 {
00101   FILE *fp = fopen(dst.c_str(), "wb");
00102   if (fp == NULL) {
00103     show_file_error("fopen", dst.c_str(), NULL);
00104   }
00105   if (fwrite(data, 1, size, fp) != size) {
00106     show_file_error("fwrite", dst.c_str(), fp);
00107   }
00108   fclose(fp);
00109 }
00110 
00111 #endif  /* NCCLOUD_COMMON_H */
00112 
 All Data Structures Files Functions Variables Enumerations Enumerator Defines