2008-09-16 3 views
17

SQLite를 사용하는 C++ 프로그램이 있습니다. 별도의 파일에 SQL 쿼리를 저장하려면 이 아닌 소스 코드 파일을 리소스와 같은 실행 파일에 포함하십시오.C++ 프로그램에 데이터 임베드

(이것은 리눅스에서 실행하는, 그래서 Windows 용이라면 그 완벽 할 것이지만 내가, 내가 아는 한 실제 자원으로 저장할 수 없습니다.)

있는 간단한 방법이 있나요 아니면 Linux 용 자체 리소스 시스템을 작성해야 할 필요가 있습니까? (쉽게 가능하지만 더 오래 걸릴 것입니다.)

답변

24

objcopy를 사용하여 파일 내용을 프로그램에서 사용할 수있는 심볼에 바인딩 할 수 있습니다. 자세한 내용은 here을 참조하십시오.

+0

아름다운! 고맙습니다! –

3

매크로를 사용하십시오. 기술적으로 그 파일은 소스 코드 파일이 될 것입니다. 예 :

//queries.incl - SQL queries 
Q(SELECT * FROM Users) 
Q(INSERT [a] INTO Accounts) 


//source.cpp 
#define Q(query) #query, 
char * queries[] = { 
#include "queries.incl" 
}; 
#undef Q 

나중에에 동일한 파일에 의해 해당 파일에 다른 처리의 모든 종류를 할 수있는, 당신이 배열하고 해시 맵을 갖고 싶어 말, 다른 작업을 수행하기 위해 Q를 다시 정의 할 수 있습니다 그 일을 끝내라.

1

그것은 약간 추한,하지만 당신은 항상 같은 것을 사용할 수 있습니다 query_foo.txt가 인용 쿼리 텍스트를 포함 할 경우

const char *query_foo = 
#include "query_foo.txt" 

const char *query_bar = 
#include "query_bar.txt" 

합니다.

+1

줄 바꿈이 포함 된 경우 –

+0

새 라인에서는 작동하지 않습니다. – SmallChess

3

항상 작은 프로그램이나 스크립트를 작성하여 텍스트 파일을 헤더 파일로 변환하고 빌드 프로세스의 일부로 실행할 수 있습니다.

0

자원 파일의 내용을 16 진수 형식으로 포함하여 정의 된 단 하나의 char 배열을 사용하여 리소스 파일을 C 소스 파일로 변환하여이 작업을 수행하는 것으로 나타났습니다 (악의적 인 문자 문제를 방지하기 위해). 이 자동 생성 소스 파일은 컴파일되어 프로젝트에 링크됩니다.

리소스에 액세스하기위한 일부 facade 함수를 작성하는 것처럼 각 리소스 파일에 대해 C 파일을 덤프하는 변환기를 구현하는 것은 꽤 쉬워야합니다.

2

다음은 파일의 크로스 플랫폼 임베딩에 사용 된 샘플입니다. 아주 단순하지만 아마 당신을 위해 일할 것입니다.

escapeLine 함수에서 줄 바꿈을 처리하는 방법을 변경해야 할 수도 있습니다.

#include <string> 
#include <iostream> 
#include <fstream> 
#include <cstdio> 

using namespace std; 

std::string escapeLine(std::string orig) 
{ 
    string retme; 
    for (unsigned int i=0; i<orig.size(); i++) 
    { 
     switch (orig[i]) 
     { 
     case '\\': 
      retme += "\\\\"; 
      break; 
     case '"': 
      retme += "\\\""; 
      break; 
     case '\n': // Strip out the final linefeed. 
      break; 
     default: 
      retme += orig[i]; 
     } 
    } 
    retme += "\\n"; // Add an escaped linefeed to the escaped string. 
    return retme; 
} 

int main(int argc, char ** argv) 
{ 
    string filenamein, filenameout; 

    if (argc > 1) 
     filenamein = argv[ 1 ]; 
    else 
    { 
     // Not enough arguments 
     fprintf(stderr, "Usage: %s <file to convert.mel> [ <output file name.mel> ]\n", argv[0]); 
     exit(-1); 
    } 

    if (argc > 2) 
     filenameout = argv[ 2 ]; 
    else 
    { 
     string new_ending = "_mel.h"; 
     filenameout = filenamein; 
     std::string::size_type pos; 
     pos = filenameout.find(".mel"); 
     if (pos == std::string::npos) 
      filenameout += new_ending; 
     else 
      filenameout.replace(pos, new_ending.size(), new_ending); 
    } 

    printf("Converting \"%s\" to \"%s\"\n", filenamein.c_str(), filenameout.c_str()); 

    ifstream filein(filenamein.c_str(), ios::in); 
    ofstream fileout(filenameout.c_str(), ios::out); 

    if (!filein.good()) 
    { 
     fprintf(stderr, "Unable to open input file %s\n", filenamein.c_str()); 
     exit(-2); 
    } 
    if (!fileout.good()) 
    { 
     fprintf(stderr, "Unable to open output file %s\n", filenameout.c_str()); 
     exit(-3); 
    } 

    // Write the file. 
    fileout << "tempstr = "; 

    while(filein.good()) 
    { 
     string buff; 
     if (getline(filein, buff)) 
     { 
      fileout << "\"" << escapeLine(buff) << "\"" << endl; 
     } 
    } 

    fileout << ";" << endl; 

    filein.close(); 
    fileout.close(); 

    return 0; 
} 
관련 문제