Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Version 2.1: Initial upload

  • Loading branch information...
commit dfc735edadc40073ea28850f68bff0e0313148e2 0 parents
@pebble8888 pebble8888 authored committed
53 README
@@ -0,0 +1,53 @@
+This is a mirror of http://www.vim.org/scripts/script.php?script_id=4295
+
+smartgrep
+=======
+Grep word in source file excluding comment for vim plugin.
+
+Support OS
+=======
+Unix, MacOS X, Windows
+
+Suport Language and Comment Type
+=======
+C, C++, Objective-C, C#(CSharp), Java Script /* */, //
+
+Usage (smartgrep.vim command)
+=======
+Define g:base_dir in your .vimrc file.
+ example:
+ let g:base_dir="/develop/"
+
+,g -> recursive word grep for c,h file exclude comment by mouse cursored word
+,h -> recursive word grep for h file exclude comment by mouse cursored word
+:Rn -> recursive word grep for c,h file include comment
+:Rh -> recursive word grep for h file exclude comment
+:R -> recursive word grep for c,h file exclude comment
+:Rno -> recursive grep for c,h file include comment
+:Rho -> recursive grep for h file exclude comment
+:Ro -> recursive grep for c,h file exclude comment
+:Rl -> recursive word grep for c,h file exclude comment in sys_dir_w
+:Rw -> recursive word grep for c,h file exclude comment in sys_dir_l
+
+Usage (smartgrep binary)
+=======
+$ cd {directory-you-want-to-grep}
+$ smartgrep
+Usage: smartgrep /h {word} : recursive grep for .h excluding comment
+ /b {word} : recursive grep for .cpp .c .mm .m .cs .js .h excluding comment
+ /n {word} : recursive grep for .cpp .c .mm .m .cs .js .h including comment
+ /hw {word} : recursive word grep for .h excluding comment
+ /bw {word} : recursive word grep for .cpp .c .mm .m .cs .js .h excluding comment
+ /nw {word} : recursive word grep for .cpp .c .mm .m .cs .js .h including comment
+
+INSTALL
+=======
+If you are Windows or Mac OS X user, read ./plugin/smartgrep.vim file.
+If you are Unix user, type 'make + [enter]' and read ./plugin/smartgrep.vim file.
+
+git hub repogitory
+=======
+https://github.com/pebble8888/smartgrep
+
+If you have any comment, please issue it in github;
+
45 README.md
@@ -0,0 +1,45 @@
+smartgrep
+=======
+Grep word in source file excluding comment for vim plugin.
+
+Support OS
+=======
+Unix, MacOS X, Windows
+
+Suport Language and Comment Type
+=======
+C, C++, Objective-C, C#(CSharp), Java Script /* */, //
+
+Usage (smartgrep.vim command)
+=======
+Define g:base_dir in your .vimrc file.
+ example:
+ let g:base_dir="/develop/"
+
+,g -> recursive word grep for c,h file exclude comment by mouse cursored word
+,h -> recursive word grep for h file exclude comment by mouse cursored word
+:Rn -> recursive word grep for c,h file include comment
+:Rh -> recursive word grep for h file exclude comment
+:R -> recursive word grep for c,h file exclude comment
+:Rno -> recursive grep for c,h file include comment
+:Rho -> recursive grep for h file exclude comment
+:Ro -> recursive grep for c,h file exclude comment
+:Rl -> recursive word grep for c,h file exclude comment in sys_dir_w
+:Rw -> recursive word grep for c,h file exclude comment in sys_dir_l
+
+Usage (smartgrep binary)
+=======
+$ cd {directory-you-want-to-grep}
+$ smartgrep
+Usage: smartgrep /h {word} : recursive grep for .h excluding comment
+ /b {word} : recursive grep for .cpp .c .mm .m .cs .js .h excluding comment
+ /n {word} : recursive grep for .cpp .c .mm .m .cs .js .h including comment
+ /hw {word} : recursive word grep for .h excluding comment
+ /bw {word} : recursive word grep for .cpp .c .mm .m .cs .js .h excluding comment
+ /nw {word} : recursive word grep for .cpp .c .mm .m .cs .js .h including comment
+
+INSTALL
+=======
+If you are Windows or Mac OS X user, read ./plugin/smartgrep.vim file.
+If you are Unix user, type 'make + [enter]' and read ./plugin/smartgrep.vim file.
+
151 plugin/smartgrep.vim
@@ -0,0 +1,151 @@
+"
+" Filename : smartgrep.vim
+" Brief : This file is vim plugin
+" Function : grep source file excluding comment
+" Author : pebble8888@gmail.com 2009-2012 Copyright
+" History :
+" Ver1.0.0.0 2009-09-13 initial revision for grep excluding comment
+" Ver1.1.0.0 2009-09-27 add word grep function
+" Ver1.2.0.0 2010-01-23 care .hpp file extension
+" exclude hided directory begins dot(.)
+" Ver1.3.0.0 2010-01-30 add option designate .h and .cpp
+" Ver1.4.0.0 2010-03-20 add option include comment
+" delete option only .c file
+" Ver1.5.0.0 2010-08-11 add MAC version, add .m/.mm file
+" Ver1.6.0.0 2011-01-12 fix bug doesn't operate in capital letter extension
+" Ver1.7.0.0 2011-02-02 fix bug doesn't operate in option including comment
+" Ver1.7.1.0 2011-02-10 create pgrep.vim and fix up usage
+" Ver1.7.2.0 2011-02-10 fix bug in pgrep.vim
+" Ver1.7.3.0 2011-10-15 improve help and add makefile for Unix
+" Ver2.0.0.0 2012-10-18 rename pgrep -> smartgrep
+" Ver2.1.0.0 2012-10-19 cover .cs (CSharp), .js (Java Script)
+"
+" Cover OS
+" Windows/Unix
+"
+" Cover File Extention Cover Comment Type
+" C++ : *.c *.cpp *.cxx *.tli *.inc *.rc /* */, //
+" C++ heder : *.h *.hpp *.hxx *.tlh *.inl /* */, //
+" Objective-C : *.m *.mm /* */, //
+" CSharp : *.cs /* */, //
+" JavaScript : *.js /* */, //
+"
+" How To Install
+" Put smartgrep.exe or smartgrep in C:\windows\system32\ or pathed directory.
+" Put smartgrep.vim in vim plugin directory
+" example:
+" C:\vim7\runtime\plugin\ (for windows)
+" ~/.vim/plugin/ (for Unix)
+"
+" Define g:base_dir in your .vimrc file.
+" example:
+" let g:base_dir="c:\\develop" (for windows)
+" let g:base_dir="/develop/" (for Unix)
+"
+" If you use MacVim, add smartgrep path to .vimrc file.
+" example:
+" let $PATH .= ':~/bin'
+"
+" Optional
+" Define s:sys_dir_w and s:sys_dir_l in your .vimrc file.
+" example:
+" let g:sys_dir_w="c:\\WinDDK" (for windows)
+" let g:sys_dir_l="c:\\linux\\include" (for windows)
+"
+" Implementation below
+
+function! RSmartGrepNW(word)
+ set grepprg=smartgrep\ /nw
+ execute "cd " . g:base_dir
+ execute "lgrep " . a:word
+ lopen
+ set grepprg&
+endfunction
+
+function! RSmartGrepHW(word)
+ set grepprg=smartgrep\ /hw
+ execute "cd " . g:base_dir
+ execute "lgrep " . a:word
+ lopen
+ set grepprg&
+endfunction
+
+function! RSmartGrepW(word)
+ set grepprg=smartgrep\ /bw
+ execute "cd " . g:base_dir
+ execute "lgrep " . a:word
+ lopen
+ set grepprg&
+endfunction
+
+function! RSmartGrepN(word)
+ set grepprg=smartgrep\ /n
+ execute "cd " . g:base_dir
+ execute "lgrep " . a:word
+ lopen
+ set grepprg&
+endfunction
+
+function! RSmartGrepH(word)
+ set grepprg=smartgrep\ /h
+ execute "cd " . g:base_dir
+ execute "lgrep " . a:word
+ lopen
+ set grepprg&
+endfunction
+
+function! RSmartGrep(word)
+ set grepprg=smartgrep\ /b
+ execute "cd " . g:base_dir
+ execute "lgrep " . a:word
+ lopen
+ set grepprg&
+endfunction
+
+function! RSmartGrepWA(word)
+ set grepprg=smartgrep\ /bw
+ execute "cd " . g:sys_dir
+ execute "lgrep " . a:word
+ lopen
+ set grepprg&
+endfunction
+
+function! RSmartGrepWW(word)
+ set grepprg=smartgrep\ /bw
+ execute "cd " . g:sys_dir_w
+ execute "lgrep " . a:word
+ lopen
+ set grepprg&
+endfunction
+
+function! RSmartGrepWL(word)
+ set grepprg=smartgrep\ /bw
+ execute "cd " . g:sys_dir_l
+ execute "lgrep " . a:word
+ lopen
+ set grepprg&
+endfunction
+
+" ,g : recursive word grep for c,h file exclude comment by mouse cursored word
+" ,h : recursive word grep for h file exclude comment by mouse cursored word
+noremap ,g :call RSmartGrepW("<C-R><C-W>")<CR>
+noremap ,h :call RSmartGrepHW("<C-R><C-W>")<CR>
+
+" :Rn -> recursive word grep for c,h file include comment
+" :Rh -> recursive word grep for h file exclude comment
+" :R -> recursive word grep for c,h file exclude comment
+" :Rno -> recursive grep for c,h file include comment
+" :Rho -> recursive grep for h file exclude comment
+" :Ro -> recursive grep for c,h file exclude comment
+command! -nargs=1 -complete=file Rn call RSmartGrepNW("<args>")
+command! -nargs=1 -complete=file Rh call RSmartGrepHW("<args>")
+command! -nargs=1 -complete=file R call RSmartGrepW("<args>")
+command! -nargs=1 -complete=file Rno call RSmartGrepN("<args>")
+command! -nargs=1 -complete=file Rho call RSmartGrepH("<args>")
+command! -nargs=1 -complete=file Ro call RSmartGrep("<args>")
+
+" :Rl -> recursive word grep for c,h file exclude comment in sys_dir_w
+" :Rw -> recursive word grep for c,h file exclude comment in sys_dir_l
+command! -nargs=1 -complete=file Rl call RSmartGrepWL("<args>")
+command! -nargs=1 -complete=file Rw call RSmartGrepWW("<args>")
+
21 smartgrep/makefile
@@ -0,0 +1,21 @@
+#
+# makefile for Unix / Linux / MacOS
+# Copyright (C) 2011-2012 pebble8888@gmail.com
+#
+CC=gcc
+TARGET=smartgrep
+
+all:$(TARGET)
+
+OBJS=smartgrep.o
+
+$(TARGET): $(OBJS)
+ $(CC) -o $@ $?
+ rm -f $(OBJS)
+
+.c.o:
+ $(CC) -c $<
+
+clean:
+ rm -f $(OBJS) $(TARGET)
+
15 smartgrep/resource.h
@@ -0,0 +1,15 @@
+//{{NO_DEPENDENCIES}}
+// Microsoft Developer Studio generated include file.
+// Used by smartgrep.rc
+//
+
+// Next default values for new objects
+//
+#ifdef APSTUDIO_INVOKED
+#ifndef APSTUDIO_READONLY_SYMBOLS
+#define _APS_NEXT_RESOURCE_VALUE 101
+#define _APS_NEXT_COMMAND_VALUE 40001
+#define _APS_NEXT_CONTROL_VALUE 1000
+#define _APS_NEXT_SYMED_VALUE 101
+#endif
+#endif
BIN  smartgrep/smartgrep
Binary file not shown
462 smartgrep/smartgrep.cpp
@@ -0,0 +1,462 @@
+/**
+ * @file smartgrep.cpp
+ * @author pebble8888@gmail.com
+ */
+
+#ifdef WIN32
+#define WIN32_LEAN_AND_MEAN
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <ctype.h>
+#include <sys/types.h>
+
+#ifdef WIN32
+#else
+#include <unistd.h>
+#include <dirent.h>
+#endif
+
+#include "smartgrep.h"
+
+#ifdef WIN32
+#include <windows.h>
+#endif
+
+#include <string.h>
+#include <assert.h>
+
+const static int DATASIZE = 64 * 1024;
+
+void test( void )
+{
+ test_is_alnum_or_underscore();
+}
+
+int main(int argc, char* argv[])
+{
+#ifdef _DEBUG
+ test();
+#endif
+
+ if( argc - 1 != 2 ){
+ usage();
+ return 1;
+ }
+
+ int wordtype = 0;
+ int filetype = 0;
+ if( strcmp( argv[1], "/n" ) == 0 ){
+ filetype |= SMARTGREP_FILETYPE_SOURCE;
+ filetype |= SMARTGREP_FILETYPE_HEADER;
+ wordtype |= SMARTGREP_WORDTYPE_NORMAL;
+ wordtype |= SMARTGREP_WORDTYPE_INCLUDE_COMMENT;
+ } else if( strcmp( argv[1], "/h" ) == 0 ){
+ filetype |= SMARTGREP_FILETYPE_HEADER;
+ wordtype |= SMARTGREP_WORDTYPE_NORMAL;
+ wordtype |= SMARTGREP_WORDTYPE_EXCLUDE_COMMENT;
+ } else if( strcmp( argv[1], "/b" ) == 0 ){
+ filetype |= SMARTGREP_FILETYPE_SOURCE;
+ filetype |= SMARTGREP_FILETYPE_HEADER;
+ wordtype |= SMARTGREP_WORDTYPE_NORMAL;
+ wordtype |= SMARTGREP_WORDTYPE_EXCLUDE_COMMENT;
+ } else if( strcmp( argv[1], "/nw" ) == 0 ){
+ filetype |= SMARTGREP_FILETYPE_SOURCE;
+ filetype |= SMARTGREP_FILETYPE_HEADER;
+ wordtype |= SMARTGREP_WORDTYPE_WORD;
+ wordtype |= SMARTGREP_WORDTYPE_INCLUDE_COMMENT;
+ } else if( strcmp( argv[1], "/hw" ) == 0 ){
+ filetype |= SMARTGREP_FILETYPE_HEADER;
+ wordtype |= SMARTGREP_WORDTYPE_WORD;
+ wordtype |= SMARTGREP_WORDTYPE_EXCLUDE_COMMENT;
+ } else if( strcmp( argv[1], "/bw" ) == 0 ){
+ filetype |= SMARTGREP_FILETYPE_SOURCE;
+ filetype |= SMARTGREP_FILETYPE_HEADER;
+ wordtype |= SMARTGREP_WORDTYPE_WORD;
+ wordtype |= SMARTGREP_WORDTYPE_EXCLUDE_COMMENT;
+ } else {
+ usage();
+ return 1;
+ }
+ char path[512];
+ memset( path, 0, sizeof(path) );
+ smartgrep_getcwd( path, sizeof(path) );
+ char* word = argv[2];
+#ifdef WIN32
+ parse_directory_win( path, filetype, wordtype, word );
+#else
+ parse_directory_mac( path, filetype, wordtype, word );
+#endif
+ return 0;
+}
+
+void smartgrep_getcwd( char* buf, size_t size )
+{
+#ifdef WIN32
+ GetCurrentDirectory( size, buf );
+#else
+ getcwd( buf, size );
+#endif
+}
+
+
+void usage( void )
+{
+ printf(
+ "Usage: smartgrep /h {word} : recursive grep for .h excluding comment\n"
+ " /b {word} : recursive grep for .cpp .c .mm .m .h .cs .js excluding comment\n"
+ " /n {word} : recursive grep for .cpp .c .mm .m .h .cs .js including comment\n"
+ " /hw {word} : recursive word grep for .h excluding comment\n"
+ " /bw {word} : recursive word grep for .cpp .c .mm .m .h .cs .js excluding comment\n"
+ " /nw {word} : recursive word grep for .cpp .c .mm .m .h .cs .js including comment\n"
+ );
+ print_version();
+}
+
+#ifdef WIN32
+/*
+ * @param char* path
+ * @param int filetype SMARTGREP_FILETYPE_SOURCE: .c/.cpp/.m/.mm/etc
+ * SMARTGREP_FILETYPE_HEADER: .h/.hpp/etc
+ * @param int wordtype SMARTGREP_WORDTYPE_WORD_EXCLUDE_COMMENT: \<{word}\>
+ * SMARTGREP_WORDTYPE_NORMAL_EXCLUDE_COMMENT: word
+ * SMARTGREP_WORDTYPE_NORMAL_INCLUDE_COMMENT: word
+ * @param char* target_word
+ */
+void parse_directory_win( char* path, int filetype, int wordtype, char* target_word )
+{
+ char path_name[512];
+ strcpy( path_name, path );
+ strcat( path_name, "\\*.*" );
+
+ HANDLE h_find;
+ WIN32_FIND_DATA find_data;
+ h_find = FindFirstFile( path_name, &find_data );
+ if( h_find == INVALID_HANDLE_VALUE ){
+ printf( "directory read error! [%s]\n", path );
+ return;
+ }
+ while( true ){
+ if( strcmp( find_data.cFileName, "." ) == 0 ||
+ strcmp( find_data.cFileName, ".." ) == 0 ){
+ // do nothing
+ ;
+ }
+ else if( (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) &&
+ find_data.cFileName[0] != '.' ){
+ // not hided directory
+ char path_name_r[512];
+ strcpy( path_name_r, path );
+ strcat( path_name_r, "\\" );
+ strcat( path_name_r, find_data.cFileName );
+ parse_directory_win( path_name_r, filetype, wordtype, target_word );
+ } else if( ( (filetype & SMARTGREP_FILETYPE_SOURCE ) && is_source_file( find_data.cFileName ) ) ||
+ ( (filetype & SMARTGREP_FILETYPE_HEADER ) && is_header_file( find_data.cFileName ) ) ){
+ char file_name_r[512];
+ strcpy( file_name_r, path );
+ strcat( file_name_r, "\\" );
+ strcat( file_name_r, find_data.cFileName );
+ parse_file( file_name_r, wordtype, target_word );
+ }
+ BOOL ret = FindNextFile( h_find, &find_data );
+ if( !ret ){
+ DWORD dwError = GetLastError();
+ assert( dwError == ERROR_NO_MORE_FILES );
+ break;
+ }
+ }
+ FindClose( h_find );
+}
+
+#else
+
+/*
+ * @param char* path
+ * @param int filetype SMARTGREP_FILETYPE_SOURCE: .c/.cpp/.m/.mm/.cs/.js etc
+ * SMARTGREP_FILETYPE_HEADER: .h/.hpp/etc
+ * @param int wordtype SMARTGREP_WORDTYPE_WORD_EXCLUDE_COMMENT: \<{word}\>
+ * SMARTGREP_WORDTYPE_NORMAL_EXCLUDE_COMMENT: word
+ * SMARTGREP_WORDTYPE_NORMAL_INCLUDE_COMMENT: word
+ * @param char* target_word
+ */
+void parse_directory_mac( char* path, int filetype, int wordtype, char* target_word )
+{
+ char path_name[512];
+ strcpy( path_name, path );
+ strcat( path_name, "\\*.*" );
+
+ DIR* p_dir = opendir( path );
+ if( p_dir == NULL ){
+ printf( "directory read error! [%s]\n", path );
+ return;
+ }
+ struct dirent* p_dirent;
+ while( (p_dirent = readdir( p_dir )) != NULL ){
+ if( strcmp( p_dirent->d_name, "." ) == 0 ||
+ strcmp( p_dirent->d_name, ".." ) == 0 ){
+ // do nothing
+ ;
+ }
+ else if( (p_dirent->d_type == DT_DIR ) &&
+ p_dirent->d_name[0] != '.' ){
+ // not hided directory
+ char path_name_r[512];
+ strcpy( path_name_r, path );
+ strcat( path_name_r, "/" );
+ strcat( path_name_r, p_dirent->d_name );
+ parse_directory_mac( path_name_r, filetype, wordtype, target_word );
+ } else if( ( (filetype & SMARTGREP_FILETYPE_SOURCE ) && is_source_file( p_dirent->d_name ) ) ||
+ ( (filetype & SMARTGREP_FILETYPE_HEADER ) && is_header_file( p_dirent->d_name ) ) ){
+ char file_name_r[512];
+ strcpy( file_name_r, path );
+ strcat( file_name_r, "/" );
+ strcat( file_name_r, p_dirent->d_name );
+ parse_file( file_name_r, wordtype, target_word );
+ }
+ }
+ closedir( p_dir );
+}
+#endif
+
+bool is_source_file( char* file_name ){
+ if( is_ext( file_name, "c" ) ||
+ is_ext( file_name, "cpp" ) ||
+ is_ext( file_name, "cxx" ) ||
+ is_ext( file_name, "tli" ) ||
+ is_ext( file_name, "inc" ) ||
+ is_ext( file_name, "rc" ) ||
+ is_ext( file_name, "m" ) ||
+ is_ext( file_name, "mm" ) ||
+ is_ext( file_name, "cs" ) ||
+ is_ext( file_name, "js" ) ) {
+ return true;
+ } else {
+ return false;
+ }
+}
+
+bool is_header_file( char* file_name ){
+ if( is_ext( file_name, "h" ) ||
+ is_ext( file_name, "hpp" ) ||
+ is_ext( file_name, "hxx" ) ||
+ is_ext( file_name, "tlh" ) ||
+ is_ext( file_name, "inl" ) ){
+ return true;
+ } else {
+ return false;
+ }
+}
+
+/*
+ * @param char: ext_name "c", "cpp", "h", etc
+ */
+bool is_ext( char* file_name, const char* ext_name ){
+ char* period = strrchr( file_name, '.' );
+ if( period == NULL ){
+ return false;
+ }
+ char buf[512];
+ memset( buf, 0, sizeof(buf) );
+ char* p;
+ char* q;
+ for( p = period + 1, q = buf; *p != '\0'; ++p, ++q ){
+ *q = tolower( *p );
+ }
+ return ( strcmp( buf, ext_name ) == 0 );
+}
+
+/*
+ * @brief parse one file
+ * output to standard out
+ */
+void parse_file( char* file_name, int wordtype, char* target_word )
+{
+ // it is presumed that the one line byte size in file don't exceed 64k
+ char p_data[DATASIZE];
+
+ FILE* fp = fopen( file_name, "rb" );
+ if( fp == NULL )
+ return;
+
+ bool c_comment = false;
+ int lineno;
+ for( lineno = 1; ; ++lineno ){
+ memset( p_data, 0, sizeof(DATASIZE) );
+ char* ptr = fgets( p_data, DATASIZE, fp );
+ if( ptr == NULL )
+ break;
+
+ bool found;
+ if( wordtype & SMARTGREP_WORDTYPE_EXCLUDE_COMMENT ){
+ found = process_line_exclude_comment( &c_comment, p_data, wordtype, target_word );
+ } else if( wordtype & SMARTGREP_WORDTYPE_INCLUDE_COMMENT ){
+ found = process_line_include_comment( p_data, wordtype, target_word );
+ } else {
+ assert( false );
+ }
+ if( found ){
+ printf( "%s:%d:", file_name, lineno );
+ for( char* q = p_data; *q != '\r' && *q != '\n'; ++q ){
+ printf( "%c", *q );
+ }
+ printf( "\n" );
+ }
+ }
+ fclose( fp );
+}
+
+/*
+ * @brief parse one line in file excluding comemnt
+ *
+ * @retval true : found
+ * @retval false: not found
+ *
+ * @param [in/out] bool* pc_comment whether in C comment
+ * @param [in] char* buf
+ * @param [in] int wordtype
+ * @param [in] char* target_word
+ */
+bool process_line_exclude_comment( bool* pc_comment, char* buf, int wordtype, char* target_word )
+{
+ char valid_str[DATASIZE];
+ memset( valid_str, 0, sizeof(valid_str) );
+
+ int literal = 0;
+ size_t i;
+ char* ptr = valid_str;
+ for( i = 0; i < DATASIZE; ++i ){
+ if( buf[i] == '\n' || buf[i] == '\0' ) break;
+ if( !literal && !(*pc_comment) && buf[i] == '/' && buf[i+1] == '/' ){
+ // C++ comment
+ break;
+ } else if( !literal && !(*pc_comment) && buf[i] == '/' && buf[i+1] == '*' ){
+ // C comment in
+ i += 2;
+ *pc_comment = true;
+ continue;
+ } else if( !literal && *pc_comment && buf[i] == '*' && buf[i+1] == '/' ){
+ i += 2;
+ *pc_comment = false;
+ } else if( !literal && *pc_comment && !( buf[i] == '*' && buf[i+1] == '/' ) ){
+ while( true ){
+ if( buf[i] == '\n' ) goto WHILEOUT;
+ if( buf[i] == '*' && buf[i+1] == '/' )
+ break;
+ ++i;
+ }
+ i += 2;
+ *pc_comment = false;
+ } else if( !*pc_comment && ( buf[i] == '\"' || buf[i] == '\'' ) && ( buf[i-1] != '\\' ) ) {
+ // reverse literal
+ literal = !literal;
+ }
+ if( buf[i] == '\r' ) break;
+ if( buf[i] == '\n' || buf[i] == '\0' ) break;
+
+ *ptr = buf[i];
+ ++ptr;
+ }
+WHILEOUT:
+ return findword_in_line( valid_str, wordtype, target_word );
+}
+
+/*
+ * @brief search string in one line.
+ * strstr can be hit twice in one line.
+ *
+ * @retval true : found
+ * @retval false: not found
+ *
+ * @param [in] char* valid_str
+ * @param [in] int wordtype
+ * @param [in] char* target_word
+ */
+bool findword_in_line( char* valid_str, int wordtype, char* target_word )
+{
+ if( wordtype & SMARTGREP_WORDTYPE_NORMAL ){
+ // normal search
+ return ( strstr( valid_str, target_word ) != NULL );
+ } else if( wordtype & SMARTGREP_WORDTYPE_WORD ){
+ // word search
+ int target_word_len = strlen(target_word);
+ char* remain_ptr = valid_str;
+ char* findptr = strstr( valid_str, target_word );
+
+ while( findptr != NULL ){
+ bool head = false;
+ bool tail = false;
+ if( findptr == remain_ptr ){
+ head = true;
+ } else {
+ head = !is_alnum_or_underscore( *(findptr-1) );
+ }
+
+ if( head ){
+ int remain_str_len = strlen(remain_ptr);
+ if( remain_ptr + remain_str_len == findptr + target_word_len ){
+ tail = true;
+ } else {
+ tail = !is_alnum_or_underscore( *(findptr + target_word_len) );
+ }
+ if( tail ){
+ return true;
+ }
+ }
+ remain_ptr = findptr;
+ findptr = strstr( findptr + target_word_len , target_word );
+ }
+ return false;
+ }
+ assert( false );
+ return false;
+}
+
+/**
+ * @retval true: found
+ * @retval false:not found
+ */
+bool process_line_include_comment( char* buf, int wordtype, char* target_word )
+{
+ return findword_in_line( buf, wordtype, target_word );
+}
+
+void test_is_alnum_or_underscore( void )
+{
+ assert( is_alnum_or_underscore( 'a' ) );
+ assert( is_alnum_or_underscore( 'A' ) );
+ assert( is_alnum_or_underscore( '1' ) );
+ assert( is_alnum_or_underscore( '_' ) );
+ assert( !is_alnum_or_underscore( '%' ) );
+}
+
+bool is_alnum_or_underscore( int val )
+{
+ return isalnum( val ) ? true : val == '_';
+}
+
+void print_version( void )
+{
+#ifdef WIN32
+ char szFullPath[MAX_PATH];
+ GetModuleFileName( NULL, szFullPath, sizeof(szFullPath) );
+ DWORD dwZero = 0;
+ DWORD dwVerInfoSize = GetFileVersionInfoSize( szFullPath, &dwZero );
+ if( dwVerInfoSize != 0 ){
+ UCHAR* pVffInfo;
+ pVffInfo = new UCHAR[dwVerInfoSize];
+ if( pVffInfo == NULL )
+ return;
+ GetFileVersionInfo( szFullPath, dwZero, dwVerInfoSize, pVffInfo );
+
+ void* pvVersion;
+ UINT VersionLen;
+ VerQueryValue( pVffInfo, TEXT("\\StringFileInfo\\041104b0\\ProductVersion"), &pvVersion, &VersionLen );
+
+ printf( "Version %s\n", pvVersion );
+ delete [] pVffInfo;
+ }
+#endif
+}
+
113 smartgrep/smartgrep.dsp
@@ -0,0 +1,113 @@
+# Microsoft Developer Studio Project File - Name="smartgrep" - Package Owner=<4>
+# Microsoft Developer Studio Generated Build File, Format Version 6.00
+# ** 編集しないでください **
+
+# TARGTYPE "Win32 (x86) Console Application" 0x0103
+
+CFG=smartgrep - Win32 Release
+!MESSAGE これは有効なメイクファイルではありません。 このプロジェクトをビルドするためには NMAKE を使用してください。
+!MESSAGE [メイクファイルのエクスポート] コマンドを使用して実行してください
+!MESSAGE
+!MESSAGE NMAKE /f "smartgrep.mak".
+!MESSAGE
+!MESSAGE NMAKE の実行時に構成を指定できます
+!MESSAGE コマンド ライン上でマクロの設定を定義します。例:
+!MESSAGE
+!MESSAGE NMAKE /f "smartgrep.mak" CFG="smartgrep - Win32 Release"
+!MESSAGE
+!MESSAGE 選択可能なビルド モード:
+!MESSAGE
+!MESSAGE "smartgrep - Win32 Release" ("Win32 (x86) Console Application" 用)
+!MESSAGE "smartgrep - Win32 Debug" ("Win32 (x86) Console Application" 用)
+!MESSAGE
+
+# Begin Project
+# PROP AllowPerConfigDependencies 0
+# PROP Scc_ProjName ""
+# PROP Scc_LocalPath ""
+CPP=cl.exe
+RSC=rc.exe
+
+!IF "$(CFG)" == "smartgrep - Win32 Release"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "Release"
+# PROP BASE Intermediate_Dir "Release"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir ""
+# PROP Intermediate_Dir "Release"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /Yu"stdafx.h" /FD /c
+# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /FR /FD /c
+# SUBTRACT CPP /YX /Yc /Yu
+# ADD BASE RSC /l 0x411 /d "NDEBUG"
+# ADD RSC /l 0x411 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib version.lib /nologo /subsystem:console /machine:I386
+
+!ELSEIF "$(CFG)" == "smartgrep - Win32 Debug"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "smartgrep___Win32_Debug"
+# PROP BASE Intermediate_Dir "smartgrep___Win32_Debug"
+# PROP BASE Ignore_Export_Lib 0
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "smartgrep___Win32_Debug"
+# PROP Intermediate_Dir "smartgrep___Win32_Debug"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /Yu"stdafx.h" /FD /c
+# ADD CPP /nologo /W3 /GX /Zi /Od /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /FR /FD /c
+# SUBTRACT CPP /YX /Yc /Yu
+# ADD BASE RSC /l 0x411 /d "NDEBUG"
+# ADD RSC /l 0x411 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 /out:"smartgrep.exe"
+# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib version.lib /nologo /subsystem:console /debug /machine:I386 /out:"smartgrep.exe"
+
+!ENDIF
+
+# Begin Target
+
+# Name "smartgrep - Win32 Release"
+# Name "smartgrep - Win32 Debug"
+# Begin Group "Source Files"
+
+# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
+# Begin Source File
+
+SOURCE=.\smartgrep.cpp
+# End Source File
+# End Group
+# Begin Group "Resource Files"
+
+# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
+# Begin Source File
+
+SOURCE=.\resource.h
+# End Source File
+# End Group
+# Begin Group "Header Files"
+
+# PROP Default_Filter "h;hpp;hxx;hm;inl"
+# Begin Source File
+
+SOURCE=.\smartgrep.h
+# End Source File
+# End Group
+# End Target
+# End Project
29 smartgrep/smartgrep.dsw
@@ -0,0 +1,29 @@
+Microsoft Developer Studio Workspace File, Format Version 6.00
+# �x��: ����ܰ���߰� ̧�� ��ҏW�܂��͍폜���Ȃ��ł�������!
+
+###############################################################################
+
+Project: "smartgrep"=.\smartgrep.dsp - Package Owner=<4>
+
+Package=<5>
+{{{
+}}}
+
+Package=<4>
+{{{
+}}}
+
+###############################################################################
+
+Global:
+
+Package=<5>
+{{{
+}}}
+
+Package=<3>
+{{{
+}}}
+
+###############################################################################
+
BIN  smartgrep/smartgrep.exe
Binary file not shown
39 smartgrep/smartgrep.h
@@ -0,0 +1,39 @@
+/**
+ * @file smartgrep.h
+ * @author pebble8888@gmail.com
+ */
+
+#ifndef SMARTGREP_H
+#define SMARTGREP_H
+
+#define SMARTGREP_WORDTYPE_WORD 0x01
+#define SMARTGREP_WORDTYPE_NORMAL 0x02
+#define SMARTGREP_WORDTYPE_INCLUDE_COMMENT 0x04
+#define SMARTGREP_WORDTYPE_EXCLUDE_COMMENT 0x08
+
+#define SMARTGREP_FILETYPE_HEADER 0x01 // .h/.hpp/etc
+#define SMARTGREP_FILETYPE_SOURCE 0x02 // .c/.cpp/.m/.mm/.cs/.js etc
+
+void usage( void );
+void parse_file( char* file_name, int wordtype, char* target_word );
+bool process_line_exclude_comment( bool* c_comment, char* buf, int wordtype, char* target_word );
+bool process_line_include_comment( char* buf, int wordtype, char* target_word );
+#ifdef WIN32
+void parse_directory_win( char* path, int filetype, int wordtype, char* target_word );
+#else
+void parse_directory_mac( char* path, int filetype, int wordtype, char* target_word );
+#endif
+bool is_header_file( char* file_name );
+bool is_source_file( char* file_name );
+bool is_ext( char* file_name, const char* ext_name );
+bool findword_in_line( char* valid_str, int wordtype, char* target_word );
+bool is_alnum_or_underscore( int val );
+void print_version( void );
+
+void smartgrep_getcwd( char* buf, size_t size );
+
+void test_is_alnum_or_underscore( void );
+void test( void );
+
+#endif /* SMARTGREP_H */
+
Please sign in to comment.
Something went wrong with that request. Please try again.