JWS C Library
C language utility library
|
00001 /* 00002 * This work is licensed under a Creative Commons 00003 * Attribution-Noncommercial-Share Alike 3.0 United States License. 00004 * 00005 * http://creativecommons.org/licenses/by-nc-sa/3.0/us/ 00006 * 00007 * You are free: 00008 * 00009 * to Share - to copy, distribute, display, and perform the work 00010 * to Remix - to make derivative works 00011 * 00012 * Under the following conditions: 00013 * 00014 * Attribution. You must attribute the work in the manner specified by the 00015 * author or licensor (but not in any way that suggests that they endorse you 00016 * or your use of the work). 00017 * 00018 * Noncommercial. You may not use this work for commercial purposes. 00019 * 00020 * Share Alike. If you alter, transform, or build upon this work, you may 00021 * distribute the resulting work only under the same or similar license to 00022 * this one. 00023 * 00024 * For any reuse or distribution, you must make clear to others the license 00025 * terms of this work. The best way to do this is by including this header. 00026 * 00027 * Any of the above conditions can be waived if you get permission from the 00028 * copyright holder. 00029 * 00030 * Apart from the remix rights granted under this license, nothing in this 00031 * license impairs or restricts the author's moral rights. 00032 */ 00033 00034 00046 #include <jwsc/config.h> 00047 00048 #include <stdlib.h> 00049 #include <stdio.h> 00050 #include <string.h> 00051 #include <errno.h> 00052 #include <assert.h> 00053 #include <inttypes.h> 00054 #include <ctype.h> 00055 00056 #include "jwsc/base/error.h" 00057 #include "jwsc/base/file_io.h" 00058 #include "jwsc/math/complex.h" 00059 #include "jwsc/vector/vector.h" 00060 #include "jwsc/vector/vector_io.h" 00061 00062 #ifdef JWSC_HAVE_DMALLOC 00063 #include <dmalloc.h> 00064 #endif 00065 00066 00068 static int read_vector_line(char** line, FILE* fp) 00069 { 00070 static const uint32_t block_size = 1024; 00071 00072 uint32_t num_elts; 00073 uint32_t blocks; 00074 00075 char c; 00076 00077 num_elts = 0; 00078 blocks = 0; 00079 00080 while ((c = fgetc(fp)) != EOF && c != '\n') 00081 { 00082 if (num_elts % block_size == 0) 00083 { 00084 *line = realloc(*line, (++blocks)*block_size*sizeof(char)); 00085 assert(*line); 00086 } 00087 00088 (*line)[ num_elts++ ] = c; 00089 } 00090 00091 if (num_elts % block_size == 0) 00092 { 00093 *line = realloc(*line, (blocks*block_size+1)*sizeof(char)); 00094 assert(*line); 00095 } 00096 (*line)[ num_elts ] = '\0'; 00097 00098 return (c == EOF); 00099 } 00100 00101 00103 static uint32_t count_vector_line_columns(const char* line) 00104 { 00105 uint32_t count; 00106 uint32_t done; 00107 uint32_t in_column; 00108 00109 const char* c; 00110 00111 count = 0; 00112 done = 0; 00113 in_column = 0; 00114 00115 c = line; 00116 00117 while(!done) 00118 { 00119 if (*c == '\0') 00120 { 00121 done = 1; 00122 } 00123 else if (isspace(*c)) 00124 { 00125 in_column = 0; 00126 } 00127 else if (!in_column) 00128 { 00129 count++; 00130 in_column = 1; 00131 } 00132 c++; 00133 } 00134 00135 return count; 00136 } 00137 00138 00140 static const char* advance_line_column_ptr(const char* col) 00141 { 00142 while (*col != ' ' && *col != '\0') col++; 00143 00144 while (*col == ' ' && *col != '\0') col++; 00145 00146 return col; 00147 } 00148 00149 00150 00151 00179 Error* read_vector_u32(Vector_u32** v_out, const char* fname) 00180 { 00181 FILE* fp; 00182 Error* e; 00183 00184 if ((fp = fopen(fname, "r")) == NULL) 00185 { 00186 free_vector_u32(*v_out); *v_out = NULL; 00187 return JWSC_EIO(strerror(errno)); 00188 } 00189 00190 if ((e = read_vector_fp_u32(v_out, fp)) != NULL) 00191 { 00192 fclose(fp); 00193 return e; 00194 } 00195 00196 if (fclose(fp) != 0) 00197 { 00198 free_vector_u32(*v_out); *v_out = NULL; 00199 return JWSC_EIO(strerror(errno)); 00200 } 00201 00202 return NULL; 00203 } 00204 00225 Error* read_vector_i32(Vector_i32** v_out, const char* fname) 00226 { 00227 FILE* fp; 00228 Error* e; 00229 00230 if ((fp = fopen(fname, "r")) == NULL) 00231 { 00232 free_vector_i32(*v_out); *v_out = NULL; 00233 return JWSC_EIO(strerror(errno)); 00234 } 00235 00236 if ((e = read_vector_fp_i32(v_out, fp)) != NULL) 00237 { 00238 fclose(fp); 00239 return e; 00240 } 00241 00242 if (fclose(fp) != 0) 00243 { 00244 free_vector_i32(*v_out); *v_out = NULL; 00245 return JWSC_EIO(strerror(errno)); 00246 } 00247 00248 return NULL; 00249 } 00250 00271 Error* read_vector_i64(Vector_i64** v_out, const char* fname) 00272 { 00273 FILE* fp; 00274 Error* e; 00275 00276 if ((fp = fopen(fname, "r")) == NULL) 00277 { 00278 free_vector_i64(*v_out); *v_out = NULL; 00279 return JWSC_EIO(strerror(errno)); 00280 } 00281 00282 if ((e = read_vector_fp_i64(v_out, fp)) != NULL) 00283 { 00284 fclose(fp); 00285 return e; 00286 } 00287 00288 if (fclose(fp) != 0) 00289 { 00290 free_vector_i64(*v_out); *v_out = NULL; 00291 return JWSC_EIO(strerror(errno)); 00292 } 00293 00294 return NULL; 00295 } 00296 00317 Error* read_vector_f(Vector_f** v_out, const char* fname) 00318 { 00319 FILE* fp; 00320 Error* e; 00321 00322 if ((fp = fopen(fname, "r")) == NULL) 00323 { 00324 free_vector_f(*v_out); *v_out = NULL; 00325 return JWSC_EIO(strerror(errno)); 00326 } 00327 00328 if ((e = read_vector_fp_f(v_out, fp)) != NULL) 00329 { 00330 fclose(fp); 00331 return e; 00332 } 00333 00334 if (fclose(fp) != 0) 00335 { 00336 free_vector_f(*v_out); *v_out = NULL; 00337 return JWSC_EIO(strerror(errno)); 00338 } 00339 00340 return NULL; 00341 } 00342 00363 Error* read_vector_d(Vector_d** v_out, const char* fname) 00364 { 00365 FILE* fp; 00366 Error* e; 00367 00368 if ((fp = fopen(fname, "r")) == NULL) 00369 { 00370 free_vector_d(*v_out); *v_out = NULL; 00371 return JWSC_EIO(strerror(errno)); 00372 } 00373 00374 if ((e = read_vector_fp_d(v_out, fp)) != NULL) 00375 { 00376 fclose(fp); 00377 return e; 00378 } 00379 00380 if (fclose(fp) != 0) 00381 { 00382 free_vector_d(*v_out); *v_out = NULL; 00383 return JWSC_EIO(strerror(errno)); 00384 } 00385 00386 return NULL; 00387 } 00388 00409 Error* read_vector_cf(Vector_cf** v_out, const char* fname) 00410 { 00411 FILE* fp; 00412 Error* e; 00413 00414 if ((fp = fopen(fname, "r")) == NULL) 00415 { 00416 free_vector_cf(*v_out); *v_out = NULL; 00417 return JWSC_EIO(strerror(errno)); 00418 } 00419 00420 if ((e = read_vector_fp_cf(v_out, fp)) != NULL) 00421 { 00422 fclose(fp); 00423 return e; 00424 } 00425 00426 if (fclose(fp) != 0) 00427 { 00428 free_vector_cf(*v_out); *v_out = NULL; 00429 return JWSC_EIO(strerror(errno)); 00430 } 00431 00432 return NULL; 00433 } 00434 00455 Error* read_vector_cd(Vector_cd** v_out, const char* fname) 00456 { 00457 FILE* fp; 00458 Error* e; 00459 00460 if ((fp = fopen(fname, "r")) == NULL) 00461 { 00462 free_vector_cd(*v_out); *v_out = NULL; 00463 return JWSC_EIO(strerror(errno)); 00464 } 00465 00466 if ((e = read_vector_fp_cd(v_out, fp)) != NULL) 00467 { 00468 fclose(fp); 00469 return e; 00470 } 00471 00472 if (fclose(fp) != 0) 00473 { 00474 free_vector_cd(*v_out); *v_out = NULL; 00475 return JWSC_EIO(strerror(errno)); 00476 } 00477 00478 return NULL; 00479 } 00480 00513 Error* read_vector_fp_u32(Vector_u32** v_out, FILE* fp) 00514 { 00515 static const uint32_t block_size = 1024; 00516 00517 uint32_t num_elts, num_cols; 00518 uint32_t elt, col; 00519 uint32_t blocks; 00520 00521 char* line = NULL; 00522 const char* line_col; 00523 00524 uint32_t val; 00525 uint32_t* elts = NULL; 00526 uint32_t* v_elts = NULL; 00527 00528 if (skip_fp_spaces_and_comments(fp) == 0) 00529 { 00530 free_vector_u32(*v_out); *v_out = NULL; 00531 return JWSC_EIO("Nothing to read"); 00532 } 00533 00534 num_cols = 0; 00535 num_elts = 0; 00536 blocks = 0; 00537 00538 read_vector_line(&line, fp); 00539 00540 if ((num_cols = count_vector_line_columns(line)) == 0) 00541 { 00542 free(line); 00543 free_vector_u32(*v_out); *v_out = NULL; 00544 return JWSC_EIO("Vector to read not formatted properly"); 00545 } 00546 00547 line_col = line; 00548 00549 for (col = 0; col < num_cols; col++) 00550 { 00551 if (sscanf(line_col, "%d", &val) != 1) 00552 { 00553 free(line); 00554 free(elts); 00555 free_vector_u32(*v_out); *v_out = NULL; 00556 return JWSC_EIO("Vector to read not formatted properly"); 00557 } 00558 00559 line_col = advance_line_column_ptr(line_col); 00560 00561 if (num_elts % block_size == 0) 00562 { 00563 elts = realloc(elts, (++blocks)*block_size*sizeof(uint32_t)); 00564 assert(elts); 00565 } 00566 00567 elts[ num_elts++ ] = val; 00568 } 00569 00570 create_vector_u32(v_out, num_elts); 00571 v_elts = (*v_out)->elts; 00572 00573 for (elt = 0; elt < num_elts; elt++) 00574 { 00575 v_elts[ elt ] = elts[ elt ]; 00576 } 00577 00578 free(line); 00579 free(elts); 00580 00581 return NULL; 00582 } 00583 00604 Error* read_vector_fp_i32(Vector_i32** v_out, FILE* fp) 00605 { 00606 static const uint32_t block_size = 1024; 00607 00608 uint32_t num_elts, num_cols; 00609 uint32_t elt, col; 00610 uint32_t blocks; 00611 00612 char* line = NULL; 00613 const char* line_col; 00614 00615 int32_t val; 00616 int32_t* elts = NULL; 00617 int32_t* v_elts = NULL; 00618 00619 if (skip_fp_spaces_and_comments(fp) == 0) 00620 { 00621 free_vector_i32(*v_out); *v_out = NULL; 00622 return JWSC_EIO("Nothing to read"); 00623 } 00624 00625 num_cols = 0; 00626 num_elts = 0; 00627 blocks = 0; 00628 00629 read_vector_line(&line, fp); 00630 00631 if ((num_cols = count_vector_line_columns(line)) == 0) 00632 { 00633 free(line); 00634 free_vector_i32(*v_out); *v_out = NULL; 00635 return JWSC_EIO("Vector to read not formatted properly"); 00636 } 00637 00638 line_col = line; 00639 00640 for (col = 0; col < num_cols; col++) 00641 { 00642 if (sscanf(line_col, "%d", &val) != 1) 00643 { 00644 free(line); 00645 free(elts); 00646 free_vector_i32(*v_out); *v_out = NULL; 00647 return JWSC_EIO("Vector to read not formatted properly"); 00648 } 00649 00650 line_col = advance_line_column_ptr(line_col); 00651 00652 if (num_elts % block_size == 0) 00653 { 00654 elts = realloc(elts, (++blocks)*block_size*sizeof(int32_t)); 00655 assert(elts); 00656 } 00657 00658 elts[ num_elts++ ] = val; 00659 } 00660 00661 create_vector_i32(v_out, num_elts); 00662 v_elts = (*v_out)->elts; 00663 00664 for (elt = 0; elt < num_elts; elt++) 00665 { 00666 v_elts[ elt ] = elts[ elt ]; 00667 } 00668 00669 free(line); 00670 free(elts); 00671 00672 return NULL; 00673 } 00674 00695 Error* read_vector_fp_i64(Vector_i64** v_out, FILE* fp) 00696 { 00697 static const uint32_t block_size = 1024; 00698 00699 uint32_t num_elts, num_cols; 00700 uint32_t elt, col; 00701 uint32_t blocks; 00702 00703 char* line = NULL; 00704 const char* line_col; 00705 00706 int64_t val; 00707 int64_t* elts = NULL; 00708 int64_t* v_elts = NULL; 00709 00710 if (skip_fp_spaces_and_comments(fp) == 0) 00711 { 00712 free_vector_i64(*v_out); *v_out = NULL; 00713 return JWSC_EIO("Nothing to read"); 00714 } 00715 00716 num_cols = 0; 00717 num_elts = 0; 00718 blocks = 0; 00719 00720 read_vector_line(&line, fp); 00721 00722 if ((num_cols = count_vector_line_columns(line)) == 0) 00723 { 00724 free(line); 00725 free_vector_i64(*v_out); *v_out = NULL; 00726 return JWSC_EIO("Vector to read not formatted properly"); 00727 } 00728 00729 line_col = line; 00730 00731 for (col = 0; col < num_cols; col++) 00732 { 00733 if (sscanf(line_col, "%" SCNd64, &val) != 1) 00734 { 00735 free(line); 00736 free(elts); 00737 free_vector_i64(*v_out); *v_out = NULL; 00738 return JWSC_EIO("Vector to read not formatted properly"); 00739 } 00740 00741 line_col = advance_line_column_ptr(line_col); 00742 00743 if (num_elts % block_size == 0) 00744 { 00745 elts = realloc(elts, (++blocks)*block_size*sizeof(int64_t)); 00746 assert(elts); 00747 } 00748 00749 elts[ num_elts++ ] = val; 00750 } 00751 00752 create_vector_i64(v_out, num_elts); 00753 v_elts = (*v_out)->elts; 00754 00755 for (elt = 0; elt < num_elts; elt++) 00756 { 00757 v_elts[ elt ] = elts[ elt ]; 00758 } 00759 00760 free(line); 00761 free(elts); 00762 00763 return NULL; 00764 } 00765 00786 Error* read_vector_fp_f(Vector_f** v_out, FILE* fp) 00787 { 00788 static const uint32_t block_size = 1024; 00789 00790 uint32_t num_elts, num_cols; 00791 uint32_t elt, col; 00792 uint32_t blocks; 00793 00794 char* line = NULL; 00795 const char* line_col; 00796 00797 float val; 00798 float* elts = NULL; 00799 float* v_elts = NULL; 00800 00801 if (skip_fp_spaces_and_comments(fp) == 0) 00802 { 00803 free_vector_f(*v_out); *v_out = NULL; 00804 return JWSC_EIO("Nothing to read"); 00805 } 00806 00807 num_cols = 0; 00808 num_elts = 0; 00809 blocks = 0; 00810 00811 read_vector_line(&line, fp); 00812 00813 if ((num_cols = count_vector_line_columns(line)) == 0) 00814 { 00815 free(line); 00816 free_vector_f(*v_out); *v_out = NULL; 00817 return JWSC_EIO("Vector to read not formatted properly"); 00818 } 00819 00820 line_col = line; 00821 00822 for (col = 0; col < num_cols; col++) 00823 { 00824 if (sscanf(line_col, "%f", &val) != 1) 00825 { 00826 free(line); 00827 free(elts); 00828 free_vector_f(*v_out); *v_out = NULL; 00829 return JWSC_EIO("Vector to read not formatted properly"); 00830 } 00831 00832 line_col = advance_line_column_ptr(line_col); 00833 00834 if (num_elts % block_size == 0) 00835 { 00836 elts = realloc(elts, (++blocks)*block_size*sizeof(float)); 00837 assert(elts); 00838 } 00839 00840 elts[ num_elts++ ] = val; 00841 } 00842 00843 create_vector_f(v_out, num_elts); 00844 v_elts = (*v_out)->elts; 00845 00846 for (elt = 0; elt < num_elts; elt++) 00847 { 00848 v_elts[ elt ] = elts[ elt ]; 00849 } 00850 00851 free(line); 00852 free(elts); 00853 00854 return NULL; 00855 } 00856 00877 Error* read_vector_fp_d(Vector_d** v_out, FILE* fp) 00878 { 00879 static const uint32_t block_size = 1024; 00880 00881 uint32_t num_elts, num_cols; 00882 uint32_t elt, col; 00883 uint32_t blocks; 00884 00885 char* line = NULL; 00886 const char* line_col; 00887 00888 double val; 00889 double* elts = NULL; 00890 double* v_elts = NULL; 00891 00892 if (skip_fp_spaces_and_comments(fp) == 0) 00893 { 00894 free_vector_d(*v_out); *v_out = NULL; 00895 return JWSC_EIO("Nothing to read"); 00896 } 00897 00898 num_cols = 0; 00899 num_elts = 0; 00900 blocks = 0; 00901 00902 read_vector_line(&line, fp); 00903 00904 if ((num_cols = count_vector_line_columns(line)) == 0) 00905 { 00906 free(line); 00907 free_vector_d(*v_out); *v_out = NULL; 00908 return JWSC_EIO("Vector to read not formatted properly"); 00909 } 00910 00911 line_col = line; 00912 00913 for (col = 0; col < num_cols; col++) 00914 { 00915 if (sscanf(line_col, "%lf", &val) != 1) 00916 { 00917 free(line); 00918 free(elts); 00919 free_vector_d(*v_out); *v_out = NULL; 00920 return JWSC_EIO("Vector to read not formatted properly"); 00921 } 00922 00923 line_col = advance_line_column_ptr(line_col); 00924 00925 if (num_elts % block_size == 0) 00926 { 00927 elts = realloc(elts, (++blocks)*block_size*sizeof(double)); 00928 assert(elts); 00929 } 00930 00931 elts[ num_elts++ ] = val; 00932 } 00933 00934 create_vector_d(v_out, num_elts); 00935 v_elts = (*v_out)->elts; 00936 00937 for (elt = 0; elt < num_elts; elt++) 00938 { 00939 v_elts[ elt ] = elts[ elt ]; 00940 } 00941 00942 free(line); 00943 free(elts); 00944 00945 return NULL; 00946 } 00947 00968 Error* read_vector_fp_cf(Vector_cf** v_out, FILE* fp) 00969 { 00970 static const uint32_t block_size = 1024; 00971 00972 uint32_t num_elts, num_cols; 00973 uint32_t elt, col; 00974 uint32_t blocks; 00975 00976 char* line = NULL; 00977 const char* line_col; 00978 00979 Complex_f val; 00980 Complex_f* elts = NULL; 00981 Complex_f* v_elts = NULL; 00982 00983 if (skip_fp_spaces_and_comments(fp) == 0) 00984 { 00985 free_vector_cf(*v_out); *v_out = NULL; 00986 return JWSC_EIO("Nothing to read"); 00987 } 00988 00989 num_cols = 0; 00990 num_elts = 0; 00991 blocks = 0; 00992 00993 read_vector_line(&line, fp); 00994 00995 if ((num_cols = count_vector_line_columns(line)) == 0) 00996 { 00997 free(line); 00998 free_vector_cf(*v_out); *v_out = NULL; 00999 return JWSC_EIO("Vector to read not formatted properly"); 01000 } 01001 01002 line_col = line; 01003 01004 for (col = 0; col < num_cols; col++) 01005 { 01006 if (sscanf(line_col, "%f %f", &(val.r), &(val.i)) != 2) 01007 { 01008 free(line); 01009 free(elts); 01010 free_vector_cf(*v_out); *v_out = NULL; 01011 return JWSC_EIO("Vector to read not formatted properly"); 01012 } 01013 01014 line_col = advance_line_column_ptr(line_col); 01015 line_col = advance_line_column_ptr(line_col); 01016 01017 if (num_elts % block_size == 0) 01018 { 01019 elts = realloc(elts, (++blocks)*block_size*sizeof(Complex_f)); 01020 assert(elts); 01021 } 01022 01023 elts[ num_elts++ ] = val; 01024 } 01025 01026 create_vector_cf(v_out, num_elts); 01027 v_elts = (*v_out)->elts; 01028 01029 for (elt = 0; elt < num_elts; elt++) 01030 { 01031 v_elts[ elt ] = elts[ elt ]; 01032 } 01033 01034 free(line); 01035 free(elts); 01036 01037 return NULL; 01038 } 01039 01060 Error* read_vector_fp_cd(Vector_cd** v_out, FILE* fp) 01061 { 01062 static const uint32_t block_size = 1024; 01063 01064 uint32_t num_elts, num_cols; 01065 uint32_t elt, col; 01066 uint32_t blocks; 01067 01068 char* line = NULL; 01069 const char* line_col; 01070 01071 Complex_d val; 01072 Complex_d* elts = NULL; 01073 Complex_d* v_elts = NULL; 01074 01075 if (skip_fp_spaces_and_comments(fp) == 0) 01076 { 01077 free_vector_cd(*v_out); *v_out = NULL; 01078 return JWSC_EIO("Nothing to read"); 01079 } 01080 01081 num_cols = 0; 01082 num_elts = 0; 01083 blocks = 0; 01084 01085 read_vector_line(&line, fp); 01086 01087 if ((num_cols = count_vector_line_columns(line)) == 0) 01088 { 01089 free(line); 01090 free_vector_cd(*v_out); *v_out = NULL; 01091 return JWSC_EIO("Vector to read not formatted properly"); 01092 } 01093 01094 line_col = line; 01095 01096 for (col = 0; col < num_cols; col++) 01097 { 01098 if (sscanf(line_col, "%lf %lf", &(val.r), &(val.i)) != 2) 01099 { 01100 free(line); 01101 free(elts); 01102 free_vector_cd(*v_out); *v_out = NULL; 01103 return JWSC_EIO("Vector to read not formatted properly"); 01104 } 01105 01106 line_col = advance_line_column_ptr(line_col); 01107 line_col = advance_line_column_ptr(line_col); 01108 01109 if (num_elts % block_size == 0) 01110 { 01111 elts = realloc(elts, (++blocks)*block_size*sizeof(Complex_d)); 01112 assert(elts); 01113 } 01114 01115 elts[ num_elts++ ] = val; 01116 } 01117 01118 create_vector_cd(v_out, num_elts); 01119 v_elts = (*v_out)->elts; 01120 01121 for (elt = 0; elt < num_elts; elt++) 01122 { 01123 v_elts[ elt ] = elts[ elt ]; 01124 } 01125 01126 free(line); 01127 free(elts); 01128 01129 return NULL; 01130 } 01131 01159 Error* write_vector_u32(const Vector_u32* v, const char* fname) 01160 { 01161 FILE* fp; 01162 Error* e; 01163 01164 if ((fp = fopen(fname, "w")) == NULL) 01165 { 01166 return JWSC_EIO(strerror(errno)); 01167 } 01168 01169 if ((e = write_vector_fp_u32(v, fp)) != NULL) 01170 { 01171 fclose(fp); 01172 return e; 01173 } 01174 01175 if (fclose(fp) != 0) 01176 { 01177 return JWSC_EIO(strerror(errno)); 01178 } 01179 01180 return NULL; 01181 } 01182 01198 Error* write_vector_i32(const Vector_i32* v, const char* fname) 01199 { 01200 FILE* fp; 01201 Error* e; 01202 01203 if ((fp = fopen(fname, "w")) == NULL) 01204 { 01205 return JWSC_EIO(strerror(errno)); 01206 } 01207 01208 if ((e = write_vector_fp_i32(v, fp)) != NULL) 01209 { 01210 fclose(fp); 01211 return e; 01212 } 01213 01214 if (fclose(fp) != 0) 01215 { 01216 return JWSC_EIO(strerror(errno)); 01217 } 01218 01219 return NULL; 01220 } 01221 01237 Error* write_vector_i64(const Vector_i64* v, const char* fname) 01238 { 01239 FILE* fp; 01240 Error* e; 01241 01242 if ((fp = fopen(fname, "w")) == NULL) 01243 { 01244 return JWSC_EIO(strerror(errno)); 01245 } 01246 01247 if ((e = write_vector_fp_i64(v, fp)) != NULL) 01248 { 01249 fclose(fp); 01250 return e; 01251 } 01252 01253 if (fclose(fp) != 0) 01254 { 01255 return JWSC_EIO(strerror(errno)); 01256 } 01257 01258 return NULL; 01259 } 01260 01276 Error* write_vector_f(const Vector_f* v, const char* fname) 01277 { 01278 FILE* fp; 01279 Error* e; 01280 01281 if ((fp = fopen(fname, "w")) == NULL) 01282 { 01283 return JWSC_EIO(strerror(errno)); 01284 } 01285 01286 if ((e = write_vector_fp_f(v, fp)) != NULL) 01287 { 01288 fclose(fp); 01289 return e; 01290 } 01291 01292 if (fclose(fp) != 0) 01293 { 01294 return JWSC_EIO(strerror(errno)); 01295 } 01296 01297 return NULL; 01298 } 01299 01315 Error* write_vector_d(const Vector_d* v, const char* fname) 01316 { 01317 FILE* fp; 01318 Error* e; 01319 01320 if ((fp = fopen(fname, "w")) == NULL) 01321 { 01322 return JWSC_EIO(strerror(errno)); 01323 } 01324 01325 if ((e = write_vector_fp_d(v, fp)) != NULL) 01326 { 01327 fclose(fp); 01328 return e; 01329 } 01330 01331 if (fclose(fp) != 0) 01332 { 01333 return JWSC_EIO(strerror(errno)); 01334 } 01335 01336 return NULL; 01337 } 01338 01354 Error* write_vector_cf(const Vector_cf* v, const char* fname) 01355 { 01356 FILE* fp; 01357 Error* e; 01358 01359 if ((fp = fopen(fname, "w")) == NULL) 01360 { 01361 return JWSC_EIO(strerror(errno)); 01362 } 01363 01364 if ((e = write_vector_fp_cf(v, fp)) != NULL) 01365 { 01366 fclose(fp); 01367 return e; 01368 } 01369 01370 if (fclose(fp) != 0) 01371 { 01372 return JWSC_EIO(strerror(errno)); 01373 } 01374 01375 return NULL; 01376 } 01377 01393 Error* write_vector_cd(const Vector_cd* v, const char* fname) 01394 { 01395 FILE* fp; 01396 Error* e; 01397 01398 if ((fp = fopen(fname, "w")) == NULL) 01399 { 01400 return JWSC_EIO(strerror(errno)); 01401 } 01402 01403 if ((e = write_vector_fp_cd(v, fp)) != NULL) 01404 { 01405 fclose(fp); 01406 return e; 01407 } 01408 01409 if (fclose(fp) != 0) 01410 { 01411 return JWSC_EIO(strerror(errno)); 01412 } 01413 01414 return NULL; 01415 } 01416 01445 Error* write_formatted_vector_u32 01446 ( 01447 const Vector_u32* v, 01448 const char* fmt, 01449 const char* fname 01450 ) 01451 { 01452 FILE* fp; 01453 Error* e; 01454 01455 if ((fp = fopen(fname, "w")) == NULL) 01456 { 01457 return JWSC_EIO(strerror(errno)); 01458 } 01459 01460 if ((e = write_formatted_vector_fp_u32(v, fmt, fp)) != NULL) 01461 { 01462 fclose(fp); 01463 return e; 01464 } 01465 01466 if (fclose(fp) != 0) 01467 { 01468 return JWSC_EIO(strerror(errno)); 01469 } 01470 01471 return NULL; 01472 } 01473 01490 Error* write_formatted_vector_i32 01491 ( 01492 const Vector_i32* v, 01493 const char* fmt, 01494 const char* fname 01495 ) 01496 { 01497 FILE* fp; 01498 Error* e; 01499 01500 if ((fp = fopen(fname, "w")) == NULL) 01501 { 01502 return JWSC_EIO(strerror(errno)); 01503 } 01504 01505 if ((e = write_formatted_vector_fp_i32(v, fmt, fp)) != NULL) 01506 { 01507 fclose(fp); 01508 return e; 01509 } 01510 01511 if (fclose(fp) != 0) 01512 { 01513 return JWSC_EIO(strerror(errno)); 01514 } 01515 01516 return NULL; 01517 } 01518 01535 Error* write_formatted_vector_i64 01536 ( 01537 const Vector_i64* v, 01538 const char* fmt, 01539 const char* fname 01540 ) 01541 { 01542 FILE* fp; 01543 Error* e; 01544 01545 if ((fp = fopen(fname, "w")) == NULL) 01546 { 01547 return JWSC_EIO(strerror(errno)); 01548 } 01549 01550 if ((e = write_formatted_vector_fp_i64(v, fmt, fp)) != NULL) 01551 { 01552 fclose(fp); 01553 return e; 01554 } 01555 01556 if (fclose(fp) != 0) 01557 { 01558 return JWSC_EIO(strerror(errno)); 01559 } 01560 01561 return NULL; 01562 } 01563 01580 Error* write_formatted_vector_f 01581 ( 01582 const Vector_f* v, 01583 const char* fmt, 01584 const char* fname 01585 ) 01586 { 01587 FILE* fp; 01588 Error* e; 01589 01590 if ((fp = fopen(fname, "w")) == NULL) 01591 { 01592 return JWSC_EIO(strerror(errno)); 01593 } 01594 01595 if ((e = write_formatted_vector_fp_f(v, fmt, fp)) != NULL) 01596 { 01597 fclose(fp); 01598 return e; 01599 } 01600 01601 if (fclose(fp) != 0) 01602 { 01603 return JWSC_EIO(strerror(errno)); 01604 } 01605 01606 return NULL; 01607 } 01608 01625 Error* write_formatted_vector_d 01626 ( 01627 const Vector_d* v, 01628 const char* fmt, 01629 const char* fname 01630 ) 01631 { 01632 FILE* fp; 01633 Error* e; 01634 01635 if ((fp = fopen(fname, "w")) == NULL) 01636 { 01637 return JWSC_EIO(strerror(errno)); 01638 } 01639 01640 if ((e = write_formatted_vector_fp_d(v, fmt, fp)) != NULL) 01641 { 01642 fclose(fp); 01643 return e; 01644 } 01645 01646 if (fclose(fp) != 0) 01647 { 01648 return JWSC_EIO(strerror(errno)); 01649 } 01650 01651 return NULL; 01652 } 01653 01670 Error* write_formatted_vector_cf 01671 ( 01672 const Vector_cf* v, 01673 const char* fmt, 01674 const char* fname 01675 ) 01676 { 01677 FILE* fp; 01678 Error* e; 01679 01680 if ((fp = fopen(fname, "w")) == NULL) 01681 { 01682 return JWSC_EIO(strerror(errno)); 01683 } 01684 01685 if ((e = write_formatted_vector_fp_cf(v, fmt, fp)) != NULL) 01686 { 01687 fclose(fp); 01688 return e; 01689 } 01690 01691 if (fclose(fp) != 0) 01692 { 01693 return JWSC_EIO(strerror(errno)); 01694 } 01695 01696 return NULL; 01697 } 01698 01715 Error* write_formatted_vector_cd 01716 ( 01717 const Vector_cd* v, 01718 const char* fmt, 01719 const char* fname 01720 ) 01721 { 01722 FILE* fp; 01723 Error* e; 01724 01725 if ((fp = fopen(fname, "w")) == NULL) 01726 { 01727 return JWSC_EIO(strerror(errno)); 01728 } 01729 01730 if ((e = write_formatted_vector_fp_cd(v, fmt, fp)) != NULL) 01731 { 01732 fclose(fp); 01733 return e; 01734 } 01735 01736 if (fclose(fp) != 0) 01737 { 01738 return JWSC_EIO(strerror(errno)); 01739 } 01740 01741 return NULL; 01742 } 01743 01771 Error* write_vector_fp_u32(const Vector_u32* v, FILE* fp) 01772 { 01773 return write_formatted_vector_fp_u32(v, "%u", fp); 01774 } 01775 01791 Error* write_vector_fp_i32(const Vector_i32* v, FILE* fp) 01792 { 01793 return write_formatted_vector_fp_i32(v, "%d", fp); 01794 } 01795 01811 Error* write_vector_fp_i64(const Vector_i64* v, FILE* fp) 01812 { 01813 #ifdef JWSC_HAVE_64_BIT_LONG_INT 01814 return write_formatted_vector_fp_i64(v, "%ld", fp); 01815 #else 01816 return write_formatted_vector_fp_i64(v, "%lld", fp); 01817 #endif 01818 } 01819 01835 Error* write_vector_fp_f(const Vector_f* v, FILE* fp) 01836 { 01837 return write_formatted_vector_fp_f(v, "%f", fp); 01838 } 01839 01855 Error* write_vector_fp_d(const Vector_d* v, FILE* fp) 01856 { 01857 return write_formatted_vector_fp_d(v, "%.8e", fp); 01858 } 01859 01875 Error* write_vector_fp_cf(const Vector_cf* v, FILE* fp) 01876 { 01877 return write_formatted_vector_fp_cf(v, "%f %f", fp); 01878 } 01879 01895 Error* write_vector_fp_cd(const Vector_cd* v, FILE* fp) 01896 { 01897 return write_formatted_vector_fp_cd(v, "%lf %lf", fp); 01898 } 01899 01928 Error* write_formatted_vector_fp_u32 01929 ( 01930 const Vector_u32* v, 01931 const char* fmt, 01932 FILE* fp 01933 ) 01934 { 01935 uint32_t num_elts, elt; 01936 01937 num_elts = v->num_elts; 01938 for (elt = 0; elt < num_elts; elt++) 01939 { 01940 fprintf(fp, fmt, v->elts[ elt ]); 01941 fprintf(fp, " "); 01942 } 01943 fprintf(fp, "\n"); 01944 01945 return NULL; 01946 } 01947 01964 Error* write_formatted_vector_fp_i32 01965 ( 01966 const Vector_i32* v, 01967 const char* fmt, 01968 FILE* fp 01969 ) 01970 { 01971 uint32_t num_elts, elt; 01972 01973 num_elts = v->num_elts; 01974 for (elt = 0; elt < num_elts; elt++) 01975 { 01976 fprintf(fp, fmt, v->elts[ elt ]); 01977 fprintf(fp, " "); 01978 } 01979 fprintf(fp, "\n"); 01980 01981 return NULL; 01982 } 01983 02000 Error* write_formatted_vector_fp_i64 02001 ( 02002 const Vector_i64* v, 02003 const char* fmt, 02004 FILE* fp 02005 ) 02006 { 02007 uint32_t num_elts, elt; 02008 02009 num_elts = v->num_elts; 02010 for (elt = 0; elt < num_elts; elt++) 02011 { 02012 fprintf(fp, fmt, v->elts[ elt ]); 02013 fprintf(fp, " "); 02014 } 02015 fprintf(fp, "\n"); 02016 02017 return NULL; 02018 } 02019 02036 Error* write_formatted_vector_fp_f 02037 ( 02038 const Vector_f* v, 02039 const char* fmt, 02040 FILE* fp 02041 ) 02042 { 02043 uint32_t num_elts, elt; 02044 02045 num_elts = v->num_elts; 02046 for (elt = 0; elt < num_elts; elt++) 02047 { 02048 fprintf(fp, fmt, v->elts[ elt ]); 02049 fprintf(fp, " "); 02050 } 02051 fprintf(fp, "\n"); 02052 02053 return NULL; 02054 } 02055 02072 Error* write_formatted_vector_fp_d 02073 ( 02074 const Vector_d* v, 02075 const char* fmt, 02076 FILE* fp 02077 ) 02078 { 02079 uint32_t num_elts, elt; 02080 02081 num_elts = v->num_elts; 02082 for (elt = 0; elt < num_elts; elt++) 02083 { 02084 fprintf(fp, fmt, v->elts[ elt ]); 02085 fprintf(fp, " "); 02086 } 02087 fprintf(fp, "\n"); 02088 02089 return NULL; 02090 } 02091 02108 Error* write_formatted_vector_fp_cf 02109 ( 02110 const Vector_cf* v, 02111 const char* fmt, 02112 FILE* fp 02113 ) 02114 { 02115 uint32_t num_elts, elt; 02116 02117 num_elts = v->num_elts; 02118 for (elt = 0; elt < num_elts; elt++) 02119 { 02120 fprintf(fp, fmt, v->elts[ elt ].r, v->elts[ elt ].i); 02121 fprintf(fp, " "); 02122 } 02123 fprintf(fp, "\n"); 02124 02125 return NULL; 02126 } 02127 02144 Error* write_formatted_vector_fp_cd 02145 ( 02146 const Vector_cd* v, 02147 const char* fmt, 02148 FILE* fp 02149 ) 02150 { 02151 uint32_t num_elts, elt; 02152 02153 num_elts = v->num_elts; 02154 for (elt = 0; elt < num_elts; elt++) 02155 { 02156 fprintf(fp, fmt, v->elts[ elt ].r, v->elts[ elt ].i); 02157 fprintf(fp, " "); 02158 } 02159 fprintf(fp, "\n"); 02160 02161 return NULL; 02162 } 02163 02197 Error* read_vector_with_header_u32(Vector_u32** v_out, const char* fname) 02198 { 02199 FILE* fp; 02200 Error* e; 02201 02202 if ((fp = fopen(fname, "r")) == NULL) 02203 { 02204 free_vector_u32(*v_out); *v_out = NULL; 02205 return JWSC_EIO(strerror(errno)); 02206 } 02207 02208 if ((e = read_vector_with_header_fp_u32(v_out, fp)) != NULL) 02209 { 02210 fclose(fp); 02211 return e; 02212 } 02213 02214 if (fclose(fp) != 0) 02215 { 02216 free_vector_u32(*v_out); *v_out = NULL; 02217 return JWSC_EIO(strerror(errno)); 02218 } 02219 02220 return NULL; 02221 } 02222 02244 Error* read_vector_with_header_i32(Vector_i32** v_out, const char* fname) 02245 { 02246 FILE* fp; 02247 Error* e; 02248 02249 if ((fp = fopen(fname, "r")) == NULL) 02250 { 02251 free_vector_i32(*v_out); *v_out = NULL; 02252 return JWSC_EIO(strerror(errno)); 02253 } 02254 02255 if ((e = read_vector_with_header_fp_i32(v_out, fp)) != NULL) 02256 { 02257 fclose(fp); 02258 return e; 02259 } 02260 02261 if (fclose(fp) != 0) 02262 { 02263 free_vector_i32(*v_out); *v_out = NULL; 02264 return JWSC_EIO(strerror(errno)); 02265 } 02266 02267 return NULL; 02268 } 02269 02291 Error* read_vector_with_header_i64(Vector_i64** v_out, const char* fname) 02292 { 02293 FILE* fp; 02294 Error* e; 02295 02296 if ((fp = fopen(fname, "r")) == NULL) 02297 { 02298 free_vector_i64(*v_out); *v_out = NULL; 02299 return JWSC_EIO(strerror(errno)); 02300 } 02301 02302 if ((e = read_vector_with_header_fp_i64(v_out, fp)) != NULL) 02303 { 02304 fclose(fp); 02305 return e; 02306 } 02307 02308 if (fclose(fp) != 0) 02309 { 02310 free_vector_i64(*v_out); *v_out = NULL; 02311 return JWSC_EIO(strerror(errno)); 02312 } 02313 02314 return NULL; 02315 } 02316 02338 Error* read_vector_with_header_f(Vector_f** v_out, const char* fname) 02339 { 02340 FILE* fp; 02341 Error* e; 02342 02343 if ((fp = fopen(fname, "r")) == NULL) 02344 { 02345 free_vector_f(*v_out); *v_out = NULL; 02346 return JWSC_EIO(strerror(errno)); 02347 } 02348 02349 if ((e = read_vector_with_header_fp_f(v_out, fp)) != NULL) 02350 { 02351 fclose(fp); 02352 return e; 02353 } 02354 02355 if (fclose(fp) != 0) 02356 { 02357 free_vector_f(*v_out); *v_out = NULL; 02358 return JWSC_EIO(strerror(errno)); 02359 } 02360 02361 return NULL; 02362 } 02363 02385 Error* read_vector_with_header_d(Vector_d** v_out, const char* fname) 02386 { 02387 FILE* fp; 02388 Error* e; 02389 02390 if ((fp = fopen(fname, "r")) == NULL) 02391 { 02392 free_vector_d(*v_out); *v_out = NULL; 02393 return JWSC_EIO(strerror(errno)); 02394 } 02395 02396 if ((e = read_vector_with_header_fp_d(v_out, fp)) != NULL) 02397 { 02398 fclose(fp); 02399 return e; 02400 } 02401 02402 if (fclose(fp) != 0) 02403 { 02404 free_vector_d(*v_out); *v_out = NULL; 02405 return JWSC_EIO(strerror(errno)); 02406 } 02407 02408 return NULL; 02409 } 02410 02432 Error* read_vector_with_header_cf(Vector_cf** v_out, const char* fname) 02433 { 02434 FILE* fp; 02435 Error* e; 02436 02437 if ((fp = fopen(fname, "r")) == NULL) 02438 { 02439 free_vector_cf(*v_out); *v_out = NULL; 02440 return JWSC_EIO(strerror(errno)); 02441 } 02442 02443 if ((e = read_vector_with_header_fp_cf(v_out, fp)) != NULL) 02444 { 02445 fclose(fp); 02446 return e; 02447 } 02448 02449 if (fclose(fp) != 0) 02450 { 02451 free_vector_cf(*v_out); *v_out = NULL; 02452 return JWSC_EIO(strerror(errno)); 02453 } 02454 02455 return NULL; 02456 } 02457 02479 Error* read_vector_with_header_cd(Vector_cd** v_out, const char* fname) 02480 { 02481 FILE* fp; 02482 Error* e; 02483 02484 if ((fp = fopen(fname, "r")) == NULL) 02485 { 02486 free_vector_cd(*v_out); *v_out = NULL; 02487 return JWSC_EIO(strerror(errno)); 02488 } 02489 02490 if ((e = read_vector_with_header_fp_cd(v_out, fp)) != NULL) 02491 { 02492 fclose(fp); 02493 return e; 02494 } 02495 02496 if (fclose(fp) != 0) 02497 { 02498 free_vector_cd(*v_out); *v_out = NULL; 02499 return JWSC_EIO(strerror(errno)); 02500 } 02501 02502 return NULL; 02503 } 02504 02538 Error* read_vector_with_header_fp_u32(Vector_u32** v_out, FILE* fp) 02539 { 02540 uint32_t num_elts, elt; 02541 Vector_u32* v; 02542 02543 if (skip_fp_spaces_and_comments(fp) == 0) 02544 { 02545 free_vector_u32(*v_out); *v_out = NULL; 02546 return JWSC_EIO("No vector to read in the file"); 02547 } 02548 02549 if (fscanf(fp, "num_elts=%u\n", &num_elts) != 1) 02550 { 02551 free_vector_u32(*v_out); *v_out = NULL; 02552 return JWSC_EIO("File to read vector from not formatted properly"); 02553 } 02554 02555 create_vector_u32(v_out, num_elts); 02556 v = *v_out; 02557 02558 for (elt = 0; elt < num_elts; elt++) 02559 { 02560 if (fscanf(fp, "%u", &(v->elts[ elt ])) != 1) 02561 { 02562 free_vector_u32(*v_out); *v_out = NULL; 02563 return JWSC_EIO("File to read vector from not formatted properly"); 02564 } 02565 } 02566 02567 return NULL; 02568 } 02569 02591 Error* read_vector_with_header_fp_i32(Vector_i32** v_out, FILE* fp) 02592 { 02593 uint32_t num_elts, elt; 02594 Vector_i32* v; 02595 02596 if (skip_fp_spaces_and_comments(fp) == 0) 02597 { 02598 free_vector_i32(*v_out); *v_out = NULL; 02599 return JWSC_EIO("No vector to read in the file"); 02600 } 02601 02602 if (fscanf(fp, "num_elts=%u\n", &num_elts) != 1) 02603 { 02604 free_vector_i32(*v_out); *v_out = NULL; 02605 return JWSC_EIO("File to read vector from not formatted properly"); 02606 } 02607 02608 create_vector_i32(v_out, num_elts); 02609 v = *v_out; 02610 02611 for (elt = 0; elt < num_elts; elt++) 02612 { 02613 if (fscanf(fp, "%d", &(v->elts[ elt ])) != 1) 02614 { 02615 free_vector_i32(*v_out); *v_out = NULL; 02616 return JWSC_EIO("File to read vector from not formatted properly"); 02617 } 02618 } 02619 02620 return NULL; 02621 } 02622 02644 Error* read_vector_with_header_fp_i64(Vector_i64** v_out, FILE* fp) 02645 { 02646 uint32_t num_elts, elt; 02647 Vector_i64* v; 02648 02649 if (skip_fp_spaces_and_comments(fp) == 0) 02650 { 02651 free_vector_i64(*v_out); *v_out = NULL; 02652 return JWSC_EIO("No vector to read in the file"); 02653 } 02654 02655 if (fscanf(fp, "num_elts=%u\n", &num_elts) != 1) 02656 { 02657 free_vector_i64(*v_out); *v_out = NULL; 02658 return JWSC_EIO("File to read vector from not formatted properly"); 02659 } 02660 02661 create_vector_i64(v_out, num_elts); 02662 v = *v_out; 02663 02664 for (elt = 0; elt < num_elts; elt++) 02665 { 02666 if (fscanf(fp, "%" SCNd64, &(v->elts[ elt ])) != 1) 02667 { 02668 free_vector_i64(*v_out); *v_out = NULL; 02669 return JWSC_EIO("File to read vector from not formatted properly"); 02670 } 02671 } 02672 02673 return NULL; 02674 } 02675 02697 Error* read_vector_with_header_fp_f(Vector_f** v_out, FILE* fp) 02698 { 02699 uint32_t num_elts, elt; 02700 Vector_f* v; 02701 02702 if (skip_fp_spaces_and_comments(fp) == 0) 02703 { 02704 free_vector_f(*v_out); *v_out = NULL; 02705 return JWSC_EIO("No vector to read in the file"); 02706 } 02707 02708 if (fscanf(fp, "num_elts=%u\n", &num_elts) != 1) 02709 { 02710 free_vector_f(*v_out); *v_out = NULL; 02711 return JWSC_EIO("File to read vector from not formatted properly"); 02712 } 02713 02714 create_vector_f(v_out, num_elts); 02715 v = *v_out; 02716 02717 for (elt = 0; elt < num_elts; elt++) 02718 { 02719 if (fscanf(fp, "%f", &(v->elts[ elt ])) != 1) 02720 { 02721 free_vector_f(*v_out); *v_out = NULL; 02722 return JWSC_EIO("File to read vector from not formatted properly"); 02723 } 02724 } 02725 02726 return NULL; 02727 } 02728 02750 Error* read_vector_with_header_fp_d(Vector_d** v_out, FILE* fp) 02751 { 02752 uint32_t num_elts, elt; 02753 Vector_d* v; 02754 02755 if (skip_fp_spaces_and_comments(fp) == 0) 02756 { 02757 free_vector_d(*v_out); *v_out = NULL; 02758 return JWSC_EIO("No vector to read in the file"); 02759 } 02760 02761 if (fscanf(fp, "num_elts=%u\n", &num_elts) != 1) 02762 { 02763 free_vector_d(*v_out); *v_out = NULL; 02764 return JWSC_EIO("File to read vector from not formatted properly"); 02765 } 02766 02767 create_vector_d(v_out, num_elts); 02768 v = *v_out; 02769 02770 for (elt = 0; elt < num_elts; elt++) 02771 { 02772 if (fscanf(fp, "%lf", &(v->elts[ elt ])) != 1) 02773 { 02774 free_vector_d(*v_out); *v_out = NULL; 02775 return JWSC_EIO("File to read vector from not formatted properly"); 02776 } 02777 } 02778 02779 return NULL; 02780 } 02781 02803 Error* read_vector_with_header_fp_cf(Vector_cf** v_out, FILE* fp) 02804 { 02805 uint32_t num_elts, elt; 02806 Vector_cf* v; 02807 02808 if (skip_fp_spaces_and_comments(fp) == 0) 02809 { 02810 free_vector_cf(*v_out); *v_out = NULL; 02811 return JWSC_EIO("No vector to read in the file"); 02812 } 02813 02814 if (fscanf(fp, "num_elts=%u\n", &num_elts) != 1) 02815 { 02816 free_vector_cf(*v_out); *v_out = NULL; 02817 return JWSC_EIO("File to read vector from not formatted properly"); 02818 } 02819 02820 create_vector_cf(v_out, num_elts); 02821 v = *v_out; 02822 02823 for (elt = 0; elt < num_elts; elt++) 02824 { 02825 if (fscanf(fp, "%f %f", &(v->elts[ elt ].r), &(v->elts[ elt ].i)) != 2) 02826 { 02827 free_vector_cf(*v_out); *v_out = NULL; 02828 return JWSC_EIO("File to read vector from not formatted properly"); 02829 } 02830 } 02831 02832 return NULL; 02833 } 02834 02856 Error* read_vector_with_header_fp_cd(Vector_cd** v_out, FILE* fp) 02857 { 02858 uint32_t num_elts, elt; 02859 Vector_cd* v; 02860 02861 if (skip_fp_spaces_and_comments(fp) == 0) 02862 { 02863 free_vector_cd(*v_out); *v_out = NULL; 02864 return JWSC_EIO("No vector to read in the file"); 02865 } 02866 02867 if (fscanf(fp, "num_elts=%u\n", &num_elts) != 1) 02868 { 02869 free_vector_cd(*v_out); *v_out = NULL; 02870 return JWSC_EIO("File to read vector from not formatted properly"); 02871 } 02872 02873 create_vector_cd(v_out, num_elts); 02874 v = *v_out; 02875 02876 for (elt = 0; elt < num_elts; elt++) 02877 { 02878 if (fscanf(fp, "%lf %lf", &(v->elts[ elt ].r), &(v->elts[ elt ].i)) !=2) 02879 { 02880 free_vector_cd(*v_out); *v_out = NULL; 02881 return JWSC_EIO("File to read vector from not formatted properly"); 02882 } 02883 } 02884 02885 return NULL; 02886 } 02887 02917 Error* write_vector_with_header_u32(const Vector_u32* v, const char* fname) 02918 { 02919 FILE* fp; 02920 02921 if ((fp = fopen(fname, "w")) == NULL) 02922 { 02923 return JWSC_EIO(strerror(errno)); 02924 } 02925 02926 write_vector_with_header_fp_u32(v, fp); 02927 02928 if (fclose(fp) != 0) 02929 { 02930 return JWSC_EIO(strerror(errno)); 02931 } 02932 02933 return NULL; 02934 } 02935 02953 Error* write_vector_with_header_i32(const Vector_i32* v, const char* fname) 02954 { 02955 FILE* fp; 02956 02957 if ((fp = fopen(fname, "w")) == NULL) 02958 { 02959 return JWSC_EIO(strerror(errno)); 02960 } 02961 02962 write_vector_with_header_fp_i32(v, fp); 02963 02964 if (fclose(fp) != 0) 02965 { 02966 return JWSC_EIO(strerror(errno)); 02967 } 02968 02969 return NULL; 02970 } 02971 02989 Error* write_vector_with_header_i64(const Vector_i64* v, const char* fname) 02990 { 02991 FILE* fp; 02992 02993 if ((fp = fopen(fname, "w")) == NULL) 02994 { 02995 return JWSC_EIO(strerror(errno)); 02996 } 02997 02998 write_vector_with_header_fp_i64(v, fp); 02999 03000 if (fclose(fp) != 0) 03001 { 03002 return JWSC_EIO(strerror(errno)); 03003 } 03004 03005 return NULL; 03006 } 03007 03025 Error* write_vector_with_header_f(const Vector_f* v, const char* fname) 03026 { 03027 FILE* fp; 03028 03029 if ((fp = fopen(fname, "w")) == NULL) 03030 { 03031 return JWSC_EIO(strerror(errno)); 03032 } 03033 03034 write_vector_with_header_fp_f(v, fp); 03035 03036 if (fclose(fp) != 0) 03037 { 03038 return JWSC_EIO(strerror(errno)); 03039 } 03040 03041 return NULL; 03042 } 03043 03061 Error* write_vector_with_header_d(const Vector_d* v, const char* fname) 03062 { 03063 FILE* fp; 03064 03065 if ((fp = fopen(fname, "w")) == NULL) 03066 { 03067 return JWSC_EIO(strerror(errno)); 03068 } 03069 03070 write_vector_with_header_fp_d(v, fp); 03071 03072 if (fclose(fp) != 0) 03073 { 03074 return JWSC_EIO(strerror(errno)); 03075 } 03076 03077 return NULL; 03078 } 03079 03097 Error* write_vector_with_header_cf(const Vector_cf* v, const char* fname) 03098 { 03099 FILE* fp; 03100 03101 if ((fp = fopen(fname, "w")) == NULL) 03102 { 03103 return JWSC_EIO(strerror(errno)); 03104 } 03105 03106 write_vector_with_header_fp_cf(v, fp); 03107 03108 if (fclose(fp) != 0) 03109 { 03110 return JWSC_EIO(strerror(errno)); 03111 } 03112 03113 return NULL; 03114 } 03115 03133 Error* write_vector_with_header_cd(const Vector_cd* v, const char* fname) 03134 { 03135 FILE* fp; 03136 03137 if ((fp = fopen(fname, "w")) == NULL) 03138 { 03139 return JWSC_EIO(strerror(errno)); 03140 } 03141 03142 write_vector_with_header_fp_cd(v, fp); 03143 03144 if (fclose(fp) != 0) 03145 { 03146 return JWSC_EIO(strerror(errno)); 03147 } 03148 03149 return NULL; 03150 } 03151 03182 Error* write_formatted_vector_with_header_u32 03183 ( 03184 const Vector_u32* v, 03185 const char* fmt, 03186 const char* fname 03187 ) 03188 { 03189 FILE* fp; 03190 03191 if ((fp = fopen(fname, "w")) == NULL) 03192 { 03193 return JWSC_EIO(strerror(errno)); 03194 } 03195 03196 write_formatted_vector_with_header_fp_u32(v, fmt, fp); 03197 03198 if (fclose(fp) != 0) 03199 { 03200 return JWSC_EIO(strerror(errno)); 03201 } 03202 03203 return NULL; 03204 } 03205 03224 Error* write_formatted_vector_with_header_i32 03225 ( 03226 const Vector_i32* v, 03227 const char* fmt, 03228 const char* fname 03229 ) 03230 { 03231 FILE* fp; 03232 03233 if ((fp = fopen(fname, "w")) == NULL) 03234 { 03235 return JWSC_EIO(strerror(errno)); 03236 } 03237 03238 write_formatted_vector_with_header_fp_i32(v, fmt, fp); 03239 03240 if (fclose(fp) != 0) 03241 { 03242 return JWSC_EIO(strerror(errno)); 03243 } 03244 03245 return NULL; 03246 } 03247 03266 Error* write_formatted_vector_with_header_i64 03267 ( 03268 const Vector_i64* v, 03269 const char* fmt, 03270 const char* fname 03271 ) 03272 { 03273 FILE* fp; 03274 03275 if ((fp = fopen(fname, "w")) == NULL) 03276 { 03277 return JWSC_EIO(strerror(errno)); 03278 } 03279 03280 write_formatted_vector_with_header_fp_i64(v, fmt, fp); 03281 03282 if (fclose(fp) != 0) 03283 { 03284 return JWSC_EIO(strerror(errno)); 03285 } 03286 03287 return NULL; 03288 } 03289 03308 Error* write_formatted_vector_with_header_f 03309 ( 03310 const Vector_f* v, 03311 const char* fmt, 03312 const char* fname 03313 ) 03314 { 03315 FILE* fp; 03316 03317 if ((fp = fopen(fname, "w")) == NULL) 03318 { 03319 return JWSC_EIO(strerror(errno)); 03320 } 03321 03322 write_formatted_vector_with_header_fp_f(v, fmt, fp); 03323 03324 if (fclose(fp) != 0) 03325 { 03326 return JWSC_EIO(strerror(errno)); 03327 } 03328 03329 return NULL; 03330 } 03331 03350 Error* write_formatted_vector_with_header_d 03351 ( 03352 const Vector_d* v, 03353 const char* fmt, 03354 const char* fname 03355 ) 03356 { 03357 FILE* fp; 03358 03359 if ((fp = fopen(fname, "w")) == NULL) 03360 { 03361 return JWSC_EIO(strerror(errno)); 03362 } 03363 03364 write_formatted_vector_with_header_fp_d(v, fmt, fp); 03365 03366 if (fclose(fp) != 0) 03367 { 03368 return JWSC_EIO(strerror(errno)); 03369 } 03370 03371 return NULL; 03372 } 03373 03392 Error* write_formatted_vector_with_header_cf 03393 ( 03394 const Vector_cf* v, 03395 const char* fmt, 03396 const char* fname 03397 ) 03398 { 03399 FILE* fp; 03400 03401 if ((fp = fopen(fname, "w")) == NULL) 03402 { 03403 return JWSC_EIO(strerror(errno)); 03404 } 03405 03406 write_formatted_vector_with_header_fp_cf(v, fmt, fp); 03407 03408 if (fclose(fp) != 0) 03409 { 03410 return JWSC_EIO(strerror(errno)); 03411 } 03412 03413 return NULL; 03414 } 03415 03434 Error* write_formatted_vector_with_header_cd 03435 ( 03436 const Vector_cd* v, 03437 const char* fmt, 03438 const char* fname 03439 ) 03440 { 03441 FILE* fp; 03442 03443 if ((fp = fopen(fname, "w")) == NULL) 03444 { 03445 return JWSC_EIO(strerror(errno)); 03446 } 03447 03448 write_formatted_vector_with_header_fp_cd(v, fmt, fp); 03449 03450 if (fclose(fp) != 0) 03451 { 03452 return JWSC_EIO(strerror(errno)); 03453 } 03454 03455 return NULL; 03456 } 03457 03487 Error* write_vector_with_header_fp_u32(const Vector_u32* v, FILE* fp) 03488 { 03489 return write_formatted_vector_with_header_fp_u32(v, "%u", fp); 03490 } 03491 03509 Error* write_vector_with_header_fp_i32(const Vector_i32* v, FILE* fp) 03510 { 03511 return write_formatted_vector_with_header_fp_i32(v, "%d", fp); 03512 } 03513 03531 Error* write_vector_with_header_fp_i64(const Vector_i64* v, FILE* fp) 03532 { 03533 #ifdef JWSC_HAVE_64_BIT_LONG_INT 03534 return write_formatted_vector_with_header_fp_i64(v, "%ld", fp); 03535 #else 03536 return write_formatted_vector_with_header_fp_i64(v, "%lld", fp); 03537 #endif 03538 } 03539 03557 Error* write_vector_with_header_fp_f(const Vector_f* v, FILE* fp) 03558 { 03559 return write_formatted_vector_with_header_fp_f(v, "%f", fp); 03560 } 03561 03579 Error* write_vector_with_header_fp_d(const Vector_d* v, FILE* fp) 03580 { 03581 return write_formatted_vector_with_header_fp_d(v, "%.8e", fp); 03582 } 03583 03601 Error* write_vector_with_header_fp_cf(const Vector_cf* v, FILE* fp) 03602 { 03603 return write_formatted_vector_with_header_fp_cf(v, "%f %f", fp); 03604 } 03605 03623 Error* write_vector_with_header_fp_cd(const Vector_cd* v, FILE* fp) 03624 { 03625 return write_formatted_vector_with_header_fp_cd(v, "%lf %lf", fp); 03626 } 03627 03658 Error* write_formatted_vector_with_header_fp_u32 03659 ( 03660 const Vector_u32* v, 03661 const char* fmt, 03662 FILE* fp 03663 ) 03664 { 03665 fprintf(fp, "num_elts=%u\n", v->num_elts); 03666 03667 return write_formatted_vector_fp_u32(v, fmt, fp); 03668 } 03669 03688 Error* write_formatted_vector_with_header_fp_i32 03689 ( 03690 const Vector_i32* v, 03691 const char* fmt, 03692 FILE* fp 03693 ) 03694 { 03695 fprintf(fp, "num_elts=%u\n", v->num_elts); 03696 03697 return write_formatted_vector_fp_i32(v, fmt, fp); 03698 } 03699 03718 Error* write_formatted_vector_with_header_fp_i64 03719 ( 03720 const Vector_i64* v, 03721 const char* fmt, 03722 FILE* fp 03723 ) 03724 { 03725 fprintf(fp, "num_elts=%u\n", v->num_elts); 03726 03727 return write_formatted_vector_fp_i64(v, fmt, fp); 03728 } 03729 03748 Error* write_formatted_vector_with_header_fp_f 03749 ( 03750 const Vector_f* v, 03751 const char* fmt, 03752 FILE* fp 03753 ) 03754 { 03755 fprintf(fp, "num_elts=%u\n", v->num_elts); 03756 03757 return write_formatted_vector_fp_f(v, fmt, fp); 03758 } 03759 03778 Error* write_formatted_vector_with_header_fp_d 03779 ( 03780 const Vector_d* v, 03781 const char* fmt, 03782 FILE* fp 03783 ) 03784 { 03785 fprintf(fp, "num_elts=%u\n", v->num_elts); 03786 03787 return write_formatted_vector_fp_d(v, fmt, fp); 03788 } 03789 03808 Error* write_formatted_vector_with_header_fp_cf 03809 ( 03810 const Vector_cf* v, 03811 const char* fmt, 03812 FILE* fp 03813 ) 03814 { 03815 fprintf(fp, "num_elts=%u\n", v->num_elts); 03816 03817 return write_formatted_vector_fp_cf(v, fmt, fp); 03818 } 03819 03838 Error* write_formatted_vector_with_header_fp_cd 03839 ( 03840 const Vector_cd* v, 03841 const char* fmt, 03842 FILE* fp 03843 ) 03844 { 03845 fprintf(fp, "num_elts=%u\n", v->num_elts); 03846 03847 return write_formatted_vector_fp_cd(v, fmt, fp); 03848 } 03849