본문 바로가기
C 언어

C언어 개인 프로젝트 (매크로 함수 이용)

by treeCoder 2021. 10. 26.

C언어 개인 프로젝트이다. 여러 줄의 매크로 함수를 이용했다.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxRow 300

#define ARR_SEARCH ({ \
            flag = 0; \
                      \
            for( int i = 0; i < knt; i++ ) { \
                  strncpy(tmp2, tmp, 35 );   \
                  if( strcmp( arr[i], tmp2 ) == 0 ) { \
                      flag = 1; \
                      break; \
                  } \
            } \
})

int main( ) {
    
    int c;
    int flag;
    int i = 0;
    int knt = 0;
    
    char arr[maxRow][40];
    char tmp[2048];
    char tmp2[80];
    char *p;
    
    puts("<meta charset=\"utf-8\">");
    
    while( ( c = getchar() ) != EOF ) {
        
        tmp[i++] = c;
        
        if( i > 6 ) {
            if( tmp[i-7] == '<' &&
            tmp[i-6] == 't' &&
            tmp[i-5] == 'r' &&
            tmp[i-4] == '>' &&
            tmp[i-3] == '<' &&
            tmp[i-2] == 't' &&
            tmp[i-1] == 'd'  ) {
                
                tmp[i-7] = 0x00;  // remove "<tr><td" and finish string
                
               ARR_SEARCH;  // ************ search 
                
                // ************ if not found, add new entry
                if( flag == 0 ) {
                    if( knt == maxRow ) {
                       printf("max data: %d\n", maxRow);
                       exit(1);
                    } 
                    strncpy( arr[ knt++ ], tmp, 35 );
                    puts( tmp );
                }
                strcpy( tmp, "<tr><td"); //  "<tr><td" is on a new line
                i = 7;    
            }
        } 
    }
    
    tmp[i] = 0x00;  // finish string
    
    if(   (p = strstr( tmp, "</table></body></html>"))  )  // find  "</table></body></html>"
         *p = 0x00;                                                 // remove "</table></body></html>"

    ARR_SEARCH;
    if( flag == 0 ) puts( tmp );
    puts( "</table></body></html>" );                       // "</table></body></html>" is on a new line
    
    return 0;
}

---------------------------

위의 프로그램을 xPlant.exe로 컴파일한다.

 

아래와 같이 batch 파일을 만들고 x.bat로 저장한다.

@echo off

rem *****************************************************

rem                   x.bat                211205.xls

rem *****************************************************

rem echo ^<meta charset="utf-8"^> > temp_115115.xls
rem type %1 >> temp_115115.xls

xPlant < %1 > temp_115115.xls

"C:\Program Files\LibreOffice\program\soffice.exe" --convert-to xls temp_115115.xls -outdir "C:\temp"

ren c:\temp\temp_115115.xls %~n1b.xls
move c:\temp\%~n1b.xls    .
del temp_115115.xls

 

도스 명령창에서 아래와 같이 사용한다. (c:\temp 폴더가 미리 마련되어 있어야 한다.)

 

x.bat 211205.xls

 

결과물은 c:\temp에 211205b.xls 와 같이 파일이름에 b가 추가되어 생성된다.

 

*******************************

 

다음의 파일은 파워쉘을 이용한 대화상자가 나오는 프로그램이다. 위의 프로그램과 기능은 같다. 다만 대화상자가 있을 뿐이다.

<# : chooser.bat
:: launches a File... Open sort of file chooser and outputs choice(s) to the console
:: https://stackoverflow.com/a/15885133/1683264
:: https://codens.info/1411

@echo off
setlocal

for /f "delims=" %%I in ('powershell -noprofile "iex (${%~f0} | out-string)"') do (
  
    call :outLoop  %%~nxI

)
:outLoop
if "%1"=="" goto :EOF
echo Creating file %~n1b.xls

xPlant < %1 > temp_115115.xls

"C:\Program Files\LibreOffice\program\soffice.exe" --convert-to xls temp_115115.xls -outdir "C:\temp"

ren c:\temp\temp_115115.xls %~n1b.xls
move c:\temp\%~n1b.xls    .
del temp_115115.xls

pause
goto :EOF

: end Batch portion / begin PowerShell hybrid chimera #>

Add-Type -AssemblyName System.Windows.Forms
$f = new-object Windows.Forms.OpenFileDialog
$f.InitialDirectory = pwd
$f.Filter = "Excel Files (*.xls)|*.xls|All Files (*.*)|*.*"
$f.ShowHelp = $true
$f.Multiselect = $true
[void]$f.ShowDialog()
if ($f.Multiselect) { $f.FileNames } else { $f.FileName }

댓글