Index: /OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/plyfile.cpp
===================================================================
--- /OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/plyfile.cpp (revision 10012)
+++ /OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/plyfile.cpp (revision 11274)
@@ -40,8 +40,16 @@
 
 #include "ply.h"
+#include"typedefs.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <math.h>
 #include <string.h>
+
+#include <osg/Math>
+
+#if defined(_MSC_VER) && defined(OSG_DISABLE_MSVC_WARNINGS)
+    #pragma warning( disable : 4996 )
+#endif
 
 #ifdef WIN32
@@ -316,4 +324,6 @@
 
   plyfile = ply_write (fp, nelems, elem_names, file_type);
+
+  // If the plyfile could not load return NULL
   if (plyfile == NULL)
     return (NULL);
@@ -354,6 +364,7 @@
   elem = find_element (plyfile, elem_name);
   if (elem == NULL) {
-    fprintf(stderr,"ply_describe_element: can't find element '%s'\n",elem_name);
-    exit (-1);
+    char error[100];
+    sprintf (error, "ply_describe_element: can't find element '%s'\n",elem_name);
+    throw ply::MeshException( error );
   }
 
@@ -501,6 +512,7 @@
   elem = find_element (plyfile, elem_name);
   if (elem == NULL) {
-    fprintf(stderr,"ply_element_count: can't find element '%s'\n",elem_name);
-    exit (-1);
+    char error[100];
+    sprintf (error, "ply_element_count: can't find element '%s'\n",elem_name);
+    throw ply::MeshException( error );
   }
 
@@ -537,7 +549,8 @@
       break;
     default:
-      fprintf (stderr, "ply_header_complete: bad file type = %d\n",
+      char error[100];
+      sprintf (error, "ply_header_complete: bad file type = %d\n",
                plyfile->file_type);
-      exit (-1);
+      throw ply::MeshException( error );
   }
 
@@ -596,6 +609,7 @@
   elem = find_element (plyfile, elem_name);
   if (elem == NULL) {
-    fprintf(stderr, "ply_elements_setup: can't find element '%s'\n", elem_name);
-    exit (-1);
+    char error[100];
+    sprintf (error, "ply_elements_setup: can't find element '%s'\n", elem_name);
+    throw ply::MeshException( error );
   }
 
@@ -846,5 +860,5 @@
         return (NULL);
         }
-      plyfile->version = atof (words[2]);
+      plyfile->version = osg::asciiToDouble (words[2]);
     }
     else if (equal_strings (words[0], "element"))
@@ -941,4 +955,10 @@
   plyfile = ply_read (fp, nelems, elem_names);
 
+  if(!plyfile)
+  {
+    std::cout<<"Ply File Error : Could not read file"<<std::endl;
+    return NULL;
+  }
+
   /* determine the file type and version */
 
@@ -1060,5 +1080,5 @@
 void ply_get_property(
   PlyFile *plyfile,
-  char *elem_name,
+  const char *elem_name,
   PlyProperty *prop
 )
@@ -1331,7 +1351,7 @@
   elem = find_element (plyfile, elem_name);
   if (elem == NULL) {
-    fprintf (stderr,
-             "ply_get_other_element: can't find element '%s'\n", elem_name);
-    exit (-1);
+    char error[100];
+    sprintf (error, "ply_get_other_element: can't find element '%s'\n", elem_name);
+    throw ply::MeshException( error );
   }
 
@@ -1651,6 +1671,7 @@
   words = get_words (plyfile->fp, &nwords, &orig_line);
   if (words == NULL) {
-    fprintf (stderr, "ply_get_element: unexpected end of file\n");
-    exit (-1);
+    char error[100];
+    sprintf (error, "ply_get_element: unexpected end of file\n");
+    throw ply::MeshException( error );
   }
 
@@ -1848,6 +1869,7 @@
 
   if (code <= PLY_START_TYPE || code >= PLY_END_TYPE) {
-    fprintf (stderr, "write_scalar_type: bad data code = %d\n", code);
-    exit (-1);
+    char error[100];
+    sprintf (error, "write_scalar_type: bad data code = %d\n", code);
+    throw ply::MeshException( error );
   }
 
@@ -2119,6 +2141,7 @@
       break;
     default:
-      fprintf (stderr, "write_binary_item: bad type = %d\n", type);
-      exit (-1);
+      char error[100];
+      sprintf (error, "write_binary_item: bad type = %d\n", type);
+      throw ply::MeshException( error );
   }
 }
@@ -2163,6 +2186,7 @@
       break;
     default:
-      fprintf (stderr, "write_ascii_item: bad type = %d\n", type);
-      exit (-1);
+      char error[100];
+      sprintf (error, "write_ascii_item: bad type = %d\n", type);
+      throw ply::MeshException( error );
   }
 }
@@ -2235,6 +2259,7 @@
       break;
     default:
-      fprintf (stderr, "get_stored_item: bad type = %d\n", type);
-      exit (-1);
+      char error[100];
+      sprintf (error, "get_stored_item: bad type = %d\n", type);
+      throw ply::MeshException( error );
   }
 }
@@ -2267,8 +2292,14 @@
 
   ptr = (void *) c;
+  size_t result = 0;
 
   switch (type) {
     case PLY_CHAR:
-      fread (ptr, 1, 1, plyfile->fp);
+      result = fread (ptr, 1, 1, plyfile->fp);
+      if(result < 1)
+      {
+          throw ply::MeshException( "Error in reading PLY file."
+                                 "fread not succeeded." );
+      }
       *int_val = *((char *) ptr);
       *uint_val = *int_val;
@@ -2277,5 +2308,10 @@
       case PLY_UCHAR:
       case PLY_UINT8:
-          fread (ptr, 1, 1, plyfile->fp);
+          result = fread (ptr, 1, 1, plyfile->fp);
+          if(result < 1)
+          {
+              throw ply::MeshException( "Error in reading PLY file."
+                                 "fread not succeeded." );
+          }
           *uint_val = *((unsigned char *) ptr);
           *int_val = *uint_val;
@@ -2283,5 +2319,10 @@
           break;
       case PLY_SHORT:
-          fread (ptr, 2, 1, plyfile->fp);
+          result = fread (ptr, 2, 1, plyfile->fp);
+          if(result < 1 )
+          {
+              throw ply::MeshException( "Error in reading PLY file."
+                                 "fread not succeeded." );
+          }
           if( plyfile->file_type == PLY_BINARY_BE )
           {
@@ -2297,5 +2338,10 @@
           break;
       case PLY_USHORT:
-          fread (ptr, 2, 1, plyfile->fp);
+          result = fread (ptr, 2, 1, plyfile->fp);
+          if(result < 1)
+          {
+              throw ply::MeshException( "Error in reading PLY file."
+                                 "fread not succeeded." );
+          }
           if( plyfile->file_type == PLY_BINARY_BE )
           {
@@ -2312,5 +2358,10 @@
       case PLY_INT:
       case PLY_INT32:
-          fread (ptr, 4, 1, plyfile->fp);
+          result = fread (ptr, 4, 1, plyfile->fp);
+          if(result < 1)
+          {
+              throw ply::MeshException( "Error in reading PLY file."
+                                 "fread not succeeded." );
+          }
           if( plyfile->file_type == PLY_BINARY_BE )
           {
@@ -2326,5 +2377,10 @@
           break;
       case PLY_UINT:
-          fread (ptr, 4, 1, plyfile->fp);
+          result = fread (ptr, 4, 1, plyfile->fp);
+          if(result < 1)
+          {
+              throw ply::MeshException( "Error in reading PLY file."
+                                 "fread not succeeded." );
+          }
           if( plyfile->file_type == PLY_BINARY_BE )
           {
@@ -2341,5 +2397,10 @@
       case PLY_FLOAT:
       case PLY_FLOAT32:
-          fread (ptr, 4, 1, plyfile->fp);
+          result = fread (ptr, 4, 1, plyfile->fp);
+          if(result < 1)
+          {
+              throw ply::MeshException( "Error in reading PLY file."
+                                 "fread not succeeded." );
+          }
           if( plyfile->file_type == PLY_BINARY_BE )
           {
@@ -2355,20 +2416,26 @@
           break;
       case PLY_DOUBLE:
-          fread (ptr, 8, 1, plyfile->fp);
-          if( plyfile->file_type == PLY_BINARY_BE )
-          {
-              swap8BE(ptr);
-          }
-          else
-          {
-              swap8LE(ptr);
-          }
-      *double_val = *((double *) ptr);
-      *int_val = (int) *double_val;
-      *uint_val = (unsigned int) *double_val;
-      break;
+        result = fread (ptr, 8, 1, plyfile->fp);
+        if(result < 1)
+        {
+            throw ply::MeshException( "Error in reading PLY file."
+                                "fread not succeeded." );
+        }
+        if( plyfile->file_type == PLY_BINARY_BE )
+        {
+            swap8BE(ptr);
+        }
+        else
+        {
+            swap8LE(ptr);
+        }
+        *double_val = *((double *) ptr);
+        *int_val = (int) *double_val;
+        *uint_val = (unsigned int) *double_val;
+        break;
     default:
-      fprintf (stderr, "get_binary_item: bad type = %d\n", type);
-      exit (-1);
+      char error[100];
+      sprintf (error, "get_binary_item: bad type = %d\n", type);
+      throw ply::MeshException( error );
   }
 }
@@ -2419,5 +2486,5 @@
     case PLY_FLOAT32:
     case PLY_DOUBLE:
-      *double_val = atof (word);
+      *double_val = osg::asciiToDouble(word);
       *int_val = (int) *double_val;
       *uint_val = (unsigned int) *double_val;
@@ -2425,6 +2492,7 @@
 
     default:
-      fprintf (stderr, "get_ascii_item: bad type = %d\n", type);
-      exit (-1);
+      char error[100];
+      sprintf (error, "get_ascii_item: bad type = %d\n", type);
+      throw ply::MeshException( error );
   }
 }
@@ -2497,6 +2565,7 @@
       break;
     default:
-      fprintf (stderr, "store_item: bad type = %d\n", type);
-      exit (-1);
+      char error[100];
+      sprintf (error, "store_item: bad type = %d\n", type);
+      throw ply::MeshException( error );
   }
 }
Index: /OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/vertexData.cpp
===================================================================
--- /OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/vertexData.cpp (revision 10012)
+++ /OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/vertexData.cpp (revision 11274)
@@ -10,7 +10,7 @@
 
 #include "typedefs.h"
-
 #include "vertexData.h"
 #include "ply.h"
+
 #include <cstdlib>
 #include <algorithm>
@@ -127,6 +127,6 @@
     
     // read in the faces, asserting that they are only triangles
-    uint8_t ind1 = _invertFaces ? 2 : 0;
-    uint8_t ind3 = _invertFaces ? 0 : 2;
+    int ind1 = _invertFaces ? 2 : 0;
+    int ind3 = _invertFaces ? 0 : 2;
     for( int i = 0; i < nFaces; ++i )
     {
@@ -158,10 +158,21 @@
     float   version;
     bool    result = false;
-    int        nComments;
-    char**    comments;
-    
-    PlyFile* file = ply_open_for_reading( const_cast< char* >( filename ), 
+    int     nComments;
+    char**  comments;
+    
+    PlyFile* file = NULL;
+
+    // Try to open ply file as for reading
+    try{
+            file  = ply_open_for_reading( const_cast< char* >( filename ), 
                                           &nPlyElems, &elemNames, 
                                           &fileType, &version );
+    }
+    // Catch the if any exception thrown
+    catch( exception& e )
+    {
+        MESHERROR << "Unable to read PLY file, an exception occured:  " 
+                    << e.what() << endl;
+    }
 
     if( !file )
@@ -173,4 +184,5 @@
 
     MESHASSERT( elemNames != 0 );
+    
 
     nComments = file->num_comments;
@@ -196,6 +208,14 @@
         int nProps;
         
-        PlyProperty** props = ply_get_element_description( file, elemNames[i], 
-                                                           &nElems, &nProps );
+        PlyProperty** props = NULL;
+        try{
+                props = ply_get_element_description( file, elemNames[i], 
+                                                     &nElems, &nProps );
+        }
+        catch( exception& e )
+        {
+            MESHERROR << "Unable to get PLY file description, an exception occured:  " 
+                        << e.what() << endl;
+        }
         MESHASSERT( props != 0 );
         
@@ -222,12 +242,27 @@
             if( ignoreColors )
                 MESHINFO << "Colors in PLY file ignored per request." << endl;
-            
-            // Read vertices and store in a std::vector array
-            readVertices( file, nElems, hasColors && !ignoreColors );
-            // Check whether all vertices are loaded or not
-            MESHASSERT( _vertices->size() == static_cast< size_t >( nElems ) );
-            // Check all color elements read or not
-            if( hasColors && !ignoreColors )
-                MESHASSERT( _colors->size() == static_cast< size_t >( nElems ) );
+         
+            try {   
+                // Read vertices and store in a std::vector array
+                readVertices( file, nElems, hasColors && !ignoreColors );
+                // Check whether all vertices are loaded or not
+                MESHASSERT( _vertices->size() == static_cast< size_t >( nElems ) );
+                // Check all color elements read or not
+                if( hasColors && !ignoreColors )
+                {
+                    MESHASSERT( _colors->size() == static_cast< size_t >( nElems ) );
+                }
+
+                result = true;
+            }
+            catch( exception& e )
+            {
+                MESHERROR << "Unable to read vertex in PLY file, an exception occured:  " 
+                            << e.what() << endl;
+                // stop for loop by setting the loop variable to break condition
+                // this way resources still get released even on error cases
+                i = nPlyElems;
+                
+            }
         }
         // If the string is face means triangle info started
@@ -273,14 +308,19 @@
 
         // If the normals are not calculated calculate the normals for faces
-        if(!_normals.valid())
-            _calculateNormals();
-
-        
-        // set the normals
-        geom->setNormalArray(_normals.get());
-        geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
+        if(_triangles.valid())
+        {
+            if(!_normals.valid())
+                _calculateNormals();
+
+            // set the normals
+            geom->setNormalArray(_normals.get());
+            geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
+        }
         
         // Add the premetive set
-        geom->addPrimitiveSet(_triangles.get());
+        if (_triangles.valid() && _triangles->size() > 0 )
+            geom->addPrimitiveSet(_triangles.get());
+        else
+            geom->addPrimitiveSet(new osg::DrawArrays(GL_POINTS, 0, _vertices->size()));
 
         // if color info is given set the color array
Index: /OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/typedefs.h
===================================================================
--- /OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/typedefs.h (revision 10012)
+++ /OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/typedefs.h (revision 11274)
@@ -13,5 +13,5 @@
 #define MESH_TYPEDEFS_H
 
-#   ifdef WIN32
+#   if defined(_MSC_VER)
 #      include <Winsock2.h>
 #      include <Windows.h>
@@ -19,11 +19,17 @@
 
 #   include <osg/Notify>
-#    include <cassert>
-#   define MESHASSERT  assert
+
+#ifdef NDEBUG
+#   define MESHASSERT( x )
+#else
+#    define MESHASSERT(x) { if( !(x) )                                      \
+              osg::notify(osg::WARN) << "Ply Loader ##### Assert: " << #x << " #####" << std::endl; }
+#endif
+
 #   define MESHERROR   osg::notify(osg::WARN)
 #   define MESHWARN    osg::notify(osg::WARN)
 #   define MESHINFO    osg::notify(osg::INFO)
 
-#ifdef WIN32
+#if defined(_MSC_VER)
 typedef int        socklen_t;
 
@@ -38,5 +44,5 @@
 #    endif
 
-#endif // Win32
+#endif // defined(_MSC_VER)
 
 #include <exception>
Index: /OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/ply.h
===================================================================
--- /OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/ply.h (revision 10012)
+++ /OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/ply.h (revision 11274)
@@ -34,7 +34,6 @@
 #define __PLY_H__
 
-#ifdef __cplusplus
-extern "C" {
-#endif
+// include to quieten down silly VS warnings
+#include <osg/Export>
 
 #include <stdio.h>
@@ -70,5 +69,5 @@
 typedef struct PlyProperty {    /* description of a property */
 
-  char *name;                           /* property name */
+  const char *name;                           /* property name */
   int external_type;                    /* file's data type */
   int internal_type;                    /* program's data type */
@@ -151,5 +150,5 @@
 extern PlyProperty **ply_get_element_description(PlyFile *, char *, int*, int*);
 extern void ply_get_element_setup( PlyFile *, char *, int, PlyProperty *);
-extern void ply_get_property(PlyFile *, char *, PlyProperty *);
+extern void ply_get_property(PlyFile *, const char *, PlyProperty *);
 extern PlyOtherProp *ply_get_other_properties(PlyFile *, char *, int);
 extern void ply_get_element(PlyFile *, void *);
@@ -165,8 +164,4 @@
 extern int equal_strings(const char *, const char *);
 
-
-#ifdef __cplusplus
-}
-#endif
 #endif /* !__PLY_H__ */
 
Index: /OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/vertexData.h
===================================================================
--- /OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/vertexData.h (revision 10012)
+++ /OpenSceneGraph/branches/OpenSceneGraph-2.8/src/osgPlugins/ply/vertexData.h (revision 11274)
@@ -27,5 +27,5 @@
 
 // defined elsewhere
-class PlyFile;
+struct PlyFile;
 
 namespace ply 
