| 1 | /* |
| 2 | Copyright (C) 1999-2006 Id Software, Inc. and contributors. |
| 3 | For a list of contributors, see the accompanying CONTRIBUTORS file. |
| 4 | |
| 5 | This file is part of GtkRadiant. |
| 6 | |
| 7 | GtkRadiant is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | GtkRadiant is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with GtkRadiant; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include "shaders.h" |
| 23 | |
| 24 | #include "ifilesystem.h" |
| 25 | |
| 26 | #include "stream/stringstream.h" |
| 27 | |
| 28 | #include "gtkdlgs.h" |
| 29 | |
| 30 | void ViewShader( const char *pFile, const char *pName, bool external_editor ){ |
| 31 | char* pBuff = 0; |
| 32 | //int nSize = |
| 33 | vfsLoadFile( pFile, reinterpret_cast<void**>( &pBuff ) ); |
| 34 | if ( pBuff == 0 ) { |
| 35 | globalErrorStream() << "Failed to load shader file " << pFile << "\n"; |
| 36 | return; |
| 37 | } |
| 38 | // look for the shader declaration |
| 39 | StringOutputStream strFind( string_length( pName ) ); |
| 40 | strFind << LowerCase( pName ); |
| 41 | StringOutputStream strLook( string_length( pBuff ) ); |
| 42 | strLook << LowerCase( pBuff ); |
| 43 | // offset used when jumping over commented out definitions |
| 44 | int length = string_length( pBuff ); |
| 45 | std::size_t nOffset = 0; |
| 46 | bool startOK = false; |
| 47 | bool endOK = false; |
| 48 | while ( !startOK || !endOK ){ |
| 49 | const char* substr = strstr( strLook.c_str() + nOffset, strFind.c_str() ); |
| 50 | if ( substr == 0 ) { |
| 51 | break; |
| 52 | } |
| 53 | std::size_t nStart = substr - strLook.c_str(); |
| 54 | startOK = endOK = false; |
| 55 | if ( nStart == 0 ){ |
| 56 | startOK = true; |
| 57 | } |
| 58 | //validate found one... |
| 59 | for ( const char* i = substr - 1; i > strLook.c_str(); i-- ){ |
| 60 | if( (strncmp( i, "\t", 1 ) == 0) || (strncmp( i, " ", 1 ) == 0) ){ |
| 61 | startOK = true; |
| 62 | continue; |
| 63 | } |
| 64 | else if ( (strncmp( i, "\n", 1 ) == 0) || (strncmp( i, "\r", 1 ) == 0) ){ |
| 65 | startOK = true; |
| 66 | break; |
| 67 | } |
| 68 | else{ |
| 69 | startOK = false; |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | const char* b = strLook.c_str() + strlen( strLook.c_str() ); |
| 74 | for ( const char* i = substr + strlen( strFind.c_str() ); i < b; i++ ){ |
| 75 | if( (strncmp( i, "\t", 1 ) == 0) || (strncmp( i, " ", 1 ) == 0) ){ |
| 76 | endOK = true; |
| 77 | continue; |
| 78 | } |
| 79 | else if ( (strncmp( i, "\n", 1 ) == 0) || (strncmp( i, "\r", 1 ) == 0) || (strncmp( i, "{", 1 ) == 0) || (strncmp( i, ":q3map", 6 ) == 0) ){ |
| 80 | endOK = true; |
| 81 | break; |
| 82 | } |
| 83 | else{ |
| 84 | endOK = false; |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | if( !startOK || !endOK ){ |
| 89 | nOffset = nStart + 1; |
| 90 | } |
| 91 | else{ |
| 92 | //globalErrorStream() << "Validated successfully" << "\n"; |
| 93 | nOffset = nStart; |
| 94 | //fix cr+lf |
| 95 | for ( const char* i = strLook.c_str(); i < substr ; i++ ){ |
| 96 | if ( (strncmp( i, "\r\n", 2 ) == 0) ){ |
| 97 | nOffset--; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | /*// we have found something, maybe it's a commented out shader name? |
| 102 | char *strCheck = new char[string_length( strLook.c_str() ) + 1]; |
| 103 | strcpy( strCheck, strLook.c_str() ); |
| 104 | strCheck[nStart] = 0; |
| 105 | char *pCheck = strrchr( strCheck, '\n' ); |
| 106 | // if there's a commentary sign in-between we'll continue |
| 107 | if ( pCheck && strstr( pCheck, "//" ) ) { |
| 108 | delete[] strCheck; |
| 109 | nOffset = nStart + 1; |
| 110 | continue; |
| 111 | } |
| 112 | delete[] strCheck; |
| 113 | nOffset = nStart; |
| 114 | break;*/ |
| 115 | } |
| 116 | //fix up length |
| 117 | const char* b = strLook.c_str() + strlen( strLook.c_str() ) - 1; |
| 118 | for ( const char* i = strLook.c_str(); i < b; i++ ){ |
| 119 | if ( (strncmp( i, "\r\n", 2 ) == 0) ){ |
| 120 | length--; |
| 121 | } |
| 122 | } |
| 123 | // now close the file |
| 124 | vfsFreeFile( pBuff ); |
| 125 | |
| 126 | DoTextEditor( pFile, static_cast<int>( nOffset ), length, external_editor ); |
| 127 | } |