LoaderUtils.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (C) 2018 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdio.h>
  17. #include <iostream>
  18. #include <string>
  19. #include <vector>
  20. #include <android-base/strings.h>
  21. static std::string pathToFilename(const std::string& path, bool noext = false) {
  22. std::vector<std::string> spath = android::base::Split(path, "/");
  23. std::string ret = spath.back();
  24. if (noext) {
  25. size_t lastindex = ret.find_last_of('.');
  26. return ret.substr(0, lastindex);
  27. }
  28. return ret;
  29. }
  30. static int getMachineKvers(void) {
  31. struct utsname un;
  32. char* unameOut;
  33. int nums[3]; // maj, min, sub
  34. if (uname(&un)) return -1;
  35. unameOut = un.release;
  36. std::string s = unameOut;
  37. std::string token;
  38. size_t pos = 0;
  39. int cur_num = 0;
  40. while ((pos = s.find('.')) != std::string::npos && cur_num < 3) {
  41. token = s.substr(0, pos);
  42. s.erase(0, pos + 1);
  43. if ((pos = token.find('-')) != std::string::npos) token = token.substr(0, pos);
  44. nums[cur_num++] = stoi(token);
  45. }
  46. if ((pos = s.find('-')) != std::string::npos)
  47. token = s.substr(0, pos);
  48. else
  49. token = s;
  50. if (token.length() > 0 && cur_num < 3) {
  51. nums[cur_num++] = stoi(token);
  52. }
  53. if (cur_num != 3)
  54. return -1;
  55. else
  56. return (65536 * nums[0] + 256 * nums[1] + nums[2]);
  57. }
  58. static void deslash(std::string& s) {
  59. std::replace(s.begin(), s.end(), '/', '_');
  60. }