Exemplo n.º 1
0
void CompilationCppParser::parse_src_file_rec( CompilationEnvironment &ce, const String &filename ) {
    if ( is_a_directory( filename ) )
        return;
    String current_dir = directory_of( filename ) + "/";
    File file( filename, "r" );
    for ( const char *c = file.c_str(); *c; ) {
        // string ?
        if ( c[ 0 ] == '"' ) {
            go_after_next_double_quote( ++c );
            continue;
        }
        // comment ?
        if ( c[ 0 ] == '/' ) {
            if ( c[ 1 ] == '/' ) {
                go_to_next_line( c += 2 );
                continue;
            }
            if ( c[ 1 ] == '*' ) {
                go_to_next_mul_slash( c += 2 );
                continue;
            }
        }
        // QOBJECT ?
        if ( c[ 0 ] == 'Q' ) {
            if ( strncmp( c + 1, "_OBJECT", 7 ) == 0 ) {
                c += 8;
                if ( not filename.begins_by( "/usr/" ) ) // hum !!
                    moc_files.push_back_unique( filename );
                continue;
            }
        }
        //
        if ( c[ 0 ] == '#' ) {
            if ( c[ 1 ] == 'd' and c[ 2 ] == 'e' and c[ 3 ] == 'f' and c[ 4 ] == 'i' and c[ 5 ] == 'n' and c[ 6 ] == 'e' and c[ 7 ] == ' ' ) {
                defines[ get_next_word( c += 8 ) ];
                continue;
            }
            if ( c[ 1 ] == 'i' ) {
                if ( c[ 2 ] == 'f' ) {
                    if ( c[ 3 ] == 'd' and c[ 4 ] == 'e' and c[ 5 ] == 'f' and c[ 6 ] == ' ' ) {
                        if ( not defines.count( get_next_word( c += 7 ) ) )
                            skip_lines_until_endif_or_else( c );
                        continue;
                    }
                    if ( c[ 3 ] == 'n' and c[ 4 ] == 'd' and c[ 5 ] == 'e' and c[ 6 ] == 'f' and c[ 7 ] == ' ' ) {
                        if ( defines.count( get_next_word( c += 8 ) ) )
                            skip_lines_until_endif_or_else( c );
                        continue;
                    }
                }
                if ( c[ 2 ] == 'n' and c[ 3 ] == 'c' and c[ 4 ] == 'l' and c[ 5 ] == 'u' and c[ 6 ] == 'd' and c[ 7 ] == 'e' and c[ 8 ] == ' ' ) {
                    bool syst;
                    String bas_name = get_include_filename( c += 9, syst );
                    String inc_file =  ce.find_src( bas_name, current_dir, inc_paths, not syst );
                    if ( not inc_file )
                        inc_file = ce.find_src( bas_name, current_dir, ce_inc_paths, not syst );

                    // formulation.
                    if ( not inc_file ) {
                        int form_ind = bas_name.find( "formulation." );
                        if ( form_ind >= 0 and bas_name != "formulation.h" ) {
                            String h_py = bas_name + ".py";
                            if ( not file_exists( h_py ) ) {
                                int beg_form = form_ind + 12;
                                int end_form = bas_name.find( ".", beg_form );
                                String form = bas_name.beg_upto( end_form ).end_from( beg_form );

                                int beg_elem = end_form + 1;
                                int end_elem = bas_name.find( ".", beg_elem );
                                String elem = bas_name.beg_upto( end_elem ).end_from( beg_elem );
                                BasicVec<String> elem_list = tokenize( elem, ',' );

                                int beg_parm = end_elem + 1;
                                int end_parm = bas_name.find( ".", beg_parm );
                                String parm = bas_name.beg_upto( end_parm ).end_from( beg_parm );
                                BasicVec<String> parm_list = tokenize( parm, ',' );
                                BasicVec<String> der_vars;
                                BasicVec<String> dim;
                                for( int np = 0; np < parm_list.size(); ++np ) {
                                    if ( parm_list[ np ].begins_by( "name_der_vars=" ) )
                                        der_vars << parm_list[ np ].end_from( 14 );
                                    else if ( parm_list[ np ].begins_by( "dim=" ) )
                                        dim << parm_list[ np ].end_from( 4 );
                                }

                                File f( h_py, "w" );
                                // f << "import sys, os\n";
                                // f << "sys.path.append( os.getcwd() + '/LMT' )\n";
                                // f << "sys.path.append( os.getcwd() + '/LMTpp' )\n";
                                f << "import formal_lf\n";
                                f << "formal_lf.write_pb(\n";
                                f << "    name = '" << form << "',\n";
                                f << "    formulations = ['" << form << "'],\n";
                                f << "    elements = [";
                                for( int i = 0; i < elem_list.size(); ++i )
                                    f << ( i ? ",'" : "'" ) << elem_list[ i ] << "'";
                                f << "],\n";
                                f << "    incpaths = ['.'";
                                for( int i = 0; i < ce_inc_paths.size(); ++i )
                                    if ( ce_inc_paths[ i ].find( "LMT" ) >= 0 )
                                        f << ",'" << ce_inc_paths[ i ].beg_upto( ce_inc_paths[ i ].find( "/include" ) ) << "/formulations'";
                                f << "],\n";
                                f << "    name_der_vars=[";
                                for( int nd = 0; nd < der_vars.size(); ++nd )
                                    f << ( nd ? "," : "" ) << '"' << der_vars[ nd ] << '"';
                                f << "],\n";
                                if ( dim.size() )
                                    f << "    dim='" << dim[ 0 ] << "',\n";
                                f << ")\n";
                                // f << "    options = { 'behavior_simplification' : 'plane stress', 'behavior_law' : s },
                                // f << "    # name_der_vars = [ "frac_E2", "frac_G12", "nu12" ] #
                            }
                        }
                    }

                    // .h.py ?
                    if ( bas_name.ends_with( ".h" ) ) {
                        if ( String h_py = ce.find_src( bas_name + ".py", current_dir ) ) {
                            inc_file = h_py.beg_upto( h_py.size() - 3 );
                            if ( last_modification_time_or_zero_of_file_named( h_py ) >
                                 last_modification_time_or_zero_of_file_named( inc_file ) )
                                make_h_py( h_py, inc_file );
                        }
                    }

                    // parse rec
                    if ( inc_file ) {
                        inc_file = canonicalize_filename( inc_file );
                        inc_files << inc_file;

                        //
                        if ( not already_parsed.contains( inc_file ) ) {
                            already_parsed << inc_file;
                            parse_src_file_rec( ce, inc_file );
                        }
                    }
                    continue;
                }
            }
            if ( c[ 1 ] == 'p' and c[ 2 ] == 'r' and c[ 3 ] == 'a' and c[ 4 ] == 'g' and c[ 5 ] == 'm' and c[ 6 ] == 'a' and c[ 7 ] == ' ' ) {
                c += 8;
                if ( strncmp( c, "src_file ", 9 ) == 0 ) {
                    String o = get_pragma_arg( c += 9 );
                    String s = ce.find_src( o, current_dir );
                    if ( s.size() )
                        src_files << s;
                    else
                        cerrn << "Impossible to find src '" << o << "'";
                    continue;
                }
                if ( strncmp( c, "lib_path ", 9 ) == 0 ) {
                    lib_paths << ce.find_src( get_pragma_arg( c += 9 ), current_dir );
                    continue;
                }
                if ( strncmp( c, "lib_name ", 9 ) == 0 ) {
                    lib_names << get_pragma_arg( c += 9 );
                    continue;
                }
                if ( strncmp( c, "fra_name ", 9 ) == 0 ) {
                    fra_names << get_pragma_arg( c += 9 );
                    continue;
                }
                if ( strncmp( c, "inc_path ", 9 ) == 0 ) {
                    String path = ce.find_src( get_pragma_arg( c += 9 ), current_dir );
                    if ( not ( path.ends_with( '/' ) or path.ends_with( '\\' ) ) )
                        path += '/';
                    inc_paths << path;
                    continue;
                }
                if ( strncmp( c, "cpp_flag ", 9 ) == 0 ) {
                    cpp_flags << get_pragma_arg( c += 9 );
                    continue;
                }
                if ( strncmp( c, "lnk_flag ", 9 ) == 0 ) {
                    lnk_flags << get_pragma_arg( c += 9 );
                    continue;
                }
                if ( strncmp( c, "gpu_flag ", 9 ) == 0 ) {
                    gpu_flags << get_pragma_arg( c += 9 );
                    continue;
                }
                if ( strncmp( c, "template ", 9 ) == 0 ) {
                    var_templ << get_pragma_arg( c += 9 );
                    continue;
                }
                if ( strncmp( c, "cxx_name ", 9 ) == 0 ) {
                    cxx_name << get_pragma_arg( c += 9 );
                    continue;
                }
            }
        }

        //
        ++c;
    }
}
Exemplo n.º 2
0
char *get_random_theme()
{
  DIR *dir;
  char *my_themes_dir = StrApp((char**)NULL, themes_dir, "/", (char*)NULL);
  char *result;
  struct dirent *entry;
  int n_themes = 0;
  char *themes[128];
  int i;

  dir= opendir(my_themes_dir);
  if (!dir)
	{		
    /* perror("Qingy error"); */
		/* This is not a qingy error ;-P */
		WRITELOG(ERROR, "Cannot open themes directory (%s)!\n", my_themes_dir);
		free(my_themes_dir);
    return strdup("default");
  }

  while ((entry= readdir(dir)))
  {
		char *temp;
		/*
			To the genius who wrote this snippet:
			if(!entry){
			  perror("Qingy error");
			  break;
			}
			It will never be executed!
			(See above while() condition ;-P)
		*/
    if (!strcmp(entry->d_name, "." )) continue;
    if (!strcmp(entry->d_name, "..")) continue;    
    
    temp = StrApp((char**)NULL, my_themes_dir, entry->d_name, (char*)NULL);
    if (is_a_directory(temp))
		{
			themes[n_themes] = strdup(entry->d_name);
			n_themes++;
		}
    free(temp);
  }
	closedir(dir);
	/* If the opendir() some lines above didn't fail,
	 * there is no reason to think closedir() will:
	 * if(closedir(dir)== -1)
	 *   perror("Qingy error");
	 */
  free(my_themes_dir);
  
  if (!n_themes) return strdup("default");
  
  /* let's create a random number between 0 and n_themes-1 */
  srand((unsigned int)time(NULL));
  i = rand() % n_themes;
  
  result = strdup(themes[i]);
  for (i=0; i<n_themes; i++) free(themes[i]);

  return result;
}