| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Further, this software is distributed without any warranty that it is |
| 16 |
* free of the rightful claim of any third person regarding infringement |
| 17 |
* or the like. Any license provided herein, whether implied or |
| 18 |
* otherwise, applies only to this software file. Patent licenses, if |
| 19 |
* any, provided herein do not apply to combinations of this program with |
| 20 |
* other software, or any other product whatsoever. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU Lesser General Public |
| 23 |
* License along with this library; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
* |
| 26 |
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 |
* Mountain View, CA 94043, or: |
| 28 |
* |
| 29 |
* http://www.sgi.com |
| 30 |
* |
| 31 |
* For further information regarding this notice, see: |
| 32 |
* |
| 33 |
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
/* |
| 38 |
* Copyright (C) 1994-95 Silicon Graphics, Inc. |
| 39 |
* |
| 40 |
_______________________________________________________________________ |
| 41 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 |
| |
| 43 |
| $Revision: 1.2 $ |
| 44 |
| |
| 45 |
| Functions: |
| 46 |
| MyFileRead() |
| 47 |
| |
| 48 |
| |
| 49 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 50 |
_______________________________________________________________________ |
| 51 |
*/ |
| 52 |
|
| 53 |
#include <unistd.h> |
| 54 |
#include <cstdlib> |
| 55 |
#include <libgen.h> |
| 56 |
#include <sys/param.h> |
| 57 |
#include <Inventor/SoDB.h> |
| 58 |
#include <Inventor/SoInput.h> |
| 59 |
|
| 60 |
//////////////////////////////////////////////////////////////////////// |
| 61 |
// |
| 62 |
// Convert filename to Inventor.. |
| 63 |
// Returns a pipe to read data from if conversion was successful. |
| 64 |
// That pipe is NULL if the conversion failed. |
| 65 |
// This uses routeprint to perform the conversion via FTR rules. |
| 66 |
// It will fail gracefully if routeprint and/or the FTR database don't exist. |
| 67 |
// |
| 68 |
static FILE * |
| 69 |
convertToInventor(const char *filename) |
| 70 |
// |
| 71 |
//////////////////////////////////////////////////////////////////////// |
| 72 |
{ |
| 73 |
#define BUFSIZE 512 |
| 74 |
#define destinationFileType "Inventor2.1File" |
| 75 |
|
| 76 |
char routeprintCmd[BUFSIZE]; |
| 77 |
char conversionCmd[BUFSIZE]; |
| 78 |
FILE *pipeFile; |
| 79 |
FILE *ivDataPipe = NULL; |
| 80 |
|
| 81 |
// Use routeprint to figure out how to convert to Inventor format |
| 82 |
sprintf(routeprintCmd, "/usr/sbin/routeprint -d %s %s 2> /dev/null", |
| 83 |
destinationFileType, filename); |
| 84 |
|
| 85 |
// routeprint will return a conversion command which we can |
| 86 |
// then execute to convert the file. |
| 87 |
if (NULL != (pipeFile = popen(routeprintCmd, "r"))) { |
| 88 |
// read the command, which may be garbage: check return code |
| 89 |
(void) fgets(conversionCmd, BUFSIZE, pipeFile); |
| 90 |
// use pclose to check the return code of routeprint: |
| 91 |
// you have to check for non-zero return before executing conversionCmd |
| 92 |
if (0 != pclose(pipeFile)) { |
| 93 |
// Failed! |
| 94 |
return ivDataPipe; |
| 95 |
} |
| 96 |
// Now try to execute the actual conversion. |
| 97 |
// If conversion is successful, we can read from the data pipe. |
| 98 |
// If not, the data pipe will be NULL: which is our return code. |
| 99 |
ivDataPipe = popen(conversionCmd, "r"); |
| 100 |
} |
| 101 |
|
| 102 |
return ivDataPipe; |
| 103 |
} |
| 104 |
|
| 105 |
///////////////////////////////////////////////////////////////////////////// |
| 106 |
// |
| 107 |
// Read all objects from the given file and return under one separator. |
| 108 |
// |
| 109 |
SoSeparator * |
| 110 |
MyFileRead(const char *filename, SbString &errorMessage) |
| 111 |
// |
| 112 |
///////////////////////////////////////////////////////////////////////////// |
| 113 |
{ |
| 114 |
SoInput in; |
| 115 |
SbBool needToClosePipe = FALSE; |
| 116 |
FILE *ivDataPipe; |
| 117 |
|
| 118 |
// First check to make sure the file exists and is readable |
| 119 |
if (0 != access(filename, R_OK)) { |
| 120 |
errorMessage = "Error opening the file\n\""; |
| 121 |
errorMessage += filename; |
| 122 |
errorMessage += "\"."; |
| 123 |
errorMessage += "\n\nThe file is unreadable or does not exist."; |
| 124 |
errorMessage += "\nYou may not have read permission on this file."; |
| 125 |
return NULL; |
| 126 |
} |
| 127 |
|
| 128 |
// Now open the file in Inventor so we can see if it's valid |
| 129 |
if (! in.openFile(filename, TRUE)) { |
| 130 |
errorMessage = "Error opening the file\n\""; |
| 131 |
errorMessage += filename; |
| 132 |
errorMessage += "\"."; |
| 133 |
errorMessage += "\n\nInventor was unable to read that file."; |
| 134 |
errorMessage += "\nYou may not have permission to read this file."; |
| 135 |
return NULL; |
| 136 |
} |
| 137 |
|
| 138 |
// Is the file a valid Inventor file? Use isValidFile() since the |
| 139 |
// list of file headers Inventor accepts may grow from release to release. |
| 140 |
|
| 141 |
// We check to see if it is NOT a valid Inventor file first. |
| 142 |
if (! in.isValidFile() ) { |
| 143 |
// Not recognized: try to convert to a valid Inventor filetype |
| 144 |
|
| 145 |
if ((ivDataPipe = convertToInventor(filename)) != NULL) { |
| 146 |
// Set up SoInput for this pipe |
| 147 |
in.setFilePointer(ivDataPipe); |
| 148 |
needToClosePipe = TRUE; |
| 149 |
} else { |
| 150 |
// File could not be CONVERTed to readable format. |
| 151 |
errorMessage = "Unable to read the file\n\""; |
| 152 |
errorMessage += filename; |
| 153 |
errorMessage += "\"."; |
| 154 |
errorMessage += "\n\nInventor was unable to read that file, and"; |
| 155 |
errorMessage += "\nthat file could not be translated to Inventor format.\n\n"; |
| 156 |
errorMessage += "Are you sure that this is a supported filetype?"; |
| 157 |
errorMessage += "\nYou may not have the proper translators installed,"; |
| 158 |
errorMessage += "\nor the desktop filetype database may not be built."; |
| 159 |
return NULL; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
// If we get here, we have a valid SoInput and a valid Inventor file. |
| 164 |
|
| 165 |
// Read that file |
| 166 |
SoSeparator *sep = SoDB::readAll(&in); |
| 167 |
|
| 168 |
// If a bad read occurred, or no node was read, error |
| 169 |
if (sep == NULL) { |
| 170 |
errorMessage = "Inventor encountered an error while reading\n\""; |
| 171 |
errorMessage += filename; |
| 172 |
errorMessage += "\"."; |
| 173 |
errorMessage += "\n\nThis may not be a valid Inventor file."; |
| 174 |
return NULL; |
| 175 |
} |
| 176 |
|
| 177 |
// Close all opened files (and the pipe if necessary) |
| 178 |
in.closeFile(); |
| 179 |
if (TRUE == needToClosePipe) { |
| 180 |
fclose(ivDataPipe); |
| 181 |
} |
| 182 |
return sep; |
| 183 |
} |
| 184 |
|