1 /* Utils.c */
2 /**********************************************************************************************************
3 Copyright (c) 2002-2013 Abdul-Rahman Allouche. All rights reserved
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6 documentation files (the Gabedit), to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
8 and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9 
10   The above copyright notice and this permission notice shall be included in all copies or substantial portions
11   of the Software.
12 
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
14 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
16 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17 DEALINGS IN THE SOFTWARE.
18 ************************************************************************************************************/
19 
20 
21 #include "../../Config.h"
22 #include <stdlib.h>
23 #include <ctype.h>
24 #include <math.h>
25 #include <time.h>
26 #include <GL/gl.h>
27 #include <GL/glu.h>
28 
29 #include "../Common/Global.h"
30 #include "../Utils/Constants.h"
31 #include "../Utils/Vector3d.h"
32 #include "../Geometry/GeomGlobal.h"
33 #include "../Utils/AtomsProp.h"
34 #include "../Utils/UtilsInterface.h"
35 #include "../Utils/Utils.h"
36 #include "../Utils/GabeditTextEdit.h"
37 #include "../Geometry/ResultsAnalise.h"
38 #include "../Geometry/EnergiesCurves.h"
39 #include "../Common/TextEdit.h"
40 #include "../Common/Preferences.h"
41 #include "../Common/Run.h"
42 #include "../Display/GLArea.h"
43 #include "../Geometry/Fragments.h"
44 #include "../Geometry/DrawGeom.h"
45 #include "../Geometry/AxesGeomGL.h"
46 #include "../Utils/HydrogenBond.h"
47 #ifdef G_OS_WIN32
48 #include <windows.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <winsock.h>
53 #include <fcntl.h>
54 #include <io.h>
55 #else /* G_OS_WIN32 */
56 #include <stdarg.h>
57 #include <pwd.h>
58 #include <unistd.h>
59 #include <sys/times.h>
60 #endif /* G_OS_WIN32 */
61 
62 #define DebugFlag 0
63 #define Debug1Flag 0
64 
65 void create_color_surfaces_file();
66 void read_color_surfaces_file();
67 void initAxis();
68 void save_axis_properties();
69 void read_axis_properties();
70 void save_principal_axis_properties();
71 void read_principal_axis_properties();
72 void initPrincipalAxisGL();
73 
74 
75 #define BBSIZE 10240
76 /********************************************************************************/
77 #ifndef G_OS_WIN32
78 #define TIMER_TICK      60
79 static clock_t it;
80 static struct tms itt;
timing(double * cpu,double * sys)81 void timing(double* cpu,double *sys)
82 {
83 	it=times(&itt);
84 	*cpu=(double) itt.tms_utime / (double) TIMER_TICK;
85 	*sys=(double) itt.tms_stime / (double) TIMER_TICK;
86 }
87 #endif
88 #ifdef G_OS_WIN32
addUnitDisk(FILE * file,G_CONST_RETURN gchar * name)89 void addUnitDisk(FILE* file, G_CONST_RETURN gchar* name)
90 {
91 	if(name && strlen(name)>1 && name[1]==':')
92 		fprintf(file,"%c%c\n", name[0],name[1]);
93 }
94 #endif
95 #ifdef G_OS_WIN32
96 /************************************************************************
97 *  error : display an error message and possibly the last Winsock error *
98 *************************************************************************/
winsockCheck(FILE * FileErr)99 gboolean winsockCheck(FILE* FileErr)
100 {
101     	WORD wVersionRequested;
102     	WSADATA wsaData;
103     	int err;
104     	wVersionRequested = MAKEWORD( 1, 1 );
105     	err = WSAStartup( wVersionRequested, &wsaData );
106     	if ( err != 0 )
107 	{
108 		fprintf(FileErr,"Unsupported version of winsock.dll!\n");
109 		return FALSE;
110 	}
111 
112     	if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 )
113 	{
114         	fprintf(FileErr,"Unsupported version of winsock.dll!\n");
115 		return FALSE;
116 	}
117 
118 	return TRUE;
119 }
120 #endif /* G_OS_WIN32 */
121 /********************************************************************************/
FOpen(const gchar * fileutf8,const gchar * type)122 FILE* FOpen(const gchar *fileutf8, const gchar* type)
123 {
124 	FILE* file;
125 #ifdef G_OS_WIN32
126 		/* gchar* filename = g_filename_to_utf8(fileutf8);*/
127 		gchar* filename = g_strdup(fileutf8);
128 		file = fopen(filename,type);
129 		if(file) set_last_directory(fileutf8);
130 		return file;
131 #else
132 		file = fopen(fileutf8,type);
133 		if(file) set_last_directory(fileutf8);
134 		return file;
135 #endif
136 }
137 /********************************************************************************/
free_commands_list(CommandsList * list)138 static void free_commands_list(CommandsList* list)
139 {
140 	gint i;
141 	if(!list)
142 		return;
143 
144 	if(list->numberOfCommands<1)
145 	{
146   		list->numberOfCommands = 0;
147   		list->numberOfDefaultCommand = 0;
148 		list->commands = NULL;
149 		return;
150 	}
151 	if(list->commands)
152 	{
153 		for(i=0;i<list->numberOfCommands;i++)
154 			if(list->commands[i])
155 				g_free(list->commands[i]);
156 		g_free(list->commands);
157 	}
158   	list->numberOfCommands = 0;
159   	list->numberOfDefaultCommand = 0;
160 	list->commands = NULL;
161 }
162 /********************************************************************************/
free_gamess_commands()163 void free_gamess_commands()
164 {
165 	free_commands_list(&gaussianCommands);
166 }
167 /********************************************************************************/
free_gaussian_commands()168 void free_gaussian_commands()
169 {
170 	free_commands_list(&gaussianCommands);
171 }
172 /********************************************************************************/
free_molcas_commands()173 void free_molcas_commands()
174 {
175 	free_commands_list(&molcasCommands);
176 }
177 /********************************************************************************/
free_molpro_commands()178 void free_molpro_commands()
179 {
180 	free_commands_list(&molproCommands);
181 }
182 /********************************************************************************/
free_mpqc_commands()183 void free_mpqc_commands()
184 {
185 	free_commands_list(&mpqcCommands);
186 }
187 /********************************************************************************/
free_nwchem_commands()188 void free_nwchem_commands()
189 {
190 	free_commands_list(&nwchemCommands);
191 }
192 /********************************************************************************/
free_psicode_commands()193 void free_psicode_commands()
194 {
195 	free_commands_list(&psicodeCommands);
196 }
197 /********************************************************************************/
free_orca_commands()198 void free_orca_commands()
199 {
200 	free_commands_list(&orcaCommands);
201 }
202 /********************************************************************************/
free_demon_commands()203 void free_demon_commands()
204 {
205 	free_commands_list(&demonCommands);
206 }
207 /********************************************************************************/
free_firefly_commands()208 void free_firefly_commands()
209 {
210 	free_commands_list(&fireflyCommands);
211 }
212 /********************************************************************************/
free_qchem_commands()213 void free_qchem_commands()
214 {
215 	free_commands_list(&qchemCommands);
216 }
217 /********************************************************************************/
free_mopac_commands()218 void free_mopac_commands()
219 {
220 	free_commands_list(&mopacCommands);
221 }
222 /********************************************************************************/
free_povray_commands()223 void free_povray_commands()
224 {
225 	free_commands_list(&povrayCommands);
226 }
227 /********************************************************************************/
get_time_str()228 gchar* get_time_str()
229 {
230 	gchar* str=NULL;
231 	time_t t;
232 	struct tm* ts;
233 
234 	t = time(NULL);
235 	ts = localtime(&t);
236 	str = asctime (ts);
237 	return str;
238 }
239 /********************************************************************************/
get_multipole_rank()240 gdouble get_multipole_rank()
241 {
242   return multipole_rank;
243 }
244 /********************************************************************************/
this_is_a_backspace(gchar * st)245 gboolean this_is_a_backspace(gchar *st)
246 {
247         gint i;
248         for(i=0;i<(gint)strlen(st);i++)
249         	if(st[i] != ' ' && st[i] !='\n' && st[i] !='\r')
250                 	return FALSE;
251         return TRUE;
252 }
253 /********************************************************************************/
changeDInE(gchar * st)254 void changeDInE(gchar *st)
255 {
256         gint i;
257 	gint l = 0;
258 	if(!st) return;
259 	l = strlen(st);
260         for(i=0;i<l;i++)
261        		if(st[i] == 'D' || st[i] =='d')
262 			st[i]='e';
263 }
264 /********************************************************************************/
set_file_open(gchar * remotehost,gchar * remoteuser,gchar * remotedir,GabEditNetWork netWorkProtocol)265 void  set_file_open(gchar* remotehost,gchar* remoteuser,gchar* remotedir, GabEditNetWork netWorkProtocol)
266 {
267   gchar localhost[100];
268 
269 #ifdef G_OS_WIN32
270 	winsockCheck(stderr);
271 #endif
272   gethostname(localhost,100);
273 
274   if(!fileopen.localhost)
275   	fileopen.localhost = g_strdup(localhost);
276 
277   if(!fileopen.localdir)
278   	fileopen.localdir = get_name_dir(fileopen.projectname);
279 
280   if(fileopen.remotehost)
281 	g_free(fileopen.remotehost);
282   if(fileopen.remoteuser)
283 	g_free(fileopen.remoteuser);
284   if(fileopen.remotedir)
285 	g_free(fileopen.remotedir);
286   fileopen.remotehost = g_strdup(remotehost);
287   fileopen.remoteuser = g_strdup(remotehost);
288   fileopen.remotedir = g_strdup(remotedir);
289   fileopen.netWorkProtocol = netWorkProtocol;
290 }
291 /********************************************************************************/
filegets(gchar * temp,FILE * fd)292 void filegets(gchar *temp,FILE* fd)
293 {
294 	gchar t[BBSIZE];
295         gint taille = BBSIZE;
296  	gint k = 0;
297  	gint i;
298 
299     	if(!feof(fd)) { char* e = fgets(t,taille,fd);}
300 	for(i=0;i<taille;i++)
301  	{
302   		if(t[i] =='\n')
303 			break;
304   		if(t[i] != ' ' && t[i] !='\n')
305   		{
306    			temp[k] = t[i];
307    			k++;
308   		}
309  	}
310  	temp[k] = '\0';
311 }
312 /********************************************************************************/
this_is_an_object(GtkObject * obj)313 gboolean  this_is_an_object(GtkObject *obj)
314 {
315 	return GTK_IS_OBJECT(obj);
316 }
317 /********************************************************************************/
add_dir_to_user(User * user,const gchar * dir)318 gboolean  add_dir_to_user(User* user, const gchar* dir)
319 {
320 	gint i;
321 	if(user->ndirs == 0)
322 	{
323 		user->ndirs = 1;
324 		user->dirs = g_malloc(sizeof(gchar*));
325 		user->dirs[0] = g_strdup(dir);
326 	}
327 	else
328 	{
329 		for(i=0;i<user->ndirs;i++)
330 			if(strcmp(dir,user->dirs[i]) == 0)
331 				return FALSE;
332 
333 		user->dirs = g_realloc(user->dirs,(user->ndirs+1)*sizeof(gchar*));
334 		for(i=user->ndirs;i>0;i--)
335 			user->dirs[i] = user->dirs[i-1];
336 		user->dirs[0] = g_strdup(dir);
337 		if(user->ndirs>=NHOSTMAX)
338 		{
339 /*			g_free(user->dirs[user->ndirs]);*/
340 			user->dirs = g_realloc(user->dirs,(user->ndirs)*sizeof(gchar*));
341 		}
342 		else
343 			(user->ndirs)++;
344 	}
345 	return TRUE;
346 }
347 /********************************************************************************/
add_user_to_host(Host * host,const gchar * username,const gchar * password,const gchar * dir)348 gboolean add_user_to_host(Host* host,const gchar *username, const gchar* password, const gchar *dir)
349 {
350 	gint i;
351 	if(host->nusers == 0)
352 	{
353 		host->nusers = 1;
354 		host->users = g_malloc(sizeof(User));
355 		host->users[0].username = g_strdup(username);
356 		if(password)
357 			host->users[0].password = g_strdup(password);
358 		else
359 			host->users[0].password = NULL;
360 		host->users[0].ndirs = 0;
361   		add_dir_to_user(&host->users[0] ,dir);
362 	}
363 	else
364 	{
365 		for(i=0;i<host->nusers;i++)
366 			if(strcmp(username,host->users[i].username) == 0)
367 			{
368 				return  add_dir_to_user(&host->users[i],dir);
369 			}
370 
371 		host->users = g_realloc(host->users,(host->nusers+1)*sizeof(User));
372 		for(i=host->nusers;i>0;i--)
373 			host->users[i] = host->users[i-1];
374 
375 		host->users[0].ndirs = 0;
376 		host->users[0].username = g_strdup(username);
377 		if(password)
378 			host->users[0].password = g_strdup(password);
379 		else
380 			host->users[0].password = NULL;
381   		add_dir_to_user(&host->users[0] ,dir);
382 		if(host->nusers>=NHOSTMAX)
383 		{
384 			for(i=0;i<host->users[host->nusers].ndirs;i++)
385 			{
386 				g_free(host->users[host->nusers].dirs[i]);
387 			}
388 			g_free(host->users[host->nusers].dirs);
389 /*			g_free(host->users[host->nusers]);*/
390 			host->users = g_realloc(host->users,(host->nusers)*sizeof(User));
391 		}
392 		else
393 			(host->nusers)++;
394 	}
395 	return TRUE;
396 }
397 /********************************************************************************/
add_host(const gchar * hostname,const gchar * username,const gchar * password,const gchar * dir)398 void add_host(const gchar *hostname, const gchar* username, const gchar* password, const gchar* dir)
399 {
400   gint i;
401   gint j;
402 
403   if(recenthosts.nhosts == 0)
404   {
405 	recenthosts.hosts = g_malloc(sizeof(Host));
406  	recenthosts.hosts[0].hostname = g_strdup(hostname);
407  	recenthosts.hosts[0].nusers = 0;
408  	add_user_to_host(&recenthosts.hosts[0],username,password,dir);
409 	recenthosts.nhosts = 1;
410   }
411   else
412   {
413 	for(i=0;i<recenthosts.nhosts;i++)
414 		if(strcmp(hostname,recenthosts.hosts[i].hostname) == 0)
415 		{
416  			add_user_to_host(&recenthosts.hosts[i],username,password,dir);
417 			return;
418 		}
419 
420 	recenthosts.hosts = g_realloc(recenthosts.hosts , (recenthosts.nhosts+1)*sizeof(Host));
421 	for(i=recenthosts.nhosts;i>0;i--)
422 		recenthosts.hosts[i] = recenthosts.hosts[i-1];
423  	recenthosts.hosts[0].hostname = g_strdup(hostname);
424  	recenthosts.hosts[0].nusers = 0;
425  	add_user_to_host(&recenthosts.hosts[0],username,password,dir);
426 	if(recenthosts.nhosts>=NHOSTMAX)
427 	{
428 		for(i=0;i<recenthosts.hosts[recenthosts.nhosts].nusers;i++)
429 		{
430 			for(j=0;j<recenthosts.hosts[recenthosts.nhosts].users[i].ndirs;j++)
431 				g_free(recenthosts.hosts[recenthosts.nhosts].users[i].dirs[j]);
432 			g_free(recenthosts.hosts[recenthosts.nhosts].users[i].dirs);
433 		}
434 		g_free(recenthosts.hosts[recenthosts.nhosts].users);
435 		recenthosts.hosts = g_realloc(recenthosts.hosts ,(recenthosts.nhosts)*sizeof(Host));
436 	}
437 	else
438 		recenthosts.nhosts++;
439 
440   }
441 }
442 /********************************************************************************/
get_local_user()443 G_CONST_RETURN gchar *get_local_user()
444 {
445 
446 
447 #ifdef G_OS_WIN32
448 	return NULL;
449 #else
450   	struct passwd *pw;
451   	const static gchar* localuser = NULL;
452 
453 	if(localuser) return localuser;
454 
455   	localuser = g_getenv("USER");
456   	if( (localuser == NULL) || ((pw = getpwnam(localuser)) && (pw->pw_uid != getuid())) )
457   	{
458 		if ( (pw = getpwuid(getuid())))
459 			localuser = g_strdup(pw->pw_name);
460 		else
461 			localuser = NULL;
462   	}
463   return localuser;
464 #endif
465 }
466 /*************************************************************************************/
Waiting(gdouble tsecond)467 void Waiting(gdouble tsecond)
468 {
469         GTimer *timer;
470         gdouble elaps;
471         gulong m ;
472 
473         timer =g_timer_new( );
474 	g_timer_start( timer );
475 	g_timer_reset( timer );
476         do{
477 		elaps = g_timer_elapsed( timer,&m);
478         }while(elaps<tsecond);
479  	g_timer_destroy(timer);
480 }
481 /*************************************************************************************/
Debug(char * fmt,...)482 void Debug(char *fmt,...)
483 {
484 	va_list ap;
485         if(DebugFlag)
486 		return;
487 
488 	va_start(ap,fmt);
489 	vfprintf(stdout, fmt, ap);
490 	va_end(ap);
491 }
492 /********************************************************************************/
get_line_chars(gchar c,gint n)493 gchar* get_line_chars(gchar c,gint n)
494 {
495 	gint i;
496 	gchar *line = NULL;
497 
498 	if(n<1)
499 		return line;
500 	line = g_malloc((n+1)*sizeof(gchar));
501 	for(i=0;i<n;i++)
502 		line[i] = c;
503 	line[n] = '\0';
504 
505 	return line;
506 
507 }
508 /********************************************************************************/
cat_file(gchar * namefile,gboolean tabulation)509 gchar* cat_file(gchar* namefile,gboolean tabulation)
510 {
511  gchar *t = NULL;
512  gchar *tsrt = NULL;
513  FILE *fd;
514  gchar *dump = NULL;
515 
516 
517  t=g_malloc(BBSIZE*sizeof(gchar));
518 
519  fd = FOpen(namefile, "rb");
520  if(fd)
521  {
522   	while(!feof(fd))
523   	{
524     		if(!fgets(t,BBSIZE, fd)) break;
525                 dump = tsrt;
526 		if(!tsrt)
527 		{
528 			if(tabulation)
529 				tsrt = g_strdup_printf("\t%s",t);
530 			else
531 				tsrt = g_strdup_printf("%s",t);
532 		}
533 		else
534 		{
535 			if(tabulation)
536 				tsrt = g_strdup_printf("%s\t%s",tsrt,t);
537 			else
538 				tsrt = g_strdup_printf("%s%s",tsrt,t);
539 			g_free(dump);
540 			dump = NULL;
541 		}
542   	}
543  	fclose(fd);
544 	unlink (namefile);
545  }
546  else
547  {
548    tsrt = NULL;
549  }
550  g_free(t);
551  t = tsrt;
552  if(t)
553  {
554  	tsrt = g_locale_to_utf8(t,-1,NULL,NULL,NULL);
555 	g_free(t);
556  }
557 
558  return tsrt;
559 
560 }
561 /*************************************************************************************/
run_command(gchar * command)562 gchar *run_command(gchar *command)
563 {
564  gchar *t;
565  gchar *terr = NULL;
566  FILE *fd;
567  gchar *temp;
568  gchar *outfile= g_strdup_printf("%s%stmp%soutfile",gabedit_directory(), G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S);
569  gchar *errfile= g_strdup_printf("%s%stmp%serrfile",gabedit_directory(), G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S);
570  gchar *dump;
571  gint taille = BBSIZE;
572 
573  temp = g_strdup_printf("sh -c '%s >%s 2>%s'",command,outfile,errfile);
574  {int it = system(temp);}
575 
576  t=g_malloc(taille);
577 
578  fd = FOpen(errfile, "rb");
579  if(fd)
580  {
581   	while(!feof(fd))
582   	{
583     		if(!fgets(t,taille, fd))
584 			break;
585                 dump = terr;
586 		if(!terr)
587 			terr = g_strdup_printf("%s",t);
588 		else
589 		{
590 			terr = g_strdup_printf("%s%s",terr,t);
591 			g_free(dump);
592 		}
593   	}
594  	fclose(fd);
595 	unlink (errfile);
596  }
597  else
598    terr = NULL;
599 
600  fd = FOpen(outfile, "rb");
601  if(fd)
602  {
603 	unlink (outfile);
604  }
605 
606  g_free(t);
607  g_free(temp);
608  g_free(outfile);
609  g_free(errfile);
610 
611  return terr;
612 }
613 /********************************************************************************/
614 #ifdef G_OS_WIN32
615 
createProcessWin32(char * myChildProcess)616 void createProcessWin32(char* myChildProcess)
617 {
618 	STARTUPINFO si;
619 	PROCESS_INFORMATION pi;
620 
621 	ZeroMemory( &si, sizeof(si) );
622 	si.cb = sizeof(si);
623 	ZeroMemory( &pi, sizeof(pi) );
624 
625 	printf(_("Command = %s\n"),myChildProcess);
626 	/* Start the child process. */
627 	if( !CreateProcess( NULL,   /* No module name (use command line). */
628 		TEXT(myChildProcess), /* Command line. */
629 	                     NULL,      /* Process handle not inheritable. */
630 	                     NULL,      /* Thread handle not inheritable. */
631 	                    FALSE,      /* Set handle inheritance to FALSE. */
632 	                    0,          /* No creation flags. */
633 	                   NULL,        /* Use parent's environment block. */
634 	                   NULL,        /* Use parent's starting directory. */
635 	                   &si,         /* Pointer to STARTUPINFO structure.*/
636 	                  &pi )         /* Pointer to PROCESS_INFORMATION structure.*/
637                         )
638 	{
639 		gchar buffer[BBSIZE];
640 		sprintf(buffer,_("CreateProcess failed (%d)"),(int)GetLastError());
641         	Message(buffer, _("Error"), TRUE);
642 		return;
643     }
644 
645         /* Wait until child process exits.*/
646         /* WaitForSingleObject( pi.hProcess, INFINITE );*/
647 
648         /* Close process and thread handles. */
649        CloseHandle( pi.hProcess );
650        CloseHandle( pi.hThread );
651 }
652 
653 #endif
654 /********************************************************************************/
run_local_command(gchar * outfile,gchar * errfile,gchar * command,gboolean under)655 void run_local_command(gchar *outfile,gchar *errfile,gchar* command,gboolean under)
656 {
657 	gchar *temp;
658 	gint ierr = 0;
659 
660 	unlink (outfile);
661 	unlink (errfile);
662 
663 	if(under)
664 	{
665 #ifdef G_OS_WIN32
666 		temp = g_strdup_printf("%s >%s 2>%s ",command, outfile, errfile);
667 		createProcessWin32(temp);
668 
669 #else
670 		if(strstr(command,">"))
671  		temp = g_strdup_printf("sh -c '%s 2>%s&'",command, errfile);
672 		else
673  		temp = g_strdup_printf("sh -c '%s >%s 2>%s&'",command, outfile, errfile);
674 		ierr = system(temp);
675 #endif
676 
677 		Waiting(0.5);
678 	}
679 	else
680 	{
681 #ifdef G_OS_WIN32
682 		temp = g_strdup_printf("%s >%s 2>%s",command,outfile,errfile);
683 #else
684  		temp = g_strdup_printf("sh -c '%s >%s 2>%s'",command,outfile,errfile);
685 #endif
686  		ierr = system(temp);
687 	}
688 
689 	g_free(temp);
690 }
691 /*************************************************************************************/
gabedit_directory(void)692 const gchar *gabedit_directory(void)
693 {
694   static gchar *gabedit_dir = NULL;
695   gchar *home_dir;
696   gchar *home_dir_sep;
697 #ifdef G_OS_WIN32
698   gchar* Version_S = g_strdup_printf("%d%d%d",MAJOR_VERSION,MINOR_VERSION,MICRO_VERSION);
699 #else
700   gchar* Version_S = g_strdup_printf("%d.%d.%d",MAJOR_VERSION,MINOR_VERSION,MICRO_VERSION);
701 #endif
702 
703   if (gabedit_dir != NULL)
704     return gabedit_dir;
705 
706   home_dir = g_strdup(g_get_home_dir());
707 
708   if (home_dir != NULL && home_dir[strlen(home_dir)-1] != G_DIR_SEPARATOR)
709     home_dir_sep = G_DIR_SEPARATOR_S;
710   else
711     home_dir_sep = "";
712 
713   if (!home_dir)
714   {
715 #ifdef G_OS_WIN32
716 		home_dir = g_strdup("C:");
717 #else  /* G_OS_WIN32 */
718 		home_dir = g_strdup("/tmp");
719 #endif /* G_OS_WIN32 */
720 		home_dir_sep = G_DIR_SEPARATOR_S;
721   }
722 #ifdef G_OS_WIN32
723   gabedit_dir = g_strconcat(home_dir,home_dir_sep,"gabedit",Version_S,NULL);
724 #else
725   gabedit_dir = g_strconcat(home_dir,home_dir_sep,".gabedit-",Version_S,NULL);
726 #endif
727 
728   g_free(Version_S);
729   return gabedit_dir;
730 }
731 /*************************************************************************************/
DeleteLastChar(gchar * str)732 void DeleteLastChar(gchar *str)
733 {
734         str[strlen(str)-1]='\0';
735 }
736 /*************************************************************************************/
get_dir_file_name(G_CONST_RETURN gchar * dirname,G_CONST_RETURN gchar * filename)737 gchar *get_dir_file_name(G_CONST_RETURN gchar* dirname, G_CONST_RETURN gchar* filename)
738 {
739    gchar *name = NULL;
740 
741    name = g_strdup_printf("%s%s%s",dirname,G_DIR_SEPARATOR_S,filename);
742 
743    return name;
744 }
745 /*************************************************************************************/
get_name_dir(const gchar * allname)746 gchar *get_name_dir(const gchar* allname)
747 {
748    gchar *name;
749    name = g_path_get_dirname(allname);
750    if(strcmp(name,".")==0)
751    {
752 	   g_free(name);
753 	   name = g_strdup(g_get_current_dir());
754    }
755 
756   return name;
757 }
758 /*************************************************************************************/
get_filename_without_ext(const gchar * allname)759 gchar *get_filename_without_ext(const gchar* allname)
760 {
761    gchar *filename= NULL;
762    gchar *temp= NULL;
763    gint len=0;
764    gint i;
765    gchar* name = NULL;
766 
767    if(!allname || strlen(allname)<1) return g_strdup("error");
768    temp = g_strdup(allname);
769    filename= g_strdup(allname);
770    len=strlen(filename);
771 
772    for(i=len;i>0;i--)
773 	if(temp[i]=='.')
774 	{
775 		temp[i] = '\0';
776 		break;
777 	}
778    name = g_strdup_printf("%s",temp);
779    if(temp) g_free(temp);
780    if(filename) g_free(filename);
781   return name;
782 }
783 /*************************************************************************************/
get_suffix_name_file(const gchar * allname)784 gchar *get_suffix_name_file(const gchar* allname)
785 {
786    gchar *filename= g_path_get_basename(allname);
787    gchar *dirname= g_path_get_dirname(allname);
788    gchar *temp= g_strdup(filename);
789    gint len=strlen(filename);
790    gint i;
791    gchar* name = NULL;
792 
793    if(!allname || strlen(allname)<1) return g_strdup("error");
794    filename= g_path_get_basename(allname);
795    dirname= g_path_get_dirname(allname);
796    temp= g_strdup(filename);
797    len=strlen(filename);
798 
799    for(i=len;i>0;i--)
800 	if(temp[i]=='.')
801 	{
802 		temp[i] = '\0';
803 		break;
804 	}
805    name = g_strdup_printf("%s%s%s",dirname,G_DIR_SEPARATOR_S,temp);
806    if(temp) g_free(temp);
807    if(dirname) g_free(dirname);
808    if(filename) g_free(filename);
809 
810    if(strcmp(name,".")==0) name = g_strdup(g_get_current_dir());
811 
812   return name;
813 }
814 /*************************************************************************************/
get_name_file(const gchar * allname)815 gchar *get_name_file(const gchar* allname)
816 {
817    gchar *name= g_path_get_basename(allname);
818 	/*
819    gchar *name=g_strdup(allname);
820    gint i;
821    gint len=strlen(allname);
822    gint islash=0;
823 
824    for(i=len;i>0;i--)
825    if(allname[i]==G_DIR_SEPARATOR)
826    {
827      islash=i+1;
828      break;
829    }
830    if(islash>0)
831    {
832 	if(name)
833 		g_free(name);
834    	name=g_malloc(len-islash+2);
835    	for(i=islash;i<=len;i++)
836     		name[i-islash]=allname[i];
837    	name[len-islash+1]='\0';
838    }
839    */
840 
841   return name;
842 }
843 /*************************************************************************************/
get_produit_vectoriel(Point V1,Point V2)844 Point get_produit_vectoriel(Point V1,Point V2)
845 {
846    Point PV;
847 
848    PV.C[0]= V1.C[1]*V2.C[2]-V1.C[2]*V2.C[1];
849    PV.C[1]= V1.C[2]*V2.C[0]-V1.C[0]*V2.C[2];
850    PV.C[2]= V1.C[0]*V2.C[1]-V1.C[1]*V2.C[0];
851 
852   return PV;
853 }
854 /*************************************************************************************/
get_distance_points(Point P1,Point P2,gboolean f3)855 gchar *get_distance_points(Point P1,Point P2,gboolean f3)
856 {
857    gchar *distance;
858    gdouble Distance;
859    guint i;
860 
861    Distance = 0.0;
862    for(i=0;i<3;i++)
863  	Distance += (P1.C[i]- P2.C[i])*(P1.C[i]- P2.C[i]);
864 
865    Distance = sqrt(Distance)*BOHR_TO_ANG;
866    if(f3)
867    	distance = g_strdup_printf("%7.3lf",Distance);
868    else
869    	distance = g_strdup_printf("%0.20lf",Distance);
870   return distance;
871 }
872 /*************************************************************************************/
get_module(Point V)873 gdouble get_module(Point V)
874 {
875    gdouble Module;
876    guint i;
877 
878    Module = 0.0;
879    for(i=0;i<3;i++)
880  	Module += V.C[i]*V.C[i];
881 
882   return sqrt(Module);
883 
884 }
885 /*************************************************************************************/
get_scalaire(Point V1,Point V2)886 gdouble get_scalaire(Point V1,Point V2)
887 {
888    gdouble Scalaire;
889    guint i;
890 
891    Scalaire = 0.0;
892    for(i=0;i<3;i++)
893  	Scalaire += V1.C[i]*V2.C[i];
894 
895   return Scalaire;
896 
897 }
898 /*************************************************************************************/
get_angle_vectors(Point V1,Point V2)899 gchar *get_angle_vectors(Point V1,Point V2)
900 {
901    gchar *angle;
902    gdouble Angle;
903    gdouble modv1v2 = get_module(V1)*get_module(V2);
904 
905 
906    if(fabs(modv1v2)>1e-14 )
907    {
908         Angle = get_scalaire(V1,V2)/modv1v2;
909 /*	Debug("Pscal = %f\n",Angle);*/
910 	if(Angle<=-1)
911         	return g_strdup("180.0");
912 	if(Angle>=1)
913         	return g_strdup_printf("0.0");
914 
915         Angle = acos(Angle)/DEG_TO_RAD;
916 /*	Debug("Angle = %f\n",Angle);*/
917         angle = g_strdup_printf("%0.20lf",Angle);
918    }
919    else
920         angle = g_strdup_printf("ERROR");
921 
922   return angle;
923 }
924 /*************************************************************************************/
add_fonts_in_file(FILE * fd,FontsStyle fontsstyle)925 void add_fonts_in_file(FILE *fd,FontsStyle fontsstyle)
926 {
927 
928  if(fontsstyle.fontname[strlen(fontsstyle.fontname)-1] !='\n')
929  	fprintf(fd,"%s\n",fontsstyle.fontname);
930  else
931  	fprintf(fd,"%s",fontsstyle.fontname);
932 
933  fprintf(fd,"%d\n",fontsstyle.BaseColor.red);
934  fprintf(fd,"%d\n",fontsstyle.BaseColor.green);
935  fprintf(fd,"%d\n",fontsstyle.BaseColor.blue);
936 
937  fprintf(fd,"%d\n",fontsstyle.TextColor.red);
938  fprintf(fd,"%d\n",fontsstyle.TextColor.green);
939  fprintf(fd,"%d\n",fontsstyle.TextColor.blue);
940 }
941 /*************************************************************************************/
create_hosts_file()942 void create_hosts_file()
943 {
944  gchar *hostsfile;
945  FILE *fd;
946  gint i;
947  gint j;
948  gint k;
949 
950  hostsfile = g_strdup_printf("%s%shosts",gabedit_directory(),G_DIR_SEPARATOR_S);
951 
952  fd = FOpen(hostsfile, "w");
953  if(fd)
954  {
955 	fprintf(fd,"%d\n",recenthosts.nhosts);
956 	for(i=0;i<recenthosts.nhosts;i++)
957 	{
958 		fprintf(fd,"%s\n",recenthosts.hosts[i].hostname);
959 		fprintf(fd,"%d\n",recenthosts.hosts[i].nusers);
960 		for(j=0;j<recenthosts.hosts[i].nusers;j++)
961 		{
962 			fprintf(fd,"%s\n",recenthosts.hosts[i].users[j].username);
963 			fprintf(fd,"%d\n",recenthosts.hosts[i].users[j].ndirs);
964 			for(k=0;k<recenthosts.hosts[i].users[j].ndirs;k++)
965 			{
966 				fprintf(fd,"%s\n",recenthosts.hosts[i].users[j].dirs[k]);
967 			}
968 		}
969 	}
970  	fclose(fd);
971  }
972 
973  g_free(hostsfile);
974 }
975 /*************************************************************************************/
create_fonts_file()976 void create_fonts_file()
977 {
978  gchar *fontsfile;
979  FILE *fd;
980 
981  fontsfile = g_strdup_printf("%s%sfonts",gabedit_directory(),G_DIR_SEPARATOR_S);
982 
983  fd = FOpen(fontsfile, "w");
984 
985  add_fonts_in_file(fd,FontsStyleData);
986  add_fonts_in_file(fd,FontsStyleResult);
987  add_fonts_in_file(fd,FontsStyleLabel);
988  add_fonts_in_file(fd,FontsStyleOther);
989 
990  fclose(fd);
991  g_free(fontsfile);
992 }
993 /*************************************************************************************/
readOneDir(FILE * file,char * tag,char ** pDirectory)994 static gboolean readOneDir(FILE* file, char* tag, char** pDirectory)
995 {
996 	gboolean ok=FALSE;
997 	gchar t[BSIZE];
998 	rewind(file);
999 	while(!feof(file))
1000 	{
1001  		if(!fgets(t,BSIZE,file)) break;
1002 		if(strstr(t,tag)) { ok = TRUE; break;}
1003 	}
1004 	if(ok)
1005  	if(fgets(t,BSIZE,file))
1006 	{
1007  		*pDirectory = g_strdup(t);
1008 		str_delete_n(*pDirectory);
1009 		delete_last_spaces(*pDirectory);
1010 		delete_first_spaces(*pDirectory);
1011 #ifdef G_OS_WIN32
1012 		{
1013 		gchar t[BBSIZE];
1014 		sprintf(t,"%s;%s",*pDirectory,g_getenv("PATH"));
1015 		if(strlen(t)>1) g_setenv("PATH",t,TRUE);
1016 		}
1017 #endif
1018  		if(!fgets(t,BSIZE,file)) ok=FALSE;
1019 	}
1020 	return ok;
1021 }
1022 /*************************************************************************************/
readCommandsOneSoft(FILE * file,char * tag,char ** pCommand,CommandsList * commandsList,void (* free_func)(),char * defCommand)1023 static gboolean readCommandsOneSoft(FILE* file, char* tag, char** pCommand, CommandsList* commandsList, void (*free_func)(), char* defCommand)
1024 {
1025 	int i;
1026 	gboolean ok=FALSE;
1027 	gchar t[BSIZE];
1028 	rewind(file);
1029 	while(!feof(file))
1030 	{
1031  		if(!fgets(t,BSIZE,file)) break;
1032 		if(strstr(t,tag)) { ok = TRUE; break;}
1033 	}
1034 	if(!ok) return ok;
1035 
1036  	if(fgets(t,BSIZE,file))
1037 	{
1038  		*pCommand = g_strdup(t);
1039 		str_delete_n(*pCommand);
1040 		delete_last_spaces(*pCommand);
1041 		delete_first_spaces(*pCommand);
1042 	}
1043 	if(commandsList==NULL) return ok;
1044 
1045  	if(fgets(t,BSIZE,file) && atoi(t)>0)
1046 	{
1047 		if(free_func) free_func();
1048 		commandsList->numberOfCommands = atoi(t);
1049 		commandsList->commands = g_malloc(commandsList->numberOfCommands*sizeof(gchar*));
1050 		for(i=0;i<commandsList->numberOfCommands;i++) commandsList->commands[i]  = g_strdup(" ");
1051 		for(i=0;i<commandsList->numberOfCommands;i++)
1052 		{
1053 			if(!fgets(t,BSIZE,file) || strstr(t,"End"))
1054 			{
1055 				free_func();
1056   				commandsList->numberOfCommands = 1;
1057   				commandsList->numberOfDefaultCommand = 0;
1058   				commandsList->commands = g_malloc(sizeof(gchar*));
1059   				commandsList->commands[0] = g_strdup(defCommand);
1060 				return FALSE;
1061 			}
1062 			else
1063 			{
1064 				commandsList->commands[i] = g_strdup(t);
1065 				str_delete_n(commandsList->commands[i]);
1066 				delete_last_spaces(commandsList->commands[i]);
1067 				delete_first_spaces(commandsList->commands[i]);
1068 			}
1069 		}
1070 	}
1071  	if(!fgets(t,BSIZE,file)) ok=FALSE; /* End of tag */
1072 	return ok;
1073 }
1074 /*************************************************************************************/
create_commands_file()1075 void create_commands_file()
1076 {
1077 	gchar *commandsfile;
1078 	FILE *fd;
1079 	gint i;
1080 
1081 	commandsfile = g_strdup_printf("%s%scommands",gabedit_directory(),G_DIR_SEPARATOR_S);
1082 
1083 	fd = FOpen(commandsfile, "w");
1084 
1085 	fprintf(fd,"Begin Batch\n");
1086 
1087 	str_delete_n(NameTypeBatch);
1088  	fprintf(fd,"%s\n",NameTypeBatch);
1089 
1090 	str_delete_n(NameCommandBatchAll);
1091  	fprintf(fd,"%s\n",NameCommandBatchAll);
1092 
1093 	str_delete_n(NameCommandBatchUser);
1094  	fprintf(fd,"%s\n",NameCommandBatchUser);
1095 
1096 	str_delete_n(NameCommandBatchKill);
1097  	fprintf(fd,"%s\n",NameCommandBatchKill);
1098 
1099 
1100 	str_delete_n(NamejobIdTitleBatch);
1101  	fprintf(fd,"%s\n",NamejobIdTitleBatch);
1102 
1103  	fprintf(fd,"%d\n",batchCommands.numberOfTypes);
1104 	for(i=0;i<batchCommands.numberOfTypes;i++)
1105 	{
1106 		str_delete_n(batchCommands.types[i]);
1107 		fprintf(fd,"%s\n",batchCommands.types[i]);
1108 		str_delete_n(batchCommands.commandListAll[i]);
1109 		fprintf(fd,"%s\n",batchCommands.commandListAll[i]);
1110 		str_delete_n(batchCommands.commandListUser[i]);
1111 		fprintf(fd,"%s\n",batchCommands.commandListUser[i]);
1112 		str_delete_n(batchCommands.commandKill[i]);
1113 		fprintf(fd,"%s\n",batchCommands.commandKill[i]);
1114 		str_delete_n(batchCommands.jobIdTitle[i]);
1115 		fprintf(fd,"%s\n",batchCommands.jobIdTitle[i]);
1116 	}
1117 	fprintf(fd,"End\n");
1118 /*-----------------------------------------------------------------------------*/
1119 	fprintf(fd,"Begin DeMon\n");
1120 	str_delete_n(NameCommandDeMon);
1121 	delete_last_spaces(NameCommandDeMon);
1122 	delete_first_spaces(NameCommandDeMon);
1123  	fprintf(fd,"%s\n",NameCommandDeMon);
1124  	fprintf(fd,"%d\n",demonCommands.numberOfCommands);
1125 	for(i=0;i<demonCommands.numberOfCommands;i++)
1126 	{
1127 		str_delete_n(demonCommands.commands[i]);
1128 		delete_last_spaces(demonCommands.commands[i]);
1129 		delete_first_spaces(demonCommands.commands[i]);
1130 		fprintf(fd,"%s\n",demonCommands.commands[i]);
1131 	}
1132 	fprintf(fd,"End\n");
1133 /*-----------------------------------------------------------------------------*/
1134 	fprintf(fd,"Begin Gamess\n");
1135 	str_delete_n(NameCommandGamess);
1136 	delete_last_spaces(NameCommandGamess);
1137 	delete_first_spaces(NameCommandGamess);
1138  	fprintf(fd,"%s\n",NameCommandGamess);
1139  	fprintf(fd,"%d\n",gamessCommands.numberOfCommands);
1140 	for(i=0;i<gamessCommands.numberOfCommands;i++)
1141 	{
1142 		str_delete_n(gamessCommands.commands[i]);
1143 		delete_last_spaces(gamessCommands.commands[i]);
1144 		delete_first_spaces(gamessCommands.commands[i]);
1145 		fprintf(fd,"%s\n",gamessCommands.commands[i]);
1146 	}
1147 	fprintf(fd,"End\n");
1148 
1149 /*-----------------------------------------------------------------------------*/
1150 
1151 	fprintf(fd,"Begin Gaussian\n");
1152 	str_delete_n(NameCommandGaussian);
1153 	delete_last_spaces(NameCommandGaussian);
1154 	delete_first_spaces(NameCommandGaussian);
1155  	fprintf(fd,"%s\n",NameCommandGaussian);
1156  	fprintf(fd,"%d\n",gaussianCommands.numberOfCommands);
1157 	for(i=0;i<gaussianCommands.numberOfCommands;i++)
1158 	{
1159 		str_delete_n(gaussianCommands.commands[i]);
1160 		delete_last_spaces(gaussianCommands.commands[i]);
1161 		delete_first_spaces(gaussianCommands.commands[i]);
1162 		fprintf(fd,"%s\n",gaussianCommands.commands[i]);
1163 	}
1164 	fprintf(fd,"End\n");
1165 
1166 /*-----------------------------------------------------------------------------*/
1167 
1168 	fprintf(fd,"Begin Molcas\n");
1169 	str_delete_n(NameCommandMolcas);
1170 	delete_last_spaces(NameCommandMolcas);
1171 	delete_first_spaces(NameCommandMolcas);
1172  	fprintf(fd,"%s\n",NameCommandMolcas);
1173  	fprintf(fd,"%d\n",molcasCommands.numberOfCommands);
1174 	for(i=0;i<molcasCommands.numberOfCommands;i++)
1175 	{
1176 		str_delete_n(molcasCommands.commands[i]);
1177 		delete_last_spaces(molcasCommands.commands[i]);
1178 		delete_first_spaces(molcasCommands.commands[i]);
1179 		fprintf(fd,"%s\n",molcasCommands.commands[i]);
1180 	}
1181 	fprintf(fd,"End\n");
1182 /*-----------------------------------------------------------------------------*/
1183 
1184 	fprintf(fd,"Begin Molpro\n");
1185 	str_delete_n(NameCommandMolpro);
1186 	delete_last_spaces(NameCommandMolpro);
1187 	delete_first_spaces(NameCommandMolpro);
1188  	fprintf(fd,"%s\n",NameCommandMolpro);
1189  	fprintf(fd,"%d\n",molproCommands.numberOfCommands);
1190 	for(i=0;i<molproCommands.numberOfCommands;i++)
1191 	{
1192 		str_delete_n(molproCommands.commands[i]);
1193 		delete_last_spaces(molproCommands.commands[i]);
1194 		delete_first_spaces(molproCommands.commands[i]);
1195 		fprintf(fd,"%s\n",molproCommands.commands[i]);
1196 	}
1197 	fprintf(fd,"End\n");
1198 /*-----------------------------------------------------------------------------*/
1199 
1200 	fprintf(fd,"Begin MPQC\n");
1201 	str_delete_n(NameCommandMPQC);
1202 	delete_last_spaces(NameCommandMPQC);
1203 	delete_first_spaces(NameCommandMPQC);
1204  	fprintf(fd,"%s\n",NameCommandMPQC);
1205  	fprintf(fd,"%d\n",mpqcCommands.numberOfCommands);
1206 	for(i=0;i<mpqcCommands.numberOfCommands;i++)
1207 	{
1208 		str_delete_n(mpqcCommands.commands[i]);
1209 		delete_last_spaces(mpqcCommands.commands[i]);
1210 		delete_first_spaces(mpqcCommands.commands[i]);
1211 		fprintf(fd,"%s\n",mpqcCommands.commands[i]);
1212 	}
1213 	fprintf(fd,"End\n");
1214 /*-----------------------------------------------------------------------------*/
1215 	fprintf(fd,"Begin NWChem\n");
1216 	str_delete_n(NameCommandNWChem);
1217 	delete_last_spaces(NameCommandNWChem);
1218 	delete_first_spaces(NameCommandNWChem);
1219  	fprintf(fd,"%s\n",NameCommandNWChem);
1220  	fprintf(fd,"%d\n",nwchemCommands.numberOfCommands);
1221 	for(i=0;i<nwchemCommands.numberOfCommands;i++)
1222 	{
1223 		str_delete_n(nwchemCommands.commands[i]);
1224 		delete_last_spaces(nwchemCommands.commands[i]);
1225 		delete_first_spaces(nwchemCommands.commands[i]);
1226 		fprintf(fd,"%s\n",nwchemCommands.commands[i]);
1227 	}
1228 	fprintf(fd,"End\n");
1229 /*-----------------------------------------------------------------------------*/
1230 	fprintf(fd,"Begin Psicode\n");
1231 	str_delete_n(NameCommandPsicode);
1232 	delete_last_spaces(NameCommandPsicode);
1233 	delete_first_spaces(NameCommandPsicode);
1234  	fprintf(fd,"%s\n",NameCommandPsicode);
1235  	fprintf(fd,"%d\n",psicodeCommands.numberOfCommands);
1236 	for(i=0;i<psicodeCommands.numberOfCommands;i++)
1237 	{
1238 		str_delete_n(psicodeCommands.commands[i]);
1239 		delete_last_spaces(psicodeCommands.commands[i]);
1240 		delete_first_spaces(psicodeCommands.commands[i]);
1241 		fprintf(fd,"%s\n",psicodeCommands.commands[i]);
1242 	}
1243 	fprintf(fd,"End\n");
1244 /*-----------------------------------------------------------------------------*/
1245 	fprintf(fd,"Begin Orca\n");
1246 	str_delete_n(NameCommandOrca);
1247 	delete_last_spaces(NameCommandOrca);
1248 	delete_first_spaces(NameCommandOrca);
1249  	fprintf(fd,"%s\n",NameCommandOrca);
1250  	fprintf(fd,"%d\n",orcaCommands.numberOfCommands);
1251 	for(i=0;i<orcaCommands.numberOfCommands;i++)
1252 	{
1253 		str_delete_n(orcaCommands.commands[i]);
1254 		delete_last_spaces(orcaCommands.commands[i]);
1255 		delete_first_spaces(orcaCommands.commands[i]);
1256 		fprintf(fd,"%s\n",orcaCommands.commands[i]);
1257 	}
1258 	fprintf(fd,"End\n");
1259 /*-----------------------------------------------------------------------------*/
1260 	fprintf(fd,"Begin FireFly\n");
1261 	str_delete_n(NameCommandFireFly);
1262 	delete_last_spaces(NameCommandFireFly);
1263 	delete_first_spaces(NameCommandFireFly);
1264  	fprintf(fd,"%s\n",NameCommandFireFly);
1265  	fprintf(fd,"%d\n",fireflyCommands.numberOfCommands);
1266 	for(i=0;i<fireflyCommands.numberOfCommands;i++)
1267 	{
1268 		str_delete_n(fireflyCommands.commands[i]);
1269 		delete_last_spaces(fireflyCommands.commands[i]);
1270 		delete_first_spaces(fireflyCommands.commands[i]);
1271 		fprintf(fd,"%s\n",fireflyCommands.commands[i]);
1272 	}
1273 	fprintf(fd,"End\n");
1274 /*-----------------------------------------------------------------------------*/
1275 	fprintf(fd,"Begin QChem\n");
1276 	str_delete_n(NameCommandQChem);
1277 	delete_last_spaces(NameCommandQChem);
1278 	delete_first_spaces(NameCommandQChem);
1279  	fprintf(fd,"%s\n",NameCommandQChem);
1280  	fprintf(fd,"%d\n",qchemCommands.numberOfCommands);
1281 	for(i=0;i<qchemCommands.numberOfCommands;i++)
1282 	{
1283 		str_delete_n(qchemCommands.commands[i]);
1284 		delete_last_spaces(qchemCommands.commands[i]);
1285 		delete_first_spaces(qchemCommands.commands[i]);
1286 		fprintf(fd,"%s\n",qchemCommands.commands[i]);
1287 	}
1288 	fprintf(fd,"End\n");
1289 /*-----------------------------------------------------------------------------*/
1290 	fprintf(fd,"Begin Mopac\n");
1291 	str_delete_n(NameCommandMopac);
1292 	delete_last_spaces(NameCommandMopac);
1293 	delete_first_spaces(NameCommandMopac);
1294  	fprintf(fd,"%s\n",NameCommandMopac);
1295  	fprintf(fd,"%d\n",mopacCommands.numberOfCommands);
1296 	for(i=0;i<mopacCommands.numberOfCommands;i++)
1297 	{
1298 		str_delete_n(mopacCommands.commands[i]);
1299 		delete_last_spaces(mopacCommands.commands[i]);
1300 		delete_first_spaces(mopacCommands.commands[i]);
1301 		fprintf(fd,"%s\n",mopacCommands.commands[i]);
1302 	}
1303 	fprintf(fd,"End\n");
1304 /*-----------------------------------------------------------------------------*/
1305 	fprintf(fd,"Begin PovRay\n");
1306 	str_delete_n(NameCommandPovray);
1307 	delete_last_spaces(NameCommandPovray);
1308 	delete_first_spaces(NameCommandPovray);
1309  	fprintf(fd,"%s\n",NameCommandPovray);
1310  	fprintf(fd,"%d\n",povrayCommands.numberOfCommands);
1311 	for(i=0;i<povrayCommands.numberOfCommands;i++)
1312 	{
1313 		str_delete_n(povrayCommands.commands[i]);
1314 		delete_last_spaces(povrayCommands.commands[i]);
1315 		delete_first_spaces(povrayCommands.commands[i]);
1316 		fprintf(fd,"%s\n",povrayCommands.commands[i]);
1317 	}
1318 	fprintf(fd,"End\n");
1319 /*-----------------------------------------------------------------------------*/
1320 	fprintf(fd,"Begin Babel\n");
1321 	str_delete_n(babelCommand);
1322 	delete_last_spaces(babelCommand);
1323 	delete_first_spaces(babelCommand);
1324 	fprintf(fd,"%s\n",babelCommand);
1325 	fprintf(fd,"End\n");
1326 
1327 	fprintf(fd,"Begin DemonDir\n");
1328 	str_delete_n(demonDirectory);
1329 	delete_last_spaces(demonDirectory);
1330 	delete_first_spaces(demonDirectory);
1331 	fprintf(fd,"%s\n",demonDirectory);
1332 	fprintf(fd,"End\n");
1333 
1334 	fprintf(fd,"Begin GamessDir\n");
1335 	str_delete_n(gamessDirectory);
1336 	delete_last_spaces(gamessDirectory);
1337 	delete_first_spaces(gamessDirectory);
1338 	fprintf(fd,"%s\n",gamessDirectory);
1339 	fprintf(fd,"End\n");
1340 
1341 	fprintf(fd,"Begin NWChemDir\n");
1342 	str_delete_n(nwchemDirectory);
1343 	delete_last_spaces(nwchemDirectory);
1344 	delete_first_spaces(nwchemDirectory);
1345 	fprintf(fd,"%s\n",nwchemDirectory);
1346 	fprintf(fd,"End\n");
1347 
1348 	fprintf(fd,"Begin PsicodeDir\n");
1349 	str_delete_n(psicodeDirectory);
1350 	delete_last_spaces(psicodeDirectory);
1351 	delete_first_spaces(psicodeDirectory);
1352 	fprintf(fd,"%s\n",psicodeDirectory);
1353 	fprintf(fd,"End\n");
1354 
1355 	fprintf(fd,"Begin OrcaDir\n");
1356 	str_delete_n(orcaDirectory);
1357 	delete_last_spaces(orcaDirectory);
1358 	delete_first_spaces(orcaDirectory);
1359 	fprintf(fd,"%s\n",orcaDirectory);
1360 	fprintf(fd,"End\n");
1361 
1362 	fprintf(fd,"Begin FireFlyDir\n");
1363 	str_delete_n(fireflyDirectory);
1364 	delete_last_spaces(fireflyDirectory);
1365 	delete_first_spaces(fireflyDirectory);
1366 	fprintf(fd,"%s\n",fireflyDirectory);
1367 	fprintf(fd,"End\n");
1368 
1369 	fprintf(fd,"Begin MopacDir\n");
1370 	str_delete_n(mopacDirectory);
1371 	delete_last_spaces(mopacDirectory);
1372 	delete_first_spaces(mopacDirectory);
1373 	fprintf(fd,"%s\n",mopacDirectory);
1374 	fprintf(fd,"End\n");
1375 
1376 	fprintf(fd,"Begin GaussDir\n");
1377 	str_delete_n(gaussDirectory);
1378 	delete_last_spaces(gaussDirectory);
1379 	delete_first_spaces(gaussDirectory);
1380 	fprintf(fd,"%s\n",gaussDirectory);
1381 	fprintf(fd,"End\n");
1382 
1383 	fprintf(fd,"Begin PovRayDir\n");
1384 	str_delete_n(povrayDirectory);
1385 	delete_last_spaces(povrayDirectory);
1386 	delete_first_spaces(povrayDirectory);
1387 	fprintf(fd,"%s\n",povrayDirectory);
1388 	fprintf(fd,"End\n");
1389 
1390 	fprintf(fd,"Begin OpenBabelDir\n");
1391 	str_delete_n(openbabelDirectory);
1392 	delete_last_spaces(openbabelDirectory);
1393 	delete_first_spaces(openbabelDirectory);
1394 	fprintf(fd,"%s\n",openbabelDirectory);
1395 	fprintf(fd,"End\n");
1396 
1397 
1398 	fclose(fd);
1399 
1400 	g_free(commandsfile);
1401 }
1402 /*************************************************************************************/
create_network_file()1403 void create_network_file()
1404 {
1405  gchar *networkfile;
1406  FILE *fd;
1407 
1408  networkfile = g_strdup_printf("%s%snetwork",gabedit_directory(),G_DIR_SEPARATOR_S);
1409 
1410  fd = FOpen(networkfile, "w");
1411 
1412  if(defaultNetWorkProtocol == GABEDIT_NETWORK_FTP_RSH) fprintf(fd,"0\n");
1413  else fprintf(fd,"1\n");
1414 
1415  fprintf(fd,"%s\n",pscpplinkDirectory);
1416  fclose(fd);
1417 
1418  g_free(networkfile);
1419 }
1420 /*********************************************************************************************/
get_alpha_opacity()1421 gdouble get_alpha_opacity()
1422 {
1423 	return alpha_opacity;
1424 }
1425 /*********************************************************************************************/
set_alpha_opacity(gdouble a)1426 void set_alpha_opacity(gdouble a)
1427 {
1428 	alpha_opacity = a;
1429 	if(alpha_opacity>1) alpha_opacity = 1;
1430 	if(alpha_opacity<0) alpha_opacity = 0;
1431 }
1432 /*************************************************************************************/
create_opengl_file()1433 void create_opengl_file()
1434 {
1435 	gchar *openglfile;
1436 	FILE *fd;
1437 
1438 	openglfile = g_strdup_printf("%s%sopengl",gabedit_directory(),G_DIR_SEPARATOR_S);
1439 
1440 	fd = FOpen(openglfile, "w");
1441 	if(fd !=NULL)
1442 	{
1443 		fprintf(fd,"%d\n",openGLOptions.activateText);
1444 		fprintf(fd,"%d\n",openGLOptions.rgba);
1445 		fprintf(fd,"%d\n",openGLOptions.doubleBuffer);
1446 		fprintf(fd,"%d\n",openGLOptions.alphaSize);
1447 		fprintf(fd,"%d\n",openGLOptions.depthSize);
1448 		fprintf(fd,"%d\n",openGLOptions.numberOfSubdivisionsCylindre);
1449 		fprintf(fd,"%d\n",openGLOptions.numberOfSubdivisionsSphere);
1450 		fprintf(fd,"%d\n",getOptCol());
1451 		fprintf(fd,"%lf %lf\n",getScaleBall(),getScaleStick());
1452 		fprintf(fd,"%d\n",colorMapType);
1453 		fprintf(fd,"%lf %lf %lf\n",colorMapColors[0][0], colorMapColors[0][1],colorMapColors[0][2]);
1454 		fprintf(fd,"%lf %lf %lf\n",colorMapColors[1][0], colorMapColors[1][1],colorMapColors[1][2]);
1455 		fprintf(fd,"%lf %lf %lf\n",colorMapColors[2][0], colorMapColors[2][1],colorMapColors[2][2]);
1456 		fprintf(fd,"%d\n",getShowOneSurface());
1457 		fprintf(fd,"%lf\n",get_alpha_opacity());
1458 		fclose(fd);
1459 	}
1460 	g_free(openglfile);
1461 }
1462 /*************************************************************************************/
read_opengl_file()1463 void read_opengl_file()
1464 {
1465 	gchar *openglfile;
1466 	FILE *fd;
1467 	gint optcol = 0;
1468 	gboolean showOneSurface = TRUE;
1469 
1470 	openglfile = g_strdup_printf("%s%sopengl",gabedit_directory(),G_DIR_SEPARATOR_S);
1471 
1472 	fd = fopen(openglfile, "rb");
1473 	openGLOptions.activateText = 1;
1474 	openGLOptions.rgba = 1;
1475 	openGLOptions.doubleBuffer = 1;
1476 	openGLOptions.alphaSize = 0;
1477 	openGLOptions.depthSize = 1;
1478 	openGLOptions.numberOfSubdivisionsCylindre = 20;
1479 	openGLOptions.numberOfSubdivisionsSphere = 30;
1480 	colorMapType =1;
1481 	colorMapColors[0][0] = 1;
1482 	colorMapColors[0][1] = 1;
1483 	colorMapColors[0][2] = 1;
1484 	colorMapColors[1][0] = 1;
1485 	colorMapColors[1][1] = 1;
1486 	colorMapColors[1][2] = 1;
1487 	colorMapColors[2][0] = 1;
1488 	colorMapColors[2][1] = 1;
1489 	colorMapColors[2][2] = 1;
1490 	if(fd !=NULL)
1491 	{
1492  		guint taille = BBSIZE;
1493  		gchar t[BBSIZE];
1494  		if(fgets(t,taille,fd))
1495 			if(sscanf(t,"%d",&openGLOptions.activateText)!=1)
1496 				openGLOptions.activateText = 1;
1497  		if(fgets(t,taille,fd))
1498 			if(sscanf(t,"%d",&openGLOptions.rgba)!=1)
1499 				openGLOptions.rgba = 1;
1500  		if(fgets(t,taille,fd))
1501 			if(sscanf(t,"%d",&openGLOptions.doubleBuffer)!=1)
1502 				openGLOptions.doubleBuffer = 1;
1503  		if(fgets(t,taille,fd))
1504 			if(sscanf(t,"%d",&openGLOptions.alphaSize)!=1)
1505 				openGLOptions.alphaSize = 1;
1506  		if(fgets(t,taille,fd))
1507 			if(sscanf(t,"%d",&openGLOptions.depthSize)!=1)
1508 				openGLOptions.depthSize = 1;
1509  		if(fgets(t,taille,fd))
1510 			if(sscanf(t,"%d",&openGLOptions.numberOfSubdivisionsCylindre)!=1)
1511 				openGLOptions.numberOfSubdivisionsCylindre = 10;
1512  		if(fgets(t,taille,fd))
1513 			if(sscanf(t,"%d",&openGLOptions.numberOfSubdivisionsSphere)!=1)
1514 				openGLOptions.numberOfSubdivisionsSphere = 10;
1515  		if(fgets(t,taille,fd))
1516 			if(sscanf(t,"%d",&optcol)!=1) optcol = 0;
1517 		setOptCol(optcol);
1518  		if(fgets(t,taille,fd))
1519 		{
1520 			gdouble b,s;
1521 			if(sscanf(t,"%lf %lf",&b,&s)==2)
1522 			{
1523 				setScaleBall(b);
1524 				setScaleStick(b);
1525 			}
1526 		}
1527  		if(fgets(t,taille,fd))
1528 			if(sscanf(t,"%d",&colorMapType)!=1) colorMapType =1;
1529  		if(fgets(t,taille,fd))
1530 		{
1531 			if(sscanf(t,"%lf %lf %lf",&colorMapColors[0][0], &colorMapColors[0][1],&colorMapColors[0][2])!=3)
1532 			{
1533 				colorMapColors[0][0] = 1.0;
1534 				colorMapColors[0][1] = 1.0;
1535 				colorMapColors[0][2] = 1.0;
1536 			}
1537 		}
1538  		if(fgets(t,taille,fd))
1539 		{
1540 			if(sscanf(t,"%lf %lf %lf",&colorMapColors[1][0], &colorMapColors[1][1],&colorMapColors[1][2])!=3)
1541 			{
1542 				colorMapColors[1][0] = 1.0;
1543 				colorMapColors[1][1] = 1.0;
1544 				colorMapColors[1][2] = 1.0;
1545 			}
1546 		}
1547  		if(fgets(t,taille,fd))
1548 		{
1549 			if(sscanf(t,"%lf %lf %lf",&colorMapColors[2][0], &colorMapColors[2][1],&colorMapColors[2][2])!=3)
1550 			{
1551 				colorMapColors[2][0] = 1.0;
1552 				colorMapColors[2][1] = 1.0;
1553 				colorMapColors[2][2] = 1.0;
1554 			}
1555 		}
1556  		if(fgets(t,taille,fd))
1557 			if(sscanf(t,"%d",&showOneSurface)!=1) showOneSurface = 0;
1558 		setShowOneSurface(showOneSurface);
1559  		if(fgets(t,taille,fd))
1560 		{
1561 			gdouble alpha;
1562 			if(sscanf(t,"%lf",&alpha)==1) set_alpha_opacity(alpha);
1563 		}
1564 
1565 		fclose(fd);
1566 	}
1567 	g_free(openglfile);
1568 }
1569 /*************************************************************************************/
create_ressource_file()1570 void create_ressource_file()
1571 {
1572 	save_atoms_prop();
1573 	create_commands_file();
1574 	create_network_file();
1575 	create_fonts_file();
1576 	create_color_surfaces_file();
1577 	create_opengl_file();
1578 	save_axis_properties();
1579 #ifdef DRAWGEOMGL
1580 	save_axes_geom_properties();
1581 #endif
1582 	save_principal_axis_properties();
1583 	save_HBonds_properties();
1584 	create_drawmolecule_file();
1585 }
1586 /*************************************************************************************/
read_hosts_file()1587 void read_hosts_file()
1588 {
1589  gchar *hostsfile;
1590  FILE *fd;
1591  gint i;
1592  gint j;
1593  gint k;
1594  gchar t[BBSIZE];
1595  gint len = BBSIZE;
1596 
1597  hostsfile = g_strdup_printf("%s%shosts",gabedit_directory(),G_DIR_SEPARATOR_S);
1598 
1599  fd = FOpen(hostsfile, "rb");
1600  if(fd)
1601  {
1602     	if(!feof(fd)) { char* e = fgets(t,len,fd);}
1603 	recenthosts.nhosts = atoi(t);
1604 	recenthosts.hosts = g_malloc(recenthosts.nhosts*sizeof(Host));
1605 	for(i=0;i<recenthosts.nhosts;i++)
1606 	{
1607 		filegets(t,fd);recenthosts.hosts[i].hostname = g_strdup(t);
1608     		if(!feof(fd)) { char* e = fgets(t,len,fd);}
1609 		recenthosts.hosts[i].nusers = atoi(t);
1610 		recenthosts.hosts[i].users = g_malloc(recenthosts.hosts[i].nusers*sizeof(User));
1611 		for(j=0;j<recenthosts.hosts[i].nusers;j++)
1612 		{
1613 			filegets(t,fd);
1614 				recenthosts.hosts[i].users[j].username = g_strdup(t);
1615 				recenthosts.hosts[i].users[j].password = NULL;
1616     			if(!feof(fd)) { char* e = fgets(t,len,fd);}
1617 			recenthosts.hosts[i].users[j].ndirs = atoi(t);
1618 			recenthosts.hosts[i].users[j].dirs = g_malloc(recenthosts.hosts[i].users[j].ndirs*sizeof(gchar*));
1619 			for(k=0;k<recenthosts.hosts[i].users[j].ndirs;k++)
1620 			{
1621 				filegets(t,fd);recenthosts.hosts[i].users[j].dirs[k] = g_strdup(t);
1622 			}
1623 		}
1624 	}
1625  	fclose(fd);
1626  }
1627 
1628  g_free(hostsfile);
1629 }
1630 /*************************************************************************************/
read_fonts_in_file(FILE * fd,FontsStyle * fontsstyle)1631 void read_fonts_in_file(FILE *fd,FontsStyle* fontsstyle)
1632 {
1633 	guint taille = BBSIZE;
1634 	gchar *t = NULL;
1635 	gchar *temp = NULL;
1636 	gint i;
1637 	gint k;
1638 
1639 	t = g_malloc0(taille*sizeof(gchar));
1640 	temp = g_malloc0(taille*sizeof(gchar));
1641     	if(!feof(fd)) { char* e = fgets(t,taille,fd);}
1642 
1643 	k = 0;
1644 	for(i=0;i<(gint)taille;i++)
1645 	{
1646 		if(t[i] =='\n')
1647 		break;
1648 		temp[k++] = t[i];
1649 	}
1650 	temp[k] = '\0';
1651 	g_strchug(temp);
1652 	g_strchomp(temp);
1653 
1654 	fontsstyle->fontname= g_strdup(temp);
1655 
1656     	if(!feof(fd)) { char* e = fgets(t,taille,fd);}
1657 	fontsstyle->BaseColor.red =(gushort) atoi(t);
1658     	if(!feof(fd)) { char* e = fgets(t,taille,fd);}
1659 	fontsstyle->BaseColor.green =(gushort)  atoi(t);
1660     	if(!feof(fd)) { char* e = fgets(t,taille,fd);}
1661 	fontsstyle->BaseColor.blue = (gushort) atoi(t);
1662 
1663     	if(!feof(fd)) { char* e = fgets(t,taille,fd);}
1664 	fontsstyle->TextColor.red = (gushort) atoi(t);
1665     	if(!feof(fd)) { char* e = fgets(t,taille,fd);}
1666 	fontsstyle->TextColor.green = (gushort) atoi(t);
1667     	if(!feof(fd)) { char* e = fgets(t,taille,fd);}
1668 	fontsstyle->TextColor.blue = (gushort) atoi(t);
1669 	g_free(t);
1670 	g_free(temp);
1671 }
1672 /*************************************************************************************/
read_fonts_file()1673 void read_fonts_file()
1674 {
1675  gchar *fontsfile;
1676  FILE *fd;
1677 
1678  fontsfile = g_strdup_printf("%s%sfonts",gabedit_directory(),G_DIR_SEPARATOR_S);
1679 
1680  fd = FOpen(fontsfile, "rb");
1681  if(fd !=NULL)
1682  {
1683  	read_fonts_in_file(fd,&FontsStyleData);
1684  	read_fonts_in_file(fd,&FontsStyleResult);
1685  	read_fonts_in_file(fd,&FontsStyleLabel);
1686  	read_fonts_in_file(fd,&FontsStyleOther);
1687  	fclose(fd);
1688 
1689   	set_font (text,FontsStyleData.fontname);
1690   	set_base_style(text,FontsStyleData.BaseColor.red ,FontsStyleData.BaseColor.green ,FontsStyleData.BaseColor.blue);
1691   	set_text_style(text,FontsStyleData.TextColor.red ,FontsStyleData.TextColor.green ,FontsStyleData.TextColor.blue);
1692 
1693   	set_font (textresult,FontsStyleResult.fontname);
1694   	set_base_style(textresult,FontsStyleResult.BaseColor.red ,FontsStyleResult.BaseColor.green ,FontsStyleResult.BaseColor.blue);
1695   	set_text_style(textresult,FontsStyleResult.TextColor.red ,FontsStyleResult.TextColor.green ,FontsStyleResult.TextColor.blue);
1696 
1697   	set_font (TextOutput,FontsStyleResult.fontname);
1698   	set_base_style(TextOutput,FontsStyleResult.BaseColor.red ,FontsStyleResult.BaseColor.green ,FontsStyleResult.BaseColor.blue);
1699   	set_text_style(TextOutput,FontsStyleResult.TextColor.red ,FontsStyleResult.TextColor.green ,FontsStyleResult.TextColor.blue);
1700 
1701   	set_font (TextError,FontsStyleResult.fontname);
1702   	set_base_style(TextError,FontsStyleResult.BaseColor.red ,FontsStyleResult.BaseColor.green ,FontsStyleResult.BaseColor.blue);
1703   	set_text_style(TextError,FontsStyleResult.TextColor.red ,FontsStyleResult.TextColor.green ,FontsStyleResult.TextColor.blue);
1704 
1705  }
1706  set_font_other (FontsStyleOther.fontname);
1707 
1708 }
1709 /********************************************************************************/
free_batch_commands()1710 void free_batch_commands()
1711 {
1712 	gint i;
1713 	for(i=0;i<batchCommands.numberOfTypes;i++)
1714 	{
1715 		if(batchCommands.types[i])
1716 			g_free(batchCommands.types[i]);
1717 		if(batchCommands.commandListAll[i])
1718 			g_free(batchCommands.commandListAll[i]);
1719 		if(batchCommands.commandListUser[i])
1720 			g_free(batchCommands.commandListUser[i]);
1721 		if(batchCommands.jobIdTitle[i])
1722 			g_free(batchCommands.jobIdTitle[i]);
1723 
1724 	}
1725 	if(batchCommands.types)
1726 		g_free(batchCommands.types);
1727 	if(batchCommands.commandListAll)
1728 		g_free(batchCommands.commandListAll);
1729 	if(batchCommands.commandListUser)
1730 		g_free(batchCommands.commandListUser);
1731 	if(batchCommands.jobIdTitle)
1732 		g_free(batchCommands.jobIdTitle);
1733 
1734 	if(NameTypeBatch)
1735 		g_free(NameTypeBatch);
1736 	if(NameCommandBatchAll)
1737 		g_free(NameCommandBatchAll);
1738 	if(NameCommandBatchUser)
1739 		g_free(NameCommandBatchUser);
1740 	if(NamejobIdTitleBatch)
1741 		g_free(NamejobIdTitleBatch);
1742 }
1743 /*************************************************************************************/
read_commands_file()1744 void read_commands_file()
1745 {
1746  guint taille = BBSIZE;
1747  gchar t[BBSIZE];
1748  gchar *commandsfile;
1749  FILE *fd;
1750  gint i;
1751  gint k;
1752  gchar *tmp[4] = {NULL,NULL,NULL,NULL};
1753 
1754  commandsfile = g_strdup_printf("%s%scommands",gabedit_directory(),G_DIR_SEPARATOR_S);
1755 
1756  fd = FOpen(commandsfile, "rb");
1757  if(fd !=NULL)
1758  {
1759 
1760  	if(fgets(t,taille,fd))
1761 	if(!strstr(t,"Begin Batch"))
1762 	{
1763 		fclose(fd);
1764 		return;
1765 	}
1766  	if(fgets(t,taille,fd))
1767 	{
1768 		free_batch_commands();
1769 
1770  		NameTypeBatch= g_strdup(t);
1771 		str_delete_n(NameTypeBatch);
1772 
1773 		for(k=0;k<4;k++)
1774 		if(fgets(t,taille,fd))
1775 		{
1776 			tmp[k] = g_strdup(t);
1777 			str_delete_n(tmp[k]);
1778 		}
1779 		else
1780 		{
1781 			fclose(fd);
1782 			initialise_batch_commands();
1783 			return;
1784 		}
1785 
1786 		NameCommandBatchAll  = tmp[0];
1787 		NameCommandBatchUser = tmp[1];
1788 		NameCommandBatchKill = tmp[2];
1789 		NamejobIdTitleBatch  = tmp[3];
1790 	}
1791 	else
1792 	{
1793 		fclose(fd);
1794 		initialise_batch_commands();
1795 		return;
1796 	}
1797 
1798 
1799  	if(fgets(t,taille,fd) && atoi(t)>0)
1800 	{
1801 		batchCommands.numberOfTypes = atoi(t);
1802 		batchCommands.types = g_malloc(batchCommands.numberOfTypes*sizeof(gchar*));
1803 		batchCommands.commandListAll = g_malloc(batchCommands.numberOfTypes*sizeof(gchar*));
1804 		batchCommands.commandListUser = g_malloc(batchCommands.numberOfTypes*sizeof(gchar*));
1805 		batchCommands.jobIdTitle = g_malloc(batchCommands.numberOfTypes*sizeof(gchar*));
1806 
1807 		for(i=0;i<batchCommands.numberOfTypes;i++)
1808 		{
1809 			batchCommands.types[i] = g_strdup(" ");
1810 			batchCommands.commandListAll[i] = g_strdup(" ");
1811 			batchCommands.commandListUser[i] = g_strdup(" ");
1812 			batchCommands.jobIdTitle[i] = g_strdup(" ");
1813 		}
1814 		for(i=0;i<batchCommands.numberOfTypes;i++)
1815 		{
1816 			if(!fgets(t,taille,fd) || strstr(t,"End"))
1817 			{
1818 				free_batch_commands();
1819 				fclose(fd);
1820 				initialise_batch_commands();
1821 				return;
1822 			}
1823 			else
1824 			{
1825 
1826  				batchCommands.types[i]= g_strdup(t);
1827 				str_delete_n(batchCommands.types[i]);
1828 
1829 				for(k=0;k<4;k++)
1830 				if(!fgets(t,taille,fd) || strstr(t,"End"))
1831 				{
1832 					free_batch_commands();
1833 					fclose(fd);
1834 					initialise_batch_commands();
1835 					return;
1836 				}
1837 				else
1838 				{
1839 					tmp[k] = g_strdup(t);
1840 					str_delete_n(tmp[k]);
1841 				}
1842 				batchCommands.commandListAll[i]  = tmp[0];
1843 				batchCommands.commandListUser[i] = tmp[1];
1844 				batchCommands.commandKill[i] = tmp[2];
1845 				batchCommands.jobIdTitle[i]  = tmp[3];
1846 
1847 			}
1848 		}
1849 	}
1850 	else
1851 	{
1852 		fclose(fd);
1853 		initialise_batch_commands();
1854 		return;
1855 	}
1856  	if(!fgets(t,taille,fd)) /* End of Batch */
1857 	{
1858 		fclose(fd);
1859 		initialise_batch_commands();
1860 		return;
1861 	}
1862 /*-----------------------------------------------------------------------------*/
1863 	readCommandsOneSoft(fd, "Begin DeMon", &NameCommandDeMon, &demonCommands, free_demon_commands, "default");
1864 	readCommandsOneSoft(fd, "Begin Gamess", &NameCommandGamess, &gamessCommands, free_gamess_commands, "nohup runGamess");
1865 	readCommandsOneSoft(fd, "Begin Gaussian", &NameCommandGaussian, &gaussianCommands, free_gaussian_commands, "nohup g09");
1866 	readCommandsOneSoft(fd, "Begin Molcas", &NameCommandMolcas, &molcasCommands, free_molcas_commands, "nohup runMolcas");
1867 	readCommandsOneSoft(fd, "Begin Molpro", &NameCommandMolpro, &molproCommands, free_molpro_commands, "nohup runMolpro");
1868 	readCommandsOneSoft(fd, "Begin MPQC", &NameCommandMPQC, &mpqcCommands, free_mpqc_commands, "nohup mpqc");
1869 	readCommandsOneSoft(fd, "Begin NWChem", &NameCommandNWChem, &nwchemCommands, free_nwchem_commands, "nohup nwchem");
1870 	readCommandsOneSoft(fd, "Begin Psicode", &NameCommandPsicode, &psicodeCommands, free_psicode_commands, "nohup psicode");
1871 	readCommandsOneSoft(fd, "Begin Orca", &NameCommandOrca, &orcaCommands, free_orca_commands, "nohup orca");
1872 	readCommandsOneSoft(fd, "Begin FireFly", &NameCommandFireFly, &fireflyCommands, free_firefly_commands, "nohup firefly");
1873 	readCommandsOneSoft(fd, "Begin QChem", &NameCommandQChem, &qchemCommands, free_qchem_commands, "nohup qchem");
1874 	readCommandsOneSoft(fd, "Begin Mopac", &NameCommandMopac, &mopacCommands, free_mopac_commands, "nohup mopac");
1875 	readCommandsOneSoft(fd, "Begin PovRay", &NameCommandPovray, &povrayCommands, free_povray_commands, "povray +A0.3 -UV");
1876 	readCommandsOneSoft(fd, "Begin Babel", &babelCommand, NULL, NULL, "NULL");
1877 /*-----------------------------------------------------------------------------*/
1878 	readOneDir(fd, "Begin DemonDir", &demonDirectory);
1879 	readOneDir(fd, "Begin GamessDir", &gamessDirectory);
1880 	readOneDir(fd, "Begin NWChemDir", &nwchemDirectory);
1881 	readOneDir(fd, "Begin PsicodeDir", &psicodeDirectory);
1882 	readOneDir(fd, "Begin OrcaDir", &orcaDirectory);
1883 	readOneDir(fd, "Begin FireFlyDir", &fireflyDirectory);
1884 	readOneDir(fd, "Begin MopacDir", &mopacDirectory);
1885 	readOneDir(fd, "Begin GaussDir", &gaussDirectory);
1886 	readOneDir(fd, "Begin PovRayDir", &povrayDirectory);
1887 	readOneDir(fd, "Begin OpenBabelDir", &openbabelDirectory);
1888 /*-----------------------------------------------------------------------------*/
1889  }
1890 }
1891 /*************************************************************************************/
read_network_file()1892 void read_network_file()
1893 {
1894  gchar *networkfile;
1895  FILE *fd;
1896 
1897  networkfile = g_strdup_printf("%s%snetwork",gabedit_directory(),G_DIR_SEPARATOR_S);
1898 
1899  fd = FOpen(networkfile, "rb");
1900  if(fd !=NULL)
1901  {
1902  	guint taille = BBSIZE;
1903  	gchar t[BBSIZE];
1904 	gint i;
1905  	if(fgets(t,taille,fd))
1906 	{
1907 		if(sscanf(t,"%d",&i)!=1)
1908 			defaultNetWorkProtocol = GABEDIT_NETWORK_SSH;
1909 		else
1910 		{
1911  			if(i==0)
1912 				defaultNetWorkProtocol = GABEDIT_NETWORK_FTP_RSH;
1913  			else
1914 				defaultNetWorkProtocol = GABEDIT_NETWORK_SSH;
1915 		}
1916 	}
1917 	else
1918 		defaultNetWorkProtocol = GABEDIT_NETWORK_SSH;
1919 
1920  	if(fgets(t,taille,fd))
1921 	{
1922 		if(pscpplinkDirectory)
1923 			g_free(pscpplinkDirectory);
1924 		pscpplinkDirectory = g_strdup(t);
1925 
1926 		str_delete_n(pscpplinkDirectory);
1927 		delete_last_spaces(pscpplinkDirectory);
1928 		delete_first_spaces(pscpplinkDirectory);
1929 		sprintf(t,"%s;%s",pscpplinkDirectory,g_getenv("PATH"));
1930 #ifdef G_OS_WIN32
1931 		g_setenv("PATH",t,TRUE);
1932 #endif
1933 	}
1934  	fclose(fd);
1935  }
1936 }
1937 /***********************************************************************/
set_path()1938 void set_path()
1939 {
1940 #ifdef G_OS_WIN32
1941 	{
1942 		gchar t[BBSIZE];
1943 		sprintf(t,"%s;%s;%s;%s;%s;%s;%s;%s;%s",
1944 		orcaDirectory,
1945 		fireflyDirectory,
1946 		mopacDirectory,
1947 		gaussDirectory,
1948 		demonDirectory,
1949 		pscpplinkDirectory,
1950 		povrayDirectory,
1951 		openbabelDirectory,
1952 		g_getenv("PATH"));
1953 		if(strlen(t)>1) g_setenv("PATH",t,TRUE);
1954 	}
1955 #endif
1956 }
1957 /*************************************************************************************/
read_ressource_file()1958 void read_ressource_file()
1959 {
1960  gboolean rOK = FALSE;
1961 
1962  define_default_atoms_prop();
1963  rOK = read_atoms_prop();
1964  if(!rOK)
1965  	define_default_atoms_prop();
1966  read_commands_file();
1967  read_network_file();
1968  read_fonts_file();
1969  read_hosts_file();
1970  read_color_surfaces_file();
1971  read_opengl_file();
1972  fileopen.netWorkProtocol= defaultNetWorkProtocol;
1973  read_axis_properties();
1974 #ifdef DRAWGEOMGL
1975  read_axes_geom_properties();
1976 #endif
1977  read_principal_axis_properties();
1978  read_HBonds_properties();
1979  read_drawmolecule_file();
1980 }
1981 /*************************************************************************************/
ang_to_bohr(gchar * angstr)1982 gchar *ang_to_bohr(gchar *angstr)
1983 {
1984         gchar *austr;
1985         gdouble numb;
1986 
1987         austr = g_strdup(angstr);
1988         numb = atof(angstr)*ANG_TO_BOHR;
1989         austr = g_strdup_printf("%0.20lf",numb);
1990 	return austr;
1991 }
1992 /*************************************************************************************/
bohr_to_ang(gchar * angstr)1993 gchar *bohr_to_ang(gchar *angstr)
1994 {
1995         gchar *austr;
1996         gdouble numb;
1997 
1998         austr = g_strdup(angstr);
1999         numb = atof(angstr)*BOHR_TO_ANG;
2000         austr = g_strdup_printf("%0.20lf",numb);
2001 	return austr;
2002 }
2003 /*************************************************************************************/
debug1flag()2004 static gboolean debug1flag()
2005 {
2006    gchar localhost[100];
2007    if(!Debug1Flag) return FALSE;
2008 
2009 #ifdef G_OS_WIN32
2010    winsockCheck(stderr);
2011 #endif
2012    gethostname(localhost,100);
2013    if(strlen(localhost)>=5)
2014    {
2015 	   uppercase(localhost);
2016 	   gchar* d = strstr(localhost,"L");
2017 	   if(!d) return FALSE;
2018    	   if(strlen(d)<5) return FALSE;
2019 	   if(d[0]=='L' && d[1]=='A')
2020 	   if(d[2]=='S' && d[3]=='I')
2021 	   if(d[4]=='M') return TRUE;
2022    }
2023    return FALSE;
2024 }
2025 /*************************************************************************************/
get_number_electrons(guint type)2026 guint get_number_electrons(guint type)
2027 {
2028 /*
2029    type = 1 : Medium and High
2030    type = 2 : High
2031    type = other : All
2032 */
2033    guint i;
2034    guint Ne=0;
2035    SAtomsProp Atom;
2036    if(MethodeGeom == GEOM_IS_XYZ)
2037    {
2038    	for(i=0;i<NcentersXYZ;i++)
2039    	{
2040 	       Atom = prop_atom_get(GeomXYZ[i].Symb);
2041                switch (type)
2042                {
2043         	case 1 : if(this_is_a_backspace (GeomXYZ[i].Layer) ||
2044 			    !strcmp(GeomXYZ[i].Layer,"High") ||
2045 			    !strcmp(GeomXYZ[i].Layer,"Medium") )
2046 				Ne += Atom.atomicNumber;
2047 			 break;
2048         	case 2 : if(this_is_a_backspace (GeomXYZ[i].Layer) ||
2049 			    !strcmp(GeomXYZ[i].Layer,"High") )
2050 				 {
2051 				Ne += Atom.atomicNumber;
2052 				 }
2053 			 break;
2054         	default : Ne += Atom.atomicNumber;
2055                }
2056    	}
2057    }
2058    if(MethodeGeom == GEOM_IS_ZMAT)
2059    {
2060    	for(i=0;i<NcentersZmat;i++)
2061    	{
2062 		Atom = prop_atom_get(Geom[i].Symb);
2063                switch (type)
2064                {
2065         	case 1 : if(this_is_a_backspace (Geom[i].Layer) ||
2066 			    !strcmp(Geom[i].Layer,"High") ||
2067 			    !strcmp(Geom[i].Layer,"Medium") )
2068 				Ne += Atom.atomicNumber;
2069 			 break;
2070         	case 2 : if(this_is_a_backspace (Geom[i].Layer) ||
2071 			    !strcmp(Geom[i].Layer,"High") )
2072 		        	 Ne += Atom.atomicNumber;
2073 			 break;
2074         	default : Ne += Atom.atomicNumber;
2075                }
2076    	}
2077    }
2078    return Ne;
2079 }
2080 /*************************************************************************************/
get_value_variableZmat(gchar * NameV)2081 gdouble get_value_variableZmat(gchar *NameV)
2082 {
2083    guint i;
2084    for(i=0;i<NVariables;i++)
2085  	if (!strcmp((char*)NameV, Variables[i].Name))
2086 		return atof(Variables[i].Value);
2087 
2088   return 0.0;
2089 }
2090 /*************************************************************************************/
get_value_variableXYZ(gchar * NameV)2091 gdouble get_value_variableXYZ(gchar *NameV)
2092 {
2093    guint i;
2094    for(i=0;i<NVariablesXYZ;i++)
2095  	if (!strcmp((char*)NameV, VariablesXYZ[i].Name))
2096 		return atof(VariablesXYZ[i].Value);
2097 
2098   return 0.0;
2099 }
2100 /*************************************************************************************/
get_num_variableXYZ(gchar * NameV)2101 guint get_num_variableXYZ(gchar *NameV)
2102 {
2103    guint i;
2104    for(i=0;i<NVariablesXYZ;i++)
2105  	if (!strcmp((char*)NameV, VariablesXYZ[i].Name))
2106 		return i;
2107 
2108   return 0;
2109 }
2110 /*************************************************************************************/
get_num_variableZmat(gchar * NameV)2111 guint get_num_variableZmat(gchar *NameV)
2112 {
2113    guint i;
2114    for(i=0;i<NVariables;i++)
2115  	if (!strcmp((char*)NameV, Variables[i].Name))
2116 		return i;
2117 
2118   return 0;
2119 }
2120 /*************************************************************************************/
geometry_with_medium_layer()2121 gboolean geometry_with_medium_layer()
2122 {
2123 
2124    gint i;
2125 
2126    if(debug1flag()) return FALSE;
2127 
2128    if(MethodeGeom == GEOM_IS_XYZ)
2129    {
2130    	for(i=0;i<NcentersXYZ;i++)
2131 		if(strstr(GeomXYZ[i].Layer,"Med") ) return TRUE;
2132    }
2133    else if(MethodeGeom == GEOM_IS_ZMAT)
2134    {
2135    	for(i=0;i<NcentersZmat;i++)
2136 		if(strstr(Geom[i].Layer,"Med") ) return TRUE;
2137    }
2138    return FALSE;
2139 }
2140 /*************************************************************************************/
geometry_with_lower_layer()2141 gboolean geometry_with_lower_layer()
2142 {
2143    gint i;
2144 
2145    if(debug1flag()) return FALSE;
2146 
2147    if(MethodeGeom == GEOM_IS_XYZ)
2148    {
2149    	for(i=0;i<NcentersXYZ;i++)
2150 		if(strstr(GeomXYZ[i].Layer,"Lo") ) return TRUE;
2151    }
2152    else if(MethodeGeom == GEOM_IS_ZMAT)
2153    {
2154    	for(i=0;i<NcentersZmat;i++)
2155 		if(strstr(Geom[i].Layer,"Lo") ) return TRUE;
2156    }
2157    return FALSE;
2158 }
2159 /*************************************************************************************/
uppercase(gchar * str)2160 void uppercase(gchar *str)
2161 {
2162   while( *str != '\0')
2163   {
2164     if (isalpha((gint)*str))
2165       if (islower((gint)*str))
2166         *str = toupper((gint)*str);
2167     str ++;
2168   }
2169 }
2170 /*************************************************************************************/
lowercase(gchar * str)2171 void lowercase(gchar *str)
2172 {
2173   while( *str != '\0')
2174   {
2175     *str = (gchar)tolower((gint)*str);
2176     str ++;
2177   }
2178 }
2179 #ifdef G_OS_WIN32
reset_fonts(gchar * fname)2180 PangoFontDescription *reset_fonts(gchar* fname)
2181 {
2182 	if(FontsStyleOther.fontname) g_free(FontsStyleOther.fontname);
2183 	FontsStyleOther.fontname = g_strdup(fname);
2184        	if(FontsStyleData.fontname)g_free(FontsStyleData.fontname);
2185        	FontsStyleData.fontname = g_strdup(fname);
2186        	if(FontsStyleResult.fontname) g_free(FontsStyleResult.fontname);
2187        	FontsStyleResult.fontname = g_strdup(fname);
2188   	return pango_font_description_from_string (fname);
2189 }
2190 #endif
2191 /*************************************************************************************/
initialise_fonts_style()2192 void initialise_fonts_style()
2193 {
2194 #ifdef G_OS_WIN32
2195         FontsStyleData.fontname = NULL;
2196         FontsStyleResult.fontname = NULL;
2197 	FontsStyleOther.fontname = NULL;
2198 	/*if(!reset_fonts("courier 12"))*/
2199 	if(!reset_fonts("monospace 12"))
2200 	if(!reset_fonts("sans 12"))
2201 	reset_fonts("helvetica 12");
2202 	FontsStyleLabel.fontname=g_strdup("sans bold 12");
2203 	FontsStyleOther.fontname = g_strdup("sans 12");
2204 #else
2205         FontsStyleData.fontname = g_strdup("Sans 12");
2206         FontsStyleResult.fontname = g_strdup("Sans 12");
2207 	FontsStyleOther.fontname = g_strdup("helvetica 12");
2208 	FontsStyleLabel.fontname=g_strdup("courier bold 12");
2209 #endif
2210 
2211         FontsStyleData.BaseColor.red  = 65535;
2212         FontsStyleData.BaseColor.green  = 65535;
2213         FontsStyleData.BaseColor.blue  = 65535;
2214 
2215         FontsStyleData.TextColor.red  = 0;
2216         FontsStyleData.TextColor.green  = 0;
2217         FontsStyleData.TextColor.blue  = 0;
2218 
2219         FontsStyleResult.BaseColor.red  = 58980;
2220         FontsStyleResult.BaseColor.green  = 58980;
2221         FontsStyleResult.BaseColor.blue  = 58980;
2222 
2223         FontsStyleResult.TextColor.red  = 32768;
2224         FontsStyleResult.TextColor.green  = 0;
2225         FontsStyleResult.TextColor.blue  = 0;
2226 
2227         FontsStyleLabel.BaseColor.red  = 0;
2228         FontsStyleLabel.BaseColor.green  = 0;
2229         FontsStyleLabel.BaseColor.blue  = 0;
2230 
2231         FontsStyleLabel.TextColor.red  = 65535;
2232         FontsStyleLabel.TextColor.green  = 65535;
2233         FontsStyleLabel.TextColor.blue  = 65535;
2234 
2235 	FontsStyleOther.BaseColor.red  = 58980;
2236         FontsStyleOther.BaseColor.green  = 58980;
2237         FontsStyleOther.BaseColor.blue  = 58980;
2238 
2239         FontsStyleOther.TextColor.red  = 32768;
2240         FontsStyleOther.TextColor.green  = 0;
2241         FontsStyleOther.TextColor.blue  = 0;
2242 }
2243 /*************************************************************************************/
reset_name_files()2244 void reset_name_files()
2245 {
2246 	if(fileopen.projectname) g_free(fileopen.projectname);
2247 	if(fileopen.datafile) g_free(fileopen.datafile);
2248 	if(fileopen.outputfile) g_free(fileopen.outputfile);
2249 	if(fileopen.logfile) g_free(fileopen.logfile);
2250 	if(fileopen.moldenfile) g_free(fileopen.moldenfile);
2251 	if(fileopen.remotehost) g_free(fileopen.remotehost);
2252 	if(fileopen.remoteuser) g_free(fileopen.remoteuser);
2253 	if(fileopen.remotepass) g_free(fileopen.remotepass);
2254 	if(fileopen.remotedir) g_free(fileopen.remotedir);
2255 
2256 	fileopen.projectname=g_strdup("NoName");
2257 	fileopen.datafile=g_strdup("NoName");
2258 	fileopen.outputfile=g_strdup("Unknown");
2259 	fileopen.logfile=g_strdup("Unknown");
2260 	fileopen.moldenfile=g_strdup("Unknown");
2261 	fileopen.remotehost=g_strdup("");
2262 	fileopen.remoteuser=g_strdup("");
2263 	fileopen.remotepass=g_strdup("");
2264 	fileopen.remotedir=g_strdup("");
2265 	fileopen.netWorkProtocol= defaultNetWorkProtocol;
2266 }
2267 /*************************************************************************************/
initialise_name_file()2268 void initialise_name_file()
2269 {
2270 	fileopen.projectname=g_strdup("NoName");
2271 	fileopen.datafile=g_strdup("NoName");
2272 	fileopen.outputfile=g_strdup("Unknown");
2273 	fileopen.logfile=g_strdup("Unknown");
2274 	fileopen.moldenfile=g_strdup("Unknown");
2275 	fileopen.remotehost=g_strdup("");
2276 	fileopen.remoteuser=g_strdup("");
2277 	fileopen.remotepass=g_strdup("");
2278 	fileopen.remotedir=g_strdup("");
2279 	fileopen.netWorkProtocol= defaultNetWorkProtocol;
2280 }
2281 /*************************************************************************************/
initialise_name_commands()2282 void initialise_name_commands()
2283 {
2284 #ifdef G_OS_WIN32
2285 	gchar t[BBSIZE];
2286 	NameCommandDeMon=g_strdup("demon");
2287 	NameCommandGamess=g_strdup("submitGMS");
2288 	NameCommandGaussian=g_strdup("g03.exe");
2289 	NameCommandMolcas=g_strdup("molcas");
2290 	NameCommandMolpro=g_strdup("molpro");
2291 	NameCommandMPQC=g_strdup("mpqc");
2292 	NameCommandFireFly=g_strdup("firefly");
2293 	NameCommandQChem=g_strdup("qc");
2294 	NameCommandOrca=g_strdup("orca");
2295 	NameCommandNWChem=g_strdup("nwchem");
2296 	NameCommandPsicode=g_strdup("psi4");
2297 	NameCommandMopac=g_strdup("MOPAC2009");
2298 	NameCommandPovray=g_strdup("start /w pvengine /nr /exit /render +A0.3 -UV");
2299 #else
2300 	NameCommandDeMon=g_strdup("default");
2301 	NameCommandGamess=g_strdup("submitGMS");
2302 	NameCommandGaussian=g_strdup("nohup g03");
2303 	NameCommandMolcas=g_strdup("nohup molcas");
2304 	NameCommandMolpro=g_strdup("nohup molpro");
2305 	NameCommandMPQC=g_strdup("nohup mpqc");
2306 	NameCommandFireFly=g_strdup("firefly");
2307 	NameCommandQChem=g_strdup("qchem");
2308 	NameCommandOrca=g_strdup("orca");
2309 	NameCommandNWChem=g_strdup("nwchem");
2310 	NameCommandPsicode=g_strdup("psi4");
2311 	NameCommandMopac=g_strdup("/opt/mopac/MOPAC2009.exe");
2312 	NameCommandPovray=g_strdup("povray +A0.3 -UV");
2313 #endif
2314 
2315 
2316 #ifdef G_OS_WIN32
2317 	demonDirectory= g_strdup_printf("C:%sDeMon",G_DIR_SEPARATOR_S);
2318 	gamessDirectory= g_strdup_printf("C:%sUsers%sPublic%sgamess-64",G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S);
2319 	orcaDirectory= g_strdup_printf("C:%sORCA_DevCenter%sorca%sx86_exe%srelease%sOrca",G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S);
2320 	nwchemDirectory= g_strdup_printf("C:%sNWChem",G_DIR_SEPARATOR_S);
2321 	psicodeDirectory= g_strdup_printf("C:%sPsicode",G_DIR_SEPARATOR_S);
2322 	fireflyDirectory= g_strdup_printf("C:%sFIREFLY",G_DIR_SEPARATOR_S);
2323 	mopacDirectory= g_strdup_printf("\"C:%sProgram Files%sMOPAC\"",G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S);
2324 	povrayDirectory= g_strdup_printf("\"C:%sProgram Files%sPovRay%sbin\"",G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S);
2325 	openbabelDirectory= g_strdup_printf("C:%sOpenBabel",G_DIR_SEPARATOR_S);
2326 	babelCommand = g_strdup_printf("%s%sobabel.exe",openbabelDirectory,G_DIR_SEPARATOR_S);
2327 	gaussDirectory= g_strdup_printf("\"C:%sG03W\"",G_DIR_SEPARATOR_S);
2328 	sprintf(t,"%s;%s;%s;%s;%s;%s;%s;%s",orcaDirectory,fireflyDirectory,mopacDirectory,gaussDirectory,demonDirectory,povrayDirectory,openbabelDirectory,g_getenv("PATH"));
2329 	g_setenv("PATH",t,TRUE);
2330 #else
2331 	demonDirectory= g_strdup_printf("%s%sDeMon",g_get_home_dir(),G_DIR_SEPARATOR_S);
2332 	gamessDirectory= g_strdup_printf("%s%sGamess",g_get_home_dir(),G_DIR_SEPARATOR_S);
2333 	orcaDirectory= g_strdup_printf("%s%sOrca",g_get_home_dir(),G_DIR_SEPARATOR_S);
2334 	nwchemDirectory= g_strdup_printf("%s%sNWChem",g_get_home_dir(),G_DIR_SEPARATOR_S);
2335 	psicodeDirectory= g_strdup_printf("%s%sPsicode",g_get_home_dir(),G_DIR_SEPARATOR_S);
2336 	fireflyDirectory= g_strdup_printf("%s%sFireFly",g_get_home_dir(),G_DIR_SEPARATOR_S);
2337 	mopacDirectory= g_strdup_printf("/opt/mopac");
2338 	povrayDirectory= g_strdup_printf("/usr/local/bin");
2339 	openbabelDirectory= g_strdup_printf("%s%sOpenBabel",g_get_home_dir(),G_DIR_SEPARATOR_S);
2340 	babelCommand = g_strdup_printf("%s%sobabel.exe",openbabelDirectory,G_DIR_SEPARATOR_S);
2341 #endif
2342 }
2343 /*************************************************************************************/
initialise_batch_commands()2344 void initialise_batch_commands()
2345 {
2346 
2347 	batchCommands.numberOfTypes = 4;
2348 	batchCommands.types = g_malloc(batchCommands.numberOfTypes*sizeof(gchar*));
2349 	batchCommands.types[0] = g_strdup("LSF");
2350 	batchCommands.types[1] = g_strdup("LoadLeveler");
2351 	batchCommands.types[2] = g_strdup("PBS");
2352 	batchCommands.types[3] = g_strdup("Other");
2353 
2354 	batchCommands.commandListAll = g_malloc(batchCommands.numberOfTypes*sizeof(gchar*));
2355 	batchCommands.commandListAll[0] = g_strdup("bjobs -u all");
2356 	batchCommands.commandListAll[1] = g_strdup("llq");
2357 	batchCommands.commandListAll[2] = g_strdup("qstat -a");
2358 	batchCommands.commandListAll[3] = g_strdup("ps -ef");
2359 
2360 	batchCommands.commandListUser = g_malloc(batchCommands.numberOfTypes*sizeof(gchar*));
2361 	batchCommands.commandListUser[0] = g_strdup("bjobs -u ");
2362 	batchCommands.commandListUser[1] = g_strdup("llq -u ");
2363 	batchCommands.commandListUser[2] = g_strdup("qstat ");
2364 	batchCommands.commandListUser[3] = g_strdup("ps -fu");
2365 
2366 	batchCommands.commandKill = g_malloc(batchCommands.numberOfTypes*sizeof(gchar*));
2367 	batchCommands.commandKill[0] = g_strdup("bkill ");
2368 	batchCommands.commandKill[1] = g_strdup("llcancel ");
2369 	batchCommands.commandKill[2] = g_strdup("qdel ");
2370 	batchCommands.commandKill[3] = g_strdup("kill ");
2371 
2372 	batchCommands.jobIdTitle = g_malloc(batchCommands.numberOfTypes*sizeof(gchar*));
2373 	batchCommands.jobIdTitle[0] = g_strdup("JOBID");
2374 	batchCommands.jobIdTitle[1] = g_strdup("Id");
2375 	batchCommands.jobIdTitle[2] = g_strdup("Job");
2376 	batchCommands.jobIdTitle[3] = g_strdup("PID");
2377 
2378 	NameTypeBatch = g_strdup(batchCommands.types[0]);
2379 	NameCommandBatchAll = g_strdup(batchCommands.commandListAll[0]);
2380 	NameCommandBatchUser = g_strdup(batchCommands.commandListUser[0]);
2381 	NameCommandBatchKill = g_strdup(batchCommands.commandKill[0]);
2382 	NamejobIdTitleBatch = g_strdup(batchCommands.jobIdTitle[0]);
2383 
2384 }
2385 /*************************************************************************************/
initialise_global_variables()2386 void initialise_global_variables()
2387 {
2388   gint i;
2389   for(i=0;i<3;i++)
2390   {
2391  	TotalCharges[i] = 0;
2392   	SpinMultiplicities[i] = 1;
2393   }
2394 
2395   ResultEntryPass = NULL;
2396   GeomDrawingArea = NULL;
2397   FrameWins = NULL;
2398   FrameList = NULL;
2399   Hpaned  = NULL;
2400 
2401 
2402   GeomXYZ = NULL;
2403   Geom = NULL;
2404   MeasureIsHide = TRUE;
2405   VariablesXYZ = NULL;
2406   Variables = NULL;
2407   NcentersXYZ =0;
2408   NcentersZmat =0;
2409   NVariablesXYZ = 0;
2410   NVariables    = 0;
2411   Nelectrons = 0;
2412   GeomIsOpen = FALSE;
2413   iprogram = PROG_IS_OTHER;
2414   Units = 1;
2415   NSA[0] = NSA[1] = NSA[2] = NSA[3] = -1;
2416   ScreenWidth = gdk_screen_width();
2417   ScreenHeight = gdk_screen_height();
2418   GeomConvIsOpen = FALSE;
2419   recenthosts.nhosts = 0;
2420   recenthosts.hosts = NULL;
2421   defaultNetWorkProtocol = GABEDIT_NETWORK_SSH;
2422   initialise_name_file();
2423   set_file_open(NULL,NULL,NULL, defaultNetWorkProtocol);
2424   initialise_name_commands();
2425   initialise_fonts_style();
2426   lastdirectory = g_strdup_printf("%s", g_get_current_dir());
2427   pscpCommand = g_strdup_printf("pscp.exe");
2428   plinkCommand = g_strdup_printf("plink.exe");
2429   pscpplinkDirectory = g_strdup_printf("%s",g_get_current_dir());
2430 
2431 #ifdef G_OS_WIN32
2432   {
2433 	gchar* t = g_strdup_printf("%s;%s",pscpplinkDirectory,g_getenv("PATH"));
2434 	g_setenv("PATH",t,TRUE);
2435 	g_free(t);
2436   }
2437 #endif
2438 
2439   gamessCommands.numberOfCommands = 2;
2440   gamessCommands.numberOfDefaultCommand = 0;
2441   gamessCommands.commands = g_malloc(gamessCommands.numberOfCommands*sizeof(gchar*));
2442   gamessCommands.commands[0] = g_strdup("submitGMS");
2443   gamessCommands.commands[1] = g_strdup("submitGamess 1:0:0");
2444 
2445   gaussianCommands.numberOfCommands = 2;
2446 #ifdef G_OS_WIN32
2447   gaussianCommands.numberOfCommands = 3;
2448 #endif
2449   gaussianCommands.numberOfDefaultCommand = 0;
2450   gaussianCommands.commands = g_malloc(gaussianCommands.numberOfCommands*sizeof(gchar*));
2451   gaussianCommands.commands[0] = g_strdup("nohup g03");
2452   gaussianCommands.commands[1] = g_strdup("submitGaussian 1:0:0");
2453 #ifdef G_OS_WIN32
2454     gaussianCommands.commands[2] = g_strdup("g03.exe");
2455 #endif
2456 
2457 
2458   molcasCommands.numberOfCommands = 2;
2459 #ifdef G_OS_WIN32
2460     molcasCommands.numberOfCommands = 3;
2461 #endif
2462   molcasCommands.numberOfDefaultCommand = 0;
2463   molcasCommands.commands = g_malloc(molcasCommands.numberOfCommands*sizeof(gchar*));
2464   molcasCommands.commands[0] = g_strdup("nohup molcas");
2465   molcasCommands.commands[1] = g_strdup("submitMolcas 1:0:0");
2466 #ifdef G_OS_WIN32
2467   molcasCommands.commands[2] = g_strdup("molcas");
2468 #endif
2469 
2470   molproCommands.numberOfCommands = 2;
2471 #ifdef G_OS_WIN32
2472       molproCommands.numberOfCommands = 3;
2473 #endif
2474 
2475   molproCommands.numberOfDefaultCommand = 0;
2476   molproCommands.commands = g_malloc(molproCommands.numberOfCommands*sizeof(gchar*));
2477   molproCommands.commands[0] = g_strdup("nohup molpro");
2478   molproCommands.commands[1] = g_strdup("submitMolpro 1:0:0");
2479 #ifdef G_OS_WIN32
2480   molproCommands.commands[2] = g_strdup("molpro");
2481 #endif
2482 
2483 	mpqcCommands.numberOfCommands = 2;
2484 #ifdef G_OS_WIN32
2485       mpqcCommands.numberOfCommands = 3;
2486 #endif
2487 
2488   mpqcCommands.numberOfDefaultCommand = 0;
2489   mpqcCommands.commands = g_malloc(mpqcCommands.numberOfCommands*sizeof(gchar*));
2490   mpqcCommands.commands[0] = g_strdup("nohup mpqc");
2491   mpqcCommands.commands[1] = g_strdup("submitMPQC 1:0:0");
2492 #ifdef G_OS_WIN32
2493   mpqcCommands.commands[2] = g_strdup("mpqc");
2494 #endif
2495 
2496 	orcaCommands.numberOfCommands = 2;
2497 #ifdef G_OS_WIN32
2498       orcaCommands.numberOfCommands = 3;
2499 #endif
2500 
2501   orcaCommands.numberOfDefaultCommand = 0;
2502   orcaCommands.commands = g_malloc(orcaCommands.numberOfCommands*sizeof(gchar*));
2503   orcaCommands.commands[0] = g_strdup("nohup orca");
2504   orcaCommands.commands[1] = g_strdup("submitOrca 1:0:0");
2505 #ifdef G_OS_WIN32
2506   orcaCommands.commands[2] = g_strdup("orca");
2507 #endif
2508 
2509 /*---------------------------------------------------------------------*/
2510 	demonCommands.numberOfCommands = 2;
2511 #ifdef G_OS_WIN32
2512       demonCommands.numberOfCommands = 3;
2513 #endif
2514 
2515   demonCommands.numberOfDefaultCommand = 0;
2516   demonCommands.commands = g_malloc(demonCommands.numberOfCommands*sizeof(gchar*));
2517   demonCommands.commands[0] = g_strdup("default");
2518   demonCommands.commands[1] = g_strdup("submitDeMon 1:0:0");
2519 #ifdef G_OS_WIN32
2520   demonCommands.commands[2] = g_strdup("demon");
2521 #endif
2522 
2523 /*---------------------------------------------------------------------*/
2524 	nwchemCommands.numberOfCommands = 2;
2525 #ifdef G_OS_WIN32
2526       nwchemCommands.numberOfCommands = 3;
2527 #endif
2528 
2529   nwchemCommands.numberOfDefaultCommand = 0;
2530   nwchemCommands.commands = g_malloc(nwchemCommands.numberOfCommands*sizeof(gchar*));
2531   nwchemCommands.commands[0] = g_strdup("nohup nwchem");
2532   nwchemCommands.commands[1] = g_strdup("submitNWChem 1:0:0");
2533 #ifdef G_OS_WIN32
2534   nwchemCommands.commands[2] = g_strdup("nwchem");
2535 #endif
2536 /*---------------------------------------------------------------------*/
2537 	psicodeCommands.numberOfCommands = 2;
2538 #ifdef G_OS_WIN32
2539       psicodeCommands.numberOfCommands = 3;
2540 #endif
2541 
2542   psicodeCommands.numberOfDefaultCommand = 0;
2543   psicodeCommands.commands = g_malloc(psicodeCommands.numberOfCommands*sizeof(gchar*));
2544   psicodeCommands.commands[0] = g_strdup("nohup psicode");
2545   psicodeCommands.commands[1] = g_strdup("submitPsicode 1:0:0");
2546 #ifdef G_OS_WIN32
2547   psicodeCommands.commands[2] = g_strdup("psi4");
2548 #endif
2549 /*---------------------------------------------------------------------*/
2550 
2551 	fireflyCommands.numberOfCommands = 2;
2552 #ifdef G_OS_WIN32
2553       fireflyCommands.numberOfCommands = 3;
2554 #endif
2555 
2556   fireflyCommands.numberOfDefaultCommand = 0;
2557   fireflyCommands.commands = g_malloc(fireflyCommands.numberOfCommands*sizeof(gchar*));
2558   fireflyCommands.commands[0] = g_strdup("nohup firefly");
2559   fireflyCommands.commands[1] = g_strdup("submitFireFly 1:0:0");
2560 #ifdef G_OS_WIN32
2561   fireflyCommands.commands[2] = g_strdup("firefly");
2562 #endif
2563 
2564 	qchemCommands.numberOfCommands = 2;
2565 #ifdef G_OS_WIN32
2566       qchemCommands.numberOfCommands = 3;
2567 #endif
2568 
2569   qchemCommands.numberOfDefaultCommand = 0;
2570   qchemCommands.commands = g_malloc(qchemCommands.numberOfCommands*sizeof(gchar*));
2571   qchemCommands.commands[0] = g_strdup("nohup qchem");
2572   qchemCommands.commands[1] = g_strdup("submitQChem 1:0:0");
2573 #ifdef G_OS_WIN32
2574   qchemCommands.commands[2] = g_strdup("qc");
2575 #endif
2576 
2577 	mopacCommands.numberOfCommands = 2;
2578 #ifdef G_OS_WIN32
2579 	mopacCommands.numberOfCommands = 3;
2580 #endif
2581   mopacCommands.numberOfDefaultCommand = 0;
2582   mopacCommands.commands = g_malloc(mopacCommands.numberOfCommands*sizeof(gchar*));
2583   mopacCommands.commands[0] = g_strdup("mopac");
2584   mopacCommands.commands[1] = g_strdup("submitMopac 1:0:0");
2585 #ifdef G_OS_WIN32
2586   mopacCommands.commands[2] = g_strdup("MOPAC2009");
2587 #endif
2588 
2589 	povrayCommands.numberOfCommands = 1;
2590   povrayCommands.numberOfDefaultCommand = 0;
2591   povrayCommands.commands = g_malloc(povrayCommands.numberOfCommands*sizeof(gchar*));
2592 #ifdef G_OS_WIN32
2593   povrayCommands.commands[0] = g_strdup("start /w pvengine /nr /exit /render +A0.3 -UV");
2594 #else
2595   povrayCommands.commands[0] = g_strdup("povray +A0.3 -UV");
2596 #endif
2597 
2598 
2599 
2600   initialise_batch_commands();
2601   init_dipole();
2602 
2603   initAxis();
2604   initPrincipalAxisGL();
2605 	colorMapColors[0][0] = 1;
2606 	colorMapColors[0][1] = 1;
2607 	colorMapColors[0][2] = 1;
2608 	colorMapColors[1][0] = 1;
2609 	colorMapColors[1][1] = 1;
2610 	colorMapColors[1][2] = 1;
2611 	colorMapColors[2][0] = 1;
2612 	colorMapColors[2][1] = 1;
2613 	colorMapColors[2][2] = 1;
2614 	colorMapType = 1;
2615 #ifdef DRAWGEOMGL
2616   initAxesGeom();
2617 #endif
2618   multipole_rank = 3;
2619   alpha_opacity = 0.5;
2620 }
2621 /*************************************************************************************/
run_molden(gchar * titre)2622 void run_molden (gchar *titre)
2623 {
2624 	gchar *fout =  g_strdup_printf("%s%stmp%sfout",gabedit_directory(),G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S);
2625 	gchar *ferr =  g_strdup_printf("%s%stmp%sferr",gabedit_directory(),G_DIR_SEPARATOR_S,G_DIR_SEPARATOR_S);
2626 	gchar* strout = NULL;
2627 	gchar* strerr = NULL;
2628 	GtkWidget* Text[2];
2629 	GtkWidget* Frame[2];
2630 	GtkWidget* Win;
2631 	gchar*  title;
2632 
2633 	gchar *temp=NULL;
2634 	gchar *NameLower=NULL;
2635 
2636 	NameLower = g_strdup_printf("%s%s%s",fileopen.localdir,G_DIR_SEPARATOR_S,fileopen.moldenfile);
2637 	if(iprogram == PROG_IS_MOLPRO)
2638   		lowercase(NameLower);
2639 
2640 	temp=g_strdup_printf("molden %s ",NameLower);
2641 
2642 	run_local_command(fout,ferr,temp,TRUE);
2643 
2644 	title = g_strdup_printf("Run Molden: %s",temp);
2645 	Win = create_text_result_command(Text,Frame,title);
2646 	g_free(title);
2647 	strout = cat_file(fout,FALSE);
2648 	strerr = cat_file(ferr,FALSE);
2649 	if(!strout && !strerr) destroy_children(Win);
2650 	else
2651 	{
2652   		if(strout)
2653 		{
2654  			gabedit_text_insert (GABEDIT_TEXT(Text[0]), NULL, NULL, NULL,strout,-1);
2655 			g_free(strout);
2656 		}
2657   		if(strerr)
2658 		{
2659  			gabedit_text_insert (GABEDIT_TEXT(Text[1]), NULL, NULL, NULL,strerr,-1);
2660 			g_free(strerr);
2661 		}
2662   		gtk_widget_show_all(Win);
2663   		if(!strout)
2664   			gtk_widget_hide(Frame[0]);
2665 	}
2666 
2667 	if(temp !=NULL)
2668 		g_free(temp);
2669 	if(NameLower !=NULL)
2670 		g_free(NameLower);
2671 	g_free(fout);
2672 	g_free(ferr);
2673 }
2674 /*************************************************************************************/
variable_name_valid(gchar * t)2675 gboolean variable_name_valid(gchar *t)
2676 {
2677     gchar FirstForbidden[]={
2678 	'0','1','2','3','4','5','6','7','8','9',
2679     	'+','-','/','%','$','*','!','@','#','^',
2680     	'&','(',')','|','\\','<','>','?',',','~',
2681     	'`','\'','.','"',':',';'};
2682     guint All=36;
2683     guint j;
2684     guint i;
2685     for(i=0;i<All;i++)
2686 	if(t[0]==FirstForbidden[i] ) return FALSE;
2687 
2688     for(i=0;i<strlen(t);i++)
2689     	for(j=11;j<All;j++)
2690 	if(t[i]==FirstForbidden[j] ) return FALSE;
2691 
2692 	return TRUE;
2693 
2694 }
2695 /*************************************************************************************/
testa(char c)2696 gboolean testa(char c)
2697 {
2698 	switch ( c )
2699 	{
2700 	case	'0':
2701 	case	'1':
2702 	case	'2':
2703 	case	'3':
2704 	case	'4':
2705 	case	'5':
2706 	case	'6':
2707 	case	'7':
2708 	case	'8':
2709 	case	'9':
2710 	case	'.':
2711 	case	'e':
2712 	case	'E':
2713 	case	'+':
2714 	case	'-':return TRUE;
2715 	}
2716 	return FALSE;
2717 }
2718 /*************************************************************************************/
test(const gchar * t)2719 gboolean test(const gchar *t)
2720 {
2721 	guint i;
2722 	for(i=0;i<strlen(t);i++)
2723 		if(!testa(t[i]) ) return FALSE;
2724 	if(t[0] =='e' || t[0] =='E' ) return FALSE;
2725 	return TRUE;
2726 
2727 }
2728 /*************************************************************************************/
testapointeE(char c)2729 gboolean testapointeE(char c)
2730 {
2731 	switch ( c )
2732 	{
2733 	case	'.':
2734 	case	'e':
2735 	case	'E':return TRUE;
2736 	}
2737 	return FALSE;
2738 }
2739 /*************************************************************************************/
testpointeE(const gchar * t)2740 gboolean testpointeE(const gchar *t)
2741 {
2742 	guint i;
2743 	for(i=0;i<strlen(t);i++)
2744 		if(testapointeE(t[i]) ) return TRUE;
2745 	return FALSE;
2746 
2747 }
2748 /*************************************************************************************/
set_font_style(GtkStyle * style,gchar * fontname)2749 void set_font_style (GtkStyle* style,gchar *fontname)
2750 {
2751   	PangoFontDescription *font_desc;
2752   	font_desc = pango_font_description_from_string (fontname);
2753 	if (font_desc)
2754 	{
2755 		/*
2756 		pango_font_description_free (style->font_desc);
2757 		*/
2758 		style->font_desc = font_desc;
2759 	}
2760 }
2761 /*************************************************************************************/
set_font(GtkWidget * view,gchar * fontname)2762 void set_font (GtkWidget *view, gchar *fontname)
2763 {
2764         GtkStyle *style;
2765   	PangoFontDescription *font_desc;
2766 
2767 	if(!GTK_IS_WIDGET(view)) return;
2768         style = gtk_style_copy (gtk_widget_get_style (view));
2769   	font_desc = pango_font_description_from_string (fontname);
2770 
2771 	if (font_desc)
2772 	{
2773 		/*
2774 		pango_font_description_free (style->font_desc);
2775 		*/
2776 		style->font_desc = font_desc;
2777 	}
2778 
2779         gtk_widget_set_style (GTK_WIDGET(view), style);
2780 
2781         g_object_unref (style);
2782 }
2783 /*************************************************************************************/
set_tab_size(GtkWidget * view,gint tab_size)2784 void set_tab_size (GtkWidget *view, gint tab_size)
2785 {
2786 	PangoTabArray* tabs = pango_tab_array_new(tab_size,FALSE);
2787 	gtk_text_view_set_tabs          ((GtkTextView *)view, tabs);
2788 }
2789 /*************************************************************************************/
set_text_style(GtkWidget * text,gushort red,gushort green,gushort blue)2790 GtkStyle *set_text_style(GtkWidget *text,gushort red,gushort green,gushort blue)
2791 {
2792 	  GtkStyle *style;
2793           style =  gtk_style_copy(text->style);
2794           style->text[0].red=red;
2795           style->text[0].green=green;
2796           style->text[0].blue=blue;
2797 	  gtk_widget_set_style(text, style );
2798           return style;
2799 }
2800 /********************************************************************************/
set_base_style(GtkWidget * text,gushort red,gushort green,gushort blue)2801 GtkStyle *set_base_style(GtkWidget *text,gushort red,gushort green,gushort blue)
2802 {
2803 	  GtkStyle *style;
2804           style =  gtk_style_copy(text->style);
2805           style->base[0].red=red;
2806           style->base[0].green=green;
2807           style->base[0].blue=blue;
2808 	  gtk_widget_set_style(text, style );
2809           return style;
2810 }
2811 /********************************************************************************/
set_fg_style(GtkWidget * wid,gushort red,gushort green,gushort blue)2812 GtkStyle *set_fg_style(GtkWidget *wid,gushort red,gushort green,gushort blue)
2813 {
2814 	  GtkStyle *style;
2815           style =  gtk_style_copy(wid->style);
2816           style->fg[0].red=red;
2817           style->fg[0].green=green;
2818           style->fg[0].blue=blue;
2819 	  gtk_widget_set_style(wid, style );
2820           return style;
2821 }
2822 /********************************************************************************/
set_bg_style(GtkWidget * wid,gushort red,gushort green,gushort blue)2823 GtkStyle *set_bg_style(GtkWidget *wid,gushort red,gushort green,gushort blue)
2824 {
2825 	  GtkStyle *style;
2826           style =  gtk_style_copy(wid->style);
2827           style->bg[0].red=red;
2828           style->bg[0].green=green;
2829           style->bg[0].blue=blue;
2830 	  gtk_widget_set_style(wid, style );
2831           return style;
2832 }
2833 /********************************************************************************/
numb_of_string_by_row(gchar * str)2834 gint numb_of_string_by_row(gchar *str)
2835 {
2836 	gint n=0;
2837 	gchar* t=str;
2838 	while(*t!='\n' && *t !='\0')
2839 	{
2840 		if(*t!=' ')
2841 		{
2842 			n++;
2843 			while(*t!=' ')
2844 			{
2845 				t++;
2846 				if(*t =='\n' || *t =='\0')
2847 					break;
2848 			}
2849 
2850 		}
2851 		else
2852 		{
2853 			while(*t ==' ' )
2854 			{
2855 				t++;
2856 				if(*t =='\n' || *t =='\0')
2857 					break;
2858 			}
2859 		}
2860 	}
2861 	return n;
2862 }
2863 /********************************************************************************/
numb_of_reals_by_row(gchar * str)2864 gint numb_of_reals_by_row(gchar *str)
2865 {
2866 	gint n=0;
2867 	gchar* t=str;
2868 	gchar p[BBSIZE];
2869 	while(*t!='\n' && *t !='\0')
2870 	{
2871 		if(*t =='\t') *t =' ';
2872 		if(*t =='\r') *t =' ';
2873 		if(*t!=' ')
2874 		{
2875 			sscanf(t,"%s",p);
2876 			if(test(p)) n++;
2877 			while(*t!=' ')
2878 			{
2879 				t++;
2880 				if(*t =='\n' || *t =='\0')
2881 					break;
2882 			}
2883 
2884 		}
2885 		else
2886 		{
2887 			while(*t ==' ' )
2888 			{
2889 				t++;
2890 				if(*t =='\n' || *t =='\0')
2891 					break;
2892 			}
2893 		}
2894 	}
2895 	return n;
2896 }
2897 /********************************************************************************/
gab_split(gchar * str)2898 gchar** gab_split(gchar *str)
2899 {
2900 	gchar** strsplit= g_malloc(sizeof(gchar*));
2901 	gint n=0;
2902 	gchar* t=str;
2903 	gchar p[BBSIZE];
2904 	while(*t!='\n' && *t !='\0')
2905 	{
2906 		if(*t!=' ')
2907 		{
2908 			n++;
2909 			strsplit= g_realloc(strsplit,(n+1)*sizeof(gchar*));
2910 			sscanf(t,"%s",p);
2911 			strsplit[n-1]= g_strdup(p);
2912 			while(*t!=' ')
2913 			{
2914 				t++;
2915 				if(*t =='\n' || *t =='\0')
2916 					break;
2917 			}
2918 
2919 		}
2920 		else
2921 		{
2922 			while(*t ==' ' )
2923 			{
2924 				t++;
2925 				if(*t =='\n' || *t =='\0')
2926 					break;
2927 			}
2928 		}
2929 	}
2930 	strsplit[n]= NULL;
2931 	return strsplit;
2932 }
2933 /********************************************************************************/
gab_strfreev(char ** str)2934 void gab_strfreev (char **str)
2935 {
2936         int i;
2937         if (!str) return;
2938 
2939         for (i = 0; str[i] != NULL; i++) free (str[i]);
2940 
2941         free (str);
2942 }
2943 /********************************************************************************/
get_dipole_from_gamess_output_file(FILE * fd)2944 void get_dipole_from_gamess_output_file(FILE* fd)
2945 {
2946  	guint taille=BBSIZE;
2947   	gchar *t = g_malloc(BBSIZE*sizeof(gchar));
2948   	gchar* t1;
2949 
2950 	init_dipole();
2951 
2952   	while(!feof(fd) )
2953 	{
2954     		t1 = NULL;
2955     		if(!fgets(t,taille,fd))break;
2956     		t1 = strstr( t, "ELECTROSTATIC MOMENTS");
2957 		if(t1)
2958 		{
2959   			while(!feof(fd) )
2960 			{
2961     				if(!fgets(t,taille,fd))break;
2962     				t1 = strstr( t, "DEBYE");
2963 				if(t1)
2964 				{
2965 					gint i;
2966     					if(!fgets(t,taille,fd))break;
2967 					sscanf(t,"%lf %lf %lf",&Dipole.value[0],&Dipole.value[1],&Dipole.value[2]);
2968 					for(i=0;i<3;i++) Dipole.value[i] /= AUTODEB;
2969 					Dipole.def = TRUE;
2970 					break;
2971 				}
2972 			}
2973 			break;
2974 		}
2975 		else
2976 		{
2977 			if(strstr( t, "END OF PROPERTY" )) break;
2978 		}
2979 
2980 	}
2981 	g_free(t);
2982 }
2983 /********************************************************************************/
get_dipole_from_turbomole_output_file(FILE * fd)2984 void get_dipole_from_turbomole_output_file(FILE* fd)
2985 {
2986  	guint taille=BBSIZE;
2987   	gchar *t = g_malloc(BBSIZE*sizeof(gchar));
2988   	gchar dum[100];
2989 
2990 
2991 	init_dipole();
2992 
2993   	while(!feof(fd) )
2994 	{
2995     		if(!fgets(t,taille,fd))break;
2996 		if(strstr( t, "electrostatic moments"))
2997 		{
2998   			while(!feof(fd) )
2999 			{
3000     				if(!fgets(t,taille,fd))break;
3001 				if(strstr( t, "dipole moment"))
3002 				{
3003 					gdouble d;
3004     					if(!fgets(t,taille,fd))break;
3005     					if(!fgets(t,taille,fd))break;/* x */
3006 					sscanf(t,"%s %lf %lf %lf",dum, &d, &d, &Dipole.value[0]);
3007     					if(!fgets(t,taille,fd))break;/* y */
3008 					sscanf(t,"%s %lf %lf %lf",dum, &d, &d, &Dipole.value[1]);
3009     					if(!fgets(t,taille,fd))break;/* z */
3010 					sscanf(t,"%s %lf %lf %lf",dum, &d, &d, &Dipole.value[2]);
3011 					Dipole.def = TRUE;
3012 					break;
3013 				}
3014 			}
3015 			break;
3016 		}
3017 	}
3018 	g_free(t);
3019 }
3020 /********************************************************************************/
get_dipole_from_gaussian_output_file(FILE * fd)3021 void get_dipole_from_gaussian_output_file(FILE* fd)
3022 {
3023  	guint taille=BBSIZE;
3024   	gchar *t = g_malloc(BBSIZE*sizeof(gchar));
3025   	gchar* pdest;
3026 	gint ngrad = 0;
3027 	gint i;
3028 
3029 	init_dipole();
3030 
3031   	while(!feof(fd) )
3032 	{
3033     		pdest = NULL;
3034 		init_dipole();
3035     		if(!feof(fd)) { char* e = fgets(t,taille,fd);}
3036     		pdest = strstr( t, "Dipole moment (Debye)");
3037 
3038 		if(strstr( t, "Dipole moment") && strstr( t, "Debye")) /* field-independent basis */
3039 		{
3040     		if(!feof(fd)) { char* e = fgets(t,taille,fd);}
3041 		else break;
3042 		Dipole.def = TRUE;
3043     		pdest = strstr( t, "X=")+2;
3044 		sscanf(pdest,"%lf",&Dipole.value[0]);
3045     		pdest = strstr( t, "Y=")+2;
3046 		sscanf(pdest,"%lf",&Dipole.value[1]);
3047     		pdest = strstr( t, "Z=")+2;
3048 		sscanf(pdest,"%lf",&Dipole.value[2]);
3049 		/*
3050 		Debug("t =%s\n",t);
3051 		Debug("Dipole = %f %f %f\n",Dipole.value[0],Dipole.value[1],Dipole.value[2]);
3052 		*/
3053 		for(i=0;i<3;i++)
3054 			Dipole.value[i] /= AUTODEB;
3055 		break;
3056 		}
3057 		else
3058 		{
3059           		pdest = strstr( t, "GradGradGrad" );
3060 			if(pdest)
3061 			{
3062 				ngrad++;
3063 			/*	Debug("ngrad = %d\n",ngrad);*/
3064 			}
3065 			if(ngrad>2)
3066 				break;
3067 		}
3068 
3069 	}
3070 	g_free(t);
3071 }
3072 /********************************************************************************/
get_dipole_from_molpro_output_file(FILE * fd)3073 void get_dipole_from_molpro_output_file(FILE* fd)
3074 {
3075  	guint taille=BBSIZE;
3076   	gchar *t = g_malloc(BBSIZE*sizeof(gchar));
3077   	gchar* t1;
3078   	gchar* t2;
3079 
3080 	init_dipole();
3081 
3082   	while(!feof(fd) )
3083 	{
3084     		t1 = NULL;
3085     		if(!feof(fd)) { char* e = fgets(t,taille,fd);}
3086     		t1 = strstr( t, "DIPOLE MOMENTS:");
3087 
3088 		if(t1)
3089 		{
3090 		Dipole.def = TRUE;
3091     		t2 = strstr( t1, ":")+2;
3092 		sscanf(t2,"%lf %lf %lf",&Dipole.value[0],&Dipole.value[1],&Dipole.value[2]);
3093 		/*
3094 		Debug("t =%s\n",t);
3095 		Debug("Dipole = %f %f %f\n",Dipole.value[0],Dipole.value[1],Dipole.value[2]);
3096 		*/
3097 		break;
3098 		}
3099 		else
3100 		{
3101           		t1 = strstr( t, "GEOMETRY OPTIMIZATION STEP" );
3102 			if(t1)
3103 				break;
3104           		t1 = strstr( t, "SEWARD" );
3105 			if(t1)
3106 				break;
3107 		}
3108 
3109 	}
3110 	g_free(t);
3111 }
3112 /********************************************************************************/
get_dipole_from_dalton_output_file(FILE * fd)3113 void get_dipole_from_dalton_output_file(FILE* fd)
3114 {
3115  	guint taille=BBSIZE;
3116   	gchar *t = g_malloc(BBSIZE*sizeof(gchar));
3117   	gchar* t1;
3118   	gchar* t2;
3119 	gchar dum[100];
3120 
3121 	init_dipole();
3122 
3123   	while(!feof(fd) )
3124 	{
3125     		t1 = NULL;
3126     		if(!fgets(t,taille,fd))break;
3127     		t1 = strstr( t, "Dipole moment components");
3128 		if(t1)
3129 		{
3130     			if(!fgets(t,taille,fd))break;
3131     			if(!fgets(t,taille,fd))break;
3132     			if(!fgets(t,taille,fd))break;
3133     			if(!fgets(t,taille,fd))break;
3134     			t2 = strstr( t1, ":")+2;
3135     			if(!fgets(t,taille,fd))break;
3136 			sscanf(t,"%s %lf",dum, &Dipole.value[0]);
3137     			if(!fgets(t,taille,fd))break;
3138 			sscanf(t,"%s %lf",dum, &Dipole.value[1]);
3139     			if(!fgets(t,taille,fd))break;
3140 			sscanf(t,"%s %lf",dum, &Dipole.value[2]);
3141 			Dipole.def = TRUE;
3142 		/*
3143 			Debug("t =%s\n",t);
3144 			Debug("Dipole = %f %f %f\n",Dipole.value[0],Dipole.value[1],Dipole.value[2]);
3145 		*/
3146 			break;
3147 		}
3148 		else
3149 		{
3150 			if(strstr( t, ">>>>" )) break;
3151 		}
3152 
3153 	}
3154 	g_free(t);
3155 }
3156 /********************************************************************************/
get_dipole_from_orca_output_file(FILE * fd)3157 void get_dipole_from_orca_output_file(FILE* fd)
3158 {
3159  	guint taille=BBSIZE;
3160   	gchar *t = g_malloc(BBSIZE*sizeof(gchar));
3161   	gchar* pdest;
3162 
3163 	init_dipole();
3164 
3165   	while(!feof(fd) )
3166 	{
3167     		pdest = NULL;
3168 		init_dipole();
3169     		if(!feof(fd)) { char* e = fgets(t,taille,fd);}
3170     		pdest = strstr( t, "Total Dipole Moment");
3171 
3172 		if(pdest && strstr( t,":"))
3173 		{
3174 			pdest = strstr( t,":")+1;
3175 			Dipole.def = TRUE;
3176 			sscanf(pdest,"%lf %lf %lf",&Dipole.value[0],&Dipole.value[1],&Dipole.value[2]);
3177 			break;
3178 		}
3179 	}
3180 	g_free(t);
3181 }
3182 /********************************************************************************/
get_dipole_from_vasp_output_file(FILE * fd)3183 void get_dipole_from_vasp_output_file(FILE* fd)
3184 {
3185  	guint taille=BBSIZE;
3186   	gchar *t = g_malloc(BBSIZE*sizeof(gchar));
3187   	gchar* pdest;
3188 
3189 	init_dipole();
3190 
3191 	g_free(t);
3192 }
3193 /********************************************************************************/
get_dipole_from_nwchem_output_file(FILE * fd)3194 void get_dipole_from_nwchem_output_file(FILE* fd)
3195 {
3196  	guint taille=BBSIZE;
3197   	gchar *t = g_malloc(BBSIZE*sizeof(gchar));
3198   	gchar* pdest;
3199 
3200 	init_dipole();
3201 
3202   	while(!feof(fd) )
3203 	{
3204     		pdest = NULL;
3205 		init_dipole();
3206     		if(!feof(fd)) { char* e = fgets(t,taille,fd);}
3207     		pdest = strstr( t, "Nuclear Dipole moment");
3208 		if(pdest)
3209 		{
3210 			gboolean OK = FALSE;
3211   			while(!feof(fd) )
3212 			{
3213     				if(!fgets(t,taille,fd)) break;
3214 				if(strstr(t,"---------------- ---------------- ----------------"))
3215 				{
3216 					OK = TRUE;
3217 					break;
3218 				}
3219 			}
3220 			if(!OK) break;
3221     			if(!fgets(t,taille,fd)) break;
3222 			Dipole.def = TRUE;
3223 			sscanf(pdest,"%lf %lf %lf",&Dipole.value[0],&Dipole.value[1],&Dipole.value[2]);
3224 			break;
3225 		}
3226 	}
3227 	g_free(t);
3228 }
3229 /********************************************************************************/
get_dipole_from_psicode_output_file(FILE * fd)3230 void get_dipole_from_psicode_output_file(FILE* fd)
3231 {
3232  	guint taille=BBSIZE;
3233   	gchar *t = g_malloc(BBSIZE*sizeof(gchar));
3234   	gchar* pdest;
3235 
3236 	init_dipole();
3237 
3238   	while(!feof(fd) )
3239 	{
3240     		pdest = NULL;
3241 		init_dipole();
3242     		if(!feof(fd)) { char* e = fgets(t,taille,fd);}
3243     		pdest = strstr( t, "Nuclear Dipole moment");
3244 		if(pdest)
3245 		{
3246 			gboolean OK = FALSE;
3247   			while(!feof(fd) )
3248 			{
3249     				if(!fgets(t,taille,fd)) break;
3250 				if(strstr(t,"---------------- ---------------- ----------------"))
3251 				{
3252 					OK = TRUE;
3253 					break;
3254 				}
3255 			}
3256 			if(!OK) break;
3257     			if(!fgets(t,taille,fd)) break;
3258 			Dipole.def = TRUE;
3259 			sscanf(pdest,"%lf %lf %lf",&Dipole.value[0],&Dipole.value[1],&Dipole.value[2]);
3260 			break;
3261 		}
3262 	}
3263 	g_free(t);
3264 }
3265 /********************************************************************************/
get_dipole_from_qchem_output_file(FILE * fd)3266 void get_dipole_from_qchem_output_file(FILE* fd)
3267 {
3268  	guint taille=BBSIZE;
3269   	gchar *t = g_malloc(BBSIZE*sizeof(gchar));
3270   	gchar* pdest;
3271 	gint ngrad = 0;
3272 	gint i;
3273 
3274 	init_dipole();
3275 
3276   	while(!feof(fd) )
3277 	{
3278     		pdest = NULL;
3279 		init_dipole();
3280     		if(!feof(fd)) { char* e = fgets(t,taille,fd);}
3281     		pdest = strstr( t, "Dipole Moment (Debye)");
3282 
3283 		if(pdest)
3284 		{
3285     		if(!feof(fd)) { char* e = fgets(t,taille,fd);}
3286 		else break;
3287 		Dipole.def = TRUE;
3288     		pdest = strstr( t, "X")+2;
3289 		if(pdest) sscanf(pdest,"%lf",&Dipole.value[0]);
3290     		pdest = strstr( t, "Y")+2;
3291 		if(pdest) sscanf(pdest,"%lf",&Dipole.value[1]);
3292     		pdest = strstr( t, "Z")+2;
3293 		if(pdest) sscanf(pdest,"%lf",&Dipole.value[2]);
3294 		for(i=0;i<3;i++) Dipole.value[i] /= AUTODEB;
3295 		break;
3296 		}
3297 		else
3298 		{
3299           		pdest = strstr( t, "GradGradGrad" );
3300 			if(pdest)
3301 			{
3302 				ngrad++;
3303 			}
3304 			if(ngrad>2) break;
3305 		}
3306 
3307 	}
3308 	g_free(t);
3309 }
3310 /********************************************************************************/
get_dipole_from_mopac_output_file(FILE * fd)3311 void get_dipole_from_mopac_output_file(FILE* fd)
3312 {
3313  	guint taille=BBSIZE;
3314   	gchar *t = g_malloc(BBSIZE*sizeof(gchar));
3315   	gchar* pdest;
3316 	gint i;
3317 	gchar dum[100];
3318 
3319 	init_dipole();
3320 
3321   	while(!feof(fd) )
3322 	{
3323     		pdest = NULL;
3324 		init_dipole();
3325     		if(!feof(fd)) { char* e = fgets(t,taille,fd);}
3326     		pdest = strstr( t, "DIPOLE           X         Y         Z");
3327 
3328 		if(pdest)
3329 		{
3330     			if(!fgets(t,taille,fd)) break;
3331     			if(!fgets(t,taille,fd)) break;
3332     			if(!fgets(t,taille,fd)) break;
3333 			Dipole.def = TRUE;
3334     			pdest = strstr( t, "SUM")+2;
3335 			sscanf(t,"%s %lf %lf %lf",dum,&Dipole.value[0],&Dipole.value[1], &Dipole.value[2]);
3336 			for(i=0;i<3;i++) Dipole.value[i] /= AUTODEB;
3337 			break;
3338 		}
3339 	}
3340 	g_free(t);
3341 }
3342 /********************************************************************************/
get_dipole_from_mopac_aux_file(FILE * fd)3343 void get_dipole_from_mopac_aux_file(FILE* fd)
3344 {
3345   	gchar t[BBSIZE];
3346   	gchar* pdest;
3347 	gint i;
3348 
3349 	init_dipole();
3350 
3351   	while(!feof(fd) )
3352 	{
3353     		pdest = NULL;
3354 		init_dipole();
3355     		if(!feof(fd)) { char* e = fgets(t,BBSIZE,fd);}
3356     		pdest = strstr( t, "DIPOLE:DEBYE=");
3357 
3358 		if(pdest)
3359 		{
3360 			Dipole.def = TRUE;
3361     			pdest = strstr( t, "=")+1;
3362 			Dipole.value[0] = 0;
3363 			Dipole.value[1] = 0;
3364 			Dipole.value[2] = 0;
3365 			if(pdest) sscanf(pdest,"%lf %lf %lf",&Dipole.value[0], &Dipole.value[1],&Dipole.value[2]);
3366 			for(i=0;i<3;i++) Dipole.value[i] /= AUTODEB;
3367 			break;
3368 		}
3369 	}
3370 }
3371 /**********************************************/
set_dipole(GtkWidget * fp,gpointer data)3372 void set_dipole(GtkWidget* fp,gpointer data)
3373 {
3374 	GtkWidget** entrys = (GtkWidget**)data;
3375 	GdkColor* color = g_object_get_data(G_OBJECT (fp), "Color");
3376 	G_CONST_RETURN gchar* tentry;
3377 	gint i;
3378 	gdouble fact=1.0;
3379 
3380 	tentry = gtk_entry_get_text(GTK_ENTRY(entrys[0]));
3381 	fact = atof(tentry);
3382 	Dipole.def = TRUE;
3383 	for(i=1;i<4;i++)
3384 	{
3385 		tentry = gtk_entry_get_text(GTK_ENTRY(entrys[i]));
3386 		Dipole.value[i-1] = atof(tentry)*fact;
3387 	}
3388 	for(i=0;i<3;i++) Dipole.value[i] /= AUTODEB;
3389 
3390 	tentry = gtk_entry_get_text(GTK_ENTRY(entrys[4]));
3391 	Dipole.radius = atof(tentry)/AUTODEB;
3392 	if(Dipole.radius<1e-6) Dipole.radius = 0.1;
3393 
3394 	for(i=5;i<8;i++)
3395 	{
3396 		tentry = gtk_entry_get_text(GTK_ENTRY(entrys[i]));
3397 		Dipole.origin[i-5] = atof(tentry)/BOHR_TO_ANG;
3398 	}
3399 
3400 	Dipole.color[0] = color->red;
3401 	Dipole.color[1] = color->green;
3402 	Dipole.color[2] = color->blue;
3403 	rafresh_window_orb();
3404 
3405         if(GeomDrawingArea != NULL)
3406 		 draw_geometry(NULL,NULL);
3407 }
3408 /**********************************************/
init_dipole()3409 void init_dipole()
3410 {
3411 	gint i;
3412 	Dipole.def=FALSE;
3413 	Dipole.radius = 0.25;
3414 	Dipole.color[0] = 0;
3415 	Dipole.color[1] = 0;
3416 	Dipole.color[2] = 65535;
3417 	for(i=0;i<3;i++) Dipole.value[i] = 0.0;
3418 	for(i=0;i<3;i++) Dipole.origin[i] = 0.0;
3419 }
3420 /**********************************************/
delete_last_spaces(gchar * str)3421 void delete_last_spaces(gchar* str)
3422 {
3423 	gchar *s;
3424 
3425 	if(str == NULL)
3426 		return;
3427 
3428 	if (!*str)
3429 		return;
3430 	for (s = str + strlen (str) - 1; s >= str && isspace ((unsigned char)*s); s--)
3431 		*s = '\0';
3432 }
3433 /**********************************************/
delete_first_spaces(gchar * str)3434 void delete_first_spaces(gchar* str)
3435 {
3436 	gchar *start;
3437 	gint i;
3438 	gint lenSpace = 0;
3439 
3440 	if(str == NULL)
3441 		return;
3442 	if (!*str)
3443 		return;
3444 
3445 	for (start = str; *start && isspace (*start); start++)lenSpace++;
3446 
3447 	for(i=0;i<(gint)(strlen(str)-lenSpace);i++)
3448 		str[i] = str[i+lenSpace];
3449 	str[strlen(str)-lenSpace] = '\0';
3450 }
3451 /**********************************************/
delete_all_spaces(gchar * str)3452 void delete_all_spaces(gchar* str)
3453 {
3454 	gint i;
3455 	gint j;
3456 	gboolean Ok = FALSE;
3457 
3458 	delete_last_spaces(str);
3459 	delete_first_spaces(str);
3460 	while(!Ok)
3461 	{
3462 		Ok = TRUE;
3463 		for(i=0;i<(gint)strlen(str);i++)
3464 		{
3465 			if(isspace(str[i]))
3466 			{
3467 				Ok = FALSE;
3468 				for(j=i;j<(gint)strlen(str);j++)
3469 				{
3470 					str[j] = str[j+1];
3471 				}
3472 				break;
3473 			}
3474 		}
3475 	}
3476 }
3477 /**********************************************/
get_to_str(gchar * str,gchar * end)3478 gchar* get_to_str(gchar* str,gchar* end)
3479 {
3480 	gchar* iend = NULL;
3481 	gchar* res = NULL;
3482 	gint len;
3483 	gint i;
3484 
3485 	if(str == NULL || end == NULL)
3486 		return NULL;
3487 
3488 	iend = strstr(str,end);
3489 	if(iend==NULL)
3490 		return g_strdup(str);
3491 	len = iend - str;
3492 	if(len<1)
3493 		return NULL;
3494 
3495 	res = g_malloc((len+1)*sizeof(gchar));
3496 	for(i=0;i<len;i++)
3497 		res[i] = str[i];
3498 
3499 	res[len] = '\0';
3500 	return res;
3501 
3502 }
3503 /*************************************************************************************/
testi(char c)3504 static gboolean testi(char c)
3505 {
3506 	switch ( c )
3507 	{
3508 	case	'0':
3509 	case	'1':
3510 	case	'2':
3511 	case	'3':
3512 	case	'4':
3513 	case	'5':
3514 	case	'6':
3515 	case	'7':
3516 	case	'8':
3517 	case	'9': return TRUE;
3518 	}
3519 	return FALSE;
3520 }
3521 /*************************************************************************************/
isInteger(gchar * t)3522 gboolean isInteger(gchar *t)
3523 {
3524 	guint i;
3525 	if(!testi(t[0])&& t[0] != '-' ) return FALSE;
3526 	for(i=1;i<strlen(t);i++)
3527 		if(!testi(t[i]) ) return FALSE;
3528 	return TRUE;
3529 
3530 }
3531 /*************************************************************************************/
testascii(char c)3532 static gboolean testascii(char c)
3533 {
3534 	switch ( c )
3535 	{
3536 	case	'0':
3537 	case	'1':
3538 	case	'2':
3539 	case	'3':
3540 	case	'4':
3541 	case	'5':
3542 	case	'6':
3543 	case	'7':
3544 	case	'8':
3545 	case	'9':
3546 	case	'.':
3547 	case	'e':
3548 	case	'E':
3549 	case	'+':
3550 	case	'-':return TRUE;
3551 	}
3552 	return FALSE;
3553 }
3554 /*************************************************************************************/
isFloat(const gchar * t)3555 gboolean isFloat(const gchar *t)
3556 {
3557 	guint i;
3558 	for(i=0;i<strlen(t);i++)
3559 		if(!testascii(t[i]) ) return FALSE;
3560 	if(t[0] =='e' || t[0] =='E' ) return FALSE;
3561 	return TRUE;
3562 
3563 }
3564 /**********************************************/
get_symb_type_charge(gchar * str,gchar symb[],gchar type[],gchar charge[])3565 void get_symb_type_charge(gchar* str,gchar symb[], gchar type[], gchar charge[])
3566 {
3567 	gint i;
3568 	gchar** split = g_strsplit(str,"-",4);
3569 
3570 	sprintf(symb,"H");
3571 	sprintf(type,"H");
3572 	sprintf(charge,"0.0");
3573 	if(!str)
3574 		return;
3575 	sprintf(symb,"%s",str);
3576 	sprintf(type,"%s",str);
3577 	if(split)
3578 	for(i=0;i<4;i++)
3579 	{
3580 		if(!split[i])
3581 			break;
3582 
3583 		switch(i)
3584 		{
3585 			case 0: sprintf(symb,"%s",split[0]);
3586 				g_free(split[0]);
3587 				break;
3588 			case 1: sprintf(type,"%s",split[1]);
3589 				g_free(split[1]);
3590 				break;
3591 			case 2: if(strlen(split[2])<1)
3592 				{
3593 					g_free(split[2]);
3594 					break;
3595 				}
3596 				sprintf(charge,"%s",split[2]);
3597 				g_free(split[2]);
3598 				break;
3599 			case 3: sprintf(charge,"-%s",split[3]);
3600 				g_free(split[3]);
3601 		}
3602 	}
3603 }
3604 /**********************************************/
str_delete_n(gchar * str)3605 void str_delete_n(gchar* str)
3606 {
3607 	gchar *s;
3608 
3609 	if(str == NULL)
3610 		return;
3611 
3612 	if (!*str)
3613 		return;
3614 	for (s = str + strlen (str) - 1; s >= str && ((guchar)*s)=='\n'; s--)
3615 		*s = '\0';
3616 }
3617 /**********************************************/
get_font_label_name()3618 gchar* get_font_label_name()
3619 {
3620 	return FontsStyleLabel.fontname;
3621 }
3622 /*************************************************************************************/
test_type_program_gaussian(FILE * file)3623 gboolean test_type_program_gaussian(FILE* file)
3624 {
3625 	gchar t[BBSIZE];
3626 	guint taille=BBSIZE;
3627 	fseek(file, 0L, SEEK_SET);
3628 	if(!fgets(t, taille, file)) return FALSE;
3629 	if((int)t[0]==(int)'#' || (int)t[0]==(int)'%' ) return TRUE;
3630 	return FALSE;
3631 }
3632 /**********************************************************************************/
test_type_program_molcas(FILE * file)3633 gboolean test_type_program_molcas(FILE* file)
3634 {
3635 	gchar t[BBSIZE];
3636 	guint taille=BBSIZE;
3637 	fseek(file, 0L, SEEK_SET);
3638 	while(!feof(file))
3639 	{
3640 		if(!fgets(t, taille, file)) return FALSE;
3641 		uppercase(t);
3642 		if( strstr(t, "&SEWARD") ) return TRUE;
3643 		if( strstr(t, "&GATEWAY") ) return TRUE;
3644 	}
3645 	return FALSE;
3646 }
3647 /**********************************************************************************/
test_type_program_molpro(FILE * file)3648 gboolean test_type_program_molpro(FILE* file)
3649 {
3650 	gchar t[BBSIZE];
3651 	guint taille=BBSIZE;
3652 	fseek(file, 0L, SEEK_SET);
3653 	while(!feof(file))
3654 	{
3655 		if(!fgets(t, taille, file)) return FALSE;
3656 		if( (int)t[0] ==(int)'!' ) continue;
3657 		if( (int)t[0] ==(int)'*' ) return TRUE;
3658 		return FALSE;
3659 	}
3660 	return FALSE;
3661 }
3662 /**********************************************************************************/
test_type_program_mpqc(FILE * file)3663 gboolean test_type_program_mpqc(FILE* file)
3664 {
3665 	gchar t[BBSIZE];
3666 	guint taille=BBSIZE;
3667 	fseek(file, 0L, SEEK_SET);
3668 	while(!feof(file))
3669 	{
3670 		if(!fgets(t, taille, file)) return FALSE;
3671 		if(strstr(t,"%"))continue;
3672 		/* Object-Oriented  input file */
3673 		if(strstr(t,"molecule") && strstr(t,"Molecule") && strstr(t,"<") && strstr(t,">")) return TRUE;
3674 		/* sample input file */
3675 		if(strstr(t,"molecule") && strstr(t,":")) return TRUE;
3676 	}
3677 	return FALSE;
3678 }
3679 /**********************************************************************************/
test_type_program_gamess(FILE * file)3680 gboolean test_type_program_gamess(FILE* file)
3681 {
3682 	gchar t[BBSIZE];
3683 	guint taille=BBSIZE;
3684 	fseek(file, 0L, SEEK_SET);
3685 	while(!feof(file))
3686 	{
3687 		if(!fgets(t, taille, file)) return FALSE;
3688 		if(strstr(t,"!"))continue;
3689 
3690 		if(strstr(t,"$CONTRL")) return TRUE;
3691 		/* sample input file */
3692 		if(strstr(t,"$BASIS")) return TRUE;
3693 		if(strstr(t,"$DATA")) return TRUE;
3694 	}
3695 	return FALSE;
3696 }
3697 /**********************************************************************************/
test_type_program_firefly(FILE * file)3698 gboolean test_type_program_firefly(FILE* file)
3699 {
3700 	gchar t[BBSIZE];
3701 	guint taille=BBSIZE;
3702 	fseek(file, 0L, SEEK_SET);
3703 	while(!feof(file))
3704 	{
3705 		if(!fgets(t, taille, file)) return FALSE;
3706 		if(strstr(t,"FireFly")) return TRUE;
3707 	}
3708 	return FALSE;
3709 }
3710 /**********************************************************************************/
test_type_program_nwchem(FILE * file)3711 gboolean test_type_program_nwchem(FILE* file)
3712 {
3713 	gchar t[BBSIZE];
3714 	guint taille=BBSIZE;
3715 	fseek(file, 0L, SEEK_SET);
3716 	while(!feof(file))
3717 	{
3718 		if(!fgets(t, taille, file)) return FALSE;
3719 		if(strstr(t,"NWChem input") && strstr(t,"#")) return TRUE;
3720 		uppercase(t);
3721 		if(strstr(t,"GEOMETRY")) return TRUE;
3722 		if(strstr(t,"ZMATRIX")) return TRUE;
3723 	}
3724 	return FALSE;
3725 }
3726 /**********************************************************************************/
test_type_program_psicode(FILE * file)3727 gboolean test_type_program_psicode(FILE* file)
3728 {
3729 	gchar t[BBSIZE];
3730 	guint taille=BBSIZE;
3731 	fseek(file, 0L, SEEK_SET);
3732 	while(!feof(file))
3733 	{
3734 		if(!fgets(t, taille, file)) return FALSE;
3735 		if(strstr(t,"Psicode input") && strstr(t,"#")) return TRUE;
3736 		uppercase(t);
3737 		if(strstr(t,"GEOMETRY")) return TRUE;
3738 		if(strstr(t,"ZMATRIX")) return TRUE;
3739 	}
3740 	return FALSE;
3741 }
3742 /**********************************************************************************/
test_type_program_orca(FILE * file)3743 gboolean test_type_program_orca(FILE* file)
3744 {
3745 	gchar t[BBSIZE];
3746 	guint taille=BBSIZE;
3747 	fseek(file, 0L, SEEK_SET);
3748 	while(!feof(file))
3749 	{
3750 		if(!fgets(t, taille, file)) return FALSE;
3751 		if(strstr(t,"Orca input") && strstr(t,"#")) return TRUE;
3752 		uppercase(t);
3753 		if(strstr(t,"* XYZ")) return TRUE;
3754 		if(strstr(t,"* INT")) return TRUE;
3755 	}
3756 	return FALSE;
3757 }
3758 /**********************************************************************************/
test_type_program_mopac(FILE * file)3759 gboolean test_type_program_mopac(FILE* file)
3760 {
3761 	gchar t[BBSIZE];
3762 	guint taille=BBSIZE;
3763 	fseek(file, 0L, SEEK_SET);
3764 	while(!feof(file))
3765 	{
3766 		if(!fgets(t, taille, file)) return FALSE;
3767 		if(t[0] != '*') break;
3768 		if(t[0] == '*' && strstr(t,"Mopac")) return TRUE;
3769 	}
3770 	if(strstr(t,"BONDS") && strstr(t,"CHARGE")) return TRUE;
3771 	if(!fgets(t, taille, file)) return FALSE;
3772 	if(strstr(t,"Mopac")) return TRUE;
3773 	return FALSE;
3774 }
3775 /**********************************************************************************/
test_type_program_qchem(FILE * file)3776 gboolean test_type_program_qchem(FILE* file)
3777 {
3778 	gchar t[BBSIZE];
3779 	guint taille=BBSIZE;
3780 	fseek(file, 0L, SEEK_SET);
3781 	while(!feof(file))
3782 	{
3783 		if(!fgets(t, taille, file)) return FALSE;
3784 		if(strstr(t,"!"))continue;
3785 
3786 		if(strstr(t,"$molecule")) return TRUE;
3787 		if(strstr(t,"$rem")) return TRUE;
3788 	}
3789 	return FALSE;
3790 }
3791 /**********************************************************************************/
test_type_program_demon(FILE * file)3792 gboolean test_type_program_demon(FILE* file)
3793 {
3794 	gchar t[BBSIZE];
3795 	guint taille=BBSIZE;
3796 	fseek(file, 0L, SEEK_SET);
3797 	while(!feof(file))
3798 	{
3799 		if(!fgets(t, taille, file)) return FALSE;
3800 		if(strstr(t,"!"))continue;
3801 		if(strstr(t,"DeMon")) return TRUE;
3802 	}
3803 	return FALSE;
3804 }
3805 /**********************************************************************************/
get_type_of_program(FILE * file)3806 gint get_type_of_program(FILE* file)
3807 {
3808 	if(test_type_program_demon(file))
3809 	{
3810 		fseek(file, 0L, SEEK_SET);
3811 		return PROG_IS_DEMON;
3812 	}
3813 	if(test_type_program_orca(file))
3814 	{
3815 		fseek(file, 0L, SEEK_SET);
3816 		return PROG_IS_ORCA;
3817 	}
3818 	if(test_type_program_firefly(file))
3819 	{
3820 		fseek(file, 0L, SEEK_SET);
3821 		return PROG_IS_FIREFLY;
3822 	}
3823 	if(test_type_program_gamess(file))
3824 	{
3825 		fseek(file, 0L, SEEK_SET);
3826 		return PROG_IS_GAMESS;
3827 	}
3828 	if(test_type_program_qchem(file))
3829 	{
3830 		fseek(file, 0L, SEEK_SET);
3831 		return PROG_IS_QCHEM;
3832 	}
3833 	if(test_type_program_mopac(file))
3834 	{
3835 		fseek(file, 0L, SEEK_SET);
3836 		return PROG_IS_MOPAC;
3837 	}
3838 	if(test_type_program_mpqc(file))
3839 	{
3840 		fseek(file, 0L, SEEK_SET);
3841 		return PROG_IS_MPQC;
3842 	}
3843 	if(test_type_program_nwchem(file))
3844 	{
3845 		fseek(file, 0L, SEEK_SET);
3846 		return PROG_IS_NWCHEM;
3847 	}
3848 	if(test_type_program_psicode(file))
3849 	{
3850 		fseek(file, 0L, SEEK_SET);
3851 		return PROG_IS_PSICODE;
3852 	}
3853 	if(test_type_program_gaussian(file))
3854 	{
3855 		fseek(file, 0L, SEEK_SET);
3856 		return PROG_IS_GAUSS;
3857 	}
3858 	if(test_type_program_molcas(file))
3859 	{
3860 		fseek(file, 0L, SEEK_SET);
3861 		return PROG_IS_MOLCAS;
3862 	}
3863 	if(test_type_program_molpro(file))
3864 	{
3865 		fseek(file, 0L, SEEK_SET);
3866 		return PROG_IS_MOLPRO;
3867 	}
3868 	fseek(file, 0L, SEEK_SET);
3869 	return PROG_IS_OTHER;
3870 }
3871 /**************************************************************************************************************************************/
gabedit_string_get_pixel_size(GtkWidget * parent,PangoFontDescription * font_desc,G_CONST_RETURN gchar * t,int * width,int * height)3872 void gabedit_string_get_pixel_size(GtkWidget* parent, PangoFontDescription *font_desc, G_CONST_RETURN gchar* t, int *width, int* height)
3873 {
3874 	PangoLayout *layout = gtk_widget_create_pango_layout(parent, t);
3875 	if(font_desc) pango_layout_set_font_description (layout,font_desc);
3876 	pango_layout_set_justify(layout, TRUE);
3877 	pango_layout_get_pixel_size(layout, width,height);
3878 	g_object_unref (layout);
3879 }
3880 /**********************************************************************************/
gabedit_draw_string(GtkWidget * parent,GdkPixmap * pixmap,PangoFontDescription * font_desc,GdkGC * gc,gint x,gint y,G_CONST_RETURN gchar * t,gboolean centerX,gboolean centerY)3881 void gabedit_draw_string(GtkWidget* parent, GdkPixmap* pixmap, PangoFontDescription *font_desc, GdkGC* gc , gint x, gint y, G_CONST_RETURN gchar* t, gboolean centerX, gboolean centerY)
3882 {
3883 	int width  = 0;
3884 	int height = 0;
3885 	PangoLayout *layout = gtk_widget_create_pango_layout(parent, t);
3886 	if(font_desc) pango_layout_set_font_description (layout,font_desc);
3887 	pango_layout_set_justify(layout, TRUE);
3888 	if(centerX || centerY) pango_layout_get_pixel_size(layout, &width,&height);
3889 	 if(centerX) x -= width/2;
3890 	 if(centerY) y -= height/2;
3891 	gdk_draw_layout (pixmap,gc,x,y,layout);
3892 	g_object_unref (layout);
3893 }
3894 /**********************************************************************************************************************************/
gabedit_save_image(GtkWidget * widget,gchar * fileName,gchar * type)3895 void gabedit_save_image(GtkWidget* widget, gchar *fileName, gchar* type)
3896 {
3897 	int width;
3898 	int height;
3899 	GError *error = NULL;
3900 	GdkPixbuf  *pixbuf = NULL;
3901 
3902 	width =  widget->allocation.width;
3903 	height = widget->allocation.height;
3904 	pixbuf = gdk_pixbuf_get_from_drawable(NULL, widget->window, NULL, 0, 0, 0, 0, width, height);
3905 	/* printf("width = %d height = %d\n",width,height);*/
3906 	if(pixbuf)
3907 	{
3908 		if(!fileName)
3909 		{
3910 			GtkClipboard * clipboard;
3911 			clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
3912 			if(clipboard)
3913 			{
3914 				gtk_clipboard_clear(clipboard);
3915 				gtk_clipboard_set_image(clipboard, pixbuf);
3916 			}
3917 		}
3918 		else
3919 		{
3920 			if(type && strstr(type,"j") && strstr(type,"g") )
3921 			gdk_pixbuf_save(pixbuf, fileName, type, &error, "quality", "100", NULL);
3922 			else if(type && strstr(type,"png"))
3923 			gdk_pixbuf_save(pixbuf, fileName, type, &error, "compression", "5", NULL);
3924 			else if(type && (strstr(type,"tif") || strstr(type,"tiff")))
3925 			gdk_pixbuf_save(pixbuf, fileName, "tiff", &error, "compression", "1", NULL);
3926 			else
3927 			gdk_pixbuf_save(pixbuf, fileName, type, &error, NULL);
3928 		}
3929 	 	g_object_unref (pixbuf);
3930 	}
3931 	/* else printf("Warnning pixbuf = NULL\n");*/
3932 }
3933 /**********************************************************************************************************************************/
get_open_babel_command()3934 G_CONST_RETURN gchar* get_open_babel_command()
3935 {
3936 	return babelCommand;
3937 }
3938 /**********************************************************************************************************************************/
get_num_orbitals_from_aux_mopac_file(FILE * file,gchar * blockName,gint * begin,gint * end)3939 gint get_num_orbitals_from_aux_mopac_file(FILE* file, gchar* blockName,  gint* begin, gint* end)
3940 {
3941 	gchar t[BBSIZE];
3942 	*begin = 0;
3943 	*end = 0;
3944 	 while(!feof(file))
3945 	 {
3946 		if(!fgets(t,BBSIZE,file))break;
3947 		if(strstr( t, blockName))
3948 		{
3949 			gchar* pdest = strstr( t, "=")+1;
3950 			gint i = sscanf(pdest,"%d %d",begin,end);
3951 			return i;
3952 		}
3953 	 }
3954 	 return 0;
3955 }
3956 /**********************************************************************************************************************************/
get_one_block_from_aux_mopac_file(FILE * file,gchar * blockName,gint * n)3957 gchar** get_one_block_from_aux_mopac_file(FILE* file, gchar* blockName,  gint* n)
3958 {
3959 	gint nElements = 0;
3960 	gchar** elements = NULL;
3961 	gchar t[BBSIZE];
3962 	 while(!feof(file))
3963 	 {
3964 		if(!fgets(t,BBSIZE,file))break;
3965 		if(strstr( t, blockName))
3966 		{
3967 			gchar* pdest = strstr( t, "[")+1;
3968 			gint i;
3969 			nElements = atoi(pdest);
3970 			if(nElements<1) break;
3971 			else
3972 			{
3973 				long int geomposok = ftell(file);
3974 				if(!fgets(t,BBSIZE,file))break;
3975 				if(!strstr(t,"# ")) fseek(file, geomposok, SEEK_SET);
3976 			}
3977 
3978 			elements = g_malloc(nElements*sizeof(gchar*));
3979 			for(i=0;i<nElements;i++)
3980 			{
3981 				gint k;
3982 				elements[i] = g_malloc(100*sizeof(gchar));
3983 				k = fscanf(file,"%s",elements[i]);
3984 				if(k<1 || strstr(elements[i],"["))
3985 				{
3986 					if(elements)
3987 					{
3988 						for(i=0;i<nElements;i++)
3989 							if(elements[i]) g_free(elements[i]);
3990 						g_free(elements);
3991 						elements = NULL;
3992 					}
3993 					break;
3994 				}
3995 				else
3996 				{
3997 					if(!strstr(blockName,"ATOM_EL"))
3998 					for(k=0;k<strlen(elements[i]);k++)
3999 					{
4000 						if(elements[i][k]=='D') elements[i][k]='e';
4001 						if(elements[i][k]=='d') elements[i][k]='e';
4002 					}
4003 				}
4004 			}
4005 			break;
4006 		}
4007 	 }
4008 	 *n = nElements;
4009 	 return elements;
4010 }
4011 /**********************************************************************************************************************************/
free_one_string_table(gchar ** table,gint n)4012 gchar** free_one_string_table(gchar** table, gint n)
4013 {
4014 	if(table)
4015 	{
4016 		gint i;
4017 		for(i=0;i<n;i++)
4018 			if(table[i]) g_free(table[i]);
4019 		g_free(table);
4020 	}
4021 	return NULL;
4022 }
4023 /********************************************************************************/
zmat_mopac_irc_output_file(gchar * FileName)4024 gboolean zmat_mopac_irc_output_file(gchar *FileName)
4025 {
4026  	guint taille=BBSIZE;
4027   	gchar t[BBSIZE];
4028  	FILE* fd = FOpen(FileName, "rb");
4029 
4030 	if(!fd) return FALSE;
4031   	while(!feof(fd) )
4032 	{
4033     		if(!fgets(t,taille,fd)) break;
4034                  if(strstr(t,"INTRINSIC REACTION COORDINATE"))
4035 		{
4036 			return TRUE;
4037 		}
4038 	}
4039 	return FALSE;
4040 }
4041 /********************************************************************************/
zmat_mopac_scan_output_file(gchar * FileName)4042 gboolean zmat_mopac_scan_output_file(gchar *FileName)
4043 {
4044  	guint taille=BBSIZE;
4045   	gchar t[BBSIZE];
4046  	FILE* fd = FOpen(FileName, "rb");
4047 
4048 	if(!fd) return FALSE;
4049   	while(!feof(fd) )
4050 	{
4051     		if(!fgets(t,taille,fd)) break;
4052 		if ( strstr(t,"ATOM")
4053 		 && strstr(t,"CHEMICAL")
4054 		 && strstr(t,"BOND")
4055 		 && strstr(t,"LENGTH")
4056 		 && strstr(t,"ANGLE")
4057 		 && strstr(t,"TWIST")
4058 		 )
4059 		{
4060 			return TRUE;
4061 		}
4062 	}
4063 	return FALSE;
4064 }
4065 /*************************************************************************************/
get_extenssion_file(const gchar * filename)4066 gchar *get_extenssion_file(const gchar* filename)
4067 {
4068 	gchar *ext = NULL;
4069 	gint len = 0;
4070 	gint i;
4071 	gint p=-1;
4072 	gint lnew = 0;
4073 
4074 	if(!filename || strlen(filename)<1) return NULL;
4075 	len=strlen(filename);
4076 
4077 	for(i=len;i>0;i--)
4078 	if(filename[i]=='.')
4079 	{
4080 		p=i+1;
4081 		break;
4082 	}
4083 	lnew  = len-p;
4084 	if(p<0 || lnew<=0) return NULL;
4085 	ext= g_malloc((lnew+1)*sizeof(gchar));
4086 
4087 	for(i=p;i<len;i++) ext[i-p]= filename[i];
4088 	ext[lnew]= '\0';
4089 	return ext;
4090 }
4091 /**********************************************/
get_type_output_file(gchar * fileName)4092 GabEditTypeFile get_type_output_file(gchar* fileName)
4093 {
4094  	gchar *t;
4095  	guint taille=BBSIZE;
4096 	GabEditTypeFile ktype = GABEDIT_TYPEFILE_UNKNOWN;
4097 	FILE* file = FOpen(fileName, "rb");
4098 
4099  	if(!file) return ktype;
4100 
4101  	t=g_malloc(taille*sizeof(gchar));
4102 
4103 	rewind(file);
4104     	if(!feof(file)) { char* e = fgets(t,taille,file);}
4105 	uppercase(t);
4106         if(strstr(t, "ENTERING" )) ktype = GABEDIT_TYPEFILE_GAUSSIAN;
4107 	else if(strstr( t, "[MOLDEN FORMAT]" )) ktype = GABEDIT_TYPEFILE_MOLDEN;
4108 	else if(strstr( t, "[GABEDIT FORMAT]" )) ktype = GABEDIT_TYPEFILE_GABEDIT;
4109 	else if(strstr( t, "GAMESS" )) ktype = GABEDIT_TYPEFILE_GAMESS;
4110 	else if(atoi(t)>0 && !strstr(t,"**********")) ktype = GABEDIT_TYPEFILE_XYZ;
4111 	if( ktype == GABEDIT_TYPEFILE_UNKNOWN)
4112 	{
4113 		while(!feof(file))
4114 		{
4115     			if(!feof(file)) { char* e = fgets(t,taille,file);}
4116 			if(strstr(t,"PROGRAM SYSTEM MOLPRO"))
4117 			{
4118 				ktype = GABEDIT_TYPEFILE_MOLPRO;
4119 				break;
4120 			}
4121 			if(strstr(t,"GAMESS VERSION") || strstr(t,"PC GAMESS"))
4122 			{
4123 				ktype = GABEDIT_TYPEFILE_GAMESS;
4124 				break;
4125 			}
4126 			if(strstr(t,"PROGRAM deMon2k"))
4127 			{
4128 				ktype = GABEDIT_TYPEFILE_DEMON;
4129 				break;
4130 			}
4131 			if(strstr(t,"GAMESS VERSION") || strstr(t,"PC GAMESS"))
4132 			{
4133 				ktype = GABEDIT_TYPEFILE_GAMESS;
4134 				break;
4135 			}
4136 			if(strstr(t,"Welcome to Q-Chem"))
4137 			{
4138 				ktype = GABEDIT_TYPEFILE_QCHEM;
4139 				break;
4140 			}
4141 			if(strstr(t,"Northwest Computational Chemistry Package"))
4142 			{
4143 				ktype = GABEDIT_TYPEFILE_NWCHEM;
4144 				break;
4145 			}
4146 			if(strstr(t,"TURBOMOLE GmbH"))
4147 			{
4148 				ktype = GABEDIT_TYPEFILE_TURBOMOLE;
4149 				break;
4150 			}
4151 			uppercase(t);
4152         		if(strstr(t, "ENTERING GAUSSIAN" ))
4153 			{
4154 				ktype = GABEDIT_TYPEFILE_GAUSSIAN;
4155 				break;
4156 			}
4157 			if(strstr( t, "[MOLDEN FORMAT]" ))
4158 			{
4159 				ktype = GABEDIT_TYPEFILE_MOLDEN;
4160 				break;
4161 			}
4162 			if(mystrcasestr( t, "<Title>"))
4163 			{
4164 				ktype = GABEDIT_TYPEFILE_WFX;
4165 				break;
4166 			}
4167 		}
4168 	}
4169 	rewind(file);
4170 	if( ktype == GABEDIT_TYPEFILE_GAUSSIAN)
4171 	{
4172 		gint iGrad = 0;
4173   		while(!feof(file) )
4174   		{
4175  			if(!fgets(t, taille, file))break;
4176 			/* fprintf(stderr,"%s\n",t);*/
4177 
4178                  	if(strstr(t,"GradGradGradGradGradGradGradGradGrad") )
4179 			{
4180 				iGrad++;
4181 				if(iGrad>1) break;
4182 			}
4183                  	if(strstr(t,"Scan        ") )
4184 			{
4185 				if(iGrad==1) ktype = GABEDIT_TYPEFILE_GAUSSIAN_SCANOPT;
4186 				/* if(ktype==GABEDIT_TYPEFILE_GAUSSIAN_SCANOPT) fprintf(stderr,"GABEDIT_TYPEFILE_GAUSSIAN_SCANOPT\n");*/
4187 				break;
4188 			}
4189                  	if(strstr(t,"IRC-IRC-IRC-IRC-IRC-IRC-IRC-IRC-IRC-IRC-IRC-IRC") )
4190 			{
4191 				ktype = GABEDIT_TYPEFILE_GAUSSIAN_IRCOPT;
4192 				break;
4193 			}
4194                  	if(strstr(t," orientation:") ) break;
4195 		}
4196 	}
4197 	if( ktype != GABEDIT_TYPEFILE_UNKNOWN)
4198 	{
4199  		g_free(t);
4200 		fclose(file);
4201 		return ktype;
4202 	}
4203 	if( ktype == GABEDIT_TYPEFILE_UNKNOWN)
4204 	{
4205 		rewind	(file);
4206 		while(!feof(file))
4207 		{
4208     			if(!feof(file)) { char* e = fgets(t,taille,file);}
4209 			if(mystrcasestr( t, "<Title>"))
4210 			{
4211 				ktype = GABEDIT_TYPEFILE_WFX;
4212 				break;
4213 			}
4214 		}
4215 	}
4216 	rewind(file);
4217 	if( ktype == GABEDIT_TYPEFILE_UNKNOWN)
4218 	{
4219 		while(!feof(file))
4220 		{
4221     			if(!feof(file)) { char* e = fgets(t,taille,file);}
4222 			if(strstr(t,"* O   R   C   A *"))
4223 			{
4224 				ktype = GABEDIT_TYPEFILE_ORCA;
4225 				break;
4226 			}
4227 		}
4228 	}
4229 	rewind(file);
4230 	if( ktype == GABEDIT_TYPEFILE_UNKNOWN)
4231 	{
4232 		while(!feof(file))
4233 		{
4234     			if(!feof(file)) { char* e = fgets(t,taille,file);}
4235 			if(strstr(t,"VASP"))
4236 			{
4237 				ktype = GABEDIT_TYPEFILE_VASPOUTCAR;
4238 				break;
4239 			}
4240 		}
4241 	}
4242 	rewind(file);
4243 	if( ktype == GABEDIT_TYPEFILE_UNKNOWN)
4244 	{
4245 		while(!feof(file))
4246 		{
4247     			if(!feof(file)) { char* e = fgets(t,taille,file);}
4248 			if(strstr(t,"GAMESS"))
4249 			{
4250     				if(!feof(file)) { char* e = fgets(t,taille,file);}
4251 				if(strstr(t,"FROM IOWA STATE UNIVERSITY"))
4252 				ktype = GABEDIT_TYPEFILE_GAMESS;
4253 				break;
4254 			}
4255 		}
4256 	}
4257 	rewind(file);
4258 	if( ktype == GABEDIT_TYPEFILE_UNKNOWN)
4259 	{
4260     		if(!feof(file)) { char* e = fgets(t,taille,file);}
4261 		if(strstr(t,"START OF MOPAC FILE"))
4262 			ktype = GABEDIT_TYPEFILE_MOPAC_AUX;
4263 	}
4264 	rewind(file);
4265 	if( ktype == GABEDIT_TYPEFILE_UNKNOWN)
4266 	{
4267   		while(!feof(file) )
4268   		{
4269  			if(!fgets(t, taille, file))break;
4270 			if( strstr(t,"VARIABLE") && strstr(t,"FUNCTION"))
4271 			{
4272 				ktype = GABEDIT_TYPEFILE_MOPAC_SCAN;
4273 				break;
4274 			}
4275 		}
4276 	}
4277 	rewind(file);
4278 	if( ktype == GABEDIT_TYPEFILE_UNKNOWN)
4279 	{
4280   		while(!feof(file) )
4281   		{
4282  			if(!fgets(t, taille, file))break;
4283                  	if(strstr(t,"INTRINSIC REACTION COORDINATE") )
4284 			{
4285 				ktype = GABEDIT_TYPEFILE_MOPAC_IRC;
4286 				break;
4287 			}
4288 		}
4289 	}
4290 
4291  	g_free(t);
4292 	fclose(file);
4293 	return ktype;
4294 }
4295 /**********************************************/
get_type_input_file(gchar * fileName)4296 GabEditTypeFile get_type_input_file(gchar* fileName)
4297 {
4298 	FILE* file = FOpen(fileName, "rb");
4299 	if(test_type_program_orca(file))
4300 	{
4301 		fseek(file, 0L, SEEK_SET);
4302 		fclose(file);
4303 		return GABEDIT_TYPEFILE_ORCAINPUT;
4304 	}
4305 	if(test_type_program_firefly(file))
4306 	{
4307 		fseek(file, 0L, SEEK_SET);
4308 		fclose(file);
4309 		return GABEDIT_TYPEFILE_FIREFLYINPUT;
4310 	}
4311 	if(test_type_program_gamess(file))
4312 	{
4313 		fseek(file, 0L, SEEK_SET);
4314 		fclose(file);
4315 		return GABEDIT_TYPEFILE_GAMESSINPUT;
4316 	}
4317 	if(test_type_program_qchem(file))
4318 	{
4319 		fseek(file, 0L, SEEK_SET);
4320 		fclose(file);
4321 		return GABEDIT_TYPEFILE_QCHEMINPUT;
4322 	}
4323 	if(test_type_program_mopac(file))
4324 	{
4325 		fseek(file, 0L, SEEK_SET);
4326 		fclose(file);
4327 		return GABEDIT_TYPEFILE_MOPACINPUT;
4328 	}
4329 	if(test_type_program_mpqc(file))
4330 	{
4331 		fseek(file, 0L, SEEK_SET);
4332 		fclose(file);
4333 		return GABEDIT_TYPEFILE_MPQCINPUT;
4334 	}
4335 	if(test_type_program_gaussian(file))
4336 	{
4337 		fseek(file, 0L, SEEK_SET);
4338 		fclose(file);
4339 		return GABEDIT_TYPEFILE_GAUSSIANINPUT;
4340 	}
4341 	if(test_type_program_molcas(file))
4342 	{
4343 		fseek(file, 0L, SEEK_SET);
4344 		fclose(file);
4345 		return GABEDIT_TYPEFILE_MOLCASINPUT;
4346 	}
4347 	if(test_type_program_molpro(file))
4348 	{
4349 		fseek(file, 0L, SEEK_SET);
4350 		fclose(file);
4351 		return GABEDIT_TYPEFILE_MOLPROINPUT;
4352 	}
4353 	fseek(file, 0L, SEEK_SET);
4354 	fclose(file);
4355 	return GABEDIT_TYPEFILE_UNKNOWN;
4356 }
4357 /************************************************/
get_type_file(gchar * filename)4358 GabEditTypeFile get_type_file(gchar* filename)
4359 {
4360 	gchar* ext = NULL;
4361 	if(!filename) return GABEDIT_TYPEFILE_UNKNOWN;
4362 	ext = get_extenssion_file(filename);
4363 	if(!ext) return GABEDIT_TYPEFILE_UNKNOWN;
4364 	uppercase(ext);
4365 	if( !strcmp(ext,"INP") || !strcmp(ext,"COM") || !strcmp(ext,"IN") || !strcmp(ext,"MOP") )
4366 		return get_type_input_file(filename);
4367 	if( !strcmp(ext,"OUT") || !strcmp(ext,"LOG") )
4368 		return get_type_output_file(filename);
4369 	if( !strcmp(ext,"MOL2")) return GABEDIT_TYPEFILE_MOL2;
4370 	if( !strcmp(ext,"XYZ")) return GABEDIT_TYPEFILE_XYZ;
4371 	if( !strcmp(ext,"PDB")) return GABEDIT_TYPEFILE_PDB;
4372 	if( !strcmp(ext,"GZMT")) return GABEDIT_TYPEFILE_GZMAT;
4373 	if( !strcmp(ext,"ZMT")) return GABEDIT_TYPEFILE_MZMAT;
4374 	if( !strcmp(ext,"HIN")) return GABEDIT_TYPEFILE_HIN;
4375 	if( !strcmp(ext,"TNK")) return GABEDIT_TYPEFILE_TINKER;
4376 	if( !strcmp(ext,"GAB")) return GABEDIT_TYPEFILE_GABEDIT;
4377 	if( !strcmp(ext,"MOLDEN")) return GABEDIT_TYPEFILE_MOLDEN;
4378 	if( !strcmp(ext,"MFJ")) return GABEDIT_TYPEFILE_MOBCAL;
4379 	if( !strcmp(ext,"AUX")) return GABEDIT_TYPEFILE_MOPAC_AUX;
4380 	if( !strcmp(ext,"WFX")) return GABEDIT_TYPEFILE_WFX;
4381 	if( !strcmp(ext,"JPG")) return GABEDIT_TYPEFILE_JPEG;
4382 	if( !strcmp(ext,"JPEG")) return GABEDIT_TYPEFILE_JPEG;
4383 	if( !strcmp(ext,"PPM")) return GABEDIT_TYPEFILE_PPM;
4384 	if( !strcmp(ext,"BMP")) return GABEDIT_TYPEFILE_BMP;
4385 	if( !strcmp(ext,"PNG")) return GABEDIT_TYPEFILE_PNG;
4386 	if( !strcmp(ext,"PS")) return GABEDIT_TYPEFILE_PS;
4387 	if( !strcmp(ext,"T41")) return GABEDIT_TYPEFILE_CUBEADF;
4388 	if( !strcmp(ext,"GRID")) return GABEDIT_TYPEFILE_CUBEMOLCAS;
4389 	if( !strcmp(ext,"HF")) return GABEDIT_TYPEFILE_CUBEQCHEM;
4390 	if( !strcmp(ext,"GCUBE")) return GABEDIT_TYPEFILE_CUBEGABEDIT;
4391 	if( !strcmp(ext,"TRJ")) return GABEDIT_TYPEFILE_TRJ;
4392 	if( !strcmp(ext,"TXT")) return GABEDIT_TYPEFILE_TXT;
4393 	if( !strcmp(ext,"IRC")) return GABEDIT_TYPEFILE_GAMESSIRC;
4394 	if( !strcmp(ext,"CUBE") &&  !strcmp(filename,"CUBE")) return GABEDIT_TYPEFILE_CUBEMOLPRO;
4395 	if( !strcmp(ext,"CUBE")) return GABEDIT_TYPEFILE_CUBEGAUSS;
4396 	if( !strcmp(ext,"FCHK")) return GABEDIT_TYPEFILE_GAUSSIAN_FCHK;
4397 
4398 	return GABEDIT_TYPEFILE_UNKNOWN;
4399 }
4400 /************************************************/
mystrcasestr(G_CONST_RETURN gchar * haystack,G_CONST_RETURN gchar * needle)4401 gchar * mystrcasestr(G_CONST_RETURN gchar *haystack, G_CONST_RETURN gchar *needle)
4402 {
4403 	gchar *i, *startn = 0, *j = 0;
4404 	for (i = (gchar*)haystack; *i; i++)
4405 	{
4406 		if(j)
4407 		{
4408 			if (toupper(*i) == toupper(*j))
4409 			{
4410 				if (!*++j)
4411 				return startn;
4412 			}
4413 			else j = 0;
4414 		}
4415 		else if (toupper(*i) == toupper(*needle))
4416 		{
4417 			j = (gchar*)needle + 1;
4418 			startn = i;
4419 		}
4420 	}
4421 	return 0;
4422 }
4423 /****************************************************************************/
get_one_int_from_fchk_gaussian_file(FILE * file,gchar * blockName)4424 gint get_one_int_from_fchk_gaussian_file(FILE* file, gchar* blockName)
4425 {
4426 	gint ipos = 47;
4427 	gchar t[BBSIZE];
4428 	 while(!feof(file))
4429 	 {
4430 		if(!fgets(t,BBSIZE,file))break;
4431 		if(strstr( t, blockName))
4432 		{
4433 			if(strlen(t)>ipos+1) return atoi(t+ipos);
4434 			return -1;
4435 		}
4436 	 }
4437 	 return -1;
4438 }
4439 /****************************************************************************/
get_one_real_from_fchk_gaussian_file(FILE * file,gchar * blockName)4440 gdouble get_one_real_from_fchk_gaussian_file(FILE* file, gchar* blockName)
4441 {
4442 	gint ipos = 47;
4443 	gchar t[BBSIZE];
4444 	 while(!feof(file))
4445 	 {
4446 		if(!fgets(t,BBSIZE,file))break;
4447 		if(strstr( t, blockName))
4448 		{
4449 			if(strlen(t)>ipos+1) return atof(t+ipos);
4450 			return -1;
4451 		}
4452 	 }
4453 	 return -1;
4454 }
4455 /****************************************************************************/
get_array_int_from_fchk_gaussian_file(FILE * file,gchar * blockName,gint * nElements)4456 gint* get_array_int_from_fchk_gaussian_file(FILE* file, gchar* blockName, gint* nElements)
4457 {
4458 	gint ipos = 43;
4459 	gint i;
4460 	gchar t[BBSIZE];
4461 	gint* elements = NULL;
4462 	*nElements = 0;
4463 	 while(!feof(file))
4464 	 {
4465 		if(!fgets(t,BBSIZE,file))break;
4466 		if(strstr( t, blockName))
4467 		{
4468 			if(!(strstr( t, blockName) && strstr(t,"N=") && strlen(strstr(t,"N="))>2)) return elements;
4469 			if(strlen(t)>ipos+1 && t[ipos]!='I') return elements;
4470 			*nElements = atof(strstr(t,"N=")+2);
4471 			if(*nElements<1) return elements;
4472 			elements = g_malloc(*nElements*sizeof(gint));
4473 			for(i=0;i<*nElements;i++)
4474 			{
4475 				if(1!=fscanf(file,"%d",&elements[i])) break;
4476 			}
4477 			if(i!=*nElements)
4478 			{
4479 				*nElements = 0;
4480 				g_free(elements);
4481 				return NULL;
4482 			}
4483 			return elements;
4484 		}
4485 	 }
4486 	 return elements;
4487 }
4488 /****************************************************************************/
get_array_real_from_fchk_gaussian_file(FILE * file,gchar * blockName,gint * nElements)4489 gdouble* get_array_real_from_fchk_gaussian_file(FILE* file, gchar* blockName, gint* nElements)
4490 {
4491 	gint ipos = 43;
4492 	gint i;
4493 	gchar t[BBSIZE];
4494 	gdouble* elements = NULL;
4495 
4496 	*nElements = 0;
4497 	 while(!feof(file))
4498 	 {
4499 		if(!fgets(t,BBSIZE,file))break;
4500 		if(strstr( t, blockName))
4501 		{
4502 			if(!(strstr( t, blockName) && strstr(t,"N=") && strlen(strstr(t,"N="))>2)) return elements;
4503 			if(strlen(t)>ipos+1 && t[ipos]!='R') return elements;
4504 			*nElements = atof(strstr(t,"N=")+2);
4505 			if(*nElements<1) return elements;
4506 			elements = g_malloc(*nElements*sizeof(gdouble));
4507 			for(i=0;i<*nElements;i++)
4508 				if(1!=fscanf(file,"%lf",&elements[i])) break;
4509 			if(i!=*nElements)
4510 			{
4511 				*nElements = 0;
4512 				g_free(elements);
4513 				return NULL;
4514 			}
4515 			return elements;
4516 		}
4517 	 }
4518 	 return elements;
4519 }
4520 /****************************************************************************/
get_array_string_from_fchk_gaussian_file(FILE * file,gchar * blockName,gint * nElements)4521 gchar** get_array_string_from_fchk_gaussian_file(FILE* file, gchar* blockName, gint* nElements)
4522 {
4523 	gint ipos = 43;
4524 	gint i;
4525 	gchar t[BBSIZE];
4526 	gchar** elements = NULL;
4527 	gchar type = ' ';
4528 
4529 	*nElements = 0;
4530 	 while(!feof(file))
4531 	 {
4532 		if(!fgets(t,BBSIZE,file))break;
4533 		if(strstr( t, blockName))
4534 		{
4535 			if(!(strstr( t, blockName) && strstr(t,"N=") && strlen(strstr(t,"N="))>2)) return elements;
4536 			if(strlen(t)>ipos+1 && t[ipos]=='C') type = 'C';
4537 			if(strlen(t)>ipos+1 && t[ipos]=='H') type = 'H';
4538 			if(type!='C' && type!='H') return elements;
4539 			*nElements = atof(strstr(t,"N=")+2);
4540 			if(*nElements<1) return elements;
4541 			elements = g_malloc(*nElements*sizeof(gchar*));
4542 			for(i=0;i<*nElements;i++) elements[i] = NULL;
4543 			if(type=='C')
4544 			for(i=0;i<*nElements;i++)
4545 			{
4546 				if(1!=fscanf(file,"%12s",t)) break;
4547 				elements[i] = g_strdup(t);
4548 			}
4549 			else
4550 			for(i=0;i<*nElements;i++)
4551 			{
4552 				if(1!=fscanf(file,"%8s",t)) break;
4553 				elements[i] = g_strdup(t);
4554 			}
4555 
4556 			if(i!=*nElements)
4557 			{
4558 				*nElements = 0;
4559 				g_free(elements);
4560 				return NULL;
4561 			}
4562 			return elements;
4563 		}
4564 	 }
4565 	 return elements;
4566 }
4567 /*************************************************************************************/
getvScaleBond(gdouble r,gdouble Center1[],gdouble Center2[],gdouble vScal[])4568 void getvScaleBond(gdouble r, gdouble Center1[], gdouble Center2[], gdouble vScal[])
4569 {
4570 	gint l;
4571 	V3d cros;
4572 	V3d sub;
4573 	V3d C0={0,0,0};
4574 	gdouble C10[3];
4575 	gdouble C20[3];
4576 	gdouble CC1[3];
4577 	gdouble CC2[3];
4578   	for(l=0;l<3;l++) vScal[l] = r*0.5;
4579 	for(l=0;l<3;l++) CC1[l] = Center1[l];
4580 	for(l=0;l<3;l++) CC2[l] = Center2[l];
4581 	v3d_sub(C0, CC1, C10);
4582 	v3d_sub(C0, CC2, C20);
4583 	v3d_cross(C10, C20, cros);
4584 	v3d_sub(CC1, CC2, sub);
4585 	v3d_cross(cros, sub, vScal);
4586 	if(v3d_dot(vScal,vScal)!=0)
4587 	{
4588 		v3d_normal(vScal);
4589 		v3d_scale(vScal, r*0.5);
4590 	}
4591 	else
4592 	{
4593         	gdouble d = 0;
4594 		gint j,k;
4595 		/* printf("Warning vScal in getvScaleBond/Utils.c = 0\n");*/
4596 		v3d_normal(sub);
4597 /*      find an orthogonal vector to CC1-CC2 */
4598 		k = 0;
4599         	for(j=1;j<3;j++) if(fabs(sub[k])>fabs(sub[j])) k = j;
4600         	for(j=0;j<3;j++) vScal[j] = -sub[k] * sub[j];
4601         	vScal[k] += 1.0;
4602 		v3d_normal(vScal);
4603 		v3d_scale(vScal, r*0.5);
4604 	}
4605 }
4606 /*************************************************************************************/
getPositionsRadiusBond3(gdouble r,gdouble Orig[],gdouble Ci[],gdouble Cj[],gdouble C11[],gdouble C12[],gdouble C21[],gdouble C22[],gdouble C31[],gdouble C32[],gdouble radius[],gint type)4607 void getPositionsRadiusBond3(gdouble r, gdouble Orig[], gdouble Ci[], gdouble Cj[], gdouble C11[], gdouble C12[],  gdouble C21[],  gdouble C22[], gdouble C31[],  gdouble C32[], gdouble radius[], gint type)
4608 {
4609 	gdouble s = 1.8;
4610 	V3d vScal;
4611 	gint k;
4612 
4613 	for(k=0;k<3;k++) Ci[k] -= Orig[k];
4614 	for(k=0;k<3;k++) Cj[k] -= Orig[k];
4615 
4616 	getvScaleBond(r, Ci, Cj, vScal);
4617 	if(type==0)
4618 	{
4619 		s = 2.8;
4620 		radius[0] = r/4;
4621 		radius[1] = r;
4622 		radius[2] = r/4;
4623 	}
4624 	else
4625 	{
4626 		s = 2.8;
4627 		radius[0] = r/2;
4628 		radius[1] = r/2;
4629 		radius[2] = r/2;
4630 	}
4631 	for(k=0;k<3;k++) C11[k] = Ci[k]-s*vScal[k];
4632 	for(k=0;k<3;k++) C12[k] = Cj[k]-s*vScal[k];
4633 	for(k=0;k<3;k++) C21[k] = Ci[k];
4634 	for(k=0;k<3;k++) C22[k] = Cj[k];
4635 	for(k=0;k<3;k++) C31[k] = Ci[k]+s*vScal[k];
4636 	for(k=0;k<3;k++) C32[k] = Cj[k]+s*vScal[k];
4637 
4638 	for(k=0;k<3;k++) Ci[k] += Orig[k];
4639 	for(k=0;k<3;k++) Cj[k] += Orig[k];
4640 	for(k=0;k<3;k++) C11[k] += Orig[k];
4641 	for(k=0;k<3;k++) C12[k] += Orig[k];
4642 	for(k=0;k<3;k++) C21[k] += Orig[k];
4643 	for(k=0;k<3;k++) C22[k] += Orig[k];
4644 	for(k=0;k<3;k++) C31[k] += Orig[k];
4645 	for(k=0;k<3;k++) C32[k] += Orig[k];
4646 }
4647 /*************************************************************************************/
getPositionsRadiusBond2(gdouble r,gdouble Orig[],gdouble Ci[],gdouble Cj[],gdouble C11[],gdouble C12[],gdouble C21[],gdouble C22[],gdouble radius[],gint type)4648 void getPositionsRadiusBond2(gdouble r, gdouble Orig[], gdouble Ci[], gdouble Cj[], gdouble C11[], gdouble C12[],  gdouble C21[],  gdouble C22[], gdouble radius[], gint type)
4649 {
4650 /* type=0=>stick, type=1=>ball&stick */
4651 	gdouble s = 1.5;
4652 	V3d vScal;
4653 	gint k;
4654 
4655 	for(k=0;k<3;k++) Ci[k] -= Orig[k];
4656 	for(k=0;k<3;k++) Cj[k] -= Orig[k];
4657 	getvScaleBond(r, Ci, Cj, vScal);
4658 
4659 	radius[2] = 0;
4660 	if(type==0)
4661 	{
4662 		s = 2.8;
4663 		radius[0] = r/4;
4664 		radius[1] = r;
4665 		for(k=0;k<3;k++) C11[k] = Ci[k]-s*vScal[k];
4666 		for(k=0;k<3;k++) C12[k] = Cj[k]-s*vScal[k];
4667 		for(k=0;k<3;k++) C21[k] = Ci[k];
4668 		for(k=0;k<3;k++) C22[k] = Cj[k];
4669 	}
4670 	else
4671 	{
4672 		s = 1.5;
4673 		radius[0] = r/1.5;
4674 		radius[1] = r/1.5;
4675 		for(k=0;k<3;k++) C11[k] = Ci[k]-s*vScal[k];
4676 		for(k=0;k<3;k++) C12[k] = Cj[k]-s*vScal[k];
4677 		for(k=0;k<3;k++) C21[k] = Ci[k]+s*vScal[k];
4678 		for(k=0;k<3;k++) C22[k] = Cj[k]+s*vScal[k];
4679 	}
4680 	for(k=0;k<3;k++) Ci[k] += Orig[k];
4681 	for(k=0;k<3;k++) Cj[k] += Orig[k];
4682 	for(k=0;k<3;k++) C11[k] += Orig[k];
4683 	for(k=0;k<3;k++) C12[k] += Orig[k];
4684 	for(k=0;k<3;k++) C21[k] += Orig[k];
4685 	for(k=0;k<3;k++) C22[k] += Orig[k];
4686 }
4687 /*********************************************************************************************************************/
getCoefsGradient(gint nBoundary,gdouble xh,gdouble yh,gdouble zh,gdouble * fcx,gdouble * fcy,gdouble * fcz)4688 void getCoefsGradient(gint nBoundary, gdouble xh, gdouble yh, gdouble zh, gdouble* fcx, gdouble* fcy, gdouble* fcz)
4689 {
4690 	gdouble* coefs =  g_malloc((nBoundary)*sizeof(gdouble));
4691 	gdouble xxh = 1.0;
4692 	gdouble yyh = 1.0;
4693 	gdouble zzh = 1.0;
4694 	gint i;
4695 
4696 	switch(nBoundary)
4697 	{
4698 		case 1:{
4699 				gdouble denom = 2.0;
4700 				gdouble c[] = {-1.0};
4701 				for(i=0;i<nBoundary;i++) coefs[i] = c[i]/denom;
4702 				break;
4703 			}
4704 		case 2:{
4705 				gdouble denom =12.0;
4706 				gdouble c[] = { 1.0, -8.0};
4707 				for(i=0;i<nBoundary;i++) coefs[i] = c[i]/denom;
4708 				break;
4709 			}
4710 		case 3:{
4711 				gdouble denom =60.0;
4712 				gdouble c[] = { -1.0, +9.0, -45.0};
4713 				for(i=0;i<nBoundary;i++) coefs[i] = c[i]/denom;
4714 				break;
4715 			}
4716 		case 4:{
4717 				gdouble denom =840.0;
4718 				gdouble c[] = { 3.0, -32.0, +168.0, -672.0};
4719 				for(i=0;i<nBoundary;i++) coefs[i] = c[i]/denom;
4720 				break;
4721 			}
4722 		case 5:{
4723 				gdouble denom =2520.0 ;
4724 				gdouble c[] = { -2.0, +25.0, -150.0,+600.0, -2100.0};
4725 				for(i=0;i<nBoundary;i++) coefs[i] = c[i]/denom;
4726 				break;
4727 			}
4728 		case 6:{
4729 				gdouble denom =27720.0 ;
4730 				gdouble c[] = { 5.0, -72.0, +495.0, -2200.0, +7425.0, -23760.0};
4731 				for(i=0;i<nBoundary;i++) coefs[i] = c[i]/denom;
4732 				break;
4733 			}
4734 		case 7:{
4735 				gdouble denom =360360.0;
4736 				gdouble c[] = { -15.0, +245.0, -1911.0, +9555.0, -35035.0, +105105.0, -315315.0};
4737 				for(i=0;i<nBoundary;i++) coefs[i] = c[i]/denom;
4738 				break;
4739 			}
4740 		case 8:{
4741 				gdouble denom =720720.0;
4742 				gdouble c[] = { 7.0, -128.0, +1120.0, -6272.0, +25480.0, -81536.0, +224224.0, -640640.0};
4743 				for(i=0;i<nBoundary;i++) coefs[i] = c[i]/denom;
4744 				break;
4745 			}
4746 	}
4747 
4748 	xxh = 1.0 / (xh);
4749 	yyh = 1.0 / (yh);
4750 	zzh = 1.0 / (zh);
4751 
4752 	for(i=0;i<nBoundary;i++)
4753 	{
4754 		fcx[i] =  xxh * coefs[i];
4755 		fcy[i] =  yyh * coefs[i];
4756 		fcz[i] =  zzh * coefs[i];
4757 	}
4758 
4759 	g_free(coefs);
4760 }
4761 /*********************************************************************************************************************************/
getCoefsLaplacian(gint nBoundary,gdouble xh,gdouble yh,gdouble zh,gdouble * fcx,gdouble * fcy,gdouble * fcz,gdouble * cc)4762 void getCoefsLaplacian(gint nBoundary, gdouble xh, gdouble yh, gdouble zh, gdouble* fcx, gdouble* fcy, gdouble* fcz, gdouble* cc)
4763 {
4764 	gdouble* coefs =  g_malloc((nBoundary+1)*sizeof(gdouble));
4765 	gdouble x2h = 1.0 / (xh * xh);
4766 	gdouble y2h = 1.0 / (yh * yh);
4767 	gdouble z2h = 1.0 / (zh * zh);
4768 	gint i;
4769 
4770 	switch(nBoundary)
4771 	{
4772 		case 1:{
4773 				gdouble c[] = {-2.0, 1.0};
4774 				for(i=0;i<=nBoundary;i++)
4775 					coefs[i] = c[i];
4776 				break;
4777 			}
4778 		case 2:{
4779 				gdouble denom = 12.0;
4780 				gdouble c[] = {-30.0, 16.0, -1.0};
4781 				for(i=0;i<=nBoundary;i++)
4782 					coefs[i] = c[i]/denom;
4783 				break;
4784 			}
4785 		case 3:{
4786 				gdouble denom = 180.0;
4787 				gdouble c[] = {-490.0, 270.0,-27.0, 2.0};
4788 				for(i=0;i<=nBoundary;i++)
4789 					coefs[i] = c[i]/denom;
4790 				break;
4791 			}
4792 		case 4:{
4793 				gdouble denom = 5040.0;
4794 				gdouble c[] = {-14350.0, 8064.0, -1008.0, 128.0, -9.0};
4795 				for(i=0;i<=nBoundary;i++)
4796 					coefs[i] = c[i]/denom;
4797 				break;
4798 			}
4799 		case 5:{
4800 				gdouble denom = 25200.0;
4801 				gdouble c[] = {-73766.0, 42000.0, -6000.0, 1000.0, -125.0, 8.0};
4802 				for(i=0;i<=nBoundary;i++)
4803 					coefs[i] = c[i]/denom;
4804 				break;
4805 			}
4806 		case 6:{
4807 				gdouble denom = 831600.0;
4808 			 	gdouble c[] = {-2480478.0,1425600.0,-222750.0,44000.0,-7425.0,864.0,-50.0};
4809 				for(i=0;i<=nBoundary;i++)
4810 					coefs[i] = c[i]/denom;
4811 				break;
4812 			}
4813 		case 7:{
4814 				gdouble denom = 75675600.0;
4815 				gdouble c[] = {-228812298.0,132432300.0,-22072050.0,4904900.0,-1003275.0, 160524.0,-17150.0,900.0};
4816 				for(i=0;i<=nBoundary;i++)
4817 					coefs[i] = c[i]/denom;
4818 				break;
4819 			}
4820 		case 8:{
4821 				gdouble denom = 302702400.0;
4822 				gdouble c[] = {-924708642.0,538137600.0,-94174080.0,22830080.0,-5350800.0,1053696.0,-156800.0,15360.0,-735.0};
4823 				for(i=0;i<=nBoundary;i++)
4824 					coefs[i] = c[i]/denom;
4825 				break;
4826 			}
4827 	}
4828 
4829 	*cc = x2h + y2h + z2h;
4830 	*cc *= coefs[0];
4831 
4832 	for(i=0;i<=nBoundary;i++)
4833 	{
4834 		fcx[i] =  x2h * coefs[i];
4835 		fcy[i] =  y2h * coefs[i];
4836 		fcz[i] =  z2h * coefs[i];
4837 	}
4838 
4839 	g_free(coefs);
4840 }
4841 /*********************************************************************************/
swapDouble(gdouble * a,gdouble * b)4842 void swapDouble(gdouble* a, gdouble* b)
4843 {
4844 	gdouble c = *a;
4845 	*a = *b;
4846 	*b = c;
4847 }
4848 /****************************************************************************************************************************/
newVectorDouble(gint n)4849 gdouble* newVectorDouble(gint n)
4850 {
4851 	gdouble* v = NULL;
4852 	if(n<1) return v;
4853 	v = g_malloc(n*sizeof(gdouble));
4854 	return v;
4855 }
4856 /****************************************************************************************************************************/
initVectorDouble(gdouble * v,gint n,gdouble val)4857 void initVectorDouble(gdouble* v, gint n, gdouble val)
4858 {
4859 	gint i;
4860 	if(!v) return;
4861 	for(i = 0;i<n; i++)  v[i] = val;
4862 }
4863 /****************************************************************************************************************************/
freeVectorDouble(gdouble ** v)4864 void freeVectorDouble(gdouble** v)
4865 {
4866 	if(*v) g_free(*v);
4867 	*v= NULL;
4868 }
4869 /****************************************************************************************************************************/
printVectorDoubleCutOff(gdouble * C,gint n,gdouble cutoff)4870 void printVectorDoubleCutOff(gdouble* C, gint n, gdouble cutoff)
4871 {
4872 	gint i;
4873 	if(!C) return;
4874 	for(i=0;i<n;i++) if(fabs(C[i])>=cutoff) printf("%d %20.10f\n",i+1,C[i]);
4875 }
4876 /****************************************************************************************************************************/
newMatrixDouble(gint nrows,gint ncolumns)4877 gdouble** newMatrixDouble(gint nrows, gint ncolumns)
4878 {
4879 
4880 	gdouble** M  = NULL;
4881 	if(nrows<1 || ncolumns<1) return M;
4882 	M  = g_malloc(nrows*sizeof(gdouble*));
4883 	gint i;
4884 	for(i = 0;i<nrows; i++)
4885 		M[i] = g_malloc(ncolumns*sizeof(gdouble));
4886 	return M;
4887 }
4888 /****************************************************************************************************************************/
freeMatrixDouble(gdouble *** M,gint nrows)4889 void freeMatrixDouble(gdouble*** M, gint nrows)
4890 {
4891 	if(*M)
4892 	{
4893 		gint i;
4894 		for(i = 0;i<nrows; i++)
4895 			if((*M)[i])g_free((*M)[i]);
4896 	}
4897 	*M= NULL;
4898 }
4899 /****************************************************************************************************************************/
initMatrixDouble(gdouble ** M,gint nrows,gint ncolumns,gdouble val)4900 void initMatrixDouble(gdouble** M, gint nrows, gint ncolumns, gdouble val)
4901 {
4902 	gint i,j;
4903 	if(!M) return;
4904 	for(i = 0;i<nrows; i++)
4905 		for(j = 0;j<ncolumns; j++)  M[i][j]  = val;
4906 }
4907 /****************************************************************************************************************************/
symmetrizeMatrixDouble(gdouble ** M,gint nrows,gint ncolumns,gdouble cutOff)4908 void symmetrizeMatrixDouble(gdouble** M, gint nrows, gint ncolumns, gdouble cutOff)
4909 {
4910 	gint i,j;
4911 	gdouble x,y;
4912 	if(!M) return;
4913 	for(i = 0;i<nrows; i++)
4914 		for(j = 0;j<i; j++)
4915 		{
4916 			if(j>ncolumns-1) continue;
4917 			x =  M[i][j];
4918 			y =  M[j][i];
4919 			if(fabs(x)>cutOff && fabs(y)>cutOff)  M[i][j] = M[j][i] = (x+y)/2;
4920 			else if(fabs(x)>cutOff)  M[i][j] = M[j][i] = x;
4921 			else if(fabs(y)>cutOff)  M[i][j] = M[j][i] = y;
4922 			else M[i][j]  = 0.0;
4923 		}
4924 }
4925 /****************************************************************************************************************************/
printMatrixDouble(gdouble ** M,gint nrows,gint ncolumns)4926 void printMatrixDouble(gdouble** M, gint nrows, gint ncolumns)
4927 {
4928 	gint i,j;
4929 	for(i = 0;i<nrows; i++)
4930 	{
4931 		for(j = 0;j<ncolumns; j++)
4932       			printf("%f ",M[i][j]);
4933 		printf("\n");
4934 	}
4935 }
4936 /****************************************************************************************************************************/
printMatrixDoubleCutOff(gdouble ** M,gint nrows,gint ncolumns,gdouble cutoff)4937 void printMatrixDoubleCutOff(gdouble** M, gint nrows, gint ncolumns, gdouble cutoff)
4938 {
4939 	gint i,j;
4940 	for(i = 0;i<nrows; i++)
4941 	{
4942 		for(j = 0;j<ncolumns; j++)
4943 		if(fabs(M[i][j])>=cutoff)
4944       			printf("%d %d %20.10f\n",i+1,j+1,M[i][j]);
4945 	}
4946 }
4947 /****************************************************************************************************************************/
newCubeDouble(gint nrows,gint ncolumns,gint nslices)4948 gdouble*** newCubeDouble(gint nrows, gint ncolumns, gint nslices)
4949 {
4950 	gdouble*** C  = NULL;
4951 	gint i,j;
4952 	if(nrows<1 || ncolumns<1 || nslices<1) return C;
4953 	C  = g_malloc(nrows*sizeof(gdouble**));
4954 	for(i = 0;i<nrows; i++)
4955 	{
4956 		C[i] = g_malloc(ncolumns*sizeof(gdouble*));
4957 		for(j = 0;j<ncolumns; j++)
4958 			C[i][j] = g_malloc(nslices*sizeof(gdouble));
4959 	}
4960 	return C;
4961 }
4962 /****************************************************************************************************************************/
printCubeDouble(gdouble *** C,gint nrows,gint ncolumns,gint nslices)4963 void printCubeDouble(gdouble*** C, gint nrows, gint ncolumns, gint nslices)
4964 {
4965 	gint i,j,k;
4966 	for(i = 0;i<nrows; i++)
4967 	{
4968 		for(j = 0;j<ncolumns; j++)
4969 		{
4970 			for(k = 0;k<nslices; k++)
4971       				printf("%f ",C[i][j][k]);
4972 			printf("\n");
4973 		}
4974 		printf("\n");
4975 	}
4976 }
4977 /****************************************************************************************************************************/
printCubeDoubleCutOff(gdouble *** C,gint nrows,gint ncolumns,gint nslices,gdouble cutoff)4978 void printCubeDoubleCutOff(gdouble*** C, gint nrows, gint ncolumns, gint nslices, gdouble cutoff)
4979 {
4980 	gint i,j,k;
4981 	for(i = 0;i<nrows; i++)
4982 	{
4983 		for(j = 0;j<ncolumns; j++)
4984 		{
4985 			for(k = 0;k<nslices; k++)
4986 			if(fabs(C[i][j][k])>=cutoff)
4987       				printf("%d %d %d %20.10f\n",i+1,j+1,k+1,C[i][j][k]);
4988 		}
4989 	}
4990 }
4991 /****************************************************************************************************************************/
symmetrizeCubeDoubleIJ(gdouble *** C,gint nrows,gint ncolumns,gint nslices,gdouble cutOff)4992 static void symmetrizeCubeDoubleIJ(gdouble*** C, gint nrows, gint ncolumns,  gint nslices, gdouble cutOff)
4993 {
4994 	gint i,j,k;
4995 	gdouble x,y;
4996 	for(i = 0;i<nrows; i++)
4997 		for(j = 0;j<i; j++)
4998 		{
4999 			if(j>ncolumns-1) continue;
5000 			for(k = 0;k<nslices; k++)
5001 			{
5002 				x =  C[i][j][k];
5003 				y =  C[j][i][k];
5004 				if(fabs(x)>cutOff && fabs(y)>cutOff)  C[i][j][k] = C[j][i][k] = (x+y)/2;
5005 				else if(fabs(x)>cutOff)  C[i][j][k] = C[j][i][k] = x;
5006 				else if(fabs(y)>cutOff)  C[i][j][k] = C[j][i][k] = y;
5007 				else C[i][j][k]  = 0.0;
5008 			}
5009 		}
5010 }
5011 /****************************************************************************************************************************/
symmetrizeCubeDoubleJK(gdouble *** C,gint nrows,gint ncolumns,gint nslices,gdouble cutOff)5012 static void symmetrizeCubeDoubleJK(gdouble*** C, gint nrows, gint ncolumns,  gint nslices, gdouble cutOff)
5013 {
5014 	gint i;
5015 	for(i=0;i<nrows;i++) symmetrizeMatrixDouble(C[i], ncolumns, nslices,  cutOff);
5016 }
5017 /****************************************************************************************************************************/
symmetrizeCubeDouble(gdouble *** C,gint nrows,gint ncolumns,gint nslices,gdouble cutOff)5018 void symmetrizeCubeDouble(gdouble*** C, gint nrows, gint ncolumns,  gint nslices, gdouble cutOff)
5019 {
5020 	symmetrizeCubeDoubleIJ(C, nrows, ncolumns,  nslices, cutOff);
5021 	symmetrizeCubeDoubleJK(C, nrows, ncolumns,  nslices, cutOff);
5022 	symmetrizeCubeDoubleIJ(C, nrows, ncolumns,  nslices, cutOff);
5023 }
5024 /****************************************************************************************************************************/
initCubeDouble(gdouble *** C,gint nrows,gint ncolumns,gint nslices,gdouble val)5025 void initCubeDouble(gdouble*** C, gint nrows, gint ncolumns, gint nslices, gdouble val)
5026 {
5027 	gint i,j,k;
5028 	if(!C) return;
5029 	for(i = 0;i<nrows; i++)
5030 		for(j = 0;j<ncolumns; j++)
5031 			for(k = 0;k<nslices; k++) C[i][j][k] = val;
5032 }
5033 /****************************************************************************************************************************/
freeCubeDouble(gdouble **** C,gint nrows,gint ncolumns)5034 void freeCubeDouble(gdouble**** C, gint nrows, gint ncolumns)
5035 {
5036 	if(*C)
5037 	{
5038 		gint i,j;
5039 		for(i = 0;i<nrows; i++)
5040 		{
5041 			if((*C)[i])
5042 			for(j = 0;j<ncolumns; j++)
5043 				if((*C)[i][j]) g_free((*C)[i][j]);
5044 
5045 			if((*C)[i])g_free((*C)[i]);
5046 		}
5047 	}
5048 	*C= NULL;
5049 }
5050 /****************************************************************************************************************************/
newQuarticDouble(gint nrows,gint ncolumns,gint nslices,gint nl)5051 gdouble**** newQuarticDouble(gint nrows, gint ncolumns, gint nslices, gint nl)
5052 {
5053 	gdouble**** C  = NULL;
5054 	gint i,j,k;
5055 	if(nrows<1 || ncolumns<1 || nslices<1) return C;
5056 	C  = g_malloc(nrows*sizeof(gdouble***));
5057 	for(i = 0;i<nrows; i++)
5058 	{
5059 		C[i] = g_malloc(ncolumns*sizeof(gdouble**));
5060 		for(j = 0;j<ncolumns; j++)
5061 		{
5062 			C[i][j] = g_malloc(nslices*sizeof(gdouble*));
5063 			for(k = 0;k<nslices; k++)
5064 			C[i][j][k] = g_malloc(nslices*sizeof(gdouble));
5065 		}
5066 	}
5067 	return C;
5068 }
5069 /****************************************************************************************************************************/
printQuarticDouble(gdouble **** C,gint nrows,gint ncolumns,gint nslices,gint nl)5070 void printQuarticDouble(gdouble**** C, gint nrows, gint ncolumns, gint nslices, gint nl)
5071 {
5072 	gint i,j,k,l;
5073 	for(i = 0;i<nrows; i++)
5074 	{
5075 		for(j = 0;j<ncolumns; j++)
5076 		{
5077 			for(k = 0;k<nslices; k++)
5078 			{
5079 				for(l = 0;l<nl; l++)
5080       					printf("%f ",C[i][j][k][l]);
5081 				printf("\n");
5082 			}
5083 		}
5084 		printf("\n");
5085 	}
5086 }
5087 /****************************************************************************************************************************/
printQuarticDoubleCutOff(gdouble **** C,gint nrows,gint ncolumns,gint nslices,gint nl,gdouble cutoff)5088 void printQuarticDoubleCutOff(gdouble**** C, gint nrows, gint ncolumns, gint nslices, gint nl, gdouble cutoff)
5089 {
5090 	gint i,j,k,l;
5091 	for(i = 0;i<nrows; i++)
5092 	{
5093 		for(j = 0;j<ncolumns; j++)
5094 		{
5095 			for(k = 0;k<nslices; k++)
5096 			{
5097 				for(l = 0;l<nl; l++)
5098 				if(fabs(C[i][j][k][l])>=cutoff)
5099       					printf("%d %d %d %d %20.10f\n",i+1,j+1,k+1,l+1,C[i][j][k][l]);
5100 			}
5101 		}
5102 	}
5103 }
5104 /****************************************************************************************************************************/
initQuarticDouble(gdouble **** C,gint nrows,gint ncolumns,gint nslices,gint nl,gdouble val)5105 void initQuarticDouble(gdouble**** C, gint nrows, gint ncolumns, gint nslices, gint nl, gdouble val)
5106 {
5107 	gint i,j,k,l;
5108 	if(!C) return;
5109 	for(i = 0;i<nrows; i++)
5110 		for(j = 0;j<ncolumns; j++)
5111 			for(k = 0;k<nslices; k++)
5112 				for(l = 0;l<nl; l++)
5113 					C[i][j][k][l] = val;
5114 }
5115 /****************************************************************************************************************************/
freeQuarticDouble(gdouble ***** C,gint nrows,gint ncolumns,gint nl)5116 void freeQuarticDouble(gdouble***** C, gint nrows, gint ncolumns, gint nl)
5117 {
5118 	if(*C)
5119 	{
5120 		gint i,j,k;
5121 		for(i = 0;i<nrows; i++)
5122 		{
5123 			if((*C)[i])
5124 			for(j = 0;j<ncolumns; j++)
5125 			{
5126 				if((*C)[i][j])
5127 				{
5128 				for(k = 0;k<nl; k++)
5129 					if((*C)[i][j][k]) g_free((*C)[i][j][k]);
5130 				if((*C)[i][j]) g_free((*C)[i][j]);
5131 				}
5132 			}
5133 			if((*C)[i])g_free((*C)[i]);
5134 		}
5135 		g_free(*C);
5136 	}
5137 	*C= NULL;
5138 }
5139 /****************************************************************************************************************************/
symmetrizeQuarticDoubleIJ(gdouble **** Q,gint nrows,gint ncolumns,gint nslices,gint nq,gdouble cutOff)5140 static void symmetrizeQuarticDoubleIJ(gdouble**** Q, gint nrows, gint ncolumns,  gint nslices, gint nq, gdouble cutOff)
5141 {
5142 	gint i,j,k,l;
5143 	gdouble x,y;
5144 	for(i = 0;i<nrows; i++)
5145 		for(j = 0;j<i; j++)
5146 		{
5147 			if(j>ncolumns-1) continue;
5148 			for(k = 0;k<nslices; k++)
5149 			for(l = 0;l<nq; l++)
5150 			{
5151 				x =  Q[i][j][k][l];
5152 				y =  Q[j][i][k][l];
5153 				if(fabs(x)>cutOff && fabs(y)>cutOff)  Q[i][j][k][l] = Q[j][i][k][l] = (x+y)/2;
5154 				else if(fabs(x)>cutOff)  Q[i][j][k][l] = Q[j][i][k][l] = x;
5155 				else if(fabs(y)>cutOff)  Q[i][j][k][l] = Q[j][i][k][l] = y;
5156 				else Q[i][j][k][l]  = 0.0;
5157 			}
5158 		}
5159 }
5160 /****************************************************************************************************************************/
symmetrizeQuarticDoubleJKL(gdouble **** Q,gint nrows,gint ncolumns,gint nslices,gint nq,gdouble cutOff)5161 static void symmetrizeQuarticDoubleJKL(gdouble**** Q, gint nrows, gint ncolumns,  gint nslices, gint nq, gdouble cutOff)
5162 {
5163 	gint i;
5164 	for(i=0;i<nrows;i++) symmetrizeCubeDouble(Q[i], ncolumns, nslices, nq,  cutOff);
5165 }
5166 /****************************************************************************************************************************/
symmetrizeQuarticDouble(gdouble **** Q,gint nrows,gint ncolumns,gint nslices,gint nq,gdouble cutOff)5167 void symmetrizeQuarticDouble(gdouble**** Q, gint nrows, gint ncolumns,  gint nslices, gint nq, gdouble cutOff)
5168 {
5169 	symmetrizeQuarticDoubleIJ(Q, nrows, ncolumns,  nslices, nq, cutOff);
5170 	symmetrizeQuarticDoubleJKL(Q, nrows, ncolumns,  nslices, nq, cutOff);
5171 	symmetrizeQuarticDoubleIJ(Q, nrows, ncolumns,  nslices, nq, cutOff);
5172 }
5173 /****************************************************************************************************************************/
newVectorInt(gint n)5174 gint* newVectorInt(gint n)
5175 {
5176 	gint* v = NULL;
5177 	if(n<1) return v;
5178 	v = g_malloc(n*sizeof(gint));
5179 	return v;
5180 }
5181 /****************************************************************************************************************************/
initVectorInt(gint * v,gint n,gint val)5182 void initVectorInt(gint* v, gint n, gint val)
5183 {
5184 	gint i;
5185 	if(!v) return;
5186 	for(i = 0;i<n; i++)  v[i] = val;
5187 }
5188 /****************************************************************************************************************************/
freeVectorInt(gint ** v)5189 void freeVectorInt(gint** v)
5190 {
5191 	if(*v) g_free(*v);
5192 	*v= NULL;
5193 }
5194 /****************************************************************************************************************************/
newMatrixInt(gint nrows,gint ncolumns)5195 gint** newMatrixInt(gint nrows, gint ncolumns)
5196 {
5197 	gint** M  = NULL;
5198 	if(nrows<1 || ncolumns<1) return M;
5199 	M  = g_malloc(nrows*sizeof(gint*));
5200 	gint i;
5201 	for(i = 0;i<nrows; i++)
5202 		M[i] = g_malloc(ncolumns*sizeof(gint));
5203 	return M;
5204 }
5205 /****************************************************************************************************************************/
initMatrixInt(gint ** M,gint nrows,gint ncolumns,gint val)5206 void initMatrixInt(gint** M, gint nrows, gint ncolumns, gint val)
5207 {
5208 	gint i,j;
5209 	if(!M) return;
5210 	for(i = 0;i<nrows; i++)
5211 		for(j = 0;j<ncolumns; j++)  M[i][j]  = val;
5212 }
5213 /****************************************************************************************************************************/
freeMatrixInt(gint *** M,gint nrows)5214 void freeMatrixInt(gint*** M, gint nrows)
5215 {
5216 	if(*M)
5217 	{
5218 		gint i;
5219 		for(i = 0;i<nrows; i++)
5220 			if((*M)[i])g_free((*M)[i]);
5221 	}
5222 	*M= NULL;
5223 }
5224 /****************************************************************************************************************************/
newCubeInt(gint nrows,gint ncolumns,gint nslices)5225 gint*** newCubeInt(gint nrows, gint ncolumns, gint nslices)
5226 {
5227 	gint*** C  = NULL;
5228 	gint i,j;
5229 	if(nrows<1 || ncolumns<1 || nslices<1) return C;
5230 	C  = g_malloc(nrows*sizeof(gint**));
5231 	for(i = 0;i<nrows; i++)
5232 	{
5233 		C[i] = g_malloc(ncolumns*sizeof(gint*));
5234 		for(j = 0;j<ncolumns; j++)
5235 			C[i][j] = g_malloc(nslices*sizeof(gint));
5236 	}
5237 	return C;
5238 }
5239 /****************************************************************************************************************************/
initCubeInt(gint *** C,gint nrows,gint ncolumns,gint nslices,gint val)5240 void initCubeInt(gint*** C, gint nrows, gint ncolumns, gint nslices, gint val)
5241 {
5242 	gint i,j,k;
5243 	if(!C) return;
5244 	for(i = 0;i<nrows; i++)
5245 		for(j = 0;j<ncolumns; j++)
5246 			for(k = 0;k<nslices; k++) C[i][j][k] = val;
5247 }
5248 /****************************************************************************************************************************/
freeCubeInt(gint **** C,gint nrows,gint ncolumns)5249 void freeCubeInt(gint**** C, gint nrows, gint ncolumns)
5250 {
5251 	if(*C)
5252 	{
5253 		gint i,j;
5254 		for(i = 0;i<nrows; i++)
5255 		{
5256 			if((*C)[i])
5257 			for(j = 0;j<ncolumns; j++)
5258 				if((*C)[i][j]) g_free((*C)[i][j]);
5259 
5260 			if((*C)[i])g_free((*C)[i]);
5261 		}
5262 	}
5263 	*C= NULL;
5264 }
5265 /****************************************************************************************************************************/
newQuarticInt(gint nrows,gint ncolumns,gint nslices,gint nl)5266 gint**** newQuarticInt(gint nrows, gint ncolumns, gint nslices, gint nl)
5267 {
5268 	gint**** C  = NULL;
5269 	gint i,j,k;
5270 	if(nrows<1 || ncolumns<1 || nslices<1) return C;
5271 	C  = g_malloc(nrows*sizeof(gint***));
5272 	for(i = 0;i<nrows; i++)
5273 	{
5274 		C[i] = g_malloc(ncolumns*sizeof(gint**));
5275 		for(j = 0;j<ncolumns; j++)
5276 		{
5277 			C[i][j] = g_malloc(nslices*sizeof(gint*));
5278 			for(k = 0;k<nslices; k++)
5279 			C[i][j][k] = g_malloc(nslices*sizeof(gint));
5280 		}
5281 	}
5282 	return C;
5283 }
5284 /****************************************************************************************************************************/
printQuarticInt(gint **** C,gint nrows,gint ncolumns,gint nslices,gint nl)5285 void printQuarticInt(gint**** C, gint nrows, gint ncolumns, gint nslices, gint nl)
5286 {
5287 	gint i,j,k,l;
5288 	for(i = 0;i<nrows; i++)
5289 	{
5290 		for(j = 0;j<ncolumns; j++)
5291 		{
5292 			for(k = 0;k<nslices; k++)
5293 			{
5294 				for(l = 0;l<nl; l++)
5295       					printf("%d ",C[i][j][k][l]);
5296 				printf("\n");
5297 			}
5298 		}
5299 		printf("\n");
5300 	}
5301 }
5302 /****************************************************************************************************************************/
initQuarticInt(gint **** C,gint nrows,gint ncolumns,gint nslices,gint nl,gint val)5303 void initQuarticInt(gint**** C, gint nrows, gint ncolumns, gint nslices, gint nl, gint val)
5304 {
5305 	gint i,j,k,l;
5306 	if(!C) return;
5307 	for(i = 0;i<nrows; i++)
5308 		for(j = 0;j<ncolumns; j++)
5309 			for(k = 0;k<nslices; k++)
5310 				for(l = 0;l<nl; l++)
5311 					C[i][j][k][l] = val;
5312 }
5313 /****************************************************************************************************************************/
freeQuarticInt(gint ***** C,gint nrows,gint ncolumns,gint nslices)5314 void freeQuarticInt(gint***** C, gint nrows, gint ncolumns, gint nslices)
5315 {
5316 	if(*C)
5317 	{
5318 		gint i,j,k;
5319 		for(i = 0;i<nrows; i++)
5320 		{
5321 			if((*C)[i])
5322 			for(j = 0;j<ncolumns; j++)
5323 			{
5324 				if((*C)[i][j])
5325 				{
5326 				for(k = 0;k<nslices; k++)
5327 					if((*C)[i][j][k]) g_free((*C)[i][j][k]);
5328 				if((*C)[i][j]) g_free((*C)[i][j]);
5329 				}
5330 			}
5331 			if((*C)[i])g_free((*C)[i]);
5332 		}
5333 		g_free(*C);
5334 	}
5335 	*C= NULL;
5336 }
5337 /****************************************************************************************************************************/
readOneReal(FILE * file,gchar * tag,double * value)5338 gboolean readOneReal(FILE* file, gchar* tag, double*value)
5339 {
5340 	static gchar *t = NULL;
5341 	gchar* TAG = NULL;
5342 	gchar* pos;
5343 	if(!tag) return FALSE;
5344 	if(!value) return FALSE;
5345 	if(t==NULL) t = g_malloc(BSIZE*sizeof(gchar));
5346 
5347 	TAG = strdup(tag);
5348 	uppercase(TAG);
5349 	rewind(file);
5350 
5351 	while(!feof(file))
5352   	{
5353     		if(!fgets(t,BSIZE, file)) break;
5354 		deleteFirstSpaces(t);
5355 		if(t[0]=='#') continue;
5356 		uppercase(t);
5357 		pos = strstr(t,TAG);
5358 		if(!pos) continue;
5359 		if(strstr(pos,"="))
5360 		{
5361 			pos = strstr(pos,"=")+1;
5362 		}
5363 		else pos += strlen(TAG)+1;
5364 		g_free(TAG);
5365 		if(1==sscanf(pos,"%lf",value)) return TRUE;
5366 		return FALSE;
5367 	}
5368 	g_free(TAG);
5369 	return FALSE;
5370 }
5371 /****************************************************************************************************************************/
readOneRealFromAFile(gchar * namefile,gchar * tag,double * value)5372 gboolean readOneRealFromAFile(gchar* namefile, gchar* tag, double* value)
5373 {
5374 	FILE* file = NULL;
5375 	gboolean res;
5376 
5377 	if(!namefile) return FALSE;
5378 
5379 	file = fopen(namefile, "rb");
5380 	res = readOneReal(file,tag,value);
5381 	fclose(file);
5382 	return res;
5383 }
5384 /****************************************************************************************************************************/
readOneInt(FILE * file,gchar * tag,gint * value)5385 gboolean readOneInt(FILE* file, gchar* tag, gint*value)
5386 {
5387 	static gchar *t = NULL;
5388 	gchar* TAG = NULL;
5389 	gchar* pos;
5390 	if(!tag) return FALSE;
5391 	if(!value) return FALSE;
5392 	if(t==NULL) t = g_malloc(BSIZE*sizeof(gchar));
5393 
5394 	TAG = strdup(tag);
5395 	uppercase(TAG);
5396 	rewind(file);
5397 
5398 	while(!feof(file))
5399   	{
5400     		if(!fgets(t,BSIZE, file)) break;
5401 		deleteFirstSpaces(t);
5402 		if(t[0]=='#') continue;
5403 		uppercase(t);
5404 		pos = strstr(t,TAG);
5405 		if(!pos) continue;
5406 		if(strstr(pos,"="))
5407 		{
5408 			pos = strstr(pos,"=")+1;
5409 		}
5410 		else pos += strlen(TAG)+1;
5411 		g_free(TAG);
5412 		if(1==sscanf(pos,"%d",value)) return TRUE;
5413 		return FALSE;
5414 	}
5415 	g_free(TAG);
5416 	return FALSE;
5417 }
5418 /****************************************************************************************************************************/
readOneIntFromAFile(gchar * namefile,gchar * tag,gint * value)5419 gboolean readOneIntFromAFile(gchar* namefile, gchar* tag, gint* value)
5420 {
5421 	FILE* file = NULL;
5422 	gboolean res;
5423 
5424 	if(!namefile) return FALSE;
5425 
5426 	file = fopen(namefile, "rb");
5427 	res = readOneInt(file,tag,value);
5428 	fclose(file);
5429 	return res;
5430 }
5431 /****************************************************************************************************************************/
readOneBoolean(FILE * file,gchar * tag,gboolean * value)5432 gboolean readOneBoolean(FILE* file, gchar* tag, gboolean*value)
5433 {
5434 	static gchar *t = NULL;
5435 	gchar* TAG = NULL;
5436 	gchar* pos;
5437 	gchar tmp[100];
5438 	if(!tag) return FALSE;
5439 	if(!value) return FALSE;
5440 	if(t==NULL) t = g_malloc(BSIZE*sizeof(gchar));
5441 
5442 	TAG = strdup(tag);
5443 	uppercase(TAG);
5444 	rewind(file);
5445 
5446 	while(!feof(file))
5447   	{
5448     		if(!fgets(t,BSIZE, file)) break;
5449 		deleteFirstSpaces(t);
5450 		if(t[0]=='#') continue;
5451 		uppercase(t);
5452 		pos = strstr(t,TAG);
5453 		if(!pos) continue;
5454 		if(strstr(pos,"="))
5455 		{
5456 			pos = strstr(pos,"=")+1;
5457 		}
5458 		else pos += strlen(TAG)+1;
5459 		g_free(TAG);
5460 		if(1==sscanf(pos,"%s",tmp))
5461 		{
5462 
5463 			if(!strcmp(tmp,"TRUE"))*value = TRUE;
5464 			else *value = FALSE;
5465 			return TRUE;
5466 		}
5467 		return FALSE;
5468 	}
5469 	g_free(TAG);
5470 	return FALSE;
5471 }
5472 /****************************************************************************************************************************/
readOneBooleanFromAFile(gchar * namefile,gchar * tag,gboolean * value)5473 gboolean readOneBooleanFromAFile(gchar* namefile, gchar* tag, gboolean* value)
5474 {
5475 	FILE* file = NULL;
5476 	gboolean res;
5477 
5478 	if(!namefile) return FALSE;
5479 
5480 	file = fopen(namefile, "rb");
5481 	res = readOneBoolean(file,tag,value);
5482 	fclose(file);
5483 	return res;
5484 }
5485 /****************************************************************************************************************************/
readOneStringFromAFile(gchar * namefile,gchar * tag,gint * value)5486 gboolean readOneStringFromAFile(gchar* namefile, gchar* tag, gint* value)
5487 {
5488 	FILE* file = NULL;
5489 	gboolean res;
5490 
5491 	if(!namefile) return FALSE;
5492 
5493 	file = fopen(namefile, "rb");
5494 	res = readOneInt(file,tag,value);
5495 	fclose(file);
5496 	return res;
5497 }
5498 /****************************************************************************************************************************/
readOneString(FILE * file,gchar * tag,gchar ** value)5499 gboolean readOneString(FILE* file, gchar* tag, gchar**value)
5500 {
5501 	static gchar *t = NULL;
5502 	static gchar *t2 = NULL;
5503 	gchar* TAG = NULL;
5504 	gchar* pos;
5505 	if(!tag) return FALSE;
5506 	if(!value) return FALSE;
5507 	if(t==NULL) t = g_malloc(BSIZE*sizeof(gchar));
5508 	if(t2==NULL) t2 = g_malloc((BSIZE+2)*sizeof(gchar));
5509 
5510 	TAG = strdup(tag);
5511 	uppercase(TAG);
5512 	rewind(file);
5513 
5514 	while(!feof(file))
5515   	{
5516     		if(!fgets(t,BSIZE, file)) break;
5517 		deleteFirstSpaces(t);
5518 		if(t[0]=='#') continue;
5519 		sprintf(t2,"%s",t);
5520 		uppercase(t2);
5521 		pos = strstr(t2,TAG);
5522 		if(!pos) continue;
5523 		if(strstr(pos,"="))
5524 		{
5525 			pos = strstr(pos,"=")+1;
5526 		}
5527 		else pos += strlen(TAG)+1;
5528 		g_free(TAG);
5529 		if(strlen(pos)>0)
5530 		{
5531 			gchar* p = t+(gint)(pos-t2);
5532 			if(*value) g_free(*value);
5533 			*value = strdup(p);
5534 			strDeleten(*value);
5535 			deleteFirstSpaces(*value);
5536 			deleteLastSpaces(*value);
5537 			return TRUE;
5538 		}
5539 		return FALSE;
5540 	}
5541 	g_free(TAG);
5542 	return FALSE;
5543 }
5544 /****************************************************************************************************************************/
strDeleten(gchar * str)5545 void strDeleten(gchar* str)
5546 {
5547         gchar *s;
5548 
5549         if(str == NULL)
5550                 return;
5551 
5552         if (!*str)
5553                 return;
5554         for (s = str + strlen (str) - 1; s >= str && ((unsigned char)*s)=='\n'; s--)
5555                 *s = '\0';
5556 }
5557 /****************************************************************************************************************************/
deleteLastSpaces(gchar * str)5558 void deleteLastSpaces(gchar* str)
5559 {
5560 	gchar *s;
5561 
5562 	if(str == NULL)
5563 		return;
5564 
5565 	if (!*str)
5566 		return;
5567 	for (s = str + strlen (str) - 1; s >= str && isspace ((unsigned char)*s); s--) *s = '\0';
5568 }
5569 /****************************************************************************************************************************/
deleteFirstSpaces(gchar * str)5570 void deleteFirstSpaces(gchar* str)
5571 {
5572 	gchar *start;
5573 	gint i;
5574 	gint lenSpace = 0;
5575 
5576 	if(str == NULL)
5577 		return;
5578 	if (!*str)
5579 		return;
5580 
5581 	for (start = str; *start && isspace (*start); start++)lenSpace++;
5582 
5583 	for(i=0;i<(gint)(strlen(str)-lenSpace);i++)
5584 		str[i] = str[i+lenSpace];
5585 	str[strlen(str)-lenSpace] = '\0';
5586 }
5587 /****************************************************************************************************************************/
deleteAllSpaces(gchar * str)5588 void deleteAllSpaces(gchar* str)
5589 {
5590 	gint i;
5591 	gint j;
5592 	gboolean Ok = FALSE;
5593 
5594 	deleteLastSpaces(str);
5595 	deleteFirstSpaces(str);
5596 	while(!Ok)
5597 	{
5598 		Ok = TRUE;
5599 		for(i=0;i<(gint)strlen(str);i++)
5600 		{
5601 			if(isspace(str[i]))
5602 			{
5603 				Ok = FALSE;
5604 				for(j=i;j<(gint)strlen(str);j++)
5605 				{
5606 					str[j] = str[j+1];
5607 				}
5608 				break;
5609 			}
5610 		}
5611 	}
5612 }
5613 /****************************************************************************************************************************/
goToStr(FILE * file,gchar * tag)5614 gboolean goToStr(FILE* file, gchar* tag)
5615 {
5616         static gchar *t = NULL;
5617         gchar* TAG = NULL;
5618         gchar* pos = NULL;
5619         int i=0;
5620         int ii,jj;
5621         double v;
5622         int** counter = NULL;
5623         if(!tag) return FALSE;
5624         if(t==NULL) t = g_malloc(BSIZE*sizeof(gchar));
5625 
5626         TAG = strdup(tag);
5627         uppercase(TAG);
5628         rewind(file);
5629 
5630         while(!feof(file))
5631         {
5632                 if(!fgets(t,BSIZE, file)) break;
5633                 deleteFirstSpaces(t);
5634                 if(t[0]=='#') continue;
5635                 uppercase(t);
5636                 pos = strstr(t,TAG);
5637                 if(!pos) continue;
5638                 break;
5639         }
5640 	return pos != NULL;
5641 }
5642 /**********************************************************************************************************************************/
get_one_block_from_wfx_file(FILE * file,gchar * blockName,gint * n)5643 gchar** get_one_block_from_wfx_file(FILE* file, gchar* blockName,  gint* n)
5644 {
5645 	gint nElements = 0;
5646 	gchar** elements = NULL;
5647 	gchar t[BBSIZE];
5648 	long int geomposok = 0;
5649 	gboolean ok = FALSE;
5650 	gint i;
5651 	 while(!feof(file))
5652 	 {
5653 		if(!fgets(t,BBSIZE,file))break;
5654 		if(mystrcasestr( t, blockName))
5655 		{
5656 			geomposok = ftell(file);
5657 			ok= TRUE;
5658 			break;
5659 		}
5660 	}
5661 	if(!ok) return NULL;
5662 	nElements = 0;
5663 	 while(!feof(file))
5664 	 {
5665 		if(!fgets(t,BBSIZE,file)) break;
5666 		if(mystrcasestr( t, blockName)) break;
5667 		nElements++;
5668 	}
5669 	if(nElements<1) return NULL;
5670 	elements = g_malloc(nElements*sizeof(gchar*));
5671 	fseek(file, geomposok, SEEK_SET);
5672 	for(i=0;i<nElements;i++)
5673 	{
5674 			gint k;
5675 			if(!fgets(t,BBSIZE,file))break;
5676 			for(k=0;k<strlen(t);k++) if(t[k]=='\n') t[k]='\0';
5677 			elements[i] = strdup(t);
5678 	}
5679 	*n = nElements;
5680 	return elements;
5681 }
5682 /**********************************************************************************************************************************/
get_one_block_int_from_wfx_file(FILE * file,gchar * blockName,gint * n)5683 gint* get_one_block_int_from_wfx_file(FILE* file, gchar* blockName,  gint* n)
5684 {
5685 	gint nElements = 0;
5686 	gint* elements = NULL;
5687 	gchar t[BBSIZE];
5688 	long int geomposok = 0;
5689 	gboolean ok = FALSE;
5690 	gchar** allstrs = NULL;
5691 	gint i;
5692 	gint nLines = 0;
5693 	gint k;
5694 	 while(!feof(file))
5695 	 {
5696 		if(!fgets(t,BBSIZE,file))break;
5697 		if(mystrcasestr( t, blockName))
5698 		{
5699 			geomposok = ftell(file);
5700 			ok= TRUE;
5701 			break;
5702 		}
5703 	}
5704 	if(!ok) return NULL;
5705 	nLines = 0;
5706 	 while(!feof(file))
5707 	 {
5708 		if(!fgets(t,BBSIZE,file)) break;
5709 		if(mystrcasestr( t, blockName)) break;
5710 		nLines++;
5711 		allstrs =gab_split (t);
5712                 if(allstrs) for(k=0; allstrs[k]!=NULL; k++) nElements++;
5713                 g_strfreev(allstrs);
5714                 allstrs = NULL;
5715 	}
5716 	if(nLines<1) return NULL;
5717 	if(nElements<1) return NULL;
5718 	elements = g_malloc(nElements*sizeof(gint));
5719 	fseek(file, geomposok, SEEK_SET);
5720 	nElements = 0;
5721 	for(i=0;i<nLines;i++)
5722 	{
5723 		if(!fgets(t,BBSIZE,file))break;
5724 		if(mystrcasestr( t, blockName)) break;
5725 		allstrs =gab_split (t);
5726                 if(allstrs) for(k=0; allstrs[k]!=NULL; k++)
5727 		{
5728 			elements[nElements] = atof(allstrs[k]);
5729 			nElements++;
5730 		}
5731                 g_strfreev(allstrs);
5732                 allstrs = NULL;
5733 	}
5734 	*n = nElements;
5735 	return elements;
5736 }
5737 /**********************************************************************************************************************************/
get_one_block_real_from_wfx_file(FILE * file,gchar * blockName,gint * n)5738 gdouble* get_one_block_real_from_wfx_file(FILE* file, gchar* blockName,  gint* n)
5739 {
5740 	gint nElements = 0;
5741 	gdouble* elements = NULL;
5742 	gchar t[BBSIZE];
5743 	long int geomposok = 0;
5744 	gboolean ok = FALSE;
5745 	gchar** allstrs = NULL;
5746 	gint i;
5747 	gint nLines = 0;
5748 	gint k;
5749 	 while(!feof(file))
5750 	 {
5751 		if(!fgets(t,BBSIZE,file))break;
5752 		if(mystrcasestr( t, blockName))
5753 		{
5754 			geomposok = ftell(file);
5755 			ok= TRUE;
5756 			break;
5757 		}
5758 	}
5759 	if(!ok) return NULL;
5760 	nLines = 0;
5761 	 while(!feof(file))
5762 	 {
5763 		if(!fgets(t,BBSIZE,file)) break;
5764 		if(mystrcasestr( t, blockName)) break;
5765 		nLines++;
5766 		allstrs =gab_split (t);
5767                 if(allstrs) for(k=0; allstrs[k]!=NULL; k++) nElements++;
5768                 g_strfreev(allstrs);
5769                 allstrs = NULL;
5770 	}
5771 	if(nLines<1) return NULL;
5772 	if(nElements<1) return NULL;
5773 	elements = g_malloc(nElements*sizeof(gdouble));
5774 	fseek(file, geomposok, SEEK_SET);
5775 	nElements = 0;
5776 	for(i=0;i<nLines;i++)
5777 	{
5778 		if(!fgets(t,BBSIZE,file))break;
5779 		if(mystrcasestr( t, blockName)) break;
5780 		for(k=0;k<strlen(t);k++) if(t[k]=='\n') t[k]='\0';
5781 		for(k=0;k<strlen(t);k++) if(t[k]=='D') t[k]='E';
5782 		for(k=0;k<strlen(t);k++) if(t[k]=='d') t[k]='E';
5783 		allstrs =gab_split (t);
5784                 if(allstrs) for(k=0; allstrs[k]!=NULL; k++)
5785 		{
5786 			elements[nElements] = atof(allstrs[k]);
5787 			nElements++;
5788 		}
5789                 g_strfreev(allstrs);
5790                 allstrs = NULL;
5791 	}
5792 	*n = nElements;
5793 	return elements;
5794 }
5795 /**********************************************************************************************************************************/
get_one_int_from_wfx_file(FILE * file,gchar * blockName,gint * n)5796 gboolean get_one_int_from_wfx_file(FILE* file, gchar* blockName, gint* n)
5797 {
5798 	gchar t[BBSIZE];
5799 	 while(!feof(file))
5800 	 {
5801 		if(!fgets(t,BBSIZE,file))break;
5802 		if(mystrcasestr( t, blockName))
5803 		{
5804 			if(!fgets(t,BBSIZE,file))break;
5805 			*n = atoi(t);
5806 			return TRUE;
5807 		}
5808 	}
5809 	return FALSE;
5810 }
5811 /**********************************************************************************************************************************/
get_one_orbital_from_wfx_file(FILE * file,gint * n,gint * numOrb)5812 gdouble* get_one_orbital_from_wfx_file(FILE* file, gint* n, gint*numOrb)
5813 {
5814 	gint nElements = 0;
5815 	gdouble* elements = NULL;
5816 	gchar t[BBSIZE];
5817 	long int geomposok = 0;
5818 	gboolean ok = FALSE;
5819 	gchar** allstrs = NULL;
5820 	gint i;
5821 	gint nLines = 0;
5822 	gint k;
5823 	if(!get_one_int_from_wfx_file(file, "MO Number", numOrb))
5824 	{
5825 		*n = 0;
5826 		return NULL;
5827 	}
5828 	fgets(t,BBSIZE,file); // <MO Number>
5829 	geomposok = ftell(file);
5830 	nLines = 0;
5831 	 while(!feof(file))
5832 	 {
5833 		if(!fgets(t,BBSIZE,file)) break;
5834 		//fprintf(stderr,"t = %s",t);
5835 
5836 		if(strstr(t, "<")) break;
5837 		nLines++;
5838 		allstrs =gab_split (t);
5839                 if(allstrs) for(k=0; allstrs[k]!=NULL; k++) nElements++;
5840                 g_strfreev(allstrs);
5841                 allstrs = NULL;
5842 	}
5843 	//fprintf(stderr,"nLines = %d\n",nLines);
5844 	fseek(file, geomposok, SEEK_SET);
5845 	if(nLines<1) return NULL;
5846 	if(nElements<1) return NULL;
5847 	elements = g_malloc(nElements*sizeof(gdouble));
5848 	fseek(file, geomposok, SEEK_SET);
5849 	nElements = 0;
5850 	for(i=0;i<nLines;i++)
5851 	{
5852 		if(!fgets(t,BBSIZE,file))break;
5853 		if(strstr( t, "<")) break;
5854 		for(k=0;k<strlen(t);k++) if(t[k]=='\n') t[k]='\0';
5855 		for(k=0;k<strlen(t);k++) if(t[k]=='D') t[k]='E';
5856 		for(k=0;k<strlen(t);k++) if(t[k]=='d') t[k]='E';
5857 		allstrs =gab_split (t);
5858                 if(allstrs) for(k=0; allstrs[k]!=NULL; k++)
5859 		{
5860 			elements[nElements] = atof(allstrs[k]);
5861 			nElements++;
5862 		}
5863                 g_strfreev(allstrs);
5864                 allstrs = NULL;
5865 	}
5866 	fseek(file, geomposok, SEEK_SET);
5867 
5868 	*n = nElements;
5869 	return elements;
5870 }
5871 /* read all chars from file */
readFile(gchar * filename)5872 gchar *readFile(gchar *filename)
5873 {
5874     gchar *fcontent = NULL;
5875     gint fsize = 0;
5876     FILE *fp;
5877 
5878     fp = fopen(filename, "rb");
5879     if(fp) {
5880         fseek(fp, 0, SEEK_END);
5881         fsize = ftell(fp);
5882         rewind(fp);
5883 
5884         fcontent = (gchar*) malloc(sizeof(gchar) * fsize);
5885         fread(fcontent, 1, fsize, fp);
5886         fclose(fp);
5887     }
5888     return fcontent;
5889 }
5890