crc_interface.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "calccrc.hpp"
  2. #include <cstring>
  3. // ʹÓÃextern "C"À´¶¨Òå½Ó¿Úº¯Êý
  4. #ifdef __cplusplus
  5. extern "C"
  6. {
  7. #endif
  8. char*** map_to_char(const std::map<std::string, std::string>& m) {
  9. // Allocate memory for the char***
  10. int size = m.size();
  11. char*** array = new char** [size];
  12. // Iterate over the map and copy each key and value to the char***
  13. int i = 0;
  14. for (const auto& p : m) {
  15. // Allocate memory for each key and value pair
  16. array[i] = new char* [2];
  17. // Copy the key
  18. int key_len = p.first.length();
  19. array[i][0] = new char[key_len + 1];
  20. strcpy(array[i][0], p.first.c_str());
  21. // Copy the value
  22. int val_len = p.second.length();
  23. array[i][1] = new char[val_len + 1];
  24. strcpy(array[i][1], p.second.c_str());
  25. i++;
  26. }
  27. return array;
  28. }
  29. const char* get_string(string str) {
  30. const char* pszSTR = str.data();
  31. char* new_str = new char[strlen(pszSTR) + 1];
  32. strcpy(new_str, pszSTR);
  33. return new_str;
  34. }
  35. const char* calcALLIEDCRC(char**** iedCrcMap, const char* pszSCDFile, int& iSize)
  36. {
  37. iSize = 0;
  38. map<string, string> iedCRCMap = CCalCRC::getIedCrc(pszSCDFile);
  39. iSize = iedCRCMap.size();
  40. *iedCrcMap = map_to_char(iedCRCMap);
  41. string strSCDCRC = CCalCRC::getScdCRC(iedCRCMap).c_str();
  42. return get_string(strSCDCRC);
  43. }
  44. const char* calcIEDCRC(const char* pszSCDFile, const char* pszIEDName)
  45. {
  46. string crcValue = CCalCRC::getIedCrc(pszSCDFile, pszIEDName).data();
  47. return get_string(crcValue);
  48. }
  49. const char* calcCRCForBytes(const char* pszBytes, int nLen)
  50. {
  51. int crc = 0xffffffff;
  52. string serial(pszBytes, nLen);
  53. crc = CCalCRC::crcbyTable(crc, serial);
  54. string crcValue = CCalCRC::dec2hex(crc);
  55. for (size_t i = crcValue.size(); i < 8; i++)
  56. crcValue.insert(0, "0");
  57. return get_string(crcValue);
  58. }
  59. void exportCCD(const char* pszSCDFile, const char* pszIEDName, const char* pszDirName)
  60. {
  61. CCalCRC::exportIedCCD(pszSCDFile, pszIEDName, pszDirName);
  62. }
  63. #ifdef __cplusplus
  64. }
  65. #endif