@ -6,31 +6,31 @@ include(CMakeParseArguments)
# V A R I A B L E - T h e n a m e o f t h e C M a k e v a r i a b l e h o l d i n g t h e s t r i n g .
# V A R I A B L E - T h e n a m e o f t h e C M a k e v a r i a b l e h o l d i n g t h e s t r i n g .
# A T _ C O L U M N - T h e c o l u m n p o s i t i o n a t w h i c h s t r i n g w i l l b e w r a p p e d .
# A T _ C O L U M N - T h e c o l u m n p o s i t i o n a t w h i c h s t r i n g w i l l b e w r a p p e d .
function ( WRAP_STRING )
function ( WRAP_STRING )
set ( oneValueArgs VARIABLE AT_COLUMN )
set ( oneValueArgs VARIABLE AT_COLUMN )
cmake_parse_arguments ( WRAP_STRING "${options}" "${oneValueArgs}" "" ${ ARGN } )
cmake_parse_arguments ( WRAP_STRING "${options}" "${oneValueArgs}" "" ${ ARGN } )
string ( LENGTH ${ ${WRAP_STRING_VARIABLE } } stringLength )
string ( LENGTH ${ ${WRAP_STRING_VARIABLE } } stringLength )
math ( EXPR offset "0" )
math ( EXPR offset "0" )
while ( stringLength GREATER 0 )
while ( stringLength GREATER 0 )
if ( stringLength GREATER ${ WRAP_STRING_AT_COLUMN } )
if ( stringLength GREATER ${ WRAP_STRING_AT_COLUMN } )
math ( EXPR length "${WRAP_STRING_AT_COLUMN}" )
math ( EXPR length "${WRAP_STRING_AT_COLUMN}" )
else ( )
else ( )
math ( EXPR length "${stringLength}" )
math ( EXPR length "${stringLength}" )
endif ( )
endif ( )
string ( SUBSTRING ${ ${WRAP_STRING_VARIABLE } } ${ offset } ${ length } line )
string ( SUBSTRING ${ ${WRAP_STRING_VARIABLE } } ${ offset } ${ length } line )
set ( lines "${lines}\n${line}" )
set ( lines "${lines}\n${line}" )
math ( EXPR stringLength "${stringLength} - ${length}" )
math ( EXPR stringLength "${stringLength} - ${length}" )
math ( EXPR offset "${offset} + ${length}" )
math ( EXPR offset "${offset} + ${length}" )
endwhile ( )
endwhile ( )
set ( ${ WRAP_STRING_VARIABLE } "${lines}" PARENT_SCOPE )
set ( ${ WRAP_STRING_VARIABLE } "${lines}" PARENT_SCOPE )
endfunction ( )
endfunction ( )
# F u n c t i o n t o e m b e d c o n t e n t s o f a f i l e a s b y t e a r r a y i n C / C + + h e a d e r file ( .h ) . T h e h e a d e r f i l e
# S c r i p t t o e m b e d c o n t e n t s o f a f i l e a s b y t e a r r a y i n C / C + + h e a d e r file ( .h ) . T h e h e a d e r f i l e
# w i l l c o n t a i n a b y t e a r r a y a n d i n t e g e r v a r i a b l e h o l d i n g t h e s i z e o f t h e a r r a y .
# w i l l c o n t a i n a b y t e a r r a y a n d i n t e g e r v a r i a b l e h o l d i n g t h e s i z e o f t h e a r r a y .
# P a r a m e t e r s
# P a r a m e t e r s
# S O U R C E _ F I L E - T h e p a t h o f s o u r c e f i l e w h o s e c o n t e n t s w i l l b e e m b e d d e d i n t h e h e a d e r f i l e .
# S O U R C E _ F I L E - T h e p a t h o f s o u r c e f i l e w h o s e c o n t e n t s w i l l b e e m b e d d e d i n t h e h e a d e r f i l e .
@ -42,45 +42,41 @@ endfunction()
# u s e f u l i f t h e s o u r c e f i l e i s a t e x t f i l e a n d w e w a n t t o u s e t h e f i l e c o n t e n t s
# u s e f u l i f t h e s o u r c e f i l e i s a t e x t f i l e a n d w e w a n t t o u s e t h e f i l e c o n t e n t s
# a s s t r i n g . B u t t h e s i z e v a r i a b l e h o l d s s i z e o f t h e b y t e a r r a y w i t h o u t t h i s
# a s s t r i n g . B u t t h e s i z e v a r i a b l e h o l d s s i z e o f t h e b y t e a r r a y w i t h o u t t h i s
# n u l l b y t e .
# n u l l b y t e .
# U s a g e :
set ( options APPEND NULL_TERMINATE )
# bin2h ( SOURCE_FILE "Logo.png" HEADER_FILE "Logo.h" VARIABLE_NAME "LOGO_PNG" )
set ( oneValueArgs SOURCE_FILE VARIABLE_NAME HEADER_FILE )
function ( BIN2H )
# cmake_parse_arguments ( BIN2H "${options}" "${oneValueArgs}" "" ${ ARGN } )
set ( options APPEND NULL_TERMINATE )
set ( oneValueArgs SOURCE_FILE VARIABLE_NAME HEADER_FILE )
cmake_parse_arguments ( BIN2H "${options}" "${oneValueArgs}" "" ${ ARGN } )
# r e a d s s o u r c e f i l e c o n t e n t s a s h e x s t r i n g
# r e a d s s o u r c e f i l e c o n t e n t s a s h e x s t r i n g
file ( READ ${ BIN2H_SOURCE_FILE } hexString HEX )
file ( READ ${ BIN2H_SOURCE_FILE } hexString HEX )
string ( LENGTH ${ hexString } hexStringLength )
string ( LENGTH ${ hexString } hexStringLength )
# a p p e n d s n u l l b y t e i f a s k e d
# a p p e n d s n u l l b y t e i f a s k e d
if ( BIN2H_NULL_TERMINATE )
if ( BIN2H_NULL_TERMINATE )
set ( hexString "${hexString}00" )
set ( hexString "${hexString}00" )
endif ( )
endif ( )
# w r a p s t h e h e x s t r i n g i n t o m u l t i p l e l i n e s a t c o l u m n 32 ( i.e. 16 bytes per line )
# w r a p s t h e h e x s t r i n g i n t o m u l t i p l e l i n e s a t c o l u m n 32 ( i.e. 16 bytes per line )
wrap_string ( VARIABLE hexString AT_COLUMN 32 )
wrap_string ( VARIABLE hexString AT_COLUMN 32 )
math ( EXPR arraySize "${hexStringLength} / 2" )
math ( EXPR arraySize "${hexStringLength} / 2" )
# a d d s ' 0 x ' p r e f i x a n d c o m m a s u f f i x b e f o r e a n d a f t e r e v e r y b y t e r e s p e c t i v e l y
# a d d s ' 0 x ' p r e f i x a n d c o m m a s u f f i x b e f o r e a n d a f t e r e v e r y b y t e r e s p e c t i v e l y
string ( REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1, " arrayValues ${ hexString } )
string ( REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1, " arrayValues ${ hexString } )
# r e m o v e s t r a i l i n g c o m m a
# r e m o v e s t r a i l i n g c o m m a
string ( REGEX REPLACE ", $" "" arrayValues ${ arrayValues } )
string ( REGEX REPLACE ", $" "" arrayValues ${ arrayValues } )
# c o n v e r t s t h e v a r i a b l e n a m e i n t o p r o p e r C i d e n t i f i e r
# c o n v e r t s t h e v a r i a b l e n a m e i n t o p r o p e r C i d e n t i f i e r
IF ( ${ CMAKE_VERSION } GREATER 2.8.10 ) # f i x f o r l e g a c y c m a k e
IF ( ${ CMAKE_VERSION } GREATER 2.8.10 ) # f i x f o r l e g a c y c m a k e
string ( MAKE_C_IDENTIFIER "${BIN2H_VARIABLE_NAME}" BIN2H_VARIABLE_NAME )
string ( MAKE_C_IDENTIFIER "${BIN2H_VARIABLE_NAME}" BIN2H_VARIABLE_NAME )
ENDIF ( )
ENDIF ( )
string ( TOUPPER "${BIN2H_VARIABLE_NAME}" BIN2H_VARIABLE_NAME )
string ( TOUPPER "${BIN2H_VARIABLE_NAME}" BIN2H_VARIABLE_NAME )
# d e c l a r e s b y t e a r r a y a n d t h e l e n g t h v a r i a b l e s
# d e c l a r e s b y t e a r r a y a n d t h e l e n g t h v a r i a b l e s
set ( arrayDefinition "const unsigned char ${BIN2H_VARIABLE_NAME}[] = { ${arrayValues} };" )
set ( arrayDefinition "const unsigned char ${BIN2H_VARIABLE_NAME}[] = { ${arrayValues} };" )
set ( arraySizeDefinition "const size_t ${BIN2H_VARIABLE_NAME}_SIZE = ${arraySize};" )
set ( arraySizeDefinition "const size_t ${BIN2H_VARIABLE_NAME}_SIZE = ${arraySize};" )
set ( declarations "${arrayDefinition}\n\n${arraySizeDefinition}\n\n" )
set ( declarations "${arrayDefinition}\n\n${arraySizeDefinition}\n\n" )
if ( BIN2H_APPEND )
if ( BIN2H_APPEND )
file ( APPEND ${ BIN2H_HEADER_FILE } "${declarations}" )
file ( APPEND ${ BIN2H_HEADER_FILE } "${declarations}" )
else ( )
else ( )
file ( WRITE ${ BIN2H_HEADER_FILE } "${declarations}" )
file ( WRITE ${ BIN2H_HEADER_FILE } "${declarations}" )
endif ( )
endif ( )
endfunction ( )