1/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/core/platform/platform_strings.h"
17
18#include <cerrno>
19#include <cstdio>
20#include <cstring>
21#include <string>
22#include <vector>
23
24namespace tensorflow {
25
26int GetPlatformStrings(const std::string& path,
27 std::vector<std::string>* found) {
28 int result;
29 FILE* ifp = fopen(path.c_str(), "rb");
30 if (ifp != nullptr) {
31 static const char prefix[] = TF_PLAT_STR_MAGIC_PREFIX_;
32 int first_char = prefix[1];
33 int last_char = -1;
34 int c;
35 while ((c = getc(ifp)) != EOF) {
36 if (c == first_char && last_char == 0) {
37 int i = 2;
38 while (prefix[i] != 0 && (c = getc(ifp)) == prefix[i]) {
39 i++;
40 }
41 if (prefix[i] == 0) {
42 std::string str;
43 while ((c = getc(ifp)) != EOF && c != 0) {
44 str.push_back(c);
45 }
46 if (!str.empty()) {
47 found->push_back(str);
48 }
49 }
50 }
51 last_char = c;
52 }
53
54 result = (ferror(ifp) == 0) ? 0 : errno;
55 if (fclose(ifp) != 0) {
56 result = errno;
57 }
58 } else {
59 result = errno;
60 }
61 return result;
62}
63
64} // namespace tensorflow
65