13f085823Sopenharmony_ci/* 23f085823Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 33f085823Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43f085823Sopenharmony_ci * you may not use this file except in compliance with the License. 53f085823Sopenharmony_ci * You may obtain a copy of the License at 63f085823Sopenharmony_ci * 73f085823Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83f085823Sopenharmony_ci * 93f085823Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103f085823Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113f085823Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123f085823Sopenharmony_ci * See the License for the specific language governing permissions and 133f085823Sopenharmony_ci * limitations under the License. 143f085823Sopenharmony_ci */ 153f085823Sopenharmony_ci 163f085823Sopenharmony_ci#include "detector.h" 173f085823Sopenharmony_ci 183f085823Sopenharmony_ci#include <iostream> 193f085823Sopenharmony_ci#include <cstdio> 203f085823Sopenharmony_ci#include <cstdlib> 213f085823Sopenharmony_ci#include <sys/stat.h> 223f085823Sopenharmony_ci 233f085823Sopenharmony_ciusing namespace std; 243f085823Sopenharmony_cinamespace DetectorTest { 253f085823Sopenharmony_ci const int HALF = 2; 263f085823Sopenharmony_ci const int START_INDEX = 3; 273f085823Sopenharmony_ci} 283f085823Sopenharmony_ci 293f085823Sopenharmony_cinamespace { 303f085823Sopenharmony_cibool IsPrime(int n) 313f085823Sopenharmony_ci{ 323f085823Sopenharmony_ci if (n <= 1) { 333f085823Sopenharmony_ci return false; 343f085823Sopenharmony_ci } 353f085823Sopenharmony_ci 363f085823Sopenharmony_ci if (!(n % DetectorTest::HALF)) { 373f085823Sopenharmony_ci return n == DetectorTest::HALF; 383f085823Sopenharmony_ci } 393f085823Sopenharmony_ci 403f085823Sopenharmony_ci for (int i = DetectorTest::START_INDEX;; i += DetectorTest::HALF) { 413f085823Sopenharmony_ci if (i > (n / i)) { 423f085823Sopenharmony_ci break; 433f085823Sopenharmony_ci } 443f085823Sopenharmony_ci if (!(n % i)) { 453f085823Sopenharmony_ci return false; 463f085823Sopenharmony_ci } 473f085823Sopenharmony_ci } 483f085823Sopenharmony_ci 493f085823Sopenharmony_ci return true; 503f085823Sopenharmony_ci} 513f085823Sopenharmony_ci 523f085823Sopenharmony_cibool FileExist(const char* fileName) 533f085823Sopenharmony_ci{ 543f085823Sopenharmony_ci if (fileName == nullptr) { 553f085823Sopenharmony_ci return false; 563f085823Sopenharmony_ci } 573f085823Sopenharmony_ci struct stat myStat; 583f085823Sopenharmony_ci return (!stat(fileName, &myStat)); 593f085823Sopenharmony_ci} 603f085823Sopenharmony_ci} 61