libatomprobe
Library for Atom Probe Tomography (APT) computation
helpFuncs.cpp
Go to the documentation of this file.
1 /* helpFuncs.cpp : Helper functions for libatomprobe
2  * Copyright (C) 2014 Daniel Haley
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include <cstring>
18 #include <algorithm>
19 #include <fstream>
20 #include <gsl/gsl_linalg.h>
21 
23 
24 #include "helpFuncs.h"
25 
26 //Needed for stat call on posix systems
27 #if !defined(__WIN32__) && !defined(__WIN64__)
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #endif
31 namespace AtomProbe
32 {
33 //Statically store the previous locale data,
34 // so that we can use push/pop locale
35 static char *oldLocaleStatic;
36 static int localeStaticType;
37 
38 
39 char *myStrDup(const char *s)
40 {
41  unsigned int bufSize=strlen(s)+1;
42  const unsigned int BUF_MAX=10000;
43 
44  if(bufSize > BUF_MAX)
45  return 0;
46 
47  char *buf =(char *)malloc(bufSize);
48  strncpy(buf,s,bufSize);
49 
50  return buf;
51 }
52 
53 #if defined(__CYGWIN__) || defined(__MINGW__)
54 int isascii(int c)
55 {
56  return c < 128 && c > 0;
57 }
58 #endif
59 
60 
61 
62 void pushLocale(const char *newLocale, int type)
63 {
64  ASSERT(!oldLocaleStatic);
65  ASSERT(!localeStaticType);
66 
67  ASSERT(type == LC_NUMERIC || type == LC_MONETARY || type == LC_CTYPE
68  || type == LC_COLLATE || type == LC_ALL || type == LC_TIME);
69 
70 
71  oldLocaleStatic=setlocale(type,NULL);
72 
73  //setlocale reserves the right to trash the returned pointer
74  // on subsequent calls (i.e. use the returned pointer for later)
75  // thus we must duplicate the pointer to own it
76  oldLocaleStatic=myStrDup(oldLocaleStatic);
77  if(!oldLocaleStatic)
78  {
79  localeStaticType=-1;
80  return;
81  }
82 
83  if(strcmp(oldLocaleStatic,newLocale))
84  {
85  setlocale(type,newLocale);
86  localeStaticType=type;
87  }
88  else
89  {
90  //record that we did not set this
91  localeStaticType=-1;
92  }
93 
94 }
95 
96 void popLocale()
97 {
98  if(localeStaticType != -1)
99  setlocale(localeStaticType,oldLocaleStatic);
100 
101  localeStaticType=0;
102 
103  free(oldLocaleStatic);
104  oldLocaleStatic=0;
105 }
106 
107 bool getFilesize(const char *fname, size_t &size)
108 {
109  std::ifstream f(fname,std::ios::binary);
110 
111  if(!f)
112  return false;
113 
114  f.seekg(0,std::ios::end);
115 
116  size = f.tellg();
117 
118  return true;
119 }
120 
121 
122 void gsl_print_matrix(const gsl_matrix *m)
123 {
124  for (size_t i = 0; i < m->size1; i++)
125  {
126  for (size_t j = 0; j < m->size2; j++)
127  printf("%g ", gsl_matrix_get(m, i, j));
128 
129  printf("\n");
130  }
131 
132 }
133 
134 void gsl_print_vector(const gsl_vector *v)
135 {
136  for (size_t i = 0; i < v->size; i++)
137  {
138  printf("%g ", gsl_vector_get(v, i));
139  }
140 
141 }
142 
143 float gsl_determinant(const gsl_matrix *m)
144 {
145  //Matrix must be square
146  ASSERT(m->size1==m->size2);
147 
148  //Create storage
149  gsl_permutation * p = gsl_permutation_alloc (m->size1);
150  gsl_matrix *tmpM=gsl_matrix_alloc(m->size1,m->size2);
151 
152  //Duplicate, as LU destroys input
153  gsl_matrix_memcpy(tmpM,m);
154 
155  //Perform decomposition
156  int sign;
157  gsl_linalg_LU_decomp (tmpM, p, &sign);
158  //Compute determinant.
159  float d=gsl_linalg_LU_det(tmpM,sign);
160 
161  //Free storage
162  gsl_matrix_free(tmpM);
163  gsl_permutation_free(p);
164 
165  return d;
166 }
167 
168 #if !defined(__WIN32__) && !defined(__WIN64)
169 //Confirm that the selected file is not a directory (posix-like systems only)
170 bool isNotDirectory(const char *filename)
171 {
172  struct stat statbuf;
173 
174  if(stat(filename,&statbuf) == -1)
175  return false;
176 
177  return (statbuf.st_mode !=S_IFDIR);
178 }
179 #endif
180 }
float gsl_determinant(const gsl_matrix *m)
Definition: helpFuncs.cpp:143
void gsl_print_matrix(const gsl_matrix *m)
Definition: helpFuncs.cpp:122
bool isNotDirectory(const char *filename)
Definition: helpFuncs.cpp:170
void pushLocale(const char *newLocale, int type)
Definition: helpFuncs.cpp:62
char * myStrDup(const char *s)
Definition: helpFuncs.cpp:39
bool getFilesize(const char *fname, size_t &size)
Definition: helpFuncs.cpp:107
void gsl_print_vector(const gsl_vector *v)
Definition: helpFuncs.cpp:134
void popLocale()
Definition: helpFuncs.cpp:96
#define ASSERT(f)