Esempio n. 1
0
/**** Local functions definitions.     ****/
static TA_ValueTreeNode *allocTreeNode( TA_Libc *libHandle,
                                        TA_ValueTreeNode *parent,
                                        TA_String *string )
{
   TA_ValueTreeNode *node;
   TA_RetCode retCode;
   TA_StringCache *stringCache;

   stringCache = TA_GetGlobalStringCache( libHandle );

   node = (TA_ValueTreeNode *)TA_Malloc( libHandle, sizeof( TA_ValueTreeNode ) );

   if( !node )
      return (TA_ValueTreeNode *)NULL;

   node->string = NULL;
   node->parent = NULL;

   node->child = TA_ListAlloc( libHandle );
   if( !node->child )
   {
      freeTreeNode( libHandle, node );
      return NULL;
   }

   if( string )
   {
      node->string = TA_StringDup( stringCache, string );
      if( !node->string )
      {
         freeTreeNode( libHandle, node );
         return NULL;
      }
   }

   if( parent )
   {
      retCode = TA_ListAddTail( parent->child, node );
      if( retCode != TA_SUCCESS )
      {
         freeTreeNode( libHandle, node );
         return NULL;
      }
      node->parent = parent;
   }

   return node;
}
Esempio n. 2
0
TA_RetCode TA_DirectoryAlloc( const char *path,
                              TA_Directory **directory )
{
   #if defined( USE_WIN32_API )
   HANDLE handle;
   WIN32_FIND_DATA data;
   DWORD win32Error;
   #endif

   #if defined( USE_OSLAYER )
   DIRST dirHandle;
   const char *filePattern;
   char *basePath;
   #endif

   unsigned pathLength;

   int findNextRetCode;

   TA_Directory *dir;
   TA_String *string;
   TA_RetCode retCode;
   TA_SystemGlobal *global;

   const char   *entryName;
   unsigned int  entryIsDirectory;

   *directory = NULL;

   if( (path == NULL) || (directory == NULL) )
      return TA_BAD_PARAM;

   retCode = TA_GetGlobal(  &TA_SystemGlobalControl, (void **)&global );
   if( retCode != TA_SUCCESS )
      return retCode;

   dir = (TA_Directory *)TA_Malloc( sizeof( TA_Directory ) );

   if( dir == NULL )
      return TA_ALLOC_ERR;

   dir->nbFile = 0;
   dir->nbDirectory = 0;

   dir->listOfFile = TA_ListAlloc();
   dir->listOfDirectory = TA_ListAlloc();

   if( (dir->listOfFile == NULL) || (dir->listOfDirectory == NULL) )
   {
      TA_DirectoryFree( dir );
      return TA_ALLOC_ERR;
   }

   /* Verify that the path is valid. */
   pathLength = strlen( path );

   if( (pathLength == 0) || (pathLength >= MAX_PATH) )
   {
      TA_DirectoryFree( dir );
      return TA_BAD_PARAM;
   }

   /* Now get the directory from the operating system. */
   #if defined( USE_WIN32_API )

   handle = FindFirstFile( path, &data );
   if( handle == INVALID_HANDLE_VALUE )
   {
      win32Error = GetLastError();
      global->lastError = win32Error;

      if( (win32Error != ERROR_FILE_NOT_FOUND) &&
          (win32Error != ERROR_PATH_NOT_FOUND) )
      {
         TA_DirectoryFree( dir );
         return TA_ACCESS_FAILED;
      }

      /* No files or directory... but still have to pass the result
       * to the caller.
       */
      *directory = dir;

      return TA_SUCCESS;
   }

   entryName = data.cFileName;
   entryIsDirectory = data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
   #endif

   #if defined( USE_OSLAYER )
   /* Split the path into the basePath and the filePattern. */
   basePath = TA_Malloc( pathLength+1 ); 
   memcpy( basePath, path, pathLength+1 );
   filePattern = split_path_and_file( basePath );

   if( !filePattern )
   {
      /* With no filePattern, no file can be found...
       * so return an empty directory to the caller.
       */
      *directory = dir;
      TA_Free(  basePath );
      return TA_SUCCESS;
   }

   /* Look for last separetor. */
   if( !open_dir(&dirHandle, basePath ) )
   {
      /* Errors, or no files or no directory... but
       * still have to pass the result to the caller.
       */      
      TA_Free(  basePath );
      *directory = dir;
      return TA_SUCCESS;
   }

   entryName = dirHandle.file_name;
   entryIsDirectory = dirHandle.file_attrs & ATTR_SUBDIR;
   #endif

   do
   {
      #if defined( USE_OSLAYER )
      if( file_matches( entryName, filePattern ) )
      {
      #endif
         if( entryIsDirectory )
         {
            if( entryName[0] != '.' )
            {
               string = TA_StringAlloc( global->dirnameCache, entryName );

               if( string == NULL )
               {
                  #if defined( USE_OSLAYER )
                     close_dir(&dirHandle);
                     TA_Free(  basePath );
                  #endif
                  TA_DirectoryFree( dir );
                  return TA_ALLOC_ERR;
               }

               retCode = TA_ListAddTail( dir->listOfDirectory, (void *)string );

               if( retCode != TA_SUCCESS )
               {
                  #if defined( USE_OSLAYER )
                     close_dir(&dirHandle);
                     TA_Free(  basePath );
                  #endif
                  TA_DirectoryFree( dir );
                  return retCode;
               }
               dir->nbDirectory++;
            }
         }
         else
         {
            string = TA_StringAlloc( global->filenameCache, entryName );

            if( string == NULL )
            {
               #if defined( USE_OSLAYER )
                  close_dir(&dirHandle);
                  TA_Free(  basePath );
               #endif
               TA_DirectoryFree( dir );
               return TA_ALLOC_ERR;
            }

            retCode = TA_ListAddTail( dir->listOfFile, (void *)string );

            if( retCode != TA_SUCCESS )
            {
               #if defined( USE_OSLAYER )
                  close_dir(&dirHandle);
                  TA_Free(  basePath );
               #endif
               TA_DirectoryFree( dir );
               return retCode;
            }
            dir->nbFile++;
         }
      #if defined( USE_OSLAYER )
      }
      #endif
      
      #if defined( USE_WIN32_API )
      findNextRetCode = FindNextFile( handle, &data );
      entryName = data.cFileName;
      entryIsDirectory = data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
      #endif

      #if defined( USE_OSLAYER )
      findNextRetCode = read_dir( &dirHandle );
      entryName = dirHandle.file_name;
      entryIsDirectory = dirHandle.file_attrs & ATTR_SUBDIR;
      #endif
   }
   while( findNextRetCode == TRUE );


   #if defined( USE_OSLAYER )
   TA_Free(  basePath );
   if( !close_dir(&dirHandle) )
   {
      TA_DirectoryFree( dir );
      return TA_INTERNAL_ERROR(11);
   }
   #endif

   #if defined( USE_WIN32_API )   
   if( FindClose( handle ) != TRUE )
   {
      global->lastError = GetLastError();
      TA_DirectoryFree( dir );
      return TA_INTERNAL_ERROR(12);
   }
   #endif

   /* Pass the result to the caller. */
   *directory = dir;

   return TA_SUCCESS;
}
Esempio n. 3
0
static TA_RetCode TA_TraceGlobalInit( void **globalToAlloc )
{
   TA_TraceGlobal *global;
   #if !defined( TA_SINGLE_THREAD )
   TA_RetCode retCode;
   #endif

   if( !globalToAlloc )
      return TA_BAD_PARAM;

   *globalToAlloc = NULL;

   global = TA_Malloc( sizeof( TA_TraceGlobal ) );
   if( !global )
      return TA_ALLOC_ERR;

   memset( global, 0, sizeof( TA_TraceGlobal ) );

   #if !defined( TA_SINGLE_THREAD )
      /* Initialize the mutexes in a non-block state. */
      retCode = TA_SemaInit( &global->callSema, 1 );
      if( retCode != TA_SUCCESS )
      {
         TA_Free(  global );
         return retCode;
      }
      retCode = TA_SemaInit( &global->fatalSema, 1 );
      if( retCode != TA_SUCCESS )
      {
         TA_SemaDestroy( &global->callSema );
         TA_Free(  global );
         return retCode;
      }
   #endif

   #ifdef TA_DEBUG   
      #if defined( TA_SINGLE_THREAD )
         /* When single threaded, maintain a calling stack. */      

         global->callStack = TA_ListAlloc();
         if( !global->callStack )
         {
            TA_Free(  global );
            return TA_ALLOC_ERR;
         }
      #endif

      /* All function call and checkpoint are maintained in a dictionary. */
      global->functionCalled = TA_DictAlloc( TA_DICT_KEY_ONE_STRING, freeTracePosition );
      if( !global->functionCalled )
      {
         #if !defined( TA_SINGLE_THREAD )
            TA_SemaDestroy( &global->callSema );
            TA_SemaDestroy( &global->fatalSema );      
         #else
            TA_ListFree( global->callStack );
         #endif      
         TA_Free(  global );
         return TA_ALLOC_ERR;
      }
   #endif

   /* Success, return the allocated memory to the caller. */
   *globalToAlloc = global;

   return TA_SUCCESS;
}
Esempio n. 4
0
/**** Global functions definitions.   ****/
TA_FileIndexPriv *TA_FileIndexPrivAlloc( TA_Libc *libHandle,
                                         TA_String *initialCategory,
                                         TA_String *initialCategoryCountry,
                                         TA_String *initialCategoryExchange,
                                         TA_String *initialCategoryType )
{
   TA_FileIndexPriv *fileIndexPrivData;
   TA_StringCache *stringCache;

   stringCache = TA_GetGlobalStringCache( libHandle );

   /* Initialize the TA_FileIndexPriv element. */
   fileIndexPrivData = (TA_FileIndexPriv *)TA_Malloc( libHandle, sizeof( TA_FileIndexPriv ) );

   if( !fileIndexPrivData )
      return NULL;

   /* initialize all fields to NULL. */
   memset( fileIndexPrivData, 0, sizeof( TA_FileIndexPriv ) );

   /* Now attempt to allocate all sub-elements. */
   fileIndexPrivData->libHandle = libHandle;

   stringCache = TA_GetGlobalStringCache( libHandle );
   fileIndexPrivData->initialCategoryString         = TA_StringDup( stringCache, initialCategory );
   fileIndexPrivData->initialCategoryCountryString  = TA_StringDup( stringCache, initialCategoryCountry);
   fileIndexPrivData->initialCategoryExchangeString = TA_StringDup( stringCache, initialCategoryExchange );
   fileIndexPrivData->initialCategoryTypeString     = TA_StringDup( stringCache, initialCategoryType );

   if( (fileIndexPrivData->initialCategoryString         == NULL) ||
       (fileIndexPrivData->initialCategoryCountryString  == NULL) ||
       (fileIndexPrivData->initialCategoryExchangeString == NULL) ||
       (fileIndexPrivData->initialCategoryTypeString     == NULL) )
   {
      freeFileIndexPriv( (void *)fileIndexPrivData );
      return NULL;
   }

   fileIndexPrivData->scratchPad = (char *)TA_Malloc( libHandle, TA_SOURCELOCATION_MAX_LENGTH+2 );
   if( !fileIndexPrivData->scratchPad )
   {
      freeFileIndexPriv( (void *)fileIndexPrivData );
      return NULL;
   }

   fileIndexPrivData->listLocationToken = TA_ListAlloc( libHandle );
   if( !fileIndexPrivData->listLocationToken )
   {
      freeFileIndexPriv( (void *)fileIndexPrivData );
      return NULL;
   }

   fileIndexPrivData->listCategory = TA_ListAlloc( libHandle );
   if( !fileIndexPrivData->listCategory )
   {
      freeFileIndexPriv( (void *)fileIndexPrivData );
      return NULL;
   }

   fileIndexPrivData->root = allocTreeNode( libHandle, NULL, NULL );
   if( !fileIndexPrivData->root )
   {
      freeFileIndexPriv( (void *)fileIndexPrivData );
      return NULL;
   }

   fileIndexPrivData->currentNode = fileIndexPrivData->root;

   fileIndexPrivData->wildOneChar = TA_StringAlloc( stringCache, "?" );
   fileIndexPrivData->wildZeroOrMoreChar = TA_StringAlloc( stringCache, "*" );
   fileIndexPrivData->wildOneOrMoreChar = TA_StringAlloc( stringCache, "?*" );

   if( (!fileIndexPrivData->wildOneChar) ||
       (!fileIndexPrivData->wildZeroOrMoreChar) ||
       (!fileIndexPrivData->wildOneOrMoreChar) )
   {
      freeFileIndexPriv( (void *)fileIndexPrivData );
      return NULL;
   }

   return fileIndexPrivData;
}
Esempio n. 5
0
TA_RetCode TA_FileIndexAddCategoryData( TA_FileIndexPriv *data,
                                        TA_String *stringCategory,
                                        TA_FileIndexCategoryData **added )
{
   TA_PROLOG;
   TA_RetCode retCode;
   TA_FileIndexCategoryData *categoryData;
   unsigned int tmpInt;
   unsigned int categoryFound; /* Boolean */
   TA_Libc *libHandle;
   TA_StringCache *stringCache;

   libHandle   = data->libHandle;
   TA_TRACE_BEGIN( libHandle, TA_FileIndexAddCategoryData );

   stringCache = TA_GetGlobalStringCache( libHandle );

   TA_ASSERT( libHandle, data != NULL );
   TA_ASSERT( libHandle, stringCategory != NULL );

   /* Trap the case where the category is already added. */
   categoryData = (TA_FileIndexCategoryData *)TA_ListAccessTail( data->listCategory );

   categoryFound = 0;
   while( categoryData && !categoryFound )
   {
      TA_ASSERT( libHandle, categoryData->string != NULL );

      tmpInt = strcmp( TA_StringToChar( stringCategory ),
                       TA_StringToChar( categoryData->string ) );
      if( tmpInt == 0 )
         categoryFound = 1;
      else
         categoryData = (TA_FileIndexCategoryData *)TA_ListAccessPrev( data->listCategory );
   }

   if( !categoryFound )
   {
      /* This is a new category, so allocate the TA_FileIndexCategoryData */
      categoryData = (TA_FileIndexCategoryData *)TA_Malloc( libHandle, sizeof(TA_FileIndexCategoryData) );

      if( !categoryData )
         TA_TRACE_RETURN( TA_ALLOC_ERR );

      /* Initialize the TA_FileIndexCategoryData */
      categoryData->parent = data;
      if( stringCategory )
      {
         categoryData->string = TA_StringDup( stringCache, stringCategory);    /* String for this category. Can be NULL. */
         if( !categoryData->string )
         {
            TA_Free( libHandle, categoryData );
            TA_TRACE_RETURN( TA_ALLOC_ERR );
         }
      }
      else
         categoryData->string = NULL;

      categoryData->listSymbol = TA_ListAlloc( libHandle );

      if( !categoryData->listSymbol )
      {
         if( categoryData->string )
            TA_StringFree( stringCache, categoryData->string );
         TA_Free( libHandle, categoryData );
         TA_TRACE_RETURN( TA_ALLOC_ERR );
      }

      /* Add it to the TA_FileIndexPriv */
      retCode = TA_ListAddTail( data->listCategory, categoryData );
      if( retCode != TA_SUCCESS )
      {
         TA_ListFree( categoryData->listSymbol );
         if( categoryData->string )
            TA_StringFree( stringCache, categoryData->string );
         TA_Free( libHandle, categoryData );
         TA_TRACE_RETURN( TA_ALLOC_ERR );
      }

      #if 0
         printf( "**ADDING CATEGORY[%s]\n", TA_StringToChar( categoryData->string ) );
      #endif
   }

   /* Return the address of the object representing that category. */
   if( added )
      *added = categoryData;


   TA_TRACE_RETURN( TA_SUCCESS );
}