use BLI_strncpy and BLI_snprintf when the size of the string is known.
[blender.git] / source / blender / editors / physics / physics_fluid.c
index 188bab9..0bb5dbe 100644 (file)
@@ -1,7 +1,6 @@
 /*
  * fluidsim.c
  * 
- * $Id$
  *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
@@ -86,7 +85,7 @@
 #include "physics_intern.h" // own include
 
 /* enable/disable overall compilation */
-#ifndef DISABLE_ELBEEM
+#ifdef WITH_MOD_FLUID
 
 #include "WM_api.h"
 
@@ -147,7 +146,8 @@ static int fluid_is_animated_mesh(FluidsimSettings *fss)
 
 #if 0
 /* helper function */
-void fluidsimGetGeometryObjFilename(Object *ob, char *dst) { //, char *srcname) {
+void fluidsimGetGeometryObjFilename(Object *ob, char *dst) { //, char *srcname)
+{
        //BLI_snprintf(dst,FILE_MAXFILE, "%s_cfgdata_%s.bobj.gz", srcname, ob->id.name);
        BLI_snprintf(dst,FILE_MAXFILE, "fluidcfgdata_%s.bobj.gz", ob->id.name);
 }
@@ -638,17 +638,20 @@ static int fluid_init_filepaths(Object *fsDomain, char *targetDir, char *targetF
        FluidsimSettings *domainSettings= fluidmd->fss; 
        FILE *fileCfg;
        int dirExist = 0;
-       char newSurfdataPath[FILE_MAXDIR+FILE_MAXFILE]; // modified output settings
+       char newSurfdataPath[FILE_MAX]; // modified output settings
        const char *suffixConfig = FLUID_SUFFIX_CONFIG;
        int outStringsChanged = 0;
-       
+
        // prepare names...
-       strncpy(targetDir, domainSettings->surfdataPath, FILE_MAXDIR);
-       strncpy(newSurfdataPath, domainSettings->surfdataPath, FILE_MAXDIR);
-       BLI_path_abs(targetDir, G.main->name); // fixed #frame-no 
+       const char *relbase= modifier_path_relbase(fsDomain);
 
-       // .tmp: dont overwrite/delete original file
-       BLI_snprintf(targetFile, FILE_MAXDIR+FILE_MAXFILE, "%s%s.tmp", targetDir, suffixConfig);
+       BLI_strncpy(targetDir, domainSettings->surfdataPath, FILE_MAXDIR);
+       BLI_strncpy(newSurfdataPath, domainSettings->surfdataPath, FILE_MAXDIR); /* if 0'd out below, this value is never used! */
+       BLI_path_abs(targetDir, relbase); // fixed #frame-no
+
+       BLI_join_dirfile(targetFile, FILE_MAX, targetDir, suffixConfig);
+       /* .tmp: dont overwrite/delete original file */
+       strncat(targetFile, ".tmp", FILE_MAX);
 
        // make sure all directories exist
        // as the bobjs use the same dir, this only needs to be checked
@@ -664,16 +667,16 @@ static int fluid_init_filepaths(Object *fsDomain, char *targetDir, char *targetF
                BLI_delete(targetFile, 0,0);
        }
        
-       if((strlen(targetDir)<1) || (!dirExist)) {
-               char blendDir[FILE_MAXDIR+FILE_MAXFILE];
-               char blendFile[FILE_MAXDIR+FILE_MAXFILE];
+       if(targetDir[0] == '\0' || (!dirExist)) {
+               char blendDir[FILE_MAX];
+               char blendFile[FILE_MAX];
                
                // invalid dir, reset to current/previous
-               BLI_strncpy(blendDir, G.main->name, FILE_MAXDIR+FILE_MAXFILE);
+               BLI_strncpy(blendDir, G.main->name, FILE_MAX);
                BLI_splitdirstring(blendDir, blendFile);
-               BLI_replace_extension(blendFile, FILE_MAXDIR+FILE_MAXFILE, ""); /* strip .blend */
+               BLI_replace_extension(blendFile, FILE_MAX, ""); /* strip .blend */
 
-               BLI_snprintf(newSurfdataPath, FILE_MAXDIR+FILE_MAXFILE ,"//fluidsimdata/%s_%s_", blendFile, fsDomain->id.name);
+               BLI_snprintf(newSurfdataPath, FILE_MAX ,"//fluidsimdata/%s_%s_", blendFile, fsDomain->id.name);
                
                BLI_snprintf(debugStrBuffer, 256, "fluidsimBake::error - warning resetting output dir to '%s'\n", newSurfdataPath);
                elbeemDebugOut(debugStrBuffer);
@@ -683,7 +686,7 @@ static int fluid_init_filepaths(Object *fsDomain, char *targetDir, char *targetF
        // check if modified output dir is ok
 #if 0
        if(outStringsChanged) {
-               char dispmsg[FILE_MAXDIR+FILE_MAXFILE+256];
+               char dispmsg[FILE_MAX+256];
                int  selection=0;
                BLI_strncpy(dispmsg,"Output settings set to: '", sizeof(dispmsg));
                strcat(dispmsg, newSurfdataPath);
@@ -767,7 +770,8 @@ static void fluidbake_endjob(void *customdata)
        }
 }
 
-int runSimulationCallback(void *data, int status, int frame) {
+int runSimulationCallback(void *data, int status, int frame)
+{
        FluidBakeJob *fb = (FluidBakeJob *)data;
        elbeemSimulationSettings *settings = fb->settings;
        
@@ -806,20 +810,20 @@ static void fluidbake_free_data(FluidAnimChannels *channels, ListBase *fobjects,
 }
 
 /* copied from rna_fluidsim.c: fluidsim_find_lastframe() */
-static void fluidsim_delete_until_lastframe(FluidsimSettings *fss)
+static void fluidsim_delete_until_lastframe(FluidsimSettings *fss, const char *relbase)
 {
-       char targetDir[FILE_MAXFILE+FILE_MAXDIR], targetFile[FILE_MAXFILE+FILE_MAXDIR];
-       char targetDirVel[FILE_MAXFILE+FILE_MAXDIR], targetFileVel[FILE_MAXFILE+FILE_MAXDIR];
-       char previewDir[FILE_MAXFILE+FILE_MAXDIR], previewFile[FILE_MAXFILE+FILE_MAXDIR];
+       char targetDir[FILE_MAX], targetFile[FILE_MAX];
+       char targetDirVel[FILE_MAX], targetFileVel[FILE_MAX];
+       char previewDir[FILE_MAX], previewFile[FILE_MAX];
        int curFrame = 1, exists = 0;
 
-       BLI_snprintf(targetDir, sizeof(targetDir), "%sfluidsurface_final_####.bobj.gz", fss->surfdataPath);
-       BLI_snprintf(targetDirVel, sizeof(targetDir), "%sfluidsurface_final_####.bvel.gz", fss->surfdataPath);
-       BLI_snprintf(previewDir, sizeof(targetDir), "%sfluidsurface_preview_####.bobj.gz", fss->surfdataPath);
+       BLI_join_dirfile(targetDir,    sizeof(targetDir),    fss->surfdataPath, OB_FLUIDSIM_SURF_FINAL_OBJ_FNAME);
+       BLI_join_dirfile(targetDirVel, sizeof(targetDirVel), fss->surfdataPath, OB_FLUIDSIM_SURF_FINAL_VEL_FNAME);
+       BLI_join_dirfile(previewDir,   sizeof(previewDir),   fss->surfdataPath, OB_FLUIDSIM_SURF_PREVIEW_OBJ_FNAME);
 
-       BLI_path_abs(targetDir, G.main->name);
-       BLI_path_abs(targetDirVel, G.main->name);
-       BLI_path_abs(previewDir, G.main->name);
+       BLI_path_abs(targetDir,    relbase);
+       BLI_path_abs(targetDirVel, relbase);
+       BLI_path_abs(previewDir,   relbase);
 
        do {
                BLI_strncpy(targetFile, targetDir, sizeof(targetFile));
@@ -852,12 +856,13 @@ static int fluidsimBake(bContext *C, ReportList *reports, Object *fsDomain, shor
        char debugStrBuffer[256];
        
        int gridlevels = 0;
+       const char *relbase= modifier_path_relbase(fsDomain);
        const char *strEnvName = "BLENDER_ELBEEMDEBUG"; // from blendercall.cpp
        const char *suffixConfig = FLUID_SUFFIX_CONFIG;
        const char *suffixSurface = FLUID_SUFFIX_SURFACE;
 
-       char targetDir[FILE_MAXDIR+FILE_MAXFILE];  // store & modify output settings
-       char targetFile[FILE_MAXDIR+FILE_MAXFILE]; // temp. store filename from targetDir for access
+       char targetDir[FILE_MAX];  // store & modify output settings
+       char targetFile[FILE_MAX]; // temp. store filename from targetDir for access
        int  outStringsChanged = 0;             // modified? copy back before baking
 
        float domainMat[4][4];
@@ -912,7 +917,7 @@ static int fluidsimBake(bContext *C, ReportList *reports, Object *fsDomain, shor
        domainSettings->lastgoodframe = -1;
 
        /* delete old baked files */
-       fluidsim_delete_until_lastframe(domainSettings);
+       fluidsim_delete_until_lastframe(domainSettings, relbase);
        
        /* rough check of settings... */
        if(domainSettings->previewresxyz > domainSettings->resolutionxyz) {
@@ -998,7 +1003,8 @@ static int fluidsimBake(bContext *C, ReportList *reports, Object *fsDomain, shor
 
        /* ********  start writing / exporting ******** */
        // use .tmp, dont overwrite/delete original file
-       BLI_snprintf(targetFile, 240, "%s%s.tmp", targetDir, suffixConfig);
+       BLI_join_dirfile(targetFile, sizeof(targetFile), targetDir, suffixConfig);
+       strncat(targetFile, ".tmp", sizeof(targetFile));
        
        // make sure these directories exist as well
        if(outStringsChanged) {
@@ -1026,7 +1032,7 @@ static int fluidsimBake(bContext *C, ReportList *reports, Object *fsDomain, shor
        fsset->aniFrameTime = channels->aniFrameTime;
        fsset->noOfFrames = noFrames; // is otherwise subtracted in parser
 
-       BLI_snprintf(targetFile, 240, "%s%s", targetDir, suffixSurface);
+       BLI_join_dirfile(targetFile, sizeof(targetFile), targetDir, suffixSurface);
 
        // defaults for compressibility and adaptive grids
        fsset->gstar = domainSettings->gstar;
@@ -1036,7 +1042,7 @@ static int fluidsimBake(bContext *C, ReportList *reports, Object *fsDomain, shor
        fsset->surfaceSmoothing = domainSettings->surfaceSmoothing; 
        fsset->surfaceSubdivs = domainSettings->surfaceSubdivs; 
        fsset->farFieldSize = domainSettings->farFieldSize; 
-       BLI_strncpy(fsset->outputPath, targetFile, 240);
+       BLI_strncpy(fsset->outputPath, targetFile, sizeof(fsset->outputPath));
 
        // domain channels
        fsset->channelSizeFrameTime = 
@@ -1112,7 +1118,7 @@ void fluidsimFreeBake(Object *UNUSED(ob))
        /* not implemented yet */
 }
 
-#else /* DISABLE_ELBEEM */
+#else /* WITH_MOD_FLUID */
 
 /* compile dummy functions for disabled fluid sim */
 
@@ -1136,7 +1142,7 @@ static int fluidsimBake(bContext *UNUSED(C), ReportList *UNUSED(reports), Object
        return 0;
 }
 
-#endif /* DISABLE_ELBEEM */
+#endif /* WITH_MOD_FLUID */
 
 /***************************** Operators ******************************/