Projet.h

Aller à la documentation de ce fichier.
00001 /* =============================================================================
00002  * Nouveau projet **************************************************************
00003 ==============================================================================*/
00004 void Projet_Nouveau()
00005 {
00006   Projet_Clean();
00007 
00008   ProjetTYPE = 0;
00009   ProjetNOMssExt = g_strdup("Projet");
00010   ProjetARGS     = g_strdup("-Wall -s");
00011   iterProj = Ajouter_TreeviewItem(NULL,ProjetNOMssExt,NULL,AppICONs[1]);
00012 }
00013 /* =============================================================================
00014  * Choisir un projet pour l' ouvrir ********************************************
00015 ==============================================================================*/
00016 void Projet_Choisir()
00017 {
00018   gchar *chem = DLG_Open(TRUE);
00019   if (strlen(chem)) Projet_Ouvrir(chem);
00020   g_free(chem);
00021 }
00022 /* =============================================================================
00023  * Ouvre un projet *************************************************************
00024 ==============================================================================*/
00025 void Projet_Ouvrir(gchar* chem)
00026 {
00027   FILE          *pF=NULL;
00028   gchar         c, mot[200], *tmp=NULL;
00029   guchar        flag=0, b=0;
00030   GtkTreeIter   pere;
00031 
00032   g_print("Ouverture projet : %s\n", chem);
00033   if ( !(pF = fopen (chem,"r")) )
00034   {
00035      DLG_error (NULL, "Impossible d'ouvrir %s", chem);
00036      return;
00037   }
00038 
00039   // cleanup de tous les onglets
00040   Projet_Clean();
00041   memset(mot,0,200);
00042 
00043   // récupère le nom du projet ouvert
00044   ProjetNOM = g_locale_to_utf8 (g_path_get_basename(chem), -1, NULL, NULL, NULL);
00045   // en déduit nom sans extension
00046   ProjetNOMssExt = getNomSansExtension(ProjetNOM);
00047   // récupère le chemin complet du projet
00048   ProjetCHEMIN = g_locale_to_utf8 (chem, -1, NULL, NULL, NULL);
00049   if (chem) g_free(chem);
00050 
00051   //(1) Insérer nouvelle entrée dans le tableau
00052   Ajouter_ELEMENT(ProjetNOM, ProjetCHEMIN, -1);
00053   //(2) Insérer nouvelle entrée dans le treeview
00054   iterProj = Ajouter_TreeviewItem(NULL, ProjetNOMssExt, ProjetCHEMIN, AppICONs[1]);
00055   pere = iterProj; // Pour projet sans sous-dossier :)
00056 
00057   // parcourt le fichier pour insérer les autres entrées au TreeView
00058   while ((c = fgetc (pF)) != EOF)
00059   {
00060    if (c == ':')
00061    {
00062      if      (g_strcasecmp(mot, "TYPE"      )==0) flag=1;
00063      else if (g_strcasecmp(mot, "OPT"       )==0) flag=2;
00064      else if (g_strcasecmp(mot, "DOSSIER"   )==0) flag=3;
00065      else if (g_strcasecmp(mot, "FILE"      )==0) flag=4;
00066      else if (g_strcasecmp(mot, "EXE"       )==0) flag=5;
00067      else if (g_strcasecmp(mot, "ARGS"      )==0) flag=6;
00068      else if (g_strcasecmp(mot, "INCS"      )==0) flag=7;
00069      else if (g_strcasecmp(mot, "LIBS"      )==0) flag=8;
00070      else if (g_strcasecmp(mot, "MAKEFILE"  )==0) flag=9;
00071      else if (g_strcasecmp(mot, "UNDOLEVELS")==0) flag=10;
00072      else if (g_strcasecmp(mot, "ICON"      )==0) flag=11;
00073      else flag=0;
00074      memset(mot,0,b);  b=0;
00075    }
00076    else if (c ==0XA)
00077    {
00078      if      (flag==1) // TYPE PROJET ////////////////////////////////////////
00079      {
00080        tmp = g_locale_to_utf8 (mot, -1, NULL, NULL, NULL);
00081        ProjetTYPE = g_ascii_strtod(tmp,NULL);
00082      }
00083      else if (flag==2) // OPTIONS ////////////////////////////////////////////
00084        ProjetOPT = g_locale_to_utf8 (mot, -1, NULL, NULL, NULL);
00085      else if (flag==3) // DOSSIER ////////////////////////////////////////////
00086      {
00087        tmp = g_locale_to_utf8 (mot, -1, NULL, NULL, NULL);
00088        //(1) Insérer nouvelle entrée dans le tableau
00089        Ajouter_ELEMENT(tmp, tmp, -1);
00090        //(2) Insérer nouvelle entrée dans le treeview
00091        pere = Ajouter_TreeviewItem(&iterProj, tmp, tmp, AppICONs[1]);
00092      }
00093      else if (flag==4) // FICHIER ////////////////////////////////////////////
00094      {
00095        tmp = g_locale_to_utf8 (mot, -1, NULL, NULL, NULL);
00096        //(1) Insérer nouvelle entrée dans le tableau
00097        Ajouter_ELEMENT(g_path_get_basename(tmp), tmp, -1);
00098        //(2) Insérer nouvelle entrée dans le treeview
00099        Ajouter_TreeviewItem(&pere, nom[NB], tmp, NULL);
00100        //Ouvrir_Fichier_Ou_Le_Passer_Au_Premier_Plan(NB-1);
00101      }
00102      else if (flag==5) // NOM EXE ////////////////////////////////////////////
00103        ProjetEXEname = g_locale_to_utf8 (mot, -1, NULL, NULL, NULL);
00104      else if (flag==6) // EXEC ARGS //////////////////////////////////////////
00105        ProjetARGS = g_locale_to_utf8 (mot, -1, NULL, NULL, NULL);
00106      else if (flag==7) // INCLUDES ///////////////////////////////////////////
00107        ProjetINCS = g_locale_to_utf8 (mot, -1, NULL, NULL, NULL);
00108      else if (flag==8) // LIBS ///////////////////////////////////////////////
00109        ProjetLIBS = g_locale_to_utf8 (mot, -1, NULL, NULL, NULL);
00110      else if (flag==9) // USE MAKEFILE ///////////////////////////////////////
00111      {
00112        tmp = g_locale_to_utf8 (mot, -1, NULL, NULL, NULL);
00113        ProjetUseMAKEFILE = (g_ascii_strtod(tmp,NULL))?TRUE:FALSE;
00114      }
00115      else if (flag==10) // UNDO LEVELS ///////////////////////////////////////
00116      {
00117        tmp = g_locale_to_utf8 (mot, -1, NULL, NULL, NULL);
00118        AppUNDOLEVELS = (g_ascii_strtod(tmp,NULL))?TRUE:FALSE;
00119      }
00120      else if (flag==11) // ICON //////////////////////////////////////////////
00121        ProjetICON = g_locale_to_utf8 (mot, -1, NULL, NULL, NULL);
00122 
00123      memset(mot,0,b);  b=flag=0;
00124    }
00125    else if (c !=0XD) mot[b++] = c;
00126   } //while (c = fgetc)
00127 
00128   fclose (pF);
00129   if (tmp) g_free(tmp);
00130 App_Lister_Tableau();
00131 }
00132 /* =============================================================================
00133  * Projet: Enregistrer *********************************************************
00134 ==============================================================================*/
00135 void Projet_Enregistrer()
00136 {
00137   Fichier_Enregistrer_Tous_Les_Fichier();
00138 
00139   //Ecraser fichier projet
00140   FILE *pF;
00141   pF = fopen(ProjetNOM,"w");
00142 
00143   fprintf(pF, "TYPE:%d\n", ProjetTYPE);
00144   fprintf(pF, "EXE:%s\n", ProjetEXEname);
00145   if (ProjetICON) fprintf(pF, "ICON:%s\n", ProjetICON);
00146   if (ProjetOPT)  fprintf(pF, "OPT:%s\n" , ProjetOPT);
00147   if (ProjetARGS) fprintf(pF, "ARGS:%s\n", ProjetARGS);
00148   if (ProjetINCS) fprintf(pF, "INCS:%s\n", ProjetINCS);
00149   if (ProjetLIBS) fprintf(pF, "LIBS:%s\n", ProjetLIBS);
00150   fprintf(pF, "\n");
00151 
00152   gshort n;
00153   for (n=1; n<NB; n++)
00154    if (ext[n]==-1) fprintf(pF, "\nDOSSIER:%s\n", chemin[n]);
00155    else            fprintf(pF, "FILE:%s\n"   , chemin[n]);
00156 
00157 /*
00158 FILE:src/EdBx.c
00159 
00160 DOSSIER:Editor
00161 FILE:src/Globals.h
00162 */
00163  fclose(pF);
00164   Liste_Add_Msg ("Projet enregistré.");
00165 }
00166 /* =============================================================================
00167  * Sort les includes d'un fichier source ***************************************
00168 ==============================================================================*/
00169 /*
00170 void Dependances(gchar *fich)
00171 {
00172  gchar *c = NULL;
00173  gsize len,n;
00174  gchar *mot;//[200];
00175  gchar flag = 0, b=0;
00176 
00177  mot = g_malloc(200);
00178  //memset(mot,0,200);
00179 
00180  if (g_file_get_contents (fich, &c, &len, NULL))
00181  {
00182   g_print("%s fait %d octets (%ld)\n", fich, len, g_utf8_strlen (tmp, 0));
00183  // glong g_utf8_strlen (tmp, 0);
00184 
00185   for (n=0; n<len; n++)
00186   {
00187     if (c[n]==0X22)
00188     {
00189       if (flag)
00190       {
00191        mot[b]=0;//'\0';
00192        g_print("b = %d INC:[%s]\n", b, mot);
00193        //memset(mot,0,b);
00194        b = flag = 0;
00195       }
00196       else if (c[n-1]==' ' && c[n-2]=='e' && c[n-3]=='d' && c[n-4]=='u' &&
00197                c[n-5]=='l' && c[n-6]=='c' && c[n-7]=='n' && c[n-8]=='i' &&
00198                c[n-9]=='#') flag=1;
00199     }
00200     else if (flag) mot[b++] = c[n];
00201   }
00202   g_free(c);
00203  }
00204 }
00205 */
00206 
00207 
00208 /* =============================================================================
00209  * Projet: Generate Makefile **************************************************
00210 ==============================================================================*/
00211 void Projet_Generate_Makefile()
00212 {
00213  gshort n,b=0;
00214  gchar  *noms[100];
00215  //gchar *chems[100];
00216  FILE *pF;
00217  gint   exit_status;
00218  gchar  *serr = NULL;
00219  GError *err  = NULL;
00220  gchar *INCS = NULL;
00221  gchar *LIBS = NULL;
00222  gchar *EXEname = g_path_get_basename(ProjetEXEname);
00223 
00224  //Identify source files list
00225  for (n=0; n<NB; n++)
00226  {
00227    if (ext[n]==1) noms[b++] = g_strdup(getNomSansExtension(nom[n]));
00228  }
00229 
00230  INCS = g_path_get_dirname (ProjetEXEname);
00231  if (INCS) INCS = g_strdup_printf("%s/makefile", INCS);
00232 
00233  pF = fopen(INCS, "w");
00234  if (!pF) return;
00235 
00236  fprintf(pF, "# Generated by EdBx\n\n");
00237  fprintf(pF, "CC=%s\n", AppCOMPILER);
00238  fprintf(pF, "OPT="); if (ProjetOPT) fprintf(pF, "%s", ProjetOPT);
00239  fprintf(pF, "\nLINKER=%s\n\n",  AppLINKER);
00240 
00241  // INCS ---------------------
00242  fprintf(pF, "INCS=");
00243  if (ProjetINCS) fprintf(pF, "%s", ProjetINCS);
00244 
00245  if (ProjetTYPE==1) // EXE GTK
00246  {
00247 
00248    if (g_spawn_command_line_sync ("pkg-config --cflags gtk+-2.0",
00249                                         &INCS, &serr, &exit_status, &err))
00250    {
00251      if (exit_status!=0)  Liste_Add_Msg(
00252                 "Generate_Makefile: pkg-config Exit code: %d.\n", exit_status);
00253      else if (INCS) fprintf(pF, "%s", INCS);
00254    }
00255    else Liste_Add_Msg("Generate_Makefile: g_spawn_command_line_sync()"
00256                         " retourne FALSE!\n");
00257  }
00258 
00259 
00260  // LIBS ----------------------
00261  fprintf(pF, "\n\nLIBS=");
00262  if (ProjetLIBS) fprintf(pF, "%s", ProjetLIBS);
00263  if (ProjetTYPE==1) // EXE GTK
00264  {
00265    if (g_spawn_command_line_sync ("pkg-config --libs gtk+-2.0",
00266                                         &LIBS, &serr, &exit_status, &err))
00267    {
00268      if (exit_status!=0)  Liste_Add_Msg(
00269                 "Generate_Makefile: pkg-config Exit code: %d.\n", exit_status);
00270      else if (LIBS) fprintf(pF, "%s", LIBS);
00271    }
00272    else Liste_Add_Msg("Generate_Makefile: g_spawn_command_line_sync()"
00273                         " retourne FALSE!\n");
00274  }
00275 
00276 
00277 
00278  fprintf(pF, "\n\n%s:", EXEname);
00279  for (n=0; n<b; n++) fprintf(pF, " %s.o", noms[n]);
00280 
00281  fprintf(pF, "\n\t${LINKER} ${OPT} ${LIBS}");
00282  for (n=0; n<b; n++) fprintf(pF, " %s.o", noms[n]);
00283  fprintf(pF, " -o %s\n\n", EXEname);
00284 
00285  for (n=0; n<b; n++)
00286  {
00287    fprintf(pF, "%s.o: %s.%s\n", noms[n], noms[n], EXTs[1]);
00288 
00289    if (g_strcasecmp(AppCOMPILER,"gcc")==0)
00290      fprintf(pF, "\t${CC} ${OPT} ${INCS} -c %s.c -o %s.o\n\n", noms[n], noms[n]);
00291    else if (g_strcasecmp(AppCOMPILER,"fasm")==0)
00292      fprintf(pF, "\t${CC} ${OPT} ${INCS} %s.%s %s.o\n\n", noms[n], EXTs[1], noms[n]);
00293 
00294    g_free(noms[n]);
00295  }
00296 
00297  fclose(pF);
00298  Liste_Add_Msg("Makefile créé.");
00299 }
00300 /* =============================================================================
00301  * Lance un pipe (pour compiler ou exécuter) ***********************************
00302 ==============================================================================*/
00303 void Creer_Pipe(gchar* str)
00304 {
00305 /*
00306   gint   exit_status;
00307   gchar  *sout = NULL;
00308   gchar  *serr = NULL;
00309   GError *err  = NULL;
00310   gchar **tab;
00311   gint n=1;
00312 //g_print("str: %s", str);
00313   if (g_spawn_command_line_sync ("ls", &sout, &serr, &exit_status, &err))
00314   {
00315    if (exit_status==0)
00316    {
00317 g_print("%s", sout);
00318      tab = g_strsplit (sout, "\n", 0);
00319      while (tab[n-1])
00320      {
00321       Liste_Add_Msg("%s", tab[n-1]);
00322       n++;
00323      }
00324    }
00325    else Liste_Add_Msg("Exit code: %d", exit_status);
00326   }
00327 */
00328 
00329   #define BUFSIZE 4096
00330   FILE * pip;
00331   gchar ligne[BUFSIZE];
00332   gchar   buf[BUFSIZE];
00333   gint n=1, x=0, i;
00334 
00335   //gtk_list_store_clear(GTK_LIST_STORE(model));
00336   pip = popen(str, "r");
00337   if (!pip)
00338      Liste_Add_Msg ("Probleme de pipe");
00339   else
00340   {
00341    while(n)
00342    {
00343      n = fread (buf, 1, BUFSIZE, pip);
00344      for (i=0; i<=n; i++) // Splitte le retour: 1 ligne = 1 entrée.
00345      {
00346       if (buf[i]==0XA || i==n)
00347       {
00348         if (x)
00349         {
00350           Liste_Add_Msg (g_locale_to_utf8 (ligne, x, NULL, NULL, NULL));
00351           x=0;
00352         }
00353       }
00354       else ligne[x++]=buf[i];
00355      }
00356    }
00357 
00358    // ferme le pipe et récupère son code de sortie
00359    i = pclose(pip);
00360    if (i==0)
00361      str = g_strdup("OK");
00362    else
00363      str = g_strdup_printf("Exit code: %d", i);
00364    Liste_Add_Msg(str);
00365    g_free(str);
00366   }
00367 }
00368 /* =============================================================================
00369  * Projet: Exécuter ************************************************************
00370 ==============================================================================*/
00371 void Projet_Executer()
00372 {
00373   gchar *S=NULL;
00374   if (g_path_is_absolute(ProjetEXEname)) S = g_strdup(ProjetEXEname);
00375   else               S = g_strdup_printf("./%s 2>&1", ProjetEXEname);
00376   Liste_Add_Msg ("Exécution de %s", S);
00377   Creer_Pipe(S);
00378 }
00379 /* =============================================================================
00380  * Projet: Compiler et exécuter ************************************************
00381 ==============================================================================*/
00382 void Projet_CompilExec()
00383 {
00384   Projet_Compiler();
00385   Projet_Executer();
00386 }
00387 /* =============================================================================
00388  * Projet: Stopper Exécution ***************************************************
00389 ==============================================================================*/
00390 void Projet_StopExec()
00391 {
00392 
00393 }
00394 /* =============================================================================
00395  * Vide le projet **************************************************************
00396 ==============================================================================*/
00397 void Projet_Clean()
00398 {
00399   guint i;
00400 
00401   for (i=0; i<NB; i++)
00402   {
00403     g_free(nom[i]);
00404     g_free(chemin[i]);
00405     ext[i]=0;
00406     estModifie[i]=0;
00407     if (IDOnglet[i]>-1)
00408       gtk_notebook_remove_page (GTK_NOTEBOOK(Onglets), 0);
00409     IDOnglet[i]=-1;
00410   }
00411 
00412   if (ProjetNOM)      g_free(ProjetNOM);        ProjetNOM=NULL;
00413   if (ProjetNOMssExt) g_free(ProjetNOMssExt);   ProjetNOMssExt=NULL;
00414   if (ProjetCHEMIN)   g_free(ProjetCHEMIN);     ProjetCHEMIN=NULL;
00415   if (ProjetOPT)      g_free(ProjetOPT);        ProjetOPT=NULL;
00416   if (ProjetARGS)     g_free(ProjetARGS);       ProjetARGS=NULL;
00417   if (ProjetINCS)     g_free(ProjetINCS);       ProjetINCS=NULL;
00418   if (ProjetLIBS)     g_free(ProjetLIBS);       ProjetLIBS=NULL;
00419   if (ProjetEXEname)  g_free(ProjetEXEname);    ProjetEXEname=NULL;
00420 
00421   NB = NBfichiersOuverts = 0;
00422   idACTIF = idOngletACTIF = ProjetTYPE = -1;
00423 
00424   gtk_tree_store_clear(pTreeStore);
00425   gtk_list_store_clear(GTK_LIST_STORE(model));
00426 
00427 //Lister_Tableau();
00428 }

Généré le Sat Jan 13 02:54:13 2007 pour EdBX:IDELangageC,Python,FASMécritavecGTK+ par  doxygen 1.4.2