문제

SQLite를 사용하는 C++ 프로그램이 있습니다.SQL 쿼리를 별도의 파일(일반 텍스트 파일)에 저장하고 싶습니다. ~ 아니다 소스 코드 파일 - 리소스처럼 실행 파일에 해당 파일을 포함합니다.

(이것은 Linux에서 실행되어야 하기 때문에 내가 아는 한 실제 리소스로 저장할 수는 없지만 Windows용이라면 완벽할 것입니다.)

이를 수행할 수 있는 간단한 방법이 있습니까? 아니면 실제로 Linux용 리소스 시스템을 직접 작성해야 합니까?(쉽게 가능하지만 시간이 훨씬 더 오래 걸립니다.)

도움이 되었습니까?

해결책

objcopy를 사용하여 파일 내용을 프로그램에서 사용할 수 있는 기호에 바인딩할 수 있습니다.예를 들어 다음을 참조하세요. 여기 자세한 내용은.

다른 팁

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

매크로를 사용하세요.기술적으로 해당 파일은 소스 코드 파일이지만 이렇게 보이지는 않습니다.예:

//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를 재정의하여 다른 작업을 수행하고 완료할 수 있습니다.

다음은 크로스 플랫폼 파일 삽입에 사용한 샘플입니다.꽤 단순하지만 아마도 효과가 있을 것입니다.

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;
}

약간 보기 흉하지만 언제든지 다음과 같은 것을 사용할 수 있습니다.

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

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

query_foo.txt에는 인용된 쿼리 텍스트가 포함됩니다.

나는 리소스 파일을 16진수 형식의 리소스 파일 내용을 포함하는 단 하나의 char 배열이 정의된 C 소스 파일로 변환함으로써 수행되는 것을 보았습니다(악성 문자 관련 문제를 피하기 위해).자동으로 생성된 이 소스 파일은 간단히 컴파일되어 프로젝트에 연결됩니다.

리소스에 액세스하기 위한 몇 가지 Facade 함수를 작성하는 것뿐만 아니라 각 리소스 파일에 대해 C 파일을 덤프하는 변환기를 구현하는 것도 매우 쉬울 것입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top