source: snovis/head/obuild/cpp/obuild_platform.cpp @ 86

Last change on this file since 86 was 6, checked in by barrand, 17 years ago
File size: 5.5 KB
Line 
1//
2//  This program is and should be standalone so that :
3//     UNIX> c++ obuild_platform.cxx
4//      DOS> cl.exe obuild_platform.cxx
5// builds at first shoot.
6//
7
8#include <string>
9#include <vector>
10#include <stdlib.h>
11#include <stdio.h>
12
13// From Lib :
14static std::vector<std::string> Lib_smanip_words(const std::string&,
15                                                 const std::string&,
16                                                 bool = false);
17
18//////////////////////////////////////////////////////////////////////////////
19static bool isKnownKeyword(
20 const std::string& aString
21)
22//////////////////////////////////////////////////////////////////////////////
23//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
24{
25  if(aString=="Windows_NT") {
26    return true;
27  } else if(aString=="Linux") {
28    return true;
29  } else if(aString=="Darwin") {
30    return true;
31  } else if(aString=="SunOS") {
32    return true;
33  } else if(aString=="OSF1") {
34    return true;
35  } else if(aString=="UNIX") {
36    return true;
37  } else {
38    return false;
39  }
40}
41//////////////////////////////////////////////////////////////////////////////
42static bool isUNIX(
43 const std::string& aString
44,bool& aResult
45)
46//////////////////////////////////////////////////////////////////////////////
47//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
48{
49  if(!isKnownKeyword(aString)) {
50    aResult = false;
51    return false;
52  }
53  if(aString=="Windows_NT") {
54    aResult = false;
55  } else {
56    aResult = true;
57  }
58  return true;
59}
60//////////////////////////////////////////////////////////////////////////////
61//////////////////////////////////////////////////////////////////////////////
62//////////////////////////////////////////////////////////////////////////////
63int main(
64 int aArgc
65,char** aArgv
66) 
67//////////////////////////////////////////////////////////////////////////////
68// aArgs[0] : program name.
69// aArgs[1] : we expect the result of `uname`.
70// aArgs[2] : an expression to compare with.
71//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
72{
73  std::vector<std::string> args;
74 {for(int index=0;index<aArgc;index++) args.push_back(aArgv[index]);}
75 
76  if(args.size()!=3) {
77    fprintf(stderr,"obuild_platform : two arguments expected.\n");
78    printf("no\n");
79    return EXIT_FAILURE;
80  }
81
82  std::string uname = args[1];
83  if(!isKnownKeyword(uname)) {
84    fprintf(stderr,"obuild_platform : unknwon keyword \"%s\".\n",
85            uname.c_str());
86    printf("no\n");
87    return EXIT_FAILURE;
88  }
89
90  std::string expression = args[2];
91
92  std::vector<std::string> words = Lib_smanip_words(expression," ");
93
94  unsigned int wordn = words.size();
95  if(wordn==1) {
96
97    const std::string& keyword = words[0];
98
99    if(keyword=="UNIX") {
100
101      bool is_unix;
102      if(!isUNIX(uname,is_unix)) {
103      } else {
104        if(is_unix) {
105          printf("yes\n");
106          return EXIT_SUCCESS;
107        } else {
108          printf("no\n");
109          return EXIT_SUCCESS;
110        }
111      }
112
113    } else if(keyword==uname) {
114
115      printf("yes\n");
116      return EXIT_SUCCESS;
117
118    } else if(isKnownKeyword(keyword)) {
119
120      printf("no\n");
121      return EXIT_SUCCESS;
122
123    }
124
125  } else if(wordn==2) {
126
127    if(words[0]=="not") {
128
129      const std::string& keyword = words[1];
130
131      if(keyword=="UNIX") {
132
133        bool is_unix;
134        if(!isUNIX(uname,is_unix)) {
135        } else {
136          if(is_unix) {
137            printf("no\n");
138            return EXIT_SUCCESS;
139          } else {
140            printf("yes\n");
141            return EXIT_SUCCESS;
142          }
143        }
144
145      } else if(keyword==uname) {
146
147        printf("no\n");
148        return EXIT_SUCCESS;
149
150      } else if(isKnownKeyword(keyword)) {
151
152        printf("yes\n");
153        return EXIT_SUCCESS;
154
155      }
156
157    }
158
159  }
160
161  fprintf(stderr,"obuild_platform : expression \"%s\" not yet supported.\n",
162          expression.c_str());
163  printf("no\n");
164  return EXIT_FAILURE;
165}
166
167//////////////////////////////////////////////////////////////////////////////
168//////////////////////////////////////////////////////////////////////////////
169//////////////////////////////////////////////////////////////////////////////
170//////////////////////////////////////////////////////////////////////////////
171//////////////////////////////////////////////////////////////////////////////
172std::vector<std::string> Lib_smanip_words(
173 const std::string& aString
174,const std::string& aLimiter
175,bool aTakeEmpty // false
176)
177//////////////////////////////////////////////////////////////////////////////
178//  If aLimiter is for exa "|" and for "xxx||xxx" :
179//  - aTakeEmpty false : {"xxx","xxx"} will be created (and NOT {"xxx","","xxx"}).
180//  - aTakeEmpty true : {"xxx","","xxx"} will be created.
181//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//
182{
183  std::vector<std::string> words;
184  if(aString=="") return words;
185  std::string::size_type lim = (aTakeEmpty?0:1);
186  if(aLimiter=="") {
187    words.push_back(aString);
188  } else {
189    std::string::size_type l = aString.length();
190    std::string::size_type llimiter = aLimiter.length();
191    std::string::size_type pos = 0;
192    while(1) {
193      std::string::size_type index = aString.find(aLimiter,pos);
194      if(index==std::string::npos){ // Last word.
195        if((l-pos)>=lim) words.push_back(aString.substr(pos,l-pos));
196        break;
197      } else {
198        //     abcxxxef
199        //     0  3  67
200        if((index-pos)>=lim) words.push_back(aString.substr(pos,index-pos));
201        pos = index + llimiter;
202      }
203    }
204  }
205  return words;
206}
Note: See TracBrowser for help on using the repository browser.