PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/amap.cpp

https://bitbucket.org/wildcarde/conquest-dicom-server
C++ | 292 lines | 230 code | 38 blank | 24 comment | 74 complexity | c939eefdc3cbb016b5c76b043bb1a234 MD5 | raw file
  1. /* mvh 19990827: added (limited) wildcard mapping in GetACRNema: AE, port or IP may end on '*'
  2. mvh 20001105: replaced m-alloc by new
  3. mvh 20011109: Made AE mapping case insensitive
  4. ljz 20020524: In function 'GetACRNema', first try case SENSITIVE then INSENSITIVE
  5. mvh 20030701: Added compression column (with this code is this column is not optional!)
  6. mvh 20030703: KNOWN BUG: number of columns may not be 3 (crashes)
  7. ljz 20030709: Solved above problem; rewritten parsing of Acrnema.map
  8. ljz 20030711: Fixed trailing spaces in acrnema.map
  9. ljz 20031118: Fixed leak InitACRNemaAddressArray
  10. */
  11. #ifndef WHEDGE
  12. # include "dgate.hpp"
  13. #else
  14. # include "master.h"
  15. #endif
  16. BYTE ACRNEMAMAP [ 256 ] = "acrnema.map";
  17. Array < ACRNemaAddress * > ACRNemaAddressArray;
  18. BOOL
  19. InitACRNemaAddressArray(void)
  20. { FILE* f;
  21. char* pBuffer;
  22. int i, iSize;
  23. char *pSrc, *pDest;
  24. ACRNemaAddress* pACR;
  25. int iNbAddresses;
  26. /* Open AcrNema.map */
  27. f = fopen((char*)ACRNEMAMAP, "rb");
  28. if (!f)
  29. return FALSE;
  30. /* Read it in memory */
  31. fseek(f, 0, SEEK_END);
  32. iSize = ftell(f);
  33. fseek(f, 0, SEEK_SET);
  34. pBuffer = (char*)malloc(iSize + 2);
  35. if (!pBuffer)
  36. { fclose(f);
  37. return FALSE;
  38. }
  39. if (fread(pBuffer, 1, iSize, f) != (unsigned int)iSize)
  40. { fclose(f);
  41. free(pBuffer);
  42. return FALSE;
  43. }
  44. fclose(f);
  45. /* Make it a CR and zero terminated string */
  46. pBuffer[iSize++] = '\r';
  47. pBuffer[iSize] = '\0';
  48. /* Get rid of all comments like this one */
  49. pSrc = pBuffer;
  50. pDest = pBuffer;
  51. i = 0;
  52. while (i<iSize)
  53. { if ((pSrc[i] == '/') && (pSrc[i + 1] == '*'))
  54. { /* Found the start of a comment */
  55. i += 2;
  56. while (i<iSize)
  57. { if ((pSrc[i] == '*') && (pSrc[i + 1] == '/'))
  58. { i++;
  59. break;
  60. }
  61. i++;
  62. }
  63. }
  64. else
  65. *pDest++ = pSrc[i];
  66. i++;
  67. }
  68. *pDest = 0;
  69. /* Replace all LF by CR, and all '\t' by ' ' */
  70. iSize = strlen(pSrc);
  71. for (i=0; i<iSize; i++)
  72. if (pSrc[i] == '\n')
  73. pSrc[i] = '\r';
  74. else if (pSrc[i] == '\t')
  75. pSrc[i] = ' ';
  76. /* The characters '#', ';' and '//' are one-line comments */
  77. pDest = pSrc;
  78. i = 0;
  79. while (i<iSize)
  80. { if ((pSrc[i] == '/') && (pSrc[i + 1] == '/'))
  81. { /* Found the start of a comment */
  82. i += 2;
  83. while (i<iSize)
  84. { if (pSrc[i] == '\r')
  85. break;
  86. i++;
  87. }
  88. }
  89. else if ((pSrc[i] == '#') || (pSrc[i + 1] == ';'))
  90. { /* Found the start of a comment */
  91. i += 1;
  92. while (i<iSize)
  93. { if (pSrc[i] == '\r')
  94. break;
  95. i++;
  96. }
  97. }
  98. else
  99. *pDest++ = pSrc[i];
  100. i++;
  101. }
  102. *pDest = 0;
  103. /* Eliminate empty lines, double space... */
  104. iSize = strlen(pSrc);
  105. pDest = pSrc;
  106. i = 0;
  107. while (i<iSize)
  108. { if (pSrc[i] == ' ')
  109. { if (pDest == pSrc)
  110. { /* Skip leading space */
  111. i++;
  112. continue;
  113. }
  114. else if ((pDest[-1] == ' ') || (pDest[-1] == '\r'))
  115. { /* Skip space when the previous was space or CR */
  116. i++;
  117. continue;
  118. }
  119. else if (pSrc[i+1] == '\r')
  120. { /* Skip space when the next is CR */
  121. i++;
  122. continue;
  123. }
  124. }
  125. else if (pSrc[i] == '\r')
  126. { if (pDest == pSrc)
  127. { /* Skip leading CR */
  128. i++;
  129. continue;
  130. }
  131. else if (pDest[-1] == '\r')
  132. { /* Skip CR when the previous was a CR */
  133. i++;
  134. continue;
  135. }
  136. }
  137. *pDest++ = pSrc[i];
  138. i++;
  139. }
  140. *pDest = 0;
  141. /* Count the iNbAddresses */
  142. iSize = strlen(pSrc);
  143. iNbAddresses = 0;
  144. for (i=0; i<iSize; i++)
  145. if (pSrc[i] == '\r')
  146. iNbAddresses++;
  147. /* Now fill the ACRNemaAddressArray */
  148. pDest = pSrc;
  149. for (i=0; i<iNbAddresses; i++)
  150. { pACR = new ACRNemaAddress;
  151. strcpy(pACR->Name, "");
  152. strcpy(pACR->IP, "");
  153. strcpy(pACR->Port, "");
  154. strcpy(pACR->Compress, "un");
  155. /* Get a line */
  156. *strchr(pDest, '\r') = 0;
  157. sscanf(pDest, "%s %s %s %s",
  158. pACR->Name,
  159. pACR->IP,
  160. pACR->Port,
  161. pACR->Compress);
  162. ACRNemaAddressArray.Add(pACR);
  163. pDest = pDest + strlen(pDest) + 1;
  164. }
  165. free(pBuffer);
  166. return TRUE;
  167. }
  168. BOOL
  169. CloseACRNemaAddressArray()
  170. {
  171. while ( ACRNemaAddressArray.GetSize() )
  172. {
  173. delete ACRNemaAddressArray.Get(0);
  174. ACRNemaAddressArray.RemoveAt(0);
  175. }
  176. return ( TRUE );
  177. }
  178. static
  179. BOOL
  180. __iswhitespacea(char ch)
  181. {
  182. switch ( ch )
  183. {
  184. case ' ':
  185. case '\t':
  186. case '\r':
  187. case '\n':
  188. case 0:
  189. return ( TRUE );
  190. }
  191. return ( FALSE );
  192. }
  193. BOOL
  194. GetACRNema(char* ACRNema, char* ip, char* port, char* compress)
  195. {
  196. UINT Index;
  197. char s[20];
  198. char *t;
  199. // trim the ACRNema address
  200. memset(s, 0, 20);
  201. strcpy(s, ACRNema);
  202. if(!strlen(s))
  203. return ( FALSE );
  204. while(__iswhitespacea(s[strlen(s)-1]))
  205. {
  206. s[strlen(s)-1] = '\0';
  207. if(!strlen(s))
  208. return ( FALSE );
  209. }
  210. Index = 0;
  211. while ( Index < ACRNemaAddressArray.GetSize())
  212. {
  213. t = ACRNemaAddressArray.Get(Index)->Name;
  214. // try wildcard mapping, e.g., s=NG12, t=NG*, IP=101.11.11.*, Port=56*
  215. if (t[strlen(t)-1] == '*')
  216. {
  217. if (!memicmp(t, s, strlen(t)-1))
  218. {
  219. strcpy(ip, ACRNemaAddressArray.Get(Index)->IP);
  220. if (ip[strlen(ip)-1]=='*')
  221. strcpy(ip+strlen(ip)-1, s+strlen(t)-1);
  222. strcpy(port, ACRNemaAddressArray.Get(Index)->Port);
  223. if (port[strlen(port)-1]=='*')
  224. strcpy(port+strlen(port)-1, s+strlen(t)-1);
  225. strcpy(compress, ACRNemaAddressArray.Get(Index)->Compress);
  226. return ( TRUE );
  227. }
  228. }
  229. // try exact match (case sensitive)
  230. if (!strcmp(t, s))
  231. {
  232. strcpy(ip, ACRNemaAddressArray.Get(Index)->IP);
  233. strcpy(port, ACRNemaAddressArray.Get(Index)->Port);
  234. strcpy(compress, ACRNemaAddressArray.Get(Index)->Compress);
  235. return ( TRUE );
  236. }
  237. ++Index;
  238. }
  239. Index = 0;
  240. while ( Index < ACRNemaAddressArray.GetSize())
  241. {
  242. t = ACRNemaAddressArray.Get(Index)->Name;
  243. // try exact match (case insensitive)
  244. if (!stricmp(t, s))
  245. {
  246. strcpy(ip, ACRNemaAddressArray.Get(Index)->IP);
  247. strcpy(port, ACRNemaAddressArray.Get(Index)->Port);
  248. strcpy(compress, ACRNemaAddressArray.Get(Index)->Compress);
  249. return ( TRUE );
  250. }
  251. ++Index;
  252. }
  253. return ( FALSE );
  254. }