Strip extra spaces from code.
[voro++.git] / branches / 2d / src / common.hh
blob4c141232551544b76b819b4377cfe74ad7bf9591
1 // Voro++, a 3D cell-based Voronoi library
2 //
3 // Author : Chris H. Rycroft (LBL / UC Berkeley)
4 // Email : chr@alum.mit.edu
5 // Date : May 18th 2011
7 /** \file common.hh
8 * \brief Header file for the small helper functions. */
10 #ifndef VOROPP_COMMON_HH
11 #define VOROPP_COMMON_HH
13 #include <cstdio>
14 #include <cstdlib>
15 #include <vector>
16 using namespace std;
18 #include "config.hh"
20 namespace voro {
22 /** \brief Function for printing fatal error messages and exiting.
24 * Function for printing fatal error messages and exiting.
25 * \param[in] p a pointer to the message to print.
26 * \param[in] status the status code to return with. */
27 inline void voro_fatal_error(const char *p,int status) {
28 fprintf(stderr,"voro++: %s\n",p);
29 exit(status);
32 /** \brief Prints a vector of 2D positions.
34 * Prints a vector of positions as bracketed triplets.
35 * \param[in] v the vector to print.
36 * \param[in] fp the file stream to print to. */
37 inline void voro_print_positions_2d(vector<double> &v,FILE *fp=stdout) {
38 if(v.size()>0) {
39 fprintf(fp,"(%g,%g)",v[0],v[1]);
40 for(int k=2;(unsigned int) k<v.size();k+=2) {
41 fprintf(fp," (%g,%g)",v[k],v[k+1]);
46 /** \brief Prints a vector of positions.
48 * Prints a vector of positions as bracketed triplets.
49 * \param[in] v the vector to print.
50 * \param[in] fp the file stream to print to. */
51 inline void voro_print_positions(vector<double> &v,FILE *fp=stdout) {
52 if(v.size()>0) {
53 fprintf(fp,"(%g,%g,%g)",v[0],v[1],v[2]);
54 for(int k=3;(unsigned int) k<v.size();k+=3) {
55 fprintf(fp," (%g,%g,%g)",v[k],v[k+1],v[k+2]);
60 /** \brief Opens a file and checks the operation was successful.
62 * Opens a file, and checks the return value to ensure that the operation
63 * was successful.
64 * \param[in] filename the file to open.
65 * \param[in] mode the cstdio fopen mode to use.
66 * \return The file handle. */
67 inline FILE* safe_fopen(const char *filename,const char *mode) {
68 FILE *fp(fopen(filename,mode));
69 if(fp==NULL) {
70 fprintf(stderr,"voro++: Unable to open file '%s'\n",filename);
71 exit(VOROPP_FILE_ERROR);
73 return fp;
76 void voro_print_vector(vector<int> &v,FILE *fp=stdout);
77 void voro_print_vector(vector<double> &v,FILE *fp=stdout);
81 #endif