File: file_info.c

package info (click to toggle)
swish++ 6.1.5-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,256 kB
  • ctags: 1,759
  • sloc: ansic: 11,931; lisp: 804; sh: 629; perl: 366; makefile: 80
file content (121 lines) | stat: -rw-r--r-- 3,816 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
**      SWISH++
**      file_info.c
**
**      Copyright (C) 1998  Paul J. Lucas
**
**      This program is free software; you can redistribute it and/or modify
**      it under the terms of the GNU General Public License as published by
**      the Free Software Foundation; either version 2 of the License, or
**      (at your option) any later version.
**
**      This program is distributed in the hope that it will be useful,
**      but WITHOUT ANY WARRANTY; without even the implied warranty of
**      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**      GNU General Public License for more details.
**
**      You should have received a copy of the GNU General Public License
**      along with this program; if not, write to the Free Software
**      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

// standard
#include <cstring>

// local
#include "config.h"
#include "directory.h"
#include "enc_int.h"
#include "FilesReserve.h"
#include "file_info.h"
#include "platform.h"
#include "util.h"                       /* for new_strdup() */

using namespace std;

file_info::list_type        file_info::list_;
file_info::name_set_type    file_info::name_set_;

FilesReserve                files_reserve;

//*****************************************************************************
//
// SYNOPSIS
//
        file_info::file_info(
            char const *path_name, int dir_index, size_t file_size,
            char const *title, int num_words
        )
//
// DESCRIPTION
//
//      Construct a file_info.  If a title is given, use it; otherwise set the
//      title to be (just) the file name (not the path name).
//
//      Additionally record its address in a list so the entire list can be
//      iterated over later in the order encountered.  The first time through,
//      reserve files_reserve slots for files.  If exceeded, the vector will
//      automatically grow, but with a slight performance penalty.
//
// PARAMETERS
//
//      path_name   The full path name of the file.
//
//      dir_index   The numerical index of the directory.
//
//      file_size   The size of the file in bytes.
//
//      title       The title of the file only if not null.
//
//      num_words   The number of words in the file.
//
//*****************************************************************************
    : dir_index_( dir_index ),
      file_name_(
        //
        // First duplicate the entire path name and put it into the set of
        // files encountered; then make file_name_ point to the base name
        // inside the same string, i.e., it shares storage.
        //
        pjl_basename( *name_set_.insert( new_strdup( path_name ) ).first )
      ),
      size_( file_size ), num_words_( num_words ),
      title_(
        //
        // If there was a title given, use that; otherwise the title is the
        // file name.  Note that it too shares storage.
        //
        title ? new_strdup( title ) : file_name_
      )
{
    if ( list_.empty() )
        list_.reserve( files_reserve );
    list_.push_back( this );
}

//*****************************************************************************
//
// SYNOPSIS
//
        file_info::file_info( unsigned char const *p )
//
// DESCRIPTION
//
//      Construct a file_info from the raw data inside an index file.
//
// PARAMETERS
//
//      p   The pointer to the raw file_info data.
//
//*****************************************************************************
    : dir_index_( dec_int( p ) ),
      file_name_( reinterpret_cast<char const*>( p ) ),
      size_(
          dec_int( p += ::strlen( reinterpret_cast<char const*>( p ) ) + 1 )
      ),
      num_words_( dec_int( p ) ),
      title_( reinterpret_cast<char const*>( p ) )
{
    // do nothing else
}
/* vim:set et sw=4 ts=4: */