/Recommender/MiningAlgorithm/src/ikmeans/file.cpp

https://github.com/lince/ginga-srpp · C++ · 378 lines · 261 code · 88 blank · 29 comment · 51 complexity · 74e235e657fb245c351bfd2329e5e5ce MD5 · raw file

  1. /*-------------------------------------------------------------------*/
  2. /* FILE.C Juha Kivij�rvi */
  3. /* Pasi Fr�nti */
  4. /* Timo Kaukoranta */
  5. /* */
  6. /* General I/O routines */
  7. /* */
  8. /*-------------------------------------------------------------------*/
  9. #define ProgName "FILE"
  10. #define VersionNumber "Version 0.16"
  11. #define LastUpdated "15.11.2001"
  12. /* ----------------------------------------------------------------- */
  13. #include <assert.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #ifdef HAVE_RECOMMENDER
  18. #include "recommender/file.h"
  19. #include "recommender/owntypes.h"
  20. #include "recommender/interfc.h"
  21. #else
  22. #include "../../include/ikmeans/file.h"
  23. #include "../../include/ikmeans/owntypes.h"
  24. #include "../../include/ikmeans/interfc.h"
  25. #endif
  26. /*======================== I/O GENERAL ============================*/
  27. int ExistFile(char* File)
  28. {
  29. FILE* f;
  30. if( (f = fopen(File, "rb")) != NULL )
  31. {
  32. fclose(f);
  33. return( YES );
  34. }
  35. else
  36. {
  37. return( NO );
  38. }
  39. }
  40. /*-------------------------------------------------------------------*/
  41. FILE* FileOpen(char* File, int Mode, int AllowOverWrite)
  42. {
  43. FILE* f;
  44. switch( Mode )
  45. {
  46. case INPUT:
  47. {
  48. f = fopen(File, "rb");
  49. if( ! f )
  50. {
  51. ErrorMessage("ERROR: Cannot find the file: %s", File);
  52. return(NULL); /* exit( -1 ); */
  53. }
  54. break;
  55. }
  56. case OUTPUT:
  57. {
  58. if( ExistFile(File) )
  59. {
  60. if( AllowOverWrite == YES )
  61. {
  62. remove(File);
  63. }
  64. else
  65. {
  66. ErrorMessage("ERROR: File already exists: %s", File);
  67. return(NULL); /* exit( -1 ); */
  68. }
  69. }
  70. f = fopen(File,"wb+");
  71. if( ! f )
  72. {
  73. ErrorMessage("ERROR: Couldn't open the file: %s", File);
  74. return(NULL); /* exit( -1 ); */
  75. }
  76. break;
  77. }
  78. default:
  79. {
  80. ErrorMessage("ERROR: Invalid file mode:%d", Mode);
  81. return(NULL); /* exit( -1 ); */
  82. }
  83. }
  84. return( f );
  85. }
  86. /*-------------------------------------------------------------------*/
  87. #ifndef _WINDOWS
  88. FILE* OpenFile(char* File, int Mode, int AllowOverWrite)
  89. {
  90. FILE* f;
  91. f = FileOpen(File, Mode, AllowOverWrite);
  92. if ( f == NULL )
  93. ExitProcessing(-1);
  94. return f;
  95. }
  96. #endif
  97. /*-------------------------------------------------------------------*/
  98. int ReadIntegerFromFile(FILE* f, int* value, int bytecount)
  99. { /* byte order is big endian */
  100. int i;
  101. int c;
  102. int res = 0;
  103. for( i=0; i<bytecount; i++ )
  104. {
  105. res <<= 8;
  106. c = getc(f);
  107. if (c == EOF)
  108. {
  109. ErrorMessage("ERROR: unable to read from file.");
  110. break;
  111. }
  112. res |= c;
  113. }
  114. *value = res;
  115. return i;
  116. }
  117. /*-------------------------------------------------------------------*/
  118. int ReadLittleEndianIntegerFromFile(FILE* f, int* value, int bytecount)
  119. { /* byte order is little endian */
  120. int i;
  121. int c;
  122. int res = 0;
  123. for( i=0; i<bytecount; i++ )
  124. {
  125. c = getc(f);
  126. if (c == EOF)
  127. {
  128. ErrorMessage("ERROR: unable to read from file.");
  129. break;
  130. }
  131. c <<= (8*i);
  132. res |= c;
  133. }
  134. *value = res;
  135. return i;
  136. }
  137. /*-------------------------------------------------------------------*/
  138. int WriteIntegerToFile(FILE* f, int value, int bytecount)
  139. { /* byte order is big endian */
  140. int i;
  141. int c;
  142. if (bytecount < (int)sizeof(int) && (value >> (8*bytecount)) > 0)
  143. {
  144. ErrorMessage("ERROR: Vector element value is too big (%d)."
  145. " Increase bytes/elem ratio.", value);
  146. return(0);
  147. }
  148. for( i=0; i<bytecount; i++ )
  149. {
  150. c = ( value >> (8*(bytecount-1-i))) % 256;
  151. if (putc(c,f)==EOF)
  152. { ErrorMessage ("ERROR: unable to write to file."); break; }
  153. }
  154. return (i);
  155. }
  156. /*======================== I/O SPECIAL ============================*/
  157. void InitializeBitStream(BITSTREAM* bs, FILE* f)
  158. /* f should already be opened */
  159. {
  160. bs->File = f;
  161. bs->BitsInQueue = 0;
  162. bs->BitQueue = 0;
  163. }
  164. /*-------------------------------------------------------------------*/
  165. int InputBit(BITSTREAM* bs)
  166. {
  167. int bit;
  168. if( bs->BitsInQueue == 0 )
  169. {
  170. bs->BitQueue = getc(bs->File);
  171. if( bs->BitQueue == EOF )
  172. {
  173. ErrorMessage("Unexpected EOF in input file.");
  174. return( EOF );
  175. }
  176. bs->BitsInQueue = 8;
  177. }
  178. bit = (bs->BitQueue & (0x80)) >> 7;
  179. bs->BitQueue <<= 1;
  180. (bs->BitsInQueue)--;
  181. return( bit );
  182. }
  183. /*-------------------------------------------------------------------*/
  184. void OutputBit(BITSTREAM* bs, int bit)
  185. {
  186. assert( bit == 0 || bit == 1 );
  187. bs->BitQueue <<= 1;
  188. bs->BitQueue |= bit;
  189. if( ++(bs->BitsInQueue) == 8 )
  190. {
  191. putc(bs->BitQueue, bs->File);
  192. bs->BitsInQueue = 0;
  193. bs->BitQueue = 0;
  194. }
  195. }
  196. /*-------------------------------------------------------------------*/
  197. void FlushInput(BITSTREAM* bs)
  198. {
  199. bs->BitsInQueue = 0;
  200. }
  201. /*-------------------------------------------------------------------*/
  202. void FlushOutput(BITSTREAM* bs)
  203. {
  204. if(bs->BitsInQueue==0)
  205. {
  206. return;
  207. }
  208. while((bs->BitsInQueue)++ < 8)
  209. {
  210. bs->BitQueue <<= 1;
  211. }
  212. putc(bs->BitQueue, bs->File);
  213. bs->BitsInQueue = 0;
  214. bs->BitQueue = 0;
  215. }
  216. /*-------------------------------------------------------------------*/
  217. int InputValue(BITSTREAM* bs, int bits)
  218. {
  219. int i;
  220. int x = 0;
  221. int mask = 0x01;
  222. for(i=1; i<=bits; i++)
  223. {
  224. if(InputBit(bs)) x |= mask;
  225. mask <<= 1;
  226. }
  227. return( x );
  228. }
  229. /*-------------------------------------------------------------------*/
  230. void OutputValue(BITSTREAM* bs, int x, int bits)
  231. {
  232. int i;
  233. for(i=1; i<=bits; i++)
  234. {
  235. OutputBit( bs, x & 0x01 );
  236. x >>= 1;
  237. }
  238. }
  239. /*===================== File name routines =========================*/
  240. void PickFileName(char* Source, char* Destination)
  241. {
  242. char* ch;
  243. strcpy(Destination, Source);
  244. ch = strchr(Destination, '.');
  245. if( ch )
  246. {
  247. (*ch) = 0x00;
  248. }
  249. }
  250. /*-------------------------------------------------------------------*/
  251. void CheckFileName(char* FileName, char* Extension)
  252. /* Checks whether the file has extension. If no extension found,
  253. then the file name is catenated by 'Extension'. */
  254. {
  255. int i = strlen(FileName);
  256. while( i >= 0 && FileName[i] != '.' && FileName[i] != '/' &&
  257. FileName[i] != '\\' )
  258. {
  259. i--;
  260. }
  261. if( i < 0 || FileName[i] == '/' || FileName[i] == '\\' )
  262. {
  263. strcat(FileName, ".");
  264. strcat(FileName, Extension);
  265. }
  266. }
  267. /* ---------------------------------------------------------- */
  268. YESNO CheckExtention(char* FileName, char* Extention)
  269. {
  270. char* ch;
  271. ch = strchr(FileName, '.');
  272. ch ++;
  273. if(ch)
  274. {
  275. if (strcmp(ch, Extention)==0) return YES;
  276. }
  277. return NO;
  278. }
  279. /*================ Miscellaneous STRING-routines ===================*/
  280. YESNO EqualFileNames(char* f1, char *f2)
  281. {
  282. int i=0;
  283. while (f1[i] && f2[i])
  284. {
  285. if (toupper(f1[i])!=toupper(f2[i]))
  286. return NO;
  287. i++;
  288. }
  289. return (YESNO) (f1[i]==f2[i]);
  290. }