Blame googlemock/scripts/generator/cpp/utils.py

Packit bd1cd8
#!/usr/bin/env python
Packit bd1cd8
#
Packit bd1cd8
# Copyright 2007 Neal Norwitz
Packit bd1cd8
# Portions Copyright 2007 Google Inc.
Packit bd1cd8
#
Packit bd1cd8
# Licensed under the Apache License, Version 2.0 (the "License");
Packit bd1cd8
# you may not use this file except in compliance with the License.
Packit bd1cd8
# You may obtain a copy of the License at
Packit bd1cd8
#
Packit bd1cd8
#      http://www.apache.org/licenses/LICENSE-2.0
Packit bd1cd8
#
Packit bd1cd8
# Unless required by applicable law or agreed to in writing, software
Packit bd1cd8
# distributed under the License is distributed on an "AS IS" BASIS,
Packit bd1cd8
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit bd1cd8
# See the License for the specific language governing permissions and
Packit bd1cd8
# limitations under the License.
Packit bd1cd8
Packit bd1cd8
"""Generic utilities for C++ parsing."""
Packit bd1cd8
Packit bd1cd8
__author__ = 'nnorwitz@google.com (Neal Norwitz)'
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
import sys
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
# Set to True to see the start/end token indices.
Packit bd1cd8
DEBUG = True
Packit bd1cd8
Packit bd1cd8
Packit bd1cd8
def ReadFile(filename, print_error=True):
Packit bd1cd8
    """Returns the contents of a file."""
Packit bd1cd8
    try:
Packit bd1cd8
        fp = open(filename)
Packit bd1cd8
        try:
Packit bd1cd8
            return fp.read()
Packit bd1cd8
        finally:
Packit bd1cd8
            fp.close()
Packit bd1cd8
    except IOError:
Packit bd1cd8
        if print_error:
Packit bd1cd8
            print('Error reading %s: %s' % (filename, sys.exc_info()[1]))
Packit bd1cd8
        return None