2015-11-10 41 views
0

我正在使用共享內存編寫一個覆蓋樹莓派的應用程序。我在我自己寫的共享內存庫中使用函數strstr()。當我用clang ++在OS X上編譯庫時,我沒有遇到任何錯誤。如果我編譯它在我的覆盆子pi我得到的錯誤:'strstr'沒有在這個範圍內聲明。錯誤:'strstr'未在此範圍內聲明

我試圖更新我的樹莓,但沒有成功,你能給我任何提示或解決方案做什麼。

頁眉Datei

#ifndef SHAREDMEMORY_H 
#define SHAREDMEMORY_H 

#include <string> 
#include <cstdlib> 
#include <stdio.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <unistd.h> 
#include <fcntl.h> 
#include <sys/mman.h> 
#include <iostream> 

#define MAX_SERVICES 99 

/** 
* Datei mit der Datenbank. 
*/ 
#define FILEPATH "database.dat" 
/** 
* Anzahl der Zeichen in der 
* Datenbank. 
*/ 
#define CHARACTERS 2500 
/** 
* Größe der Datenbank. 
*/ 
#define FILESIZE (CHARACTERS*sizeof(char)) 

class SharedMemory { 
public: 
    /** 
    * Constructor 
    */ 
    SharedMemory(); 
    /** 
    * Desctructor 
    */ 
    ~SharedMemory(); 
    /** 
    * Method to open file 
    * @param string: Path to file, has to exist 
    * @param int: for reading 0 
    *    for writing 1 
    * @return bool: true on success 
    *    false on error 
    */ 
    bool openFile(std::string, int); 
    /** 
    * Method to map file to memory 
    * @param string: Path to file, has to exist 
    * @param int: for reading 0 
    *    for writing 1 
    * @return bool: true on success 
    *    false on error 
    */ 
    bool mappingFile(int); 
    /** 
    * Method to remove file from memory 
    * @return bool: true on success 
    *    false on error 
    */ 
    bool unmapFile(); 
    /** 
    * Method to write information to file 
    * @param string: data to write 
    * e.g. string="#1:127.0.0.1:8000", #number range 0-99. 
    * @return bool: true on success 
    *    false on error 
    */ 
    bool set(std::string); 
    /** 
    * Method to read information from file 
    * @param string: need to cointains id, if success 
    *    then contains info from id. 
    * e.g. string="1", number range "0"-"99". 
    * @return bool: true on success 
    *    false on error 
    */ 
    bool get(std::string&); 
private: 
    /** 
    * Datei-Deskriptor. 
    */ 
    int fd; 
    /** 
    * Zeiger auf Dateiinhalt. 
    */ 
    char *mapPointer; 
    /** 
    * Path to file 
    */ 
    std::string filePath; 
}; 


#endif /* SHAREDMEMORY_H */ 

CPP-Datei

#include "SharedMemory.h" 

SharedMemory::SharedMemory() { } 

SharedMemory::~SharedMemory() { } 

bool SharedMemory::openFile(std::string _path, int mode) { 
    if (mode) { 
     fd = open(_path.c_str(), O_RDWR, (mode_t)0600); 
    } else { 
     fd = open(_path.c_str(), O_RDONLY, (mode_t)0600); 
    } 
    if (fd == -1) { 
     return false; 
    } 

    return true; 
} 

bool SharedMemory::mappingFile(int mode){ 
    void* tmpPointer; 
    if (mode) { 
     tmpPointer = mmap(0, FILESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 
    } else { 
     tmpPointer = mmap(0, FILESIZE, PROT_READ, MAP_SHARED, fd, 0); 
    } 

    if(tmpPointer == MAP_FAILED) { 
     close(fd); 
     return false; 
    } 
    mapPointer = (char*) tmpPointer; 

    return true; 
} 

bool SharedMemory::unmapFile() { 
    int ret = munmap(mapPointer, FILESIZE); 
    close(fd); 
    if (ret == -1) { 
     return false; 
    } 

    return true; 
} 

bool SharedMemory::set(std::string s) { 
    /** 
    * Filter id, find the id in the file. 
    * If Values exist and id is valid, insert value 
    * -> if value not exists, insert "No_Service". 
    * if given id is invalid, return false 
    */ 
    int mid = s.find(";"); 
    int begin = s.find("#"); 
    std::string id = s.substr(begin + 1, mid - begin); 
    std::string info = s.substr(mid + 1, s.length()); 
    if (info == "") { 
     info = "No_Service"; 
    } 
    char* i = strstr(mapPointer, id.c_str()); 
    while (*i++ != ';'); 
    for (auto x: info) { 
     *i++ = x; 
    } 
    for (int j = 0; j < (20 - info.length()); ++j) { 
     *i++ = ' '; 
    } 

    return true; 
} 

bool SharedMemory::get(std::string& id){ 
    /** 
    * Filter id, find the id in the file. 
    * save data in string s. 
    */ 
    int tmp; 
    try { 
     tmp = stoi(id); 
    } catch (...) { 
     id = "No_Service"; 
     return false; 
    } 

    if (tmp > 100 || tmp < 1){ 
     id = "No_Service"; 
     return false; 
    } 

    id += ";"; 
    char* i = strstr(mapPointer, id.c_str()); 
    while (*i++ != ';'); 
    id = ""; 
    do { 
     id += *i++; 
    } while(*i != ' ' && *i != ';'); 

    if (id == "No_Service"){ 
     return false; 
    } 

    return true; 
} 
+0

你至少應該感謝我們搶先幫助或插入一個笑臉來營造氛圍。你聽起來粗魯無禮地問我們事情。 – gurghet

+0

當我寫了類似嘿大家或它立即刪除。我是堆棧溢出新手,不確定爲什麼會發生這種情況。試圖多次編輯它。你能告訴我爲什麼它有這種行爲xD?和一些管理員刪除我的內容,我在哪裏感謝大家的幫助 - .- – MrNice

+0

不熟悉覆盆子派,但嘗試'包括'? – IdeaHat

回答