ysfx_preprocess.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2024 Joep Vanlier
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // SPDX-License-Identifier: Apache-2.0
  16. //
  17. #include "ysfx.hpp"
  18. #include "ysfx_utils.hpp"
  19. #include <map>
  20. #include "WDL/ptrlist.h"
  21. #include "WDL/assocarray.h"
  22. #include "WDL/mutex.h"
  23. #include "WDL/wdlstring.h"
  24. #include "WDL/eel2/eel_pproc.h"
  25. bool ysfx_preprocess(ysfx::text_reader &reader, ysfx_parse_error *error, std::string& in_str, std::map<std::string, ysfx_real> preprocessor_values)
  26. {
  27. std::string line;
  28. line.reserve(256);
  29. WDL_FastString file_str, pp_str;
  30. while (reader.read_next_line(line)) {
  31. line += "\n";
  32. const char *linep = line.c_str();
  33. file_str.Append(linep);
  34. }
  35. EEL2_PreProcessor pproc;
  36. for (auto it = preprocessor_values.begin(); it != preprocessor_values.end(); ++it) {
  37. pproc.define(it->first.c_str(), it->second);
  38. }
  39. const char *err = pproc.preprocess(file_str.Get(), &pp_str);
  40. if (err) {
  41. error->line = 0;
  42. error->message = std::string("Invalid section: ") + err;
  43. return false;
  44. }
  45. in_str.append(pp_str.Get(), pp_str.GetLength());
  46. return true;
  47. }